Saturday, August 18, 2012

CSS Positioning


Positioning can be tricky sometimes!

Decide which element to display in front!

Elements can overlap!


Positioning

The CSS positioning properties allow you to position an element. It can also place an element behind another, and specify what should happen when an element's content 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 four 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

An element with fixed position is positioned relative to the browser window.

It will not move even if the window is scrolled:

Example

p.pos_fixed
{
position:fixed;
top:30px;
right:5px;
}

Try it yourself »

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.

Example

h2.pos_left
{
position:relative;
left:-20px;
}
h2.pos_right
{
position:relative;
left:20px;
}

Try it yourself »

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.

Example

h2.pos_top
{
position:relative;
top:-50px;
}

Try it yourself »

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>:

Example

h2
{
position:absolute;
left:100px;
top:150px;
}

Try it yourself »

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:

Example

img
{
position:absolute;
left:0px;
top:0px;
z-index:-1;
}

Try it yourself »

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.

More Examples

Set the shape of an element
This example demonstrates how to set the shape of an element. The element is clipped into this shape, and displayed.

How to show overflow in an element using scroll
This example demonstrates how to set the overflow property to create a scroll bar when an element's content is too big to fit in a specified area.

How to set the browser to automatically handle overflow
This example demonstrates how to set the browser to automatically handle overflow.

Change the cursor
This example demonstrates how to change the cursor.

All CSS Positioning Properties

The number in the "CSS" column indicates in which CSS version the property is defined (CSS1 or CSS2).

PropertyDescriptionValuesCSS
bottomSets the bottom margin edge for a positioned boxauto
length
%
inherit
2
clipClips an absolutely positioned elementshapeauto
inherit
2
cursorSpecifies the type of cursor to be displayedurlauto
crosshair
default
pointer
move
e-resize
ne-resize
nw-resize
n-resize
se-resize
sw-resize
s-resize
w-resize
text
wait
help
2
leftSets the left margin edge for a positioned boxauto
length
%
inherit
2
overflowSpecifies what happens if content overflows an element's boxauto
hidden
scroll
visible
inherit
2
positionSpecifies the type of positioning for an elementabsolute
fixed
relative
static
inherit
2
rightSets the right margin edge for a positioned boxauto
length
%
inherit
2
topSets the top margin edge for a positioned boxauto
length
%
inherit
2
z-indexSets the stack order of an elementnumberauto
inherit
2


CSS Display and Visibility


The display property specifies if/how an element is displayed, and the visibility property specifies if an element should be visible or hidden.

Box 1

Box 2
 
Box 3
 

Hiding an Element - display:none or visibility:hidden

Hiding an element can be done by setting the display property to "none" or the visibility property to "hidden". However, notice that these two methods produce different results:

visibility:hidden hides an element, but it will still take up the same space as before. The element will be hidden, but still affect the layout.

Example

h1.hidden {visibility:hidden;}

Try it yourself »

display:none hides an element, and it will not take up any space. The element will be hidden, and the page will be displayed as if the element is not there:

Example

h1.hidden {display:none;}

Try it yourself »


CSS Display - Block and Inline Elements

A block element is an element that takes up the full width available, and has a line break before and after it.

Examples of block elements:
  • <h1>
  • <p>
  • <div>
An inline element only takes up as much width as necessary, and does not force line breaks.
Examples of inline elements:
  • <span>
  • <a>

Changing How an Element is Displayed

Changing an inline element to a block element, or vice versa, can be useful for making the page look a specific way, and still follow web standards.

The following example displays list items as inline elements:

Example

li {display:inline;}

Try it yourself »

The following example displays span elements as block elements:

Example

span {display:block;}

Try it yourself »

Note: Changing the display type of an element changes only how the element is displayed, NOT what kind of element it is. For example: An inline element set to display:block is not allowed to have a block element nested inside of it.

More Examples

How to display an element as an inline element.
This example demonstrates how to display an element as an inline element.

How to display an element as a block element
This example demonstrates how to display an element as a block element.

How to make a table element collapse
This example demonstrates how to make a table element collapse.

CSS Dimension


The CSS dimension properties allow you to control the height and width of an element.

Try it Yourself - Examples

Set the height of elements
This example demonstrates how to set the height of different elements.

Set the height of an image using percent
This example demonstrates how to set the height of an element using a percent value.

Set the width of an element using a pixel value
This example demonstrates how to set the width of an element using a pixel value.

Set the maximum height of an element
This example demonstrates how to set the maximum height of an element.

Set the maximum width of an element using percent
This example demonstrates how to set the maximum width of an element using a percent value.

Set the minimum height of an element
This example demonstrates how to set the minimum height of an element.

Set the minimum width of an element using a pixel value
This example demonstrates how to set the minimum width of an element using a pixel value.


All CSS Dimension Properties

The number in the "CSS" column indicates in which CSS version the property is defined (CSS1 or CSS2).

PropertyDescriptionValuesCSS
heightSets the height of an elementauto
length
%
inherit
1
max-heightSets the maximum height of an elementnone
length
%
inherit
2
max-widthSets the maximum width of an elementnone
length
%
inherit
2
min-heightSets the minimum height of an elementlength
%
inherit
2
min-widthSets the minimum width of an elementlength
%
inherit
2
widthSets the width of an elementauto
length
%
inherit
1


CSS Grouping or Nesting


Grouping Selectors

In style sheets there are often elements with the same style.

h1
{
color:green;
}
h2
{
color:green;
}
p
{
color:green;
}

To minimize the code, you can group selectors.
Separate each selector with a comma.

In the example below we have grouped the selectors from the code above:

Example

h1,h2,p
{
color:green;
}

Try it yourself »


Nesting Selectors

It is possible to apply a style for a selector within a selector.

In the example below, one style is specified for all p elements, one style is specified for all elements with class="marked", and a third style is specified only for p elements within elements with class="marked":

Example

p
{
color:blue;
text-align:center;
}
.marked
{
background-color:red;
}
.marked p
{
color:white;
}

Try it yourself »