03. Positioning an Element

There are three ways to position an element: absolute, relative and fixed.

Absolute

An element with absolute positioning is completely removed from the document flow. Thus, no other documents push it around, and you can move elements to exact locations on the screen.

To note the exact position of the element with a property of absolute, we set properties top, left, right or bottomto specify the position.

div.abs {
  position: absolute; 
  top: 10px;
  left: 10px;
}

The above code will take a div with class "abs" out of the flow and place it where it is exactly 10 pixels from the top and 10 pixels from the left.

Relative

You may place an object relative to where it's supposed to be. Again, you can use the top, left, right and bottom properties to specify its position.

div.rel {
  position: relative; 
  top: 10px;
  left: 10px;
}

The above code will place it 10 pixels down and 10 pixels to the right of where it's supposed to be.

Fixed Positioning

This will position an element relative to the viewport window. An example of this would be a navigation bar that stays up on the top of your screen. Even with the user scrolling down the page, a fixed element will keep its position.

div.fix {
  position: fixed;  
}

The element with class fix will stay in one position, no matter how the user scrolls.

Using z-index to specify element order

If boxes overlap, you may use the z-index property to control which element appears on the top. The higher z-index valued components will appear at the top.

Aching back from coding all day?

Prism Glasses

Aching back from coding all day? Try Back Problems

Ever feel achy from sitting crunched up on your computer table? Try lying down with these optical glasses that allow you to work on your laptop while lying flat on your back. This is the perfect solution with those with limited mobility or those who wish to prevent neck cramps and back strains.

$ Check price
4.454.45Amazon 4 logo(128+ reviews)

More Back Problems resources

Build modern and responsive webpages

HTML & CSS

Build modern and responsive webpages Try Front-end

This book teaches HTML as if it's for anyone - hobbyists, students, and professionals - and it's full-color throughout. It utilizes information graphics and lifestyle photography to explain the topics in a simple way that is engaging. You can progress through the chapters from beginning to end or just dip into topics of particular interest at your leisure.

$ Check price
29.9929.99Amazon 4.5 logo(926+ reviews)

More Front-end resources

Ad