Saturday, August 27, 2011

Advanced CSS3 Tutorial 2: Positioning

The CSS positioning properties allow you to position an element. It also allows you to place an element behind another, and specify what should happen when an element's conetent is too big.
Elements can be positioned using the top, bottom, left, and right properties. However, these properties will not work unless the position property is set first. They also work differently depending on the positioning method.
There are 4 different positioning methods.

Static Positioning
HTML elements are positioned static by default. A static positioned element is always positioned according to the normal flow of the page.
Static positioned elements are not affected by the top, bottom, left, and right properties.

Fixed Positioning
And element with a fixed position is positioned relative to the browser window, it will not move even if the window is scrolled.
p.pos_fixed
{
position:fixed;
top:30px;
right:5px;
}
Note: IE7 and IE8 support the fixed value only if a !DOCTYPE is specified.
Fixed positioned elements are removed from the normal flow. The document and other elements behave like the fixed positioned element does not exist.
Fixed positioned elements can overlap other elements.

Relative Positioning
A relative positioned element is positioned relative to its normal position.
h2.pos_left
{
position:relative;
left:-20px;
}
h2.pos_right
{
position:relative;
left:20px;
}
The content of relatively positioned elements can be moved and overlap other elements, but the reserved space for the element is still preserved in the normal flow.
h2.pos_top
{
position:relative;
top:-50px;
}
Relatively positioned elements are often used as container blocks for absolutely positioned elements.

Absolute Positioning
An absolute position element is positioned relative to the first parent element that has a position other than static. If no such element is found, the containing block is <html>:h2
{
position:absolute;
left:100px;
top:150px;
}
Absolutely positioned elements are removed from the normal flow. The document and other elements behave like the absolutely positioned element does not exist.
Absolutely positioned elements can overlap other elements.

Overlapping Elements
When elements are positioned outside the normal flow, they can overlap other elements.
The z-index property specifies the stack order of an element (which element should be placed in front of, or behind, the others).
An element can have a positive or negative stack order:
img
{
position:absolute;
left:0px;
top:0px;
z-index:-1
}
An element with greater stack order is always in front of an element with a lower stack order.
Note: If two positioned elements overlap, without a z-index specified, the element positioned last in the HTML code will be shown on top.

15 comments:

  1. Love this blog post! A really interesting read! Followed!

    ReplyDelete
  2. Very useful guide, keep up the good work!

    ReplyDelete
  3. Few, I have to start making websites starting on Monday, so I'll use what I need.

    ReplyDelete
  4. I can always use another CSS/HTML course :)

    ReplyDelete
  5. I gotta go over this a couple times, but it's starting to make sense to me. Thanks for the free tutorial! +followed

    ReplyDelete
  6. Nice tutorial as always. Useful info here.

    ReplyDelete
  7. Cool I've trying to do this like for a couple of hours and I was getting stressed out Thx man

    ReplyDelete
  8. I don't know much of these languages. but im intrested in python

    ReplyDelete
  9. It all makes sense now. Thanks for the tutorial.

    ReplyDelete
  10. It's nice how you defined every term before posting their codes.. Thanks.

    ReplyDelete
  11. a pretty good description, makes it a lot easier

    ReplyDelete
  12. Agreed, Really good tutorial, will be following, as I always need a little help with coding!

    ReplyDelete
  13. This is some of the best tutorials I've seen in awhile.

    ReplyDelete