Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is Position in CSS?

In this blog post, we will cover a fundamental concept in CSS called position. CSS, which stands for Cascading Style Sheets, is a language used for styling web pages. If you think of a web page as a house, HTML would be the bricks and mortar that build the structure, while CSS would be the paint and decorations that make it look nice. So, let's dive into the world of positioning in CSS and make our web pages look even better!

Understanding the Box Model

Before we discuss positioning, it's essential to understand the CSS box model. In CSS, every element on a web page is treated as a rectangular box. This box consists of:

  1. Content: The actual text or image within the element.
  2. Padding: The space between the content and the border.
  3. Border: The line that surrounds the content and padding.
  4. Margin: The space outside the border that separates the element from other elements.

Here's a visual representation of the box model:

+-----------------------+
|       Margin          |
| +-------------------+ |
| |      Border       | |
| | +---------------+ | |
| | |    Padding    | | |
| | | +-----------+ | | |
| | | |  Content  | | | |
| | | +-----------+ | | |
| | +---------------+ | |
| +-------------------+ |
+-----------------------+

With this understanding of the box model, let's move on to positioning.

Types of Positioning

In CSS, there are five types of positioning:

  1. Static (default)
  2. Relative
  3. Absolute
  4. Fixed
  5. Sticky

We'll discuss each of these in detail, along with code examples to help you understand how they work.

Static Positioning

Static positioning is the default positioning for every element on a web page. When an element has a static position, it appears in the normal flow of the document, which means it will be placed according to the order of the HTML code. You cannot use top, right, bottom, or left properties with static positioning.

Here's an example of static positioning:

<!DOCTYPE html>
<html>
<head>
<style>
    div {
        width: 100px;
        height: 100px;
        background-color: red;
    }
</style>
</head>
<body>

<div></div>
<div></div>
<div></div>

</body>
</html>

In this example, we have three div elements with a width, height, and background color. Since their position is static (by default), they will appear one after another in the order they are written in the HTML.

Relative Positioning

Relative positioning allows you to move an element from its normal position in the document flow. However, unlike other positioning methods, the space it originally occupied will still be preserved. This means that other elements around it will not be affected by its new position.

To use relative positioning, set the position property to relative and use the top, right, bottom, and left properties to move the element. These properties accept values in various units, such as pixels (px), percentages (%), or ems (em).

Here's an example of relative positioning:

<!DOCTYPE html>
<html>
<head>
<style>
    div {
        width: 100px;
        height: 100px;
        background-color: red;
    }
    .relative {
        position: relative;
        left: 50px;
        top: -20px;
    }
</style>
</head>
<body>

<div></div>
<div class="relative"></div>
<div></div>

</body>
</html>

In this example, we have the same three div elements as before, but now the second div has a class called relative. This class sets its position to relative and moves it 50 pixels to the right and 20 pixels up. However, the space it originally occupied is still preserved, so the third div remains in its original position.

Absolute Positioning

Absolute positioning allows you to position an element relative to its nearest positioned ancestor. If no positioned ancestor is found, it will be positioned relative to the initial containing block, which is usually the html or body element.

To use absolute positioning, set the position property to absolute and use the top, right, bottom, and left properties to move the element.

Here's an example of absolute positioning:

<!DOCTYPE html>
<html>
<head>
<style>
    .container {
        position: relative;
        border: 1px solid black;
        height: 200px;
    }
    div {
        width: 100px;
        height: 100px;
        background-color: red;
    }
    .absolute {
        position: absolute;
        right: 0;
        bottom: 0;
    }
</style>
</head>
<body>

<div class="container">
    <div></div>
    <div class="absolute"></div>
</div>

</body>
</html>

In this example, we have a div with a class called container that has a border and a height. Inside the container, there are two more div elements. The second div has a class called absolute, which sets its position to absolute and moves it to the bottom-right corner of the container. Since the container has a relative position, the absolute positioning of the second div is relative to the container.

Fixed Positioning

Fixed positioning is similar to absolute positioning, but it positions an element relative to the browser window, rather than its nearest positioned ancestor. This means that the element will remain in the same position even if the page is scrolled.

To use fixed positioning, set the position property to fixed and use the top, right, bottom, and left properties to move the element.

Here's an example of fixed positioning:

<!DOCTYPE html>
<html>
<head>
<style>
    div {
        width: 100px;
        height: 100px;
        background-color: red;
    }
    .fixed {
        position: fixed;
        right: 0;
        bottom: 0;
    }
</style>
</head>
<body>

<div></div>
<div class="fixed"></div>
<div style="height: 2000px;"></div>

</body>
</html>

In this example, we have a div with a class called fixed, which sets its position to fixed and moves it to the bottom-right corner of the browser window. If you scroll down the page, you'll see that the fixed div remains in the same position.

Sticky Positioning

Sticky positioning is a combination of relative and fixed positioning. An element with sticky positioning will behave like a relatively positioned element until it reaches a specified point, at which point it will become fixed.

To use sticky positioning, set the position property to sticky and use the top, right, bottom, or left properties to specify the point at which the element should become fixed.

Here's an example of sticky positioning:

<!DOCTYPE html>
<html>
<head>
<style>
    div {
        width: 100%;
        height: 100px;
        background-color: red;
    }
    .sticky {
        position: sticky;
        top: 0;
    }
</style>
</head>
<body>

<div></div>
<div class="sticky"></div>
<div style="height: 2000px;"></div>

</body>
</html>

In this example, we have a div with a class called sticky, which sets its position to sticky and specifies that it should become fixed when its top edge reaches the top edge of the browser window. As you scroll down the page, you'll see that the sticky div becomes fixed once it reaches the specified point.

Conclusion

Positioning in CSS is a powerful tool that allows you to control the layout of your web pages. By understanding the different types of positioning and how they work, you can create more visually appealing and functional web designs. Practice using static, relative, absolute, fixed, and sticky positioning in your projects, and you'll soon become a master of CSS layout techniques!