Hacker News new | past | comments | ask | show | jobs | submit login
CSS Positioning 101 (alistapart.com)
190 points by duck on Nov 16, 2010 | hide | past | favorite | 34 comments



If you're like me and like a quick-summary over a lengthy article, you might like this article: 'Learn CSS Positioning in Ten Steps'[0]. It quickly summarizes all the positionining properties, and how top/left/float/clear etc helps.

[0]: http://www.barelyfitz.com/screencast/html-training/css/posit...


I actually just logged in to leave a link to the Barley Fitz version. I've used his primer more times than I can count.


I like this one a lot. Simpler CSS, less verbose, and the in-context visual examples make it a lot easier to follow along.


nicely explained. one key trick they're missing: position:relative + position:absolute, which I find myself using more than position:absolute alone. With a parent of position:relative, child position:absolute elements are position absolutely within the parent. Nice for centering overlays and things like that.


In the full example at the end this is used twice actually.


Still though, when they're explaining absolute positioning they make this statement:

"You can set an element to top: 100px and left: 200px; and that element will sit exactly 100px from the top and 200px from the left of the document."

Which seems misleading, despite the example they show later.


ah, good point, "Our #callout element is set to position: absolute, and since its parent element, #content is set to relative, the offset properties we’re using on #callout are based off the coordinates created by #content."

would be nice to see it called out as one of the core techniques above though. or I guess I could have just read the whole article before commenting :)


One of the defining articles on this exact topic was written by Doug Bowman back in 2003:

http://stopdesign.com/archive/2003/09/03/absolute.html


Once you understand all this, you still need to KISS. Going overboard with clever CSS manipulation has two major drawbacks:

1) "Fragile" layouts, where simple changes break the entire UI.

2) Broken javascript - as an eample, jQuery-UI uses the positioning in the DOM to calculate where to display some UI elements. Mixing relative and absolute positioning breaks these functions.


IMHO 'CSS positioning 101' will not be complete without mentioning the (in)famous float property.


Float's aren't that bad, other than the fact that the parent element will suddenly lose any inherent height if all child elements have floats.

I've got a few friends who extol the virtues of inline-block, but my clients have viewer demographics of 70% ie6.

Hack here, if you really want inline-block with ie6: http://www.aaronrussell.co.uk/blog/cross-browser-support-for...


If you set overflow to auto on the parent element it will retain its height.


You also need to set the width. Explanation here

http://www.quirksmode.org/css/clearing.html


Quirksmode does an excelent job explaining this. And covering ie/chrome/safari bugs adds extra credit.


I usually put a <div style="clear:both"></div> after the floats to fix that.


I just tested it because it sounded so beautiful but that does not work in IE6 unfortunately.


It has worked for me. Does the container have hasLayout?


Excellent. Thanks for the tip!


In general are floats now out of favor, and one should one of the layout methods listed on this page?


If you're interested in all of the details, the spec itself is a good read: http://www.w3.org/TR/CSS2/cover.html

Section 9, "Visual formatting model" is especially useful: http://www.w3.org/TR/CSS2/visuren.html


I had an AHA moment few weeks ago, when I discovered aplying something like that:

  .smth{
    position:absolute;
    top:0px;
    left:0px;
    right:0px;
    bottom:0px;
  }
It beautifully solved 2 interfaces I was working on (webapps with fluid widths). I remember being smiling for 2 hours after discovering this.

Not sure about IE support though, i think its covered from IE8 and ahead


What problem did it solve? BTW you can omit 'px'


I wanna cry, i had been writing a beautiful comment with examples, explanations, divagations, and when i was done i hit ctrl+R out of habit from developing.

I really wanna cry... Please implement a cookie based autosave for comment forms :'(

Briefing of my hardworked comment that will never be read: -----------------------------------

I know about the px thing.

Fluid interfaces and traditional box model are wrong because padding and border mess things up You cant substract % - px, and % in width and height is calculated without taking in account border and padding.

