October 1, 2022

Robotic Notes

All technology News

Understanding CSS positioning

6 min read


In this article, we’ll cover a crucial concept of CSS: CSS positioning. When working on complex layouts on a webpage, you’ll need to alter the natural document flow to change how elements are positioned on a webpage. For example, you may want to have elements overlap each other, display side by side, or in a certain way. We will see the different CSS position types and how they apply.

At the end of this article, we’ll fully understand the CSS position property and the different position types, with clear code samples on each position type and how they apply.

What is the CSS position property?

The CSS position property is used to modify how elements are placed on a webpage. With the CSS position property, you can adjust the position of an element on your webpage. The CSS position property allows us to move our elements around the webpage using the top, left, rightand bottom properties.

top sets the top margin for a positioned element
bottom sets the bottom margin for a positioned element
left sets the left margin for a positioned element
right sets the right margin for a positioned element

1 movement

Positive values ​​move inwards, while negative values ​​move outwards.

Note: Without setting a position type, the top, left, right, and bottom properties will have no effect.

Types of CSS Positions

The following are the five different types of CSS positions:

  • static
  • relative
  • absolutely
  • fixed
  • sticky

Now let’s look into each position type in detail.

Static position

position: static is the default CSS position. All elements, by default, have a static position. With the static position, you cannot adjust the placement of elements on a webpage.

Let’s look at an example: here’s a webpage with a single div element on it. By default, it already has position: staticbut we’ll be adding it to confirm.

1<!DOCTYPE html>

2<html>

3<head>

4 <title>CSS Position</title>

5 <style>

6 body{

7 background: yellow;

8}

9 .box{

10 width: 80%;

11 height: 30vh;

12 background: red;

13 }

14 </style>

15</head>

16<body>

17 <div class="box">

18 </div>

19</body>

20</html>

Here’s the result of the initial code:

2

Now let’s try to move it around with the left and top properties.

1.box{

2 width: 80%;

3 height: 30vh;

4 background: red;

5 position: static;

6 left: 100px;

7 top: 80px;

8}

Here’s the result:

3

Notice you can’t see any difference? An element with the position: static cannot be moved around with the top, left, right, and bottom properties.

Relative position

In position: relative, the element is positioned according to its original position. If we position a box with position: relativethe box remains in its original position.

Let’s look at an example to understand better.

1<!DOCTYPE html>

2<html>

3<head>

4 <title>CSS Position</title>

5 <style>

6 body{

7 margin: 0;

8 }

9 .parent{

10 background: yellow;

11 display: flex;

12 align-items: center;

13 justify-content: center;

14 height: 100vh;

15 }

16

17 .box{

18 width: 50%;

19 height: 40vh;

20 background: red;

21 }

22 </style>

23</head>

24<body>

25 <div class="parent">

26 <div class="box">

27 </div>

28 </div>

29</body>

30</html>

The code on the parent element is me trying to center-align the sub-element horizontally and vertically. So don’t worry about that. Centering the element will help you understand the movement of what we’ll be doing easier.

Here’s the output of the initial code:

4

Now let’s add position: relative to the box and try to move it around.

1.box{

2 width: 50%;

3 height: 40vh;

4 background: red;

5 position: relative;

6}

Here’s our output after adding position: relative

5

Yes, the output remains the same after adding position: relative. As we mentioned earlier, in position: relative, the element is positioned according to its original position. Also, if we are to try to move our element with a value of 0, it remains at the same position. Because it maintains its original position, moving with a 0 value takes no effect.

Here’s an example:

1.box{

2 width: 50%;

3 height: 40vh;

4 background: red;

5 position: relative;

6 left: 0;

7 top: 0;

8}

Here’s the output:

6

The output remains the same.

Let’s look at another example of moving the box around with actual values.

1.box{

2 width: 50%;

3 height: 40vh;

4 background: red;

5 position: relative;

6 left: 120px;

7 top: 120px;

8}

Here’s the output:

7

We can see that the box moved away, 120px from the left of its original position and 120px from the top of its original position.

Open Source Session Replay

OpenReplay is an open-source, session replay suite that lets you see what users do on your web app, helping you troubleshoot issues faster. OpenReplay is self-hosted for full control over your data.

replayer.png

Start enjoying your debugging experience – start using OpenReplay for free.

Absolute position

In position: absolute elements are positioned according to the nearest containing (parent) element. Also, the parent element has to be positioned using position: relative.

Let’s look at an example with two boxes, setting the red box to position: relative and the blue box position: absolute.

