Positioning in CSS

Positioning is one of the most important properties of CSS, as it allows HTML elements to be positioned in different places on the page. There are four main types of positioning in CSS: static, relative, absolute, and fixed.

Static Positioning

Static positioning is the default for all HTML elements. In this type of positioning, the element is positioned according to the normal flow of the document. That is, the element is positioned in the order in which it appears in the HTML code.

Relative positioning

With relative positioning, the element is positioned relative to its normal position. This means that the element still occupies its normal space in the document flow, but can be shifted up, down, left, or right from its normal position.

To define relative positioning in CSS, we use the "position" property with the value "relative". In addition, we can use the "top", "bottom", "left" and "right" properties to define the offset of the element in relation to its normal position.

Absolute positioning

In absolute positioning, the element is positioned relative to the closest parent element that has a defined position. This means that the element does not take up space in the document flow and can be placed anywhere on the page.

To define absolute positioning in CSS, we use the "position" property with the value "absolute". In addition, we can use the "top", "bottom", "left" and "right" properties to define the position of the element relative to the parent element.

Fixed positioning

With fixed positioning, the element is positioned relative to the browser window. This means that the element stays in the same place even if the page is scrolled.

To define fixed positioning in CSS, we use the "position" property with the value "fixed". In addition, we can use the "top", "bottom", "left" and "right" properties to define the position of the element relative to the browser window.

Z-index

The z-index is a CSS property that defines the stacking order of positioned elements. That is, it determines which element appears in front of or behind other elements.

To set the z-index in CSS, we use the "z-index" property with a numerical value. The higher the value, the higher the element will appear in the stack of positioned elements.

Conclusion

Positioning in CSS is an important property that allows HTML elements to be positioned in different places on the page. With the knowledge of the four main types of positioning and the z-index property, it is possible to create complex and interesting layouts for your web page.

Now answer the exercise about the content:

_What is the type of positioning in which the element is positioned in relation to the closest parent element that has a defined position?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Colors in CSS

Next page of the Free Ebook:

8Colors in CSS

3 minutes

Obtenez votre certificat pour ce cours gratuitement ! en téléchargeant lapplication Cursa et en lisant lebook qui sy trouve. Disponible sur Google Play ou App Store !

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text