Pages

Thursday 24 May 2012

Positioning Property and Z-Index in CSS

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:

1. 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.

2. Fixed Positioning

An element with fixed position is positioned relative to the browser window. It will not move even if the window is scrolled:
p
{
position:fixed;
top:30px;
right:5px;
}

Fixed positioned elements are removed from the normal flow. The document and other elements behave like the fixed positioned element does not exist.

3. Relative Positioning

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

h2
{
position:relative;
left:-20px;
}
or
h2
{
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.

4. 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.

Z-INDEX Property (Overlapping Elements)

The z-index property specifies the stack order of an element. An element with greater stack order is always in front of an element with a lower stack order.

Note: z-index only works on positioned elements (position:absolute, position:relative, or position:fixed).

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

No comments:

Post a Comment

About the Author

I have more than 10 years of experience in IT industry. Linkedin Profile

I am currently messing up with neural networks in deep learning. I am learning Python, TensorFlow and Keras.

Author: I am an author of a book on deep learning.

Quiz: I run an online quiz on machine learning and deep learning.