Results 1 to 2 of 2

Thread: How do I stop floated divs from wrapping?

  1. #1
    BoroDrummer is offline New Member: Posts Will Be Moderated
    Join Date
    Mar 2011
    Posts
    1

    How do I stop floated divs from wrapping?

    I'm trying to dynamically add a set of divs to a parent div. I want the divs to display horizontally and for the parent div to scroll when there are too many to fit in the screen width. This is my code so far. I can't seem to stop the divs from wrapping... Is this even possible? Or should I use a table instead?

    Code:
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Test</title>
        <style type="text/css"> 
            .wrap
            {
                background-color:Red;
                width:100%;
                overflow:auto;
                white-space:nowrap;
            }
            
            .block
            {
                background-color:blue;
                margin:10px;
                width:auto;
                float:left;
            }
        </style>
    </head>
    <body>
        <div class="wrap">
            <div class="block">
                <p>This is a block. This is a block. This is a block. This is a block. This is a block. This is a block. This is a block. </p>
            </div>
            <div class="block">
                <p>This is a block. This is a block. This is a block. This is a block. This is a block. This is a block. This is a block. </p>
            </div>
            <div class="block">
                <p>This is a block. This is a block. This is a block. This is a block. This is a block. This is a block. This is a block. </p>
            </div>
        </div>    
    </body>
    </html>

  2. #2
    Join Date
    Feb 2006
    Location
    Salisbury UK
    Posts
    4,332
    To get the .block divs to show side by side you need to add a width to the float: left; but then the white-space: nowrap makes the text extend outside the divs, so delete that:-
    Code:
    <style type="text/css"> 
            .wrap
            {
                background-color:Red;
                width:100%;
                overflow:auto;
                /*white-space:nowrap;*/
            }
            
            .block
            {
                background-color:blue;
                margin:10px;
                width: 200px;/*auto;*/
                float:left;
            }
        </style>
    Your .wrap div has width: 100%; so the three divs show side by side in a wide window but when the window resolution width is too small and then one or two divs will go to a lower line.
    Code downloaded to my PC will be deleted in due course.
    WIN7; IE9, Firefox, Opera, Chrome and Safari for Windows; screen resolution usually 1366*768.
    Also IE6 on W98 with 800*600 and IE8 on Vista 1440*900.

Similar Threads

  1. wrapping Text
    By Liquid-Fire in forum Web Design, HTML Reference and CSS
    Replies: 1
    Last Post: 19 Dec 2005, 09:24 AM
  2. footer won't clear floated nested divs
    By gela in forum Web Design, HTML Reference and CSS
    Replies: 1
    Last Post: 10 Dec 2005, 10:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •