Forum Moderators: not2easy

Message Too Old, No Replies

Why does float negate a block element's automatic 100% width?

The box model should be renamed pandora's box model! ;-)

         

JAB Creations

9:58 am on Dec 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm having difficulty understanding why a float negate's a block element's automatic 100% width. Without float a div without a set width will automatically fill up all the horizontal space.

This first set of code is what the second set of code is intended to look like...

<div style="background: #ccc; float: right; margin: 1px 1px 0px 0px; width: 200px; z-index: 1;">
<p>aaa</p>
</div>

<div style="background: #aaa; border: #000 solid 1px;">
<p>ccc</p>
</div>

Now my goal is to have the 200px div appear in the code AFTER the other div and display exactly as how it does above. However when I try to float both DIVs the width becomes negated. I want to add a border to the wider div so having a border and a 100% width will cause some issues.

Ideas?

John

Fotiman

3:36 pm on Dec 27, 2005 (gmt 0)

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



It sounds like you are trying to do something like the One True Layout technique [positioniseverything.net] detailed in the articles at positioniseverything.net [positioniseverything.net]. In addition, in November 2005 Eric Meyer added an entry in his blog titled

Multi-Unit Any-Order Columns
[meyerweb.com] which may be what you're trying to achive. If I understood these techniques better, I would try to offer a solution, but since it's still very new to me I can only suggest you look into these.

Good luck.

coopersita

4:36 pm on Dec 27, 2005 (gmt 0)

10+ Year Member



That's a real problem...

Is absolute positioning for the div you want to the right an option?

<div style="background: #aaa; border: #000 solid 1px; margin-right: 200px;">
<p>ccc</p>
</div>

<div style="background: #ccc; position:absolute; top:0; right: 0; margin: 1px 1px 0px 0px; width: 200px; z-index: 1;">
<p>aaa</p>
</div>

JAB Creations

11:13 pm on Dec 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Absolute position works, except it will only work once on a page that would require multiple instances.

<div style="background: #aaa; border: #000 solid; border-width: 1px 0px 1px 1px; margin-right: 200px;">
<p>absolute positioned works</p>
</div>

<div style="background: #ccc; border: #000 solid; border-width: 1px 1px 1px 0px; position:absolute; top: 7px; right: 10px; margin: 1px 1px 0px 0px; width: 200px; z-index: 1;">
<p>aaa</p>
</div>

That looks fine in Firefox 1.5. I believe I am alreay following the basic concept of the linked page however he keeps defining the width which negates my goal entirely.

John

JAB Creations

11:21 pm on Dec 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well I remembered that I had already achieved this goal but with one oddity, I had to use THREE divs. To me that means I'm using one div in the sense of manipulating layout (the same concept as using tables for non-tabular data) but it does work...

The added benefit is that my inset (side column) automatically stretches with 100% height without any actual effort after I get my initial goal achieved. I still feel a little cheap using this method but to some I think it's a welcome compromise. It has issues in IE 4 though no one uses that anymore. IE 5 kinda works but you can use IECC anyway from that point on. Plus it even works with Netscape 4.8 and Opera 3.62 (but not Opera 2.12, though I don't have heavy Opera 2 traffic ;))!

Anyway here is the code, curious as to how others feel about this...

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<title></title>
<style type="text/css">
body {
margin: 8px;
padding: 0px;
}
div.content {
border: #000 solid;
border-width: 0px 1px 0px 0px;
height: auto;
margin-right: 159px;
min-height: 600px;
padding: 4px;
}
div.inset {
border: #000 solid;
border-width: 0px 0px 0px 1px;
float: right;
padding: 4px;
margin: 1px 1px 0px 0px;
width: 150px;
}
div.page {
border: #000 solid 1px;
float: left;
margin: 0px -161px 0px -1px;
padding: 0px;
width: 100%;
}
</style>
</head>
<body>

<div class="page">
<div class="content">
<p>Content</p>
</div>
</div>

<div class="inset">
<p>Inset</p>
</div>

</body>
</html>

Fotiman

11:30 pm on Dec 27, 2005 (gmt 0)

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




To me that means I'm using one div in the sense of manipulating layout (the same concept as using tables for non-tabular data)

I would disagree with you there. With tables, the element has some semantic meaning, which is lost when used for layout purposes. Asside from muddling up the HTML, it also makes it less user friendly for people who use non-visual browsers.

An extra div, on the other hand, is really just additional grouping, which is what a div is semantically for. From the HTML spec [w3.org]:


The DIV and SPAN elements, in conjunction with the id and class attributes, offer a generic mechanism for adding structure to documents. These elements define content to be inline (SPAN) or block-level (DIV) but impose no other presentational idioms on the content. Thus, authors may use these elements in conjunction with style sheets, the lang attribute, etc., to tailor HTML to their own needs and tastes.

Thus, comparing it to using tables for layout is like comparing apples and oranges.