473,729 Members | 2,234 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4822
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
7355
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 tables). My website consists of a few, simple parts: a "container" DIV which is centered and 750 pixels wide, a "sidebar" DIV which can be found along the left side of the container, and a "content" DIV which fills up what's left of the right part of...
8
30907
by: abracad | last post by:
Hi Is it possible to vertically align an image in the middle of a DIV of fixed height?
10
66039
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" height="29" alt="Wir schreiben die Geschichte Ihres Unternehmens."></div>
22
12479
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
7900
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 100% and vertically center content inside of it. Therefore, I'm seeking a way, preferably in CSS to vertically center a site. Here's more of a visual to what I have: /********************************
2
12143
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 looked made for that. The effect I want is img heading img heading img heading or heading
15
2260
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 done using a table. A, B, and C were each <tdelements.
3
4209
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 top level <divthen I can place it with CSS attribute 'left:', but this is a fixed offset. Is it possible to have a <divcentered in the browser window? 2. Verticle centering in <div>
3
735
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 and the number of them can vary greatly. They are all contained within a parent table cell, which is centred on the page. But I am unable to find a way of centering these table cells within the parent cell.
0
8913
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8761
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9426
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9280
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9142
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8144
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6016
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.