SpyreStudios

Web Design and Development Magazine

  • Design
  • Showcase
  • Inspirational
  • Tutorials
  • CSS
  • Resources
  • Tools
  • UX
  • More
    • Mobile
    • Usability
    • HTML5
    • Business
    • Freebies
    • Giveaway
    • About SpyreStudios
    • Advertise On SpyreStudios
    • Get In Touch With Us

CSS In Depth Part 2: Floats & Positions

March 16, 2010 by Amber Weinberg 25 Comments

Last week, in the first of our CSS In Depth post, we discussed the difference between paddings and margins, and what the box model is.

This week we’ll be discussing positions and floats as well as what the differences are and when it’s best to use them. Both floats and positions deal with the relationship of elements between each other. Without these, padding and margins would be ineffectual.

What Is Position?

Position rules are used to position the element in the document flow. The position rule can take four values: static, relative, absolute and fixed. The default value of every element is static, where each element comes after each one another in order.

Elements that are position can be moved around using the left, top, bottom and right rules and a pixel or percentage value.

If an element is not positioned inside of another positioned element, the left, top, bottom and right rules will calculate their values using the browser window. For example, if we have an element that’s 100 x 100px and positioned by itself, with a left of 20px, it will be exactly 20px to the left most side of the user’s window, no matter how big or small that is.

css1

However, if we position that element inside of another positioned element with a left of 20px, the 20px will be calculate from the left side of the element, not the browser window.

css2

Positioning seems to work well in IE with little bugs, however I generally use positioning as a last resort. It seems that floating elements allows for less bugs when dealing with repeated backgrounds, expanding content and layouts in general.

Relative Positioning

Relative position is close to floats in terms of how they work. They can be moved around using either the top, right, left, bottom rules or by margins like floats can. Relative positioning changes the element without influencing the layout of other elements.

Therefor, the element will technically remain in the document flow, but will be “relatively” position in it. Therefor if we have 3 elements and gave the middle element a position relative and a bottom of 40px, the element would move up 40px, but the element below it will not move at all, as if the middle element never moved.

[html]

div { width: 100px; height: 100px; border: 1px solid #000; background: #CCC;}
div.middle { position: relative; bottom: 40px; }

[/html]

[html]
<div></div>
<div class="middle"></div>
<div></div>
[/html]

css3

Absolute Positioning

Absolute positioning removes the element from the document flow altogether. The other elements around it will move together and act as if the absolute positioned element never existed.

css4

This rule is generally best for using when an item breaks the general layout box.

Fixed Positioning

Fixed positioning is the same as absolute positioning, except it’s always positioned against the user’s browser window. Therefor, the element remains motionless as the user scrolls up or down in the browser. Unfortunately, fixed positioning doesn’t work at all in IE6 and below.

What Are Floats?

Floats are elements that are literally “floated”, so that they render side by side to each other. The float rule accepts three values: left, right and none. Floated elements can only be moved using margins, not by using the top, left, right or bottom rules.

Let’s say we have 2 boxes with a width of 50%. Normally, these boxes will appear one beneath the other, but if we give both boxes a float left, each box will render side by side.

css5

If there’s no height and width specified on the floated element, the element will automatically size itself to the content inside just like inline elements, even though a floated element is consider a block element. Also, non-floated elements will ignore floated ones in document flow, creating layout issues, so it’s important to either float all your elements in a container, or none of them.

Floating your elements left and right will solve a lot of weird browser issues. Unfortunately, modern browser deal with float rights differently than older browsers (including the IEs).

In modern browsers, you can either float right the element before or after the left float element, and it will render the same. For example, if out boxes had a width of 30% and the first one had a float left, and the second a float right, it would render correctly like:

css6

However, in older browsers, the floated right element has to come before the floated left element, otherwise it will render slightly below the left element. So, if we were to take the same example as above and check it out in an older version of Safari, or even in IE7, here’s an idea of how it would render:

css7

Which is better?

Like I said before, I prefer to float my elements and only use positions when breaking the layout. However, you’ll quickly come to figure out which works for your coding style better, so try experimenting with different floats and positions for all your layouts!

The In Depth CSS Series

  • Part 1: Margins, Padding & the Box Model
  • Part 2: Floats & Positioning
  • Part 3: All About Text
  • Part 4: New CSS3 Styles

Your Turn To Talk

I hope you liked this second post of our CSS In Depth series. Make sure you grab the RSS feed so you’ll know when we publish the next part!

Please let us know what you think by leaving a comment below ;)

Annoyed with your HP0-D07 exam preparation? Try out our latest 70-662 dumps and other resources to prepare and pass 642-642 exam in single attempt.

Filed Under: CSS, Tutorial

