Forum Moderators: not2easy

Message Too Old, No Replies

css floats

can't understand it

         

fwordboy

2:54 pm on Jun 9, 2004 (gmt 0)

10+ Year Member



there was a time when i thought i understood floats but i seemed to have completely lost it.

if i want to have float on the same level e.g.


div div(floated)

then i would do this


<div id="nav" style="float:right; clear: none;">
<p>sdjsjd</p>
</div>
<div id="content">
<p>sjdjs</p>
</div>

which is fair enough, but say I wanted to change the order of the divs n my html for semantic purposes like this...


<div id="content">
<p>sjdjs</p>
<div id="nav" style="float:right; clear: none;">
<p>sdjsjd</p>
</div>
</div>

then then i would end up with this


div
div

I thought that by floating something that it came out of the document flow but it is clearly still in the flow as it appears below the non-floating element. Is there a way with a floating div to raise it to the same level as its preceding div (without giving the preceding div a float or placing higher in the source code)?

drbrain

3:01 pm on Jun 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Floated elements don't float up, only left or right.

In order to place an item lower on the page at a higher place, you need to use positioning.

createErrorMsg

4:03 pm on Jun 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Float affects how other elements flow around an object (ie, it lets them do that) but I don't think it actually pulls them out of the flow.

Why not switch which DIV is floated?

You had a reg. div on the left and a floated one on the right, with the float:right div first in your code. You then wanted to switch it so the non-floated div came first in your code. Right?

So if the content(I assume your previously non-floated div contains the content?) div is first in the code, and the browser needs the floated div first, then make the content div float, only make it float:left. You'd end up with

contentdiv(floated) div

Is that the effect you wanted?

SuzyUK

6:14 pm on Jun 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



the float element should always come first it's the one that getting moved then other bock elements which are normally 100% wide flow around it..

BUT :) there a mean dirty trick if you like, use of a negative right margin will allow a following left floated container to float into the negative space.. yes I know it's a bit weird..

see example:


<style type="text/css" media="screen">
#content {
float: left;
width: 100%; /* or width of container */
margin-right: -200px; /* allows space for next float to float into */
background: #cfc;
}

#nav {
float: left;
width: 200px; /* same as negative margin.. but may need to less if there's borders etc.. */
background: #fcf;
}
p {margin: 1em 0;}

</style>
</head>
<body>
<div id="content"><p>content</p></div>
<div id="nav"><p>nav</p></div>
</body>

Note the divs are no longer nested..

Fun eh?

Suzy

createErrorMsg

7:43 pm on Jun 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Suzy, I know this is wordboy's thread, but let me ask you a quick question regarding your post:
Is there a compelling reason to use your neg-margin method over switching which div is floated? Is your goal to avoid nesting the divs? If so, what's the benefit in avoiding the nesting?

GreenLeaf

1:40 pm on Jun 10, 2004 (gmt 0)

10+ Year Member



Suzy, this way the links in "nav" div are non-clickable in Opera 6.01.

fwordboy

1:57 pm on Jun 10, 2004 (gmt 0)

10+ Year Member



my problem was i wasn't thinking about floats in the right way. i was looking at them through the perspective of floating an item affected only it's own fate but i should have thought that floating an item affects the items around it, which makes more sense to me now.

thanks for everyone's help.

highman

2:21 pm on Jun 10, 2004 (gmt 0)

10+ Year Member



>Suzy, this way the links in "nav" div are non-clickable in Opera 6.01.

Add a z-index and use a higher value for the div with links - you may need to also use position: relative to get the z-index to work

GreenLeaf

6:15 pm on Jun 10, 2004 (gmt 0)

10+ Year Member



Thanks, it works with z-index + position: relative

SuzyUK

6:15 pm on Jun 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is there a compelling reason to use your neg-margin method over switching which div is floated?

No not really it was a trick that allows the content and nav (which of course could be an <h1> and "search box") to stay in the "right" (more logical) order..

More often than not you know the width of the "nav" div. Well you should have done in this case anyway because it was floated in the first place and all floated divs should really have had a width specified. Then you may require the content area to be a flexible/fluid width. If you just change the floats, which is a perfectly fine way to do things, you then have to specify a width on your content area, which you may not want to do...

In my example the width on the floated content area is taken care of by explicitly setting it to 100% which would be 100% of it's parent element, whether that's the body or a width specified containing div, doesn't matter the negative margin is the bit you need to know the width of to fit the nav into

in short: fluid width content area and fixed width sidebar is the benefit here ;)


Is your goal to avoid nesting the divs? If so, what's the benefit in avoiding the nesting?

No and nesting wouldn't be required any way you choose to do this.. I just pointed it out as the original example (2nd code) was nested, it was just a heads up..

Suzy

createErrorMsg

8:24 pm on Jun 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's a very good point about the width of floated items. See? This is why I drop by here every day, you always learn something new...