Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Dec 2010
    Location
    London
    Posts
    338
    Thanks
    63
    Thanked 11 Times in 11 Posts

    problem floating divs

    Hi there.

    I'm trying to get the layout shown in the attached picture, but I can't work it out. I've tried playing around with the clearing of floats but I can only ever seem to get boxes 2 and 3 to sit next to each other. Any pointers (and a possible explanation!) would be very much appreciated.

    Code:
    <style type="text/css">
    div {
    	border: solid;
    }
    #container {
    	padding: 30px;
    	width: 800px;
    	overflow: auto;
    }
    #div1{
    	width: 300px;
    	height: 200px;
    	float: left;
    }
    #div2{
    	width: 300px;
    	height: 200px;
    	margin-top: 20px;
    	float: left;
    	clear: both;
    }
    #div3 {
    	float: right;
    	width: 100px;
    	height: 200px;
    }
    </style>
    <div id="container">
    	<div id="div1">
    		1
    	</div>
    	<div id="div2">
    		2
    	</div>
    	<div id="div3">
    		3
    	</div>
    </div>
    Many thanks
    Attached Thumbnails Attached Thumbnails problem floating divs-layout.jpg  

  • #2
    Master Coder Excavator's Avatar
    Join Date
    Dec 2006
    Location
    Alaska
    Posts
    9,675
    Thanks
    22
    Thanked 1,827 Times in 1,811 Posts
    Hello paddyfields,
    Look what happens when you re-arrange your floats -
    Code:
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Untitled Document</title>
    <style type="text/css">
    div { border: 1px solid #f00; }
    #container {
    	width: 800px;
    	padding: 30px;
    	overflow: auto;
    }
    #div1 {
    	height: 200px;
    	width: 300px;
    	float: left;
    }
    #div2 {
    	height: 200px;
    	width: 300px;
    	float: left;
    	margin-top: 20px;
    	clear: left;
    }
    #div3 {
    	height: 400px;
    	width: 100px;
    	float: right;
    }
    </style>
    </head>
    <body>
    <div id="container">
      <div id="div3"> 3 </div>
      <div id="div1"> 1 </div>
      <div id="div2"> 2 </div>
    </div>
    </body>
    </html>
    Validate often DURING development - Use it like a splelchecker | Debug during Development |Write it for FireFox, ignore IE
    Use the right DocType | Validate your markup | Validate your CSS | Why validating is good | Why tables are bad

  • Users who have thanked Excavator for this post:

    paddyfields (02-05-2013)

  • #3
    Regular Coder
    Join Date
    Dec 2010
    Location
    London
    Posts
    338
    Thanks
    63
    Thanked 11 Times in 11 Posts
    Ah man, nice.

    Thank you!

  • #4
    Senior Coder jerry62704's Avatar
    Join Date
    Oct 2007
    Location
    Springfield, IL
    Posts
    1,100
    Thanks
    13
    Thanked 87 Times in 87 Posts
    It works for me. But div3 is moved to the top and the clear is removed from div 2.

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <style>
    <style type="text/css">
    div {
    	border: solid;
    }
    #container {
    	padding: 30px;
    	width: 800px;
    	overflow: auto;
    	background-color:yellow;
    }
    #div1{
    	width: 300px;
    	height: 200px;
    	float: left;
    	background-color:red;
    }
    #div2{
    	width: 300px;
    	height: 200px;
    	margin-top: 20px;
    	float: left;
    	background-color:blue;
    }
    #div3 {
    	float: right;
    	width: 300px;
    	height: 400px;
    	background-color:green;
    }
    </style>
    </head>
    <body>
    <div id="container">
    	<div id="div3">
    		3
    	</div>
    	<div id="div1">
    		1
    	</div>
    	<div id="div2">
    		2
    	</div>
    </div>
    </body>
    </html>
    .
    .
    ...and gladly would he learn and gladly teach

    Visit www.LiberalsWin.com for humor and the unique Bush/Obama Approval Polls

  • #5
    Master Coder Excavator's Avatar
    Join Date
    Dec 2006
    Location
    Alaska
    Posts
    9,675
    Thanks
    22
    Thanked 1,827 Times in 1,811 Posts
    Quote Originally Posted by jerry62704 View Post
    It works for me. But div3 is moved to the top and the clear is removed from div 2.
    Yes, when you increased the width of div3 there is no room for 1,2&3 to go beside each other anymore so no need for the clear.

    Narrow that div3 to 100px and the clear is needed again.
    Validate often DURING development - Use it like a splelchecker | Debug during Development |Write it for FireFox, ignore IE
    Use the right DocType | Validate your markup | Validate your CSS | Why validating is good | Why tables are bad


  •  

    Posting Permissions

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