473,416 Members | 1,659 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Vertical centering without tables

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
5 4795
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jonah Bishop | last post by:
I have a puzzling problem with centering text, and I'm hoping that someone here can help me out. First of all, let me state that I am using XHTML 1.0 Strict and CSS for all layout purposes (no...
8
by: abracad | last post by:
Hi Is it possible to vertically align an image in the middle of a DIV of fixed height?
10
by: Markus Ernst | last post by:
Hi I have a strange problem with vertical-align. The case can be viewed at http://www.brainput.info/geschichte.html. HTML code: <div id="bild"><img src="geschichte.gif" width="274"...
22
by: Mel | last post by:
i have a div with with 300 and height 100 i need to place a text centered withing this box, can someone please help ? thankx Mel
1
by: Kenneth | last post by:
Okay, I've been scouring Google for hours looking for a solution to this problem, but as of yet I can't find one. XHTML Transitional seems to specify that I can no longer set a table's height to...
2
by: Eric Lindsay | last post by:
If I want to have the middle of an image vertically aligned with the middle of a heading (or several lines of heading), is there some particular way people would recommend? vertical-align: center...
15
by: scott | last post by:
Hello, I'm working on updating some of my table-based sites to use CSS instead of tables. One of my sites has a header that is composed of three elements: A B C In my prior design, this was...
3
by: John Pote | last post by:
1. Horizontal centering a <divin browser window. The current trend seems to be to place page content in a fixed width area in the middle of the browser window. How is this achieved? If I use a...
3
by: rudicheow | last post by:
SHORT VERSION ============= I have a bunch of identical fixed-size single-celled tables that rest against each other horizontally thanks to "float:left". These tables are dynamically generated...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.