Comments

  1. Rodney Keeling says

    March 16, 2010 at 7:03 pm

    Great article; I’m excited for the next two parts of the series. And I didn’t know that tidbit about floating in older browsers. Thanks!

  2. ESN says

    March 17, 2010 at 4:13 am

    I didn’t know that the right element has to come before the floated left element in order to appear on the same level. Thank you for the tip.

  3. Pixil says

    March 17, 2010 at 4:51 am

    Floats are so essiential to my workflow :-) Clearing your floats is just as important. I recently switched from using a clear div to clear:after method. Looking forword to the next series!

  4. jay design says

    March 17, 2010 at 5:34 am

    I didn’t realise that the element floating right has to come before the left float to render properly in older browsers.. Thanks for the heads up!

  5. Bryan says

    March 17, 2010 at 9:19 am

    This is awesome. I love the in-depth analysis. It really helps me get a better understanding of how floats and positioning values work. Thanks for this. Keep it up

  6. Jan Pietrzyk says

    March 17, 2010 at 10:31 am

    @jay design

    This is exactly the reason why I prefer positioning.Most of the time the markup stays more accessible when you just position Elements acording to the layout. The only reason to use flots is that there are simple methods to contain things.

    @topic
    Whe I readf theheadline I was truely anticipating sth. like what happens when you float positioned elements, position your float relative again…. However nice introduction to the topic, but I don’t think that there is much of a choice most of the time. Since there is no real Layout property in CSS you just abuse text propertys to work out a Layout under any circumstances and hope for the best in terms of robustness.

  7. Russell Hampton says

    March 17, 2010 at 11:39 am

    Very informative. I’m a floater myself but it’s nice to get more perspective on the positions and how they can be applies. Thanks for the insight!

  8. Amber Weinberg says

    March 17, 2010 at 11:49 am

    Yup, putting the right floated element in front of the first has always been difficult to remember, but all of the IEs require that :/

  9. Joe says

    March 17, 2010 at 12:21 pm

    I’m a beginner to web design and am currently trying to build my personal portfolio site. I really appreciate these articles as these are the exact issues I’m pulling my hair out over as I work. Thanks for the in-depth, yet easily understandable, tips. If you know of any resources related to these topics that might help me even more, please let me know. I look forward to your next articles!

  10. Beth McLain | Web Designer says

    March 17, 2010 at 3:42 pm

    The information aboutCSS positions and floats are very useful to me.
    thanks for posting…

  11. Amber Weinberg says

    March 17, 2010 at 4:59 pm

    @Jan That’s interesting that you prefer to position your elements over floating. To me, it’s better to float because it plays nicer when expanding content and using repeated bkg images..but that’s just my experience :P

    I’m not sure I understand though, what you mean about abusing text properties because of layout?

  12. Kristie says

    March 18, 2010 at 9:05 am

    Thanks, this is a good refresher. Whenever I leave web dev for a time, this is the stuff that immediately leaves my head. It’s not all that intuitive I don’t think.

    thanks for your help.

  13. logolitic says

    March 18, 2010 at 4:23 pm

    very interesting and useful information, I`m curently thinking to start learn coding because it`s very useful for me as a designer.

    THanks a lot, you really made me think about it

  14. ikramhakimi says

    March 18, 2010 at 11:07 pm

    hi all,

    thanks for the information, I also love float, but
    sometimes I don’t float too much. recently i prefer this solution:

    .container {
    width: 820px;
    margin: 0 auto;
    }

    .left {
    float: left;
    width: 400px;
    background: #ccc;
    }

    .right {
    width: 400px;
    margin-left: 420px;
    background: #ccc;
    }

    so we will have 20px spacing between .left and .right,

    thanks again!

  15. nando says

    March 29, 2010 at 10:36 am

    Hi Amber, thanks for sharing this great post!

  16. Anatoly Gilderman says

    April 7, 2010 at 10:29 am

    Thanks for this very wonderful article. Can’t wait for the next one.

    I used to have difficulties in CSS and when I finally realized its importance, I did not stop until I know every single information about it.

  17. Derrick Workman says

    April 7, 2010 at 7:36 pm

    I too use floats when coding my sites, but it gave me some new insight into absolute positioning as well.

    Another great CSS article, Thanks!

  18. hybrid756 says

    October 15, 2010 at 10:30 am

    “The float rule accepts three values: left, right and none. Floated elements can only be moved using margins, not by using the top, left, right or bottom rules.”

    Bingo!

    THAT is the part I didn’t get. Now I understand what I’m trying to do :D

  19. Brett Widmann says

    November 4, 2010 at 3:56 pm

    Getting the hang of “floating” is an important aspect of design. Thanks for giving a helpful look into the CSS version.

Leave a Reply

Your email address will not be published. Required fields are marked *

Please prove you're human *

Recent Posts

  • 31 Fresh Design Elements for Spring and Easter
  • 10 Templates for Music Concert Flyers
  • How to Build a Web Scraper Using Node.js
  • Best PHP Books, Courses and Tutorials in 2022
  • How to Get Your First Web Design Client

Archives

  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • March 2016
  • February 2016
  • January 2016
  • December 2015
  • November 2015
  • October 2015
  • September 2015
  • August 2015
  • July 2015
  • June 2015
  • May 2015
  • April 2015
  • March 2015
  • February 2015
  • January 2015
  • December 2014
  • November 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • May 2014
  • April 2014
  • March 2014
  • February 2014
  • January 2014
  • December 2013
  • November 2013
  • October 2013
  • September 2013
  • August 2013
  • July 2013
  • June 2013
  • May 2013
  • April 2013
  • March 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • October 2012
  • September 2012
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • May 2008
  • April 2008

Categories

  • Accessibility
  • Android
  • Apps
  • Art
  • Article
  • Blogging
  • Books
  • Bootstrap
  • Business
  • CSS
  • Design
  • Development
  • Ecommerce
  • Fireworks
  • Flash
  • Freebies
  • Freelance
  • General
  • Giveaway
  • Graphic Design
  • HTML5
  • Icons
  • Illustrator
  • InDesign
  • Infographics
  • Inspirational
  • Interview
  • Jobs
  • jQuery
  • Learning
  • Logos
  • Matrix
  • Minimalism
  • Mobile
  • Motion Graphics
  • Music
  • News
  • Photoshop
  • PHP
  • Promoted
  • Rails
  • Resources
  • Showcase
  • Tools
  • Tutorial
  • Twitter
  • Typography
  • Uncategorized
  • Usability
  • UX
  • Wallpapers
  • Wireframing
  • WordPress
  • Work

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

SpyreStudios © 2022