By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,824 Members | 2,410 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,824 IT Pros & Developers. It's quick & easy.

Vertical centering without tables

P: n/a
Hello

This is a test example:
http://www.markusernst.ch/anthracite/
http://www.markusernst.ch/anthracite/living_divani.html

After googling and experimenting for several hours, I ended up doing
this demo with tables. The main problems are the vertical centering of
the info area, and of the text inside the squares.

Is it possible to do this without tables? Vertical-align:middle does not
seem to be supported by any browser; I even ask myself what this
property is good for...

Thanks for a comment! (I know I have to fix more on this, such as the
fixed font sizes, but I would appreciate some comments about the
vertical centering stuff :-))

--
Markus
Nov 2 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Markus Ernst <derernst@NO#SP#AMgmx.chwrote:
>This is a test example:
http://www.markusernst.ch/anthracite/
http://www.markusernst.ch/anthracite/living_divani.html

After googling and experimenting for several hours, I ended up doing
this demo with tables. The main problems are the vertical centering of
the info area, and of the text inside the squares.

Is it possible to do this without tables? Vertical-align:middle does not
seem to be supported by any browser; I even ask myself what this
property is good for...

Thanks for a comment! (I know I have to fix more on this, such as the
fixed font sizes, but I would appreciate some comments about the
vertical centering stuff :-))
http://www.student.oulu.fi/~laurirai/www/css/middle/

--
Spartanicus
Nov 2 '06 #2

P: n/a
On 2006-11-02, Markus Ernst <derernst@NO#SP#AMgmx.chwrote:
Hello

This is a test example:
http://www.markusernst.ch/anthracite/
http://www.markusernst.ch/anthracite/living_divani.html

After googling and experimenting for several hours, I ended up doing
this demo with tables. The main problems are the vertical centering of
the info area, and of the text inside the squares.

Is it possible to do this without tables? Vertical-align:middle does not
seem to be supported by any browser;
vertical-align applies to table cells (where it does roughly what you
want) and to inline elements, where typically it only results in a small
change of position unless the line height is very high.
I even ask myself what this property is good for...
On inline boxes it may be quite rarely used, I don't know. It describes
where the inline boxes go in relation to the line they're on; it doesn't
place them in the middle of their block.

You can theoretically vertically centre an inline box in a block with
vertical align like this:

div
{
line-height: 20em;
height: 20em;
vertical-align: middle;
}
<div>
centered text
</div>

This way the block contains one very high line. The text goes in the
middle of the line instead of on the baseline. But you can't centre a
block box this way.

But this is a terrible way to do it, because if the text needs to break
onto a second line, it overflows and goes 10em below the bottom of the
div.
Thanks for a comment! (I know I have to fix more on this, such as the
fixed font sizes, but I would appreciate some comments about the
vertical centering stuff :-))
You can do vertical centering of block boxes in CSS using absolute
positioning quite easily provided you don't mind setting a height on the
box. If you want the box's height to "shrink-wrap" its content, and also
be centered, then as far as I know you have to use a table.

So to centre without an auto height, you need to set top, bottom and
height all to something, and top and bottom margins to auto.

for example:

div
{
position: absolute;
top: 0px;
bottom: 0px;
height: 2em;
margin: auto 0px;
background-color: red;
color: yellow;
}
<div>hello</div>

hello will be centered vertically in its containing block.
Nov 2 '06 #3

P: n/a
Spartanicus schrieb:
Markus Ernst <derernst@NO#SP#AMgmx.chwrote:
>This is a test example:
http://www.markusernst.ch/anthracite/
http://www.markusernst.ch/anthracite/living_divani.html

After googling and experimenting for several hours, I ended up doing
this demo with tables. The main problems are the vertical centering of
the info area, and of the text inside the squares.

Is it possible to do this without tables? Vertical-align:middle does not
seem to be supported by any browser; I even ask myself what this
property is good for...

Thanks for a comment! (I know I have to fix more on this, such as the
fixed font sizes, but I would appreciate some comments about the
vertical centering stuff :-))

http://www.student.oulu.fi/~laurirai/www/css/middle/
Thank you, this is a very useful article indeed; i knew it and consulted
it again while working on the above tests. It actually says that
vertical centering is not reliably possible expect in some special cases
where you know the dimensions of at least the outer element.

I posted here as I hoped somebody had found out more on this since Lauri
Raittila published his article.

As soon as I have more time I will search through the W3C forums to find
out the reason why vertical centering is only supported inside tables
and display:table-cell elements. I don't understand it, as centering is
a pure presentation issue, while using tables for presentation is not
recommended at all. I assume the main problem is that the height of the
body element and all non-table block elements is related to the element
contents, while the width is related to the viewport, which makes
vertical centering totally different from horizontal centering.

--
Markus
Nov 3 '06 #4

P: n/a
Markus Ernst <derernst@NO#SP#AMgmx.chwrote:
>http://www.student.oulu.fi/~laurirai/www/css/middle/

Thank you, this is a very useful article indeed; i knew it and consulted
it again while working on the above tests. It actually says that
vertical centering is not reliably possible expect in some special cases
where you know the dimensions of at least the outer element.

I posted here as I hoped somebody had found out more on this since Lauri
Raittila published his article.
I haven't read it myself, but knowing Lauri I'm willing to bet that it
covers every option.
>As soon as I have more time I will search through the W3C forums to find
out the reason why vertical centering is only supported inside tables
and display:table-cell elements.
The CSS WG tries to get flow layouts to work so that an element can be
positioned as soon as the content in the line box in which the element
occurs has been parsed without the element's position on screen being
dependant on the content of line boxes below the line box that the
element occurs in.

The result being incremental rendering with a minimum amount of reflows.

Tables (assuming the default table-layout:auto mode) often both impede
incremental rendering and cause reflows, therefore supporting
vertical-align on table cells is unlikely to introduce a new problem
there.

--
Spartanicus
Nov 3 '06 #5

P: n/a
Ben C schrieb:
[...]
You can do vertical centering of block boxes in CSS using absolute
positioning quite easily provided you don't mind setting a height on the
box. If you want the box's height to "shrink-wrap" its content, and also
be centered, then as far as I know you have to use a table.

So to centre without an auto height, you need to set top, bottom and
height all to something, and top and bottom margins to auto.

for example:

div
{
position: absolute;
top: 0px;
bottom: 0px;
height: 2em;
margin: auto 0px;
background-color: red;
color: yellow;
}
<div>hello</div>

hello will be centered vertically in its containing block.
Thank you; I retried it using this technique and could get much better
(though not perfect) results in Firefox. Keeping space for the header is
possible by setting top to a positive value; unfortunately this does not
work for the footer, too.
But IE (6) does not seem to understand this kind of stuff at all...

Anyway you helped me understand the margin:auto thing I had been
wondering about before.

--
Markus
Nov 3 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.