You are on page 1of 4

8/4/13

CSS Positioning

Search w3schools.c Select Language HOME HTML C SS JAVASC RIPT JQUERY XML ASP.NET PHP SQL MORE...
R EFER ENC ES | EXAMPLES

Get Certified
Study Web Technologies and get a diploma at w3schools.com

CSS Basic
CSS HOME CSS Introduction CSS Syntax CSS Id & Class CSS How To

CSS Positioning
Previous Next Chapter
Positioning can be tricky sometimes! Decide which element to display in front! Elements can overlap!

CSS Styling
Styling Backgrounds Styling Text Styling Fonts Styling Links Styling Lists Styling Tables

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.

CSS Box Model


CSS Box Model CSS Border CSS Outline CSS Margin CSS Padding

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.

CSS Advanced
CSS Grouping/Nesting CSS Dimension CSS Display CSS Positioning CSS Floating CSS Align CSS Pseudo-class CSS Pseudo-element CSS Navigation Bar CSS Image Gallery CSS Image Opacity CSS Image Sprites CSS Media Types CSS Attribute Selectors CSS Summary

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 . p o s _ f i x e d { p o s i t i o n : f i x e d ; t o p : 3 0 p x ; r i g h t : 5 p x ; }
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.

CSS Examples
CSS Examples CSS Quiz CSS Certificate

CSS References
CSS Reference CSS Selectors CSS Reference Aural CSS Web Safe Fonts CSS Units CSS Colors CSS Color Values CSS Color Names CSS Color HEX

Relative Positioning
A relative positioned element is positioned relative to its normal position.

Example
h 2 . p o s _ l e f t { p o s i t i o n : r e l a t i v e ; l e f t : 2 0 p x ; } h 2 . p o s _ r i g h t { p o s i t i o n : r e l a t i v e ; l e f t : 2 0 p x ; }
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
h 2 . p o s _ t o p { p o s i t i o n : r e l a t i v e ;

w3schools.com/css/css_positioning.asp

1/4

8/4/13
t o p : 5 0 p x ; }
Try it yourself

CSS Positioning

Relatively positioned elements are often used as container blocks for absolutely positioned elements.

Related Searches: Free JavaScript Codes Basic HTML HTML Code Logo Design Services AJAX Applications ASP Programming W eb Design Templates CSS Code

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
h 2 { p o s i t i o n : a b s o l u t e ; l e f t : 1 0 0 p x ; t o p : 1 5 0 p x ; }
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
i m g { p o s i t i o n : a b s o l u t e ; l e f t : 0 p x ; t o p : 0 p x ; z i n d e x : 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). Property bottom Description Sets the bottom margin edge for a positioned box Values auto length % inherit shape auto inherit url auto crosshair default pointer move CSS 2

clip

Clips an absolutely positioned element

cursor

Specifies the type of cursor to be displayed

w3schools.com/css/css_positioning.asp

2/4

8/4/13

CSS Positioning
e-resize ne-resize nw-resize n-resize se-resize sw-resize s-resize w-resize text wait help left Sets the left margin edge for a positioned box auto length % inherit auto hidden scroll visible inherit absolute fixed relative static inherit 2

overflow

Specifies what happens if content overflows an element's box

position

Specifies the type of positioning for an element

right

Sets the right margin edge for a positioned box auto length % inherit Sets the top margin edge for a positioned box auto length % inherit number auto inherit

top

z-index

Sets the stack order of an element

Previous

Next Chapter

W3Schools' Online Certification


The perfect solution for professionals who need to balance work, family, and career building. More than 10 000 certificates already issued!

Get Your Certificate


The HTML Certificate documents your knowledge of HTML. The HTML5 Certificate documents your knowledge of advanced HTML5. The CSS Certificate documents your knowledge of advanced CSS. The JavaScript Certificate documents your knowledge of JavaScript and HTML DOM. The jQuery Certificate documents your knowledge of jQuery. The XML Certificate documents your knowledge of XML, XML DOM and XSLT. The ASP Certificate documents your knowledge of ASP, SQL, and ADO. The PHP Certificate documents your knowledge of PHP and SQL (MySQL).

Top 10 Tutorials
HTML Tutorial HTML5 Tutorial C SS Tutorial C SS3 Tutorial JavaScript Tutorial jQuery Tutorial SQL Tutorial

Top 10 References
HTML/HTML5 Reference C SS 1,2,3 Reference C SS 3 Browser Support JavaScript HTML DOM XML DOM PHP Reference

Examples
HTML Examples C SS Examples XML Examples JavaScript Examples HTML DOM Examples XML DOM Examples AJAX Examples

Quizzes
HTML Quiz HTML5 Quiz XHTML Quiz C SS Quiz JavaScript Quiz jQuery Quiz XML Quiz

Color Picke

Statistics

w3schools.com/css/css_positioning.asp

3/4

8/4/13
PHP Tutorial ASP.NET Tutorial XML Tutorial

CSS Positioning
jQuery Reference ASP.NET Reference HTML C olors ASP.NET Examples Razor Examples ASP Examples SVG Examples ASP Quiz PHP Quiz SQL Quiz

Browser Stat Browser OS Browser Disp

RE P O RT E RRO R

HO ME

TO P

P RI N T

FO RU M

A BO U T

W3Schools is optimized for learning, testing, and training. Examples might be simplified to improve reading and basic understanding. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use and privacy policy. C opyright 1999-2013 by Refsnes Data. All Rights Reserved.

w3schools.com/css/css_positioning.asp

4/4