Article

CSS Positioning Properties At-A-Glance Guide

Page: 1 2

Positioning Schemes

http://www.w3.org/TR/REC-CSS2/visuren.html#positioning-scheme

There are five styles of positioning in CSS 2.

Normal Flow
http://www.w3.org/TR/REC-CSS2/visuren.html#normal-flow

Normal positioning is set according to the “normal” rules that have been around for nearly a decade.

Relative Positioning
http://www.w3.org/TR/REC-CSS2/visuren.html#relative-positioning

Relatively positioned elements are positioned according to the normal flow and then moved. Elements that come after a relatively-positioned element behave as if the relatively-positioned element was in its ‘normal flow’ position, even if this means they occupy the same screen space.

Float Positioning
http://www.w3.org/TR/REC-CSS2/visuren.html#floats

Boxes positioned using float are positioned using normal flow and then moved left or right as far as they can go. Elements that appear after them will move up to fill any gap left behind, but will flow around the box — they will not occupy the same screen space.

Absolute Positioning
http://www.w3.org/TR/REC-CSS2/visuren.html#absolute-positioning

Absolutely positioned elements are not affected by normal flow. They are positioned by specifying precise distances that will exist between their sides and their containing block.

Fixed Positioning
http://www.w3.org/TR/REC-CSS2/visuren.html#fixed-positioning

Fixed-position
elements work in the same way as absolutely-positioned elements, except their position is relative to the Viewport. As such, they don’t move if the page is scrolled — they stay relative to the Viewport.

CSS Visual Display Properties

The display Property
http://www.w3.org/TR/REC-CSS2/visuren.html#display-prop

The display property is used to control the box type used for an element. This allows you to do things like:

  • set bold text to be block level,
  • have inline paragraphs, or
  • stop an element from being displayed or taking up screen space.

display:block

Sets the elements selected to block level.

display:inline

Sets the elements selected to inline.

display:none

Stops the display of the element to which it is assigned. The element is completely removed from the display—it doesn’t take up any screen space.

There are a number of other values for the display property which we’re not concerned with in this article. They deal with lists, tables and other box types that we won’t cover here, but which are covered in detail in SitePoint's online CSS reference.

The position Property
http://www.w3.org/TR/REC-CSS2/visuren.html#choose-position

The position property is used to specify the following positioning schemes:

  • Normal Flow,
  • Relative Positioning,
  • Absolute Positioning, and
  • Fixed Positioning.

This is done by setting position to one of the following values:

position:static;

Set this box to normal flow, the default for all elements.

position:relative;

Use relative positioning for this box using the top, right, bottom and left properties.

position:absolute;

Use absolute positioning for this box using the top, right, bottom and left properties.

position:fixed;

Use fixed positioning for this box using the top, right, bottom and left properties.

The top, right, bottom and left Properties
http://www.w3.org/TR/REC-CSS2/visuren.html#position-props

All positioning schemes need you to set these in order to know how to carry out your positioning request. You should always set one of top or bottom and one of left or right. Setting both top and bottom or both left and right will not usually do what you want in Internet Explorer – usually, you’ll end up with only left or top being used respectively.

You may use the following values for these properties:

Fixed Length
You may use a fixed distance such as 20px for 20 pixels.

Percentage
You may use a percentage value such as 20%, which is 20% of the containing block’s width, or height value (for left/right or top/bottom respectively).

The float Property
http://www.w3.org/TR/REC-CSS2/visuren.html#float-position

The float property takes three values. float is ignored if you have set the position property to either absolute or fixed.

float:right;
The box is floated to the right of the screen.

float:left;
The box is floated to the left of the screen.

float:none;
The box is not floated.

The clear Property
ttp://www.w3.org/TR/REC-CSS2/visuren.html#flow-control

This property is used to give control of boxes that appear after a floated box. Setting this property on a box will ensure that it will appear below any floated boxes that come before it.

clear:left;
This box will appear below any left-floating boxes that come before it.

clear:right;
This box will appear below any right-floating boxes that come before it.

clear:both;

This box will appear below any left-floating or right-floating boxes that come before it.

clear:none;
This box will not clear any floating boxes that come before it.

The z-index Property
http://www.w3.org/TR/REC-CSS2/visuren.html#q30

The z-index property is used to specify how boxes stack ‘on top’ of each other and is relevant when you have two boxes that occupy the same screen space. z-index accepts a value between 0 and 32767. In a situation where two boxes occupy the same screen space the box with the highest z-index is displayed ‘on top’. If both have the same value the one that comes last in the code is displayed on top.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links

Rate This Article

  • 1
    Poor
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
    Great

Comment on This Article

Have something to say?

Post A Comment

You need to be a member of the SitePoint Forums to comment on this post. Sign Up

Already a member? Post using your SitePoint Forums account: