How to vertically center content of a blockPublished: Wednesday, 06 February 2013 16:02 Written by Ogri Hits: 11698
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:
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
-
142507
Migrating from Joomla 1.5 to Joomla 2.5. Part 2-1. Transferring jDownloads and jComments -
74223
Joomla: Integrate reCAPTCHA v2 (NO CAPTCHA) into JComments -
44156
Migrating from Joomla 1.5 to Joomla 2.5. Part 2-2. Template, editor, and other extensions -
38858
Editing animated GIF-images in Photoshop CS3 -
36292
Migrating from Joomla 1.5 to Joomla 2.5. Part 1. Transferring content
Login
Guest Column
Recent comments
-
Custom 404 error page in Joomla 2.5
-
fafaslot 10.06.2023 05:59
-
-
Joomla 2.5: Remove breadcrumbs from specific pages
-
รับทำเว็บพนันออนไลน์ 09.06.2023 22:58
An intriguing discussion is definitely worth comment. I think that you ought to publish more about ...
-
-
Joomla: Integrate reCAPTCHA v2 (NO CAPTCHA) into JComments
-
Going Here 10.06.2023 02:46
Thank you for the auspicious writeup. It actually was a leisure account it. Look complicated to ...
-
xayalmuazzin.com 09.06.2023 23:04
Нi! This is my 1st comment here so I just wanted to give а quick shout out annd say I truly еnjoy ...
-
диплом института 15.05.2021 03:19
Fantastic items from you, man. I've be aware your stuff prior to and you're simply extremely magnificent.
-
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 ...
-
-
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.
-
-
Migrating from Joomla 1.5 to Joomla 2.5. Part 2-1. Transferring jDownloads and jComments
-
calendrier mai 09.06.2023 13:43
These are actually enormous ideas in regarding blogging. You have touched some fastidious points here.
-
-
The structure of internal links within Joomla site
-
918kiss sg 10.06.2023 06:28
Get ready for the ultimate gaming experience with 918kiss. Spin and win on classic casino favorites such ...
-
-
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.
-
Read more...