ADBLOCK_MSG
How to vertically center content of a blockPublished: Wednesday, 06 February 2013 16:02 Written by Ogri Hits: 12253
At first glance it may seem that the solution of the problem posed in the title is trivial: set the CSS-attribute vertical-align:middle
- and this is it. But it's not that simple. Principle of layout elements traditionally based on scaling by width, and content stretches or shrinks vertically in accordance with new width. vertical-align
is useful for centering contents of table cells, as well as of DIV blocks having the display:table-cell
attribute. The expected behavior of content vertical centering can also be achieved for elements of the page normal flow - blocks with the attribute display:inline-block
. In all other cases this approach does not apply, as most modern browsers simply uses vertical-align
attribute to be inherited by the child inline elements of the block. It will require a different method, the description of which is the focus of this article.
Just make a reservation: the use of this technique involves two assumptions:
- You can specify a fixed height of the inner block.
- You can assign absolute positioning to the inner block. Typically, this is not a problem, because the container can remain in the normal flow.
If these conditions are met, do the following:
- In the style of the container set the
position
attribute asrelative
orabsolute
. - Set a fixed height for the child block.
- Set
position:absolute
andtop:50%
, in the style of child block, thus matching its upper limit with the middle of the parent block. - Specify the attribute
margin-top
of child block with the value equal to half of its height. This will move the child block up to match its middle with the middle of the container.
Example:
<div style="position: relative;">
Container
<div style="position:absolute; top:50%; height:8em; margin-top:-4em;">
Vertically centered child block
</div>
</div>
Here's how it looks in a browser:
Container
Vertically centered child block
An example of the practical use of this method while inscribing an image into container can be found in the next article.
Latest News
-
Tuesday, 21 July 2020 03:18
Joomla 3: Redirect to same page after successful login -
Saturday, 30 December 2017 16:11
Joomla: how to add your own language constants or override existing ones -
Thursday, 30 November 2017 23:27
Joomla: Integrate reCAPTCHA v2 (NO CAPTCHA) into JComments -
Saturday, 25 June 2016 15:33
Unified filtering of mod_jcomments_latest module's output -
Thursday, 17 September 2015 16:23
Post an illustrated Joomla-site article on Facebook using OG-tags
Articles Most Read
-
148974
Migrating from Joomla 1.5 to Joomla 2.5. Part 2-1. Transferring jDownloads and jComments -
83763
Joomla: Integrate reCAPTCHA v2 (NO CAPTCHA) into JComments -
57202
Migrating from Joomla 1.5 to Joomla 2.5. Part 2-2. Template, editor, and other extensions -
40161
Editing animated GIF-images in Photoshop CS3 -
39329
Custom 404 error page in Joomla 2.5
Login
Guest Column
Recent comments
-
Custom 404 error page in Joomla 2.5
- Roseann 02.11.2020 08:17
-
Joomla: Integrate reCAPTCHA v2 (NO CAPTCHA) into JComments
-
PG SLOT
13.01.2021 09:21
Hey! I could have sworn I've been to this site before but after browsing through some of the post ...
-
Nidia
12.12.2020 08:04
HELP. I can't write a comment without it hanging.
-
Launa
17.11.2020 15:13
I can agree with the accuracy of this as I work in a related sector. Interesting cheers for sharing.
-
JD sports vouchers
04.10.2020 03:12
Happy to pay for something like this, or donate witth paypal even
-
PG SLOT
13.01.2021 09:21
-
Joomla: how to add your own language constants or override existing ones
-
Vouchersort
29.10.2020 11:33
Is this a free template that you are using as I really love it. As a website desugner myself I hope that ...
-
Vouchersort
29.10.2020 11:33
-
Migrating from Joomla 1.5 to Joomla 2.5. Epilogue
-
Dewitt
11.02.2021 06:17
HELP. I can't write a comment without the page freezing.
-
Dewitt
11.02.2021 06:17
-
Migrating from Joomla 1.5 to Joomla 2.5. Part 2-1. Transferring jDownloads and jComments
-
Francine
14.12.2020 08:49
I cant stomach this person so sorry x
-
Vouchersort
30.10.2020 15:43
Once again, great resource for us newbs.
-
Francine
14.12.2020 08:49
-
Unified filtering of mod_jcomments_latest module's output
-
Фильмы 2022 онлайн
03.12.2021 21:37
This web site definitely has all of the info I wanted about this subject and didn't know who to ask.
-
Фильмы 2022 онлайн
03.12.2021 21:37
Read more...