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

Centering a collection of floated divs

Hi,

I am pretty new to CSS authoring and find myself in a tricky
situation. I have a series of divs (dynamically generated - there
could be up to 8) as follows:

<div class="person">
<h2>Name</h2>
<img src="images/name.gif" alt="Chris" />
<p>Message Here</p>
</div>

I need these divs to sit alongside each other. I also need them to sit
centered in the parent content div.

I have tried floating the divs left, which aligns them horizontally,
but then they cannot be centered.

Effectively I am trying to recreate this table:

<div align="center">
<table border="0">
<tr>
<td>
<table>
<tr><td>NAME</td></tr>
<tr><td>IMAGE</td></tr>
<tr><td>MESSAGE</td></tr>
</table>
</td>
<td>
<table>
<tr><td>NAME</td></tr>
<tr><td>IMAGE</td></tr>
<tr><td>MESSAGE</td></tr>
</table>
</td>
<td>
<table>
<tr><td>NAME</td></tr>
<tr><td>IMAGE</td></tr>
<tr><td>MESSAGE</td></tr>
</table>
</td>
</tr>
</table>
</div>

How can this be done without a table?
Thanks,
Chris.
Jul 20 '05 #1
3 4781
On 9 Nov 2003 03:53:36 -0800, cg*****@csi.com (Chris) wrote:
Hi,

I am pretty new to CSS authoring and find myself in a tricky
situation. I have a series of divs (dynamically generated - there
could be up to 8) as follows:

<div class="person">
<h2>Name</h2>
<img src="images/name.gif" alt="Chris" />
<p>Message Here</p>
</div>

I need these divs to sit alongside each other. I also need them to sit
centered in the parent content div.

I have tried floating the divs left, which aligns them horizontally,
but then they cannot be centered.

Effectively I am trying to recreate this table:

<div align="center">
<table border="0">
<tr>
<td>
<table>
<tr><td>NAME</td></tr>
<tr><td>IMAGE</td></tr>
<tr><td>MESSAGE</td></tr>
</table>
</td>
<td>
<table>
<tr><td>NAME</td></tr>
<tr><td>IMAGE</td></tr>
<tr><td>MESSAGE</td></tr>
</table>
</td>
<td>
<table>
<tr><td>NAME</td></tr>
<tr><td>IMAGE</td></tr>
<tr><td>MESSAGE</td></tr>
</table>
</td>
</tr>
</table>
</div>

How can this be done without a table?
Thanks,
Chris.


In order to center a set of div's (float:left;) you need to nest them
inside a fixed width div.
There are two ways that I know of to center the fixed width "parent"
div. BlueRobot.com has a demonstration of each of these at:
margin:auto
http://www.bluerobot.com/web/css/center1.html
and negative margin
http://www.bluerobot.com/web/css/center2.html

There is an article on floating nested divs similar to what you are
after at: http://www.alistapart.com/articles/practicalcss

I have been playing with something similar and you may want to view
the source code on this page:
http://pages.prodigy.net/randyv/centerprb.htm
which I prepared as an example for a question I posted yesterday:
Why does border affect margin in centered fixed width div in NS7.1?
Message-ID: <0i********************************@4ax.com>

Of course, I should include a caution about using fixed sizes of
anything. The purist will tell you it is antithema to "liquid" page
design.

Regards,
Randy
Jul 20 '05 #2
Also see http://www.glish.com/css/ for more css based layouts,
specifically http://www.glish.com/css/3.asp which contains a fixed with
centered layout.

Hope that helps
Jul 20 '05 #3
> In order to center a set of div's (float:left;) you need to nest them
inside a fixed width div.
There are two ways that I know of to center the fixed width "parent"
div. BlueRobot.com has a demonstration of each of these at:
margin:auto
http://www.bluerobot.com/web/css/center1.html
and negative margin
http://www.bluerobot.com/web/css/center2.html

There is an article on floating nested divs similar to what you are
after at: http://www.alistapart.com/articles/practicalcss

I have been playing with something similar and you may want to view
the source code on this page:
http://pages.prodigy.net/randyv/centerprb.htm
which I prepared as an example for a question I posted yesterday:
Why does border affect margin in centered fixed width div in NS7.1?
Message-ID: <0i********************************@4ax.com>

Of course, I should include a caution about using fixed sizes of
anything. The purist will tell you it is antithema to "liquid" page
design.

Regards,
Randy

Very interesting links. I would also like to caution against using
fixed sizes (especially for everything in page). A good alternative is
to use a min size and max size, which is good for accomodating people
with large and small displays.

Jonathan
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Zaka Ferenc | last post by:
Hi all, the page http://info.etr.elte.hu/divtest5.html looks as I expected in IE6 and Opera 7.11, but the floated divs float out of the containing blocks (divs) in NN7.1 and Opera 7.0 (what I...
3
by: Philip | last post by:
I am trying to make a bunched of left-floated divs that will be contained in a larger div. the floated divs all contain a left-floated img and text of varying sizes. If I don't set a height (or...
3
by: Lee K. Seitz | last post by:
I'm redesigning this page for more up-to-date browser standards: <http://home.hiwaay.net/~lkseitz/hhs/winners.shtml>. Note the use of an "invisible" table to position the "visible" ones. I'm...
15
by: red | last post by:
How do I center two side by side divs ? I've been writing css pages for a while but there's one thing tha still eludes me. I can center a div with margin auto. I can place two divs side by side...
2
by: tasman.hayes | last post by:
I'm experimenting with converting a site from a table layout to CSS. I'm floating three DIVs in a row for the top of a website (a logo, navigation buttons and a email list signup box). The DIVs...
1
by: alex | last post by:
Hey all, May have missed this in the FAQ, advice or article links welcome. Have a content div with a navigation div floated left and an advertisement div floated right. I would like to have a...
2
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...
2
by: Ben C. | last post by:
Hi If I have a container div and inside the container div I float two divs (one left and one right), the containting div will collapse as the floated divs will no longer prop it up, meaning that...
1
by: yawnmoth | last post by:
I'd like to center two floated divs but am unsure of how to do so. Here's my code: http://www.frostjedi.com/terra/scripts/demo/div-float-center.html It seems like setting the width of the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.