Forum Moderators: not2easy

Message Too Old, No Replies

CSS and a floating DIV

         

Kysmiley

1:54 pm on Dec 12, 2004 (gmt 0)

10+ Year Member



Is it possible to have the content of one Div class to float around another one. I will try to illistrate what i am wanting to do. In the side bar I want to put a background with buttons for navigation and short of developing an image mape i thought this would be easier if i could do this so it keeps the sidebar at the top and lets the test in the main content flow around it. Any suggestions on using CSS to do this
Pat
Start here
##################################################
#________________________________________________#
#________________________________________________#
##################################################
__t___#______________b___________________________#
__e___#_______________a__________________________#
__X___#________________c_________________________#
__T___#_________________k________________________#
#######__________________g_______________________#
__________________________r______________________#
___________________________o_____________________#
____________________________u____________________#
_____________________________n___________________#
______________________________d__________________#
##################################################

orion_rus

4:10 pm on Dec 12, 2004 (gmt 0)

10+ Year Member



Better submit it in JScript forum.

createErrorMsg

8:43 pm on Dec 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is it possible to have the content of one Div class to float around another one.

I may be misunderstanding what you're after here. Are you saying you want a navigation div in the upper left corner, and you want the text content in the remainder of the div to not be overlapped by the navigation div?

As in...

t = text
n = navigation div.

nnnnn ttttttttttttttttt
nnnnn ttttttttttttttttt
nnnnn ttttttttttttttttt
nnnnn ttttttttttttttttt
nnnnn ttttttttttttttttt
ttttttttttttttttttttttt
ttttttttttttttttttttttt
ttttttttttttttttttttttt

If this is what you are after, it is VERY easy to do. Create a div to hold the navigation... <div id="nav></div>
Create a div to hold the text...<div id="text"></div>
Place the nav <div> inside of the text <div> BEFORE the actual text and style it as below...

html:
<div id="text>
<div id="nav">
<p>nnnnn</p>
<p>nnnnn</p>
<p>nnnnn</p>
</div>
<p>ttttttttttttttttttttttt</p>
<p>ttttttttttttttttttttttt</p>
<p>ttttttttttttttttttttttt</p>
<p>ttttttttttttttttttttttt</p>
<p>ttttttttttttttttttttttt</p>
<p>ttttttttttttttttttttttt</p>
</div>

css:
#nav {
float: left;
width: WHATEVER;
}

That's it. If there's any chance that the text will not be as long as the navigation div, you'll need to apply a float:left and a width to the #text div, too. If the text will ALWAYS be longer, it's not necessary.

cEM

Kysmiley

8:50 pm on Dec 12, 2004 (gmt 0)

10+ Year Member



Thank-you cEM thats exactly what i want to do. i knew how to do it with an image to float around it but could not get my thinking to get past the img part and into the nav idea
It will work perfect. I dont know if the text or content of the main part will be longer or not it depends on the page this is why i wanted to code it properly at the beginning in preparation for it.
Pat

createErrorMsg

9:02 pm on Dec 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm glad it'll work for you, Ky.

Let me add one more little peice of advice: put the text inside of it's own div, too. As in...

<div id="container">
<div id="nav">
<p>nnnnnn</p>
<p>nnnnnn</p>
<p>nnnnnn</p>
</div>
<div id="text">
<p>ttttttttttttttttttttt</p>
<p>ttttttttttttttttttttt</p>
<p>ttttttttttttttttttttt</p>
<p>ttttttttttttttttttttt</p>
</div>
</div>

Then style it like this...

#container {
float:left;
width: WHATEVER;
}
#nav {
float:left;
width: WHATEVER;
}

It doesn't look like much of a difference, and in this simple example it doesn't make any difference at all, but real world applications tend to be more complicted than WebmasterWorld examples. But giving the nav and the text their own divs, then nesting them inside of a container, you give yourself the ability to exercise far more control when/if you need it.

cEM

Kysmiley

10:23 pm on Dec 12, 2004 (gmt 0)

10+ Year Member



Still cant get it to clear the nav div.
I have tried clear: right; in each of the divs and in all of them yet the text still flows down and not back to the left after it clears the div. Any idea on why
here is some coding
####################
#main
{
position: absolute; left: 2%; top: 210px; width: 96%; float: left; clear: right;
}
#left
{
position: relative; left: 2px; width: 17.5%; height: 315px; float: left; clear: left;
}
#content
{
position: relative; left: 2px; top: 2px; width: 80%; float: left; clear: right;
}
####################
HTML code
##################
<div id="main">
<div id="left">

</div>
<div id="content">
<br>tester<BR><BR>
<br><br>now
<br><br><br>now
<br><br>end<br><br>
<br>now<br>
<br>test
<br><br>Yahoo<br>i hope<br><br><br>One
<br><br>two<br><br>tree
</div></div>

createErrorMsg

10:34 pm on Dec 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You want to leave the content div unfloated.

Kysmiley

11:07 pm on Dec 12, 2004 (gmt 0)

10+ Year Member



nope still not clearing and going back to the left under the nav div.

createErrorMsg

2:32 am on Dec 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ky, for the moment you should strip any absolute or relative positioning out of your code. They're not useful when used in conjunction with floating. (The exception would be absolutely positioning the container div (you're #main) if the overall layout requires it. However, I advise against it if at all possible. FOr one thing, it negates the usefulness of floating the container so that it encloses the floats.)

Your requirements here are as follows:

  • The container div must float and have a width (only necessary if the floating nav div might be longer than the content).
  • The navigation div must float and have a width.
  • The content div should NOT float and should NOT have a width.
  • No relative positioning or absolute positioning (for now; add these later if necessary, but for now they just get in the way of trouble shooting).
  • NO CLEARS. The clear property is a dangerous thing. It has a very specific use and can wreak havoc on a floated layout especially when used where it isn't needed. In this case, it is not necessary.

    By using the skeleton code I posted earlier, you should have exactly what you want. Here it is again with your widths in it. Cut and paste this directly into a txt editor, save it as an html file and view in your browser. It'll give you an idea of what I'm talking about...

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <title>Basic Floated Nav Bar</title>
    <style type="text/css">
    #main{
    float:left;
    width: 96%;
    background: #aaa;
    }
    #left{
    float:left;
    width: 17.5%;
    background: #ccc;
    }
    #content{
    /*style this div however you need for appearance, but it doesn't require any styling to acheive the basic float demonstrated*/
    }
    </style>
    </head>
    <body>
    <div id="main">
    <div id="left">
    <ul>
    <li><a href="#">Link</a></li>
    <li><a href="#">Link</a></li>
    <li><a href="#">Link</a></li>
    </ul>
    </div>
    <div id="content">
    <h3>Content</h3>
    <p>Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.</p>
    <p>Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.</p>
    <p>Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.</p>
    </div>
    </div>
    </body>
    </html>

    cEM

  • Kysmiley

    11:34 am on Dec 13, 2004 (gmt 0)

    10+ Year Member



    Thanks cEM I woke up about 3 am this morning thinking the same thing that the positioning values I was setting was causing my problem and if i do it correct i just need to position the main one so it lines up under the top div and in flow the others will be inside of the main so they will have to follow the scheme if the widths are not to large.
    pat