Web Design Forum  
 
Go Back   Web Design Forum > Web and Graphic Design > Web Design

Like Tree2Likes

Reply
 
LinkBack Thread Tools Display Modes
Old 08-25-2010, 05:11 PM   #1
Super Moderator
 
Join Date: Oct 2008
Location: Arizona, USA
Posts: 1,053
Default TUTORIAL: Using Floats (Floating Divs)

Several people have inquired about how to correctly use floats (e.g. How to float divs with in divs on a page).

The code below show a simple layout for floating three divs inside a container.
**Please note this layout is not limited to divs, floats can be used with a wide variety of page elements (P, UL, A, IMG, etc.)

Enjoy. PM me if you have any questions.

HTML
HTML Code:
<div class="container">
	<div>
    	<h2>Heading One</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla id nunc lacus. Integer bibendum varius fringilla. Suspendisse potenti. Donec nec tincidunt tellus. Curabitur fringilla viverra placerat. Sed quis turpis nunc.</p>
        <a href="">Read more &rarr;</a>
    </div>
    <div>
    	<h2>Heading Two</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla id nunc lacus. Integer bibendum varius fringilla. Suspendisse potenti. Donec nec tincidunt tellus. Curabitur fringilla viverra placerat. Sed quis turpis nunc.</p>
        <a href="">Read more &rarr;</a>
    </div>
    <div>
    	<h2>Heading Three</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla id nunc lacus. Integer bibendum varius fringilla. Suspendisse potenti. Donec nec tincidunt tellus. Curabitur fringilla viverra placerat. Sed quis turpis nunc.</p>
        <a href="">Read more &rarr;</a>
    </div>
</div>
<div class="container">
	<div>
    	<h2>Heading One</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla id nunc lacus. Integer bibendum varius fringilla. Suspendisse potenti. Donec nec tincidunt tellus. Curabitur fringilla viverra placerat. Sed quis turpis nunc.</p>
        <a href="">Read more &rarr;</a>
    </div>
    <div>
    	<h2>Heading Two</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla id nunc lacus. Integer bibendum varius fringilla. Suspendisse potenti. Donec nec tincidunt tellus. Curabitur fringilla viverra placerat. Sed quis turpis nunc.</p>
        <a href="">Read more &rarr;</a>
    </div>
    <div>
    	<h2>Heading Three</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla id nunc lacus. Integer bibendum varius fringilla. Suspendisse potenti. Donec nec tincidunt tellus. Curabitur fringilla viverra placerat. Sed quis turpis nunc.</p>
        <a href="">Read more &rarr;</a>
    </div>
</div>
<div class="container">
	<div>
    	<h2>Heading One</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla id nunc lacus. Integer bibendum varius fringilla. Suspendisse potenti. Donec nec tincidunt tellus. Curabitur fringilla viverra placerat. Sed quis turpis nunc.</p>
        <a href="">Read more &rarr;</a>
    </div>
    <div>
    	<h2>Heading Two</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla id nunc lacus. Integer bibendum varius fringilla. Suspendisse potenti. Donec nec tincidunt tellus. Curabitur fringilla viverra placerat. Sed quis turpis nunc.</p>
        <a href="">Read more &rarr;</a>
    </div>
    <div>
    	<h2>Heading Three</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla id nunc lacus. Integer bibendum varius fringilla. Suspendisse potenti. Donec nec tincidunt tellus. Curabitur fringilla viverra placerat. Sed quis turpis nunc.</p>
        <a href="">Read more &rarr;</a>
    </div>
</div>
CSS
Code:
div.container {
	margin:20px auto;
	border:solid 1px #ccc;
	width:790px;
	overflow:hidden;
	position:relative;
}
div.container div {
	float:left;
	margin:1px 0 1px 1px;
	border:solid 1px #069;
	width:260px;
	height:193px;
}
div.container h2, div.container p, div.container a {
	font-family:Arial, Helvetica, sans-serif;
	font-size:8pt;
	color:#444;
	padding:0 10px;
}
div.container h2 {
	font-size:10pt;
	line-height:25px;
}
div.container p {
	line-height:15pt;
}
div.container a {
	display:inline-block;
	line-height:17pt;
	color:#C60;
	text-decoration:none;
}
div.container a:hover {
	text-decoration:underline;
}
Attached Images
 
richie01 and ValiantShady like this.
__________________
John Darling
Graphic / Web Designer
SmarterTools Inc.
(877) 357-6278
www.smartertools.com
PixelPusher is offline   Reply With Quote


