CSS / CSS Positioning and Z-Index

Mastering CSS Positioning

This tutorial will provide you with a solid understanding of CSS positioning. We'll cover static, relative, absolute, and fixed positioning, and how each impacts the layout of you…

Tutorial 1 of 5 5 resources in this section

Section overview

5 resources

Explains how to control element positioning and stacking order.

1. Introduction

In this tutorial, our goal is to master CSS positioning. We will delve into the four main types of positioning: static, relative, absolute, and fixed. Understanding how these work will allow you to control where and how elements are displayed on your pages.

By the end of this tutorial, you will be able to:
- Understand the differences between static, relative, absolute, and fixed positioning
- Apply these positioning concepts to your CSS code
- Manipulate the layout of your webpage using positioning

This tutorial assumes basic knowledge of HTML and CSS. If you're new to these, you may want to get familiar with them first.

2. Step-by-Step Guide

Static Positioning

This is the default positioning for HTML elements. They are positioned according to the normal flow of the page, left to right and top to bottom.

Relative Positioning

An element with relative positioning is positioned relative to its normal position. The 'top', 'right', 'bottom', and 'left' properties can be used to move the element from its normal position.

Absolute Positioning

An absolute positioned element is positioned relative to the nearest positioned ancestor. It is taken out of the normal flow of the page and no space is created for the element in the page layout.

Fixed Positioning

A fixed positioned element is positioned relative to the browser window. Even if the page is scrolled, the element will stay in the same place.

3. Code Examples

Static Positioning

div {
    position: static;
}

This is the default, so this code doesn't change the position of the div.

Relative Positioning

div {
    position: relative;
    top: 20px;
    left: 20px;
}

This code will move the div 20px down and 20px to the right from its normal position.

Absolute Positioning

div {
    position: absolute;
    top: 30px;
    right: 10px;
}

This code positions the div 30px from the top and 10px from the right of its nearest positioned ancestor.

Fixed Positioning

div {
    position: fixed;
    bottom: 0;
    right: 0;
}

This div will always be positioned at the bottom right corner of the window, even when scrolling.

4. Summary

In this tutorial, we learned about the four types of CSS positioning: static, relative, absolute, and fixed. We also saw how they impact the layout of a webpage.

For your next steps, experiment with these different positioning styles on your own projects. Try combining them and see how they interact with each other.

You can also explore the CSS z-index property, which works in tandem with positioning to control the stack order of elements.

5. Practice Exercises

  1. Create a page with five divs, assign each a different type of positioning, and observe the results.
  2. Stack two divs on top of each other using absolute positioning.
  3. Create a fixed header that stays at the top of the page when scrolling.

Solutions are available, but try to solve it on your own first. Remember, practice makes perfect. Happy coding!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Percentage Calculator

Easily calculate percentages, discounts, and more.

Use tool

PDF Password Protector

Add or remove passwords from PDF files.

Use tool

Backlink Checker

Analyze and validate backlinks.

Use tool

Word to PDF Converter

Easily convert Word documents to PDFs.

Use tool

WHOIS Lookup Tool

Get domain and IP details with WHOIS lookup.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help