467,170 Members | 1,108 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,170 developers. It's quick & easy.

CSS Challenge: center a div like a table

OK, so I know that there has been a lot of discussion on this topic,
but none of the answers have been satisfactory. Therefore there
seems to be only one thing to do: issue a challenge to all of the CSS
gurus of the world to prove that CSS really can do what it claims to
do.

The challenge seems simple as first blush. We take a simple case of
table-based layout:

<table align="center" cellpadding="4">
<td bgcolor="yellow">
Some stuff
</td>
</table>

This simply prints "Some stuff" centered, with a yellow background.
All we want to do is recreate this example using CSS.

No problem, right?

Just to be clear, there are a few requirements:

* The text area (and its background) must automatically resize to fit
the size of the text. In other words, no div's of predefined width.

* It must be possible to put this inside another div and have the text
centered within the space of the parent div. Any surrounding content
should flow in a normal and predictable fashion, just like the
original table-based layout. In other words, no floating divs.

* The background must extend a few pixels beyond the text. In other
words no embedding a span inside a div.

* The contained text ("Some stuff") might be multiple lines or contain
additional markup. The solution must be able to accommodate this.
Like I said, a span inside a div ain't gonna cut it.

* The solution must work on all mainstream modern browsers. At a
minimum this means FireFox 1.0+, Opera 8+ and Safari. Oh and that
other one... what was it... oh yeah, IE 6.0+ (5.0+ would be nice, I'd
live with 6.0). There go some more possible solutions, like display:
table-cell.

* The solution should validate to W3C standard xhtml and CSS. Note
that I said "should" and not "must." I'm purist until purity fails to
get the job done.
So, are you up to the challenge?

I'm pretty close to convinced that it can't be done. Someone please
prove me wrong.

Apr 18 '07 #1
  • viewed: 4501
Share:
2 Replies
On 17 Apr 2007 20:50:26 -0700
donovan <dp*******@gmail.comwrote:

[....]

Therefore there
seems to be only one thing to do: issue a challenge to all of the
CSS gurus of the world to prove that CSS really can do what it
claims to do.

The challenge seems simple as first blush.
[....]
>
So, are you up to the challenge?
Hmmmmm.... is there a prize for this amazing test of skill?

Andrew

--
Andrew's Corner
http://people.aapt.net.au/~adjlstrong/
Apr 18 '07 #2
On 2007-04-18, donovan <dp*******@gmail.comwrote:
OK, so I know that there has been a lot of discussion on this topic,
but none of the answers have been satisfactory. Therefore there
seems to be only one thing to do: issue a challenge to all of the CSS
gurus of the world to prove that CSS really can do what it claims to
do.

The challenge seems simple as first blush. We take a simple case of
table-based layout:

<table align="center" cellpadding="4">
<td bgcolor="yellow">
Some stuff
</td>
</table>

This simply prints "Some stuff" centered, with a yellow background.
All we want to do is recreate this example using CSS.

No problem, right?

Just to be clear, there are a few requirements:

* The text area (and its background) must automatically resize to fit
the size of the text. In other words, no div's of predefined width.

* It must be possible to put this inside another div and have the text
centered within the space of the parent div. Any surrounding content
should flow in a normal and predictable fashion, just like the
original table-based layout. In other words, no floating divs.

* The background must extend a few pixels beyond the text. In other
words no embedding a span inside a div.

* The contained text ("Some stuff") might be multiple lines or contain
additional markup. The solution must be able to accommodate this.
Like I said, a span inside a div ain't gonna cut it.

* The solution must work on all mainstream modern browsers. At a
minimum this means FireFox 1.0+, Opera 8+ and Safari. Oh and that
other one... what was it... oh yeah, IE 6.0+ (5.0+ would be nice, I'd
live with 6.0). There go some more possible solutions, like display:
table-cell.

* The solution should validate to W3C standard xhtml and CSS. Note
that I said "should" and not "must." I'm purist until purity fails to
get the job done.
So, are you up to the challenge?

I'm pretty close to convinced that it can't be done. Someone please
prove me wrong.
Can't be done in a way that works in all those browsers you mentioned.

In an ideal world, the preferred solution would be display: inline-block
with text-align: center on the container.
Apr 18 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

42 posts views Thread by Frank Buss | last post: by
5 posts views Thread by Chris Beall | last post: by
2 posts views Thread by yangtono | last post: by
14 posts views Thread by gaijinco | last post: by
8 posts views Thread by Bob Altman | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.