Old 08-25-2010, 09:38 PM   #2
Bronze Member
 
GeneticOpera's Avatar
 
Join Date: Aug 2010
Location: Phoenix, Arizona
Posts: 32
Default

Wow, this will be really helpful! I've used floats, but usualy just left and right and I wasn't entirely positive on what else to do with them. Thank you!
GeneticOpera is offline   Reply With Quote
Old 08-26-2010, 03:53 PM   #3
Super Moderator
 
Join Date: Oct 2008
Location: Arizona, USA
Posts: 1,053
Default

Your welcome
__________________
John Darling
Graphic / Web Designer
SmarterTools Inc.
(877) 357-6278
www.smartertools.com
PixelPusher is offline   Reply With Quote
Old 09-08-2010, 10:12 PM   #4
New Member
 
Join Date: Aug 2010
Posts: 20
Default

Quote:
Originally Posted by GeneticOpera View Post
Wow, this will be really helpful! I've used floats, but usualy just left and right and I wasn't entirely positive on what else to do with them. Thank you!
It's easy to design but it's very helpful for people that are still learning.

Great tutorial Pixel,
__________________
█ Read Web Hosting Reviews and See Which Web Host Is Right For You.
Ask For Help At The Web Design Forum For Webmasters.
Marc is offline   Reply With Quote
Old 09-11-2010, 10:07 AM   #5
New Member
 
HMI Guy's Avatar
 
Join Date: Sep 2010
Posts: 5
Default

I noticed that the divs appear centered in Chrome, but on the left side in IE. Is this expected?
__________________
The HMI Guy

For the Top 7 Web Design Products head over to hmiguy.com.
HMI Guy is offline   Reply With Quote


Old 09-12-2010, 04:39 PM   #6
Super Moderator
 
Join Date: Oct 2008
Location: Arizona, USA
Posts: 1,053
Default

Quote:
Originally Posted by HMI Guy View Post
I noticed that the divs appear centered in Chrome, but on the left side in IE. Is this expected?
Which version of IE?
__________________
John Darling
Graphic / Web Designer
SmarterTools Inc.
(877) 357-6278
www.smartertools.com
PixelPusher is offline   Reply With Quote
Old 09-12-2010, 04:46 PM   #7
Super Moderator
 
Join Date: Oct 2008
Location: Arizona, USA
Posts: 1,053
Default

Just checked it now in IE6- IE8...all centered. Not sure why it is not working for you

Maybe double check the markup? You might have forgot to close the main container div
__________________
John Darling
Graphic / Web Designer
SmarterTools Inc.
(877) 357-6278
www.smartertools.com
PixelPusher is offline   Reply With Quote
Old 10-02-2010, 01:35 PM   #8
Bronze Member
 
Join Date: Oct 2010
Posts: 89
Default

I am still learning about web design, and this will most certainly be very handy. Thanks.
__________________
vps hosting-my vps hosting choice || web hosting-Web hosting blog
nedo is offline   Reply With Quote
Old 10-04-2010, 05:33 PM   #9
Super Moderator
 
Join Date: Oct 2008
Location: Arizona, USA
Posts: 1,053
Default

Quote:
Originally Posted by nedo View Post
I am still learning about web design, and this will most certainly be very handy. Thanks.
Your welcome
__________________
John Darling
Graphic / Web Designer
SmarterTools Inc.
(877) 357-6278
www.smartertools.com
PixelPusher is offline   Reply With Quote
Old 10-17-2010, 05:41 PM   #10
New Member
 
Join Date: Oct 2010
Posts: 2
Default

this code is nice and clean but i'm still a bit of a noob so i gotta ask: your second class declaration

Code:
div.container div {
	float:left;
	margin:1px 0 1px 1px;
	border:solid 1px #069;
	width:260px;
	height:193px;
}
am i understanding this correctly if i say you're defining how to handle divs that are nested within divs? and will this work for any level of nesting?

i assumed this because you only use class=container once in each div

also, since the main reason i read this was because i was trying to nest divs and align them differently from just having each under the other, which part exactly is it that allows the nested divs to appear next to each other instead of right under each other? i'm assuming it has to do with an interaction between align, setting the dimensions to be exact and maybe with overflow and position - but i'm not sure about it.
lynx is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 07:44 PM.


Camera Forum - Computer Forum - Web Design Forum

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Content Relevant URLs by vBSEO 3.6.0 ©2011, Crawlability, Inc.