I gave an example (wont rewrite it again)

I gave a few solutions: * extra markup inside the boxes with % ( ex. div.absolute>div.padding20>content ) Ugly and unsemantic, but usable * using positioning top/bottom and left/right for defining widths and heights This only works if you define boths of properties, anyway else if you specify width/height with padding/border it screws you * use new CSS3 box-sizing property Not compatible in all browsers, but maybe the best option thinking to future

Links to ilustrate: Traditional box model http://www.w3schools.com/css/css_boxmodel.asp Optional box models http://www.w3.org/TR/css3-ui/#box-sizing

I also said that a webapp like google calendar would benefit from this things, right now in chrome it gives me 2 scrollbars on the right side, one for the body and another one for the calendar pane.

Uh and heights is crappy because an element only expands to 100% height if parent is 100% height, anyway else it doesnt do shit. This is pain in the ass, redundant and messy between browsers.

Better option is to specify each box coordinates and anchors (and subsequently width and heigth) with position absolute top/left/right/bottom and then define padding without having troubles.

If somebody asks more i will jsfiddle some examples


If you're using Firefox, try Lazarus: https://addons.mozilla.org/en-US/firefox/addon/6984/


Great article to add to a FE Developer syllabus.

I've found no better way to learn these details than just opening up a text file and browser and trying out my own wacky layout experiments.

It's even more fun with other devs! Modify the CSS/HTML in different ways and take bets on how it will look before hitting refresh.

When in doubt, go back to the specs and find your explanations there! http://www.w3.org/TR/CSS21/visuren.html#propdef-position


If you have an iPad, you might want to take a look at cssMachine. It lets you try out the more graphical CSS properties with intuitive controls (rather than just typing code, although you can do that too)


This is probably off-topic. But can any design gurus recommend a beginner CSS and a Photoshop book? Is site-point books any good? Thanks for the help in advance.


For CSS, I've recommended "Learning Web Design" by Jennifer Niederst Robbins to several people. It's a bit out of date now (doesn't cover HTML5 and CSS3) but you shouldn't be learning the latest additions before you have a solid grasp of the basics anyway.

I've been unable to find a good introductory Photoshop book.


Are people still creating custom CSS for layout? I fell in love with the grid css frameworks and haven't looked back. Looking at most of the websites that come through these parts, I'd say they have too...

Do you still write css for layout?


eh, I'll take the bait.

Whether you're using 960gs or whatever it may be, you still need to be considerate of position and "write css for layout".

If you find yourself always using grid layouts, not just as guides but as the way to lay out everything, then you will soon find yourself caged inside that grid.


This is exactly it. The move away from tables had the side benefit of being able to move away from row and column thinking (there will always be an underlying grid in good design, but breaking it is where a good designer really adds value).

If you don't stick with the grid the framework provides you are just writing custom CSS anyway, at which point you probably wonder why you have another CSS file with lots of code that you are only using 50% of the time and that comes with it's own maintenance issues that you don't need.

I would also argue that from a maintanence point of view, semantically smart class and id names make it much easier to work with compared with a page full of "r3 w50 h10" type attribute properties.

To stick my flag to the mask, I hate CSS "frameworks" with something of a mild passion. CSS is too domain (domain in this sense I guess is a synonym for project) specific and abstract to benefit from a framework, you are much better keeping it lean and tidy and owning every line you write, because CSS is the kind of language where you should justify every line to yourself.


Good points. However I have to say I love frameworks (compass?) that handle all the cross-browser / ie6 stuff for me. If that is not reason enough to use CSS frameworks, I don't know what is.


Same. I've recently been experimenting with several - Less Framework, Less.js, Compass, and Flawless.css. html5boilerplate is also nice for starting templates.

Bypassing those niggling pita xbrowser/ie6 issues with just a 'git clone' is clutch.


If only we could use this in emails




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: