Forum Moderators: not2easy

Message Too Old, No Replies

Expanding floated elements to their container's width

How to expand width of floated elemnts to their containes defined width?

         

semenh

9:10 pm on Nov 4, 2007 (gmt 0)

10+ Year Member



Hi all!
Here is some advanced CSS problem that I have came up against recently.

In general, I have block-element container positioned absolutely, let's say a <div>, with defined width, for instance 150px. Now, inside that div I am floating to the left some other elements, let's say 3 another <div>'s. Now, all I want is that these three floated <div>'s will automatically expand to their parent container's width.
But,... there is always a but! :)

  1. I must not define width for these floating elements. (In general, the reason for that is to generalize layout in one parameter only, the one that holds 150px width value for the container <div>. And by that making it possible to change the layout dynamically without calculation of width of another elements)
  2. The vice versa method isn't suitable. I mean defining width for floated elements, and by that auto expanding parent container that is positioned absolutely. With this method I am loosing containers dimensions. And I need them to place at the top of the container right-floated element (see the test case).

Here is a test case. Try to play with it, please tell me if you got this.
[pre]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Test Case</title>
<style type="text/css">
#container {
position:absolute;
padding:2px;
width:150px;
top:100px;
left:100px;
border:1px solid black;
text-align:center;
}
#smallBox, #box1, #box2, #box3 {
margin:2px;
padding: 2px;
display:inline; /* The IE Doubled Float-Margin Bug (google it and you will find why) */
border:1px solid black;
}
#smallBox { float:right; }
#clearRight { clear:right; }
#box1 { clear:right; }
#box1, #box2, #box3 { float:left; }
</style>
</head>
<body>
<div id="container">
<div id="smallBox">small :)</div>
<div id="clearRight"></div>
<div id="box1">text</div>
<div id="box2">text</div>
<div id="box3">text</div>
</div>
</body>
</html>
[/pre]

This Page Is Valid HTML 4.01 Strict!

Dabrowski

10:38 pm on Nov 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Without paying too much attention to this, my first thought is that if you want the elements to expand to fill the space, why are they floated at all?

Marshall

3:14 am on Nov 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



semenh. Welcome to WebmasterWorld.

You will have to assign widths to the inner <div>'s in %. However, it cannot simply be say 33% 34% and 33%. You have to take into consideration the container's padding as well as any margins, padding and borders of the inner <div>. Therefore, the widths may actually be 31% 32% and 31% so they don't float below one another. The width setting are just guidelines and not necessarily what you need. This would apply too if you use pixels to assign widths.

Marshall

semenh

12:50 pm on Nov 5, 2007 (gmt 0)

10+ Year Member



Yes, this is paragraph's 1 unwanted solution: "define width for these floating elements". Solution with percents is also requires some calculation, just like you said, margins and padding (they can be changed dynamically at the time the page is shown and thiat will ruin all).
I am wondering if there a way to force row of inline elements to expand to their container's width, and by that avoid floating them.

By now, I have some other solution which I am not satisfied with:
I did the vice versa solution. i.e.: didn't defined width for the container, just floated the elements to the left with same defined width to all of them. Then with JavaScript calculated total width of floated elements and then gave this value to the container. Now with defined width of the container I can float other element to the right. By this solution I am achieving the "one globalized parameter" for the width requirement.