1.box1{

2 display: flex;

3 justify-content: center;

4 align-items: center;

5 width: 50%;

6 height: 40vh;

7 background: red;

8 position: relative;

9}

10

11.box2{

12 background: blue;

13 width: 40%;

14 height: 40%;

15 position: absolute;

16}

Here’s the output:

8

Now let’s try to move the blue box with the position: absolute. Like we said, in position: absolute the element is positioned according to its parent element, which is the red box in this case.

1.box2{

2 background: blue;

3 width: 40%;

4 height: 40%;

5 position: absolute;

6 top: 0;

7}

Here’s the output:

9

Notice that the box moved to the top of the parent element? Like we said, in absolute positioning, the element is positioned according to its parent element, so having a top with a value of 0, moves it to a top margin of 0 relative to its parent element.

let’s look at one more example:

1.box2{

2 background: blue;

3 width: 40%;

4 height: 40%;

5 position: absolute;

6 bottom: 50px;

7 right: 50px;

8}

Here’s what the output looks like:

10

The box moves 50px from the bottom of the parent element and 50px from the right of the parent element.

Fixed position

In position: fixed, the element is positioned according to the viewport, which is the browser window itself. According to the viewport, the element will always remain fixed at the positioned spot.

Let’s look at a similar example to the previous one. Let’s make the blue box position: fixed. Also, add a third box to see the effect of the fixed position while scrolling.

1body{

2 margin: 0;

3}

4.parent1, .parent2{

5 background: yellow;

6 display: flex;

7 align-items: center;

8 justify-content: center;

9 height: 100vh;

10}

11

12.box1{

13 width: 50%;

14 height: 40vh;

15 background: red;

16 position: relative;

17}

18

19.box2{

20 background: blue;

21 width: 20%;

22 height: 20%;

23 position: fixed;

24 top: 50px;

25 right: 50px;

26}

27

28.box3{

29 width: 50%;

30 height: 40vh;

31 background: green;

32 position: relative;

33}

Here’s a look at the output: The blue box remains fixed while we scroll through the page.

11
Source

Here’s a different example, with a header fixed at the top.

12
Source

Sticky position

The option position: sticky acts like a hybrid between relative and fixed. The element is positioned relative until a specified offset position is met while scrolling, then it sticks at a spot just like the fixed position.

Let’s see an example: we’ll be using the previous example of the fixed header, moving the header downwards for clarity.

1.stick{

2 background: #000;

3 height: 20vh;

4 width: 100%;

5 position: sticky;

6 top: 0;

7 color: #fff;

8 padding-left: 60px;

9}

Here’s the output: It acts as relative until the element meets the specified scroll position of top:0. Then, it acts as fixed, and sticks at the specified spot.

13
Source

Z-index

The z-index property specifies the stacking order of elements. In a case where multiple elements are stacked, the z-index property helps control how these elements stack on each other. Elements with a higher z-index stay on top of elements with a lower z-index. Let’s look at an example of three boxes stacked on each other:

1.red{

2 width: 30%;

3 height: 40vh;

4 background: red;

5 position: absolute;

6 }

7

8.blue{

9 background: blue;

10 width: 30%;

11 height: 40vh;

12 position: absolute;

13 top: 50px;

14 left:50px;

15}

16

17.green{

18 width: 30%;

19 height: 50vh;

20 background: green;

21 position: absolute;

22 left: 150px;

23 top: 30px;

24}

Here’s the output:

14
Source

In a case where we want the blue box to display in front of both the green and red box, all we have to do is give the blue box a z-index with a higher value.

Let’s do that:

1.red{

2 width: 30%;

3 height: 40vh;

4 background: red;

5 position: absolute;

6 z-index:1;

7 }

8

9.blue{

10 background: blue;

11 width: 30%;

12 height: 40vh;

13 position: absolute;

14 top: 50px;

15 left:50px;

16 z-index:3;

17}

18

19.green{

20 width: 30%;

21 height: 50vh;

22 background: green;

23 position: absolute;

24 left: 150px;

25 top: 30px;

26 z-index: 2;

27}

Here’s the output:

15
Source

The blue box has the highest z-index, it displays over both boxes. The green box has the next highest z-index, it displays above the red box.

Resources

Conclusion

Hopefully, we now understand the CSS position property, the different types of CSS positions, and how they apply. Now we can go on to position our elements around web pages properly.

A TIP FROM THE EDITOR: For an interesting way to include CSS in your pages, look at our Incorporating CSS to a Page via HTTP Headers article.

newsletter



Source link