473,405 Members | 2,338 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,405 software developers and data experts.

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

Similar topics

42
by: Frank Buss | last post by:
I've setup a challenge, mainly for C++, Java and Lisp, but every other language is welcome: http://www.frank-buss.de/challenge/index.html There is nothing to win, but I hope there will be some...
5
by: Chris Beall | last post by:
Objective: Using an HTML list, create a horizontal nav menu with these characteristics: - All menu items have the same width at all times. - When the window width is reduced, the menu items...
2
by: LazyLarry | last post by:
Hi there, I need serious help. Ive got a project that needs some unique select box function. OK THERE IS A LOT OF Code but If you Crack it youll get the credit. just mail me at...
2
by: joeriatscripts | last post by:
Hey there, Everytime I am trying to create something in CSS, it gives me a headache. This time no different, as FF and IE seem to have their own rules! Anyway, before I continue, have a look at...
3
by: orbitrobert | last post by:
hi there, i'm having a difficult time inserting a scrolling div into a table. i'm a graphic designer and want to use the scrolling div on the portfolio page of my site. i find that everything...
2
by: yangtono | last post by:
Hi, I am creating a table to list some data. The table is using a sorting and highlight function that I found from the net. I can't attach image here, basically html will wrap the heading,...
14
by: gaijinco | last post by:
I was a hobbist web coder for years but I had to sidestep for a while. Now I'm trying to return to it and I'm trying to clarify how am I supposed to do somethings with CSS v.s. HTML and I'm...
13
by: Stevo | last post by:
I've found that for IE6+, if you add the property text-align:center to a DIV, then *anything* inside it gets centered. That can be a table, an object/embed, another DIV, an image, or some text. ...
8
by: Bob Altman | last post by:
Hi all, This has got to be a dumb question, but... What is the CSS property that I would use to center a table on the web page? If I set the table's Align property in the VS 2008 property...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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
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,...
0
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...
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
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...

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.