Heim > Artikel > Web-Frontend > CSS-Positionierung – Absolut, Relativ, Fest und Sticky.
Welcome to the eleventh lecture of the "Basic to Brilliance" course. In this lecture, we will explore the different types of CSS positioning: relative, absolute, fixed, and sticky. Understanding positioning allows you to control where elements appear on a page and how they behave as users interact with the content.
The position property specifies how an element is positioned in the document. It can take the following values:
An element with position: relative is positioned relative to its original (static) position. It remains in the document flow, meaning other elements will still take it into account.
.box { position: relative; top: 20px; /* Moves the box 20px down from its normal position */ left: 30px; /* Moves the box 30px to the right */ }
In this example, the element is shifted down by 20px and to the right by 30px from its original position, but its space in the document flow is preserved.
Elements with position: absolute are removed from the document flow and positioned relative to their nearest positioned ancestor (i.e., an ancestor with a position other than static).
.container { position: relative; /* This container is the reference for the absolute positioning */ width: 300px; height: 200px; background-color: #f4f4f4; } .box { position: absolute; top: 50px; right: 20px; background-color: #333; color: white; padding: 10px; }
In this example:
An element with position: fixed is positioned relative to the browser window, regardless of how the page is scrolled.
.navbar { position: fixed; top: 0; left: 0; width: 100%; background-color: #333; color: white; padding: 15px; text-align: center; }
In this example:
An element with position: sticky is treated as relative until the user scrolls past a defined threshold, at which point it becomes fixed.
.header { position: sticky; top: 0; background-color: #333; color: white; padding: 10px; }
In this example:
When elements are positioned (either relative, absolute, fixed, or sticky), you can control their stacking order using the z-index property. Higher z-index values make elements appear above lower ones.
.box1 { position: absolute; top: 50px; left: 50px; z-index: 1; /* Lower z-index */ background-color: #f4f4f4; padding: 20px; } .box2 { position: absolute; top: 80px; left: 80px; z-index: 2; /* Higher z-index */ background-color: #333; color: white; padding: 20px; }
In this example:
You can combine positioning values to create advanced layouts.
.sidebar { position: fixed; top: 0; left: 0; width: 200px; height: 100vh; background-color: #333; color: white; padding: 20px; } .content { position: relative; margin-left: 220px; /* Account for the fixed sidebar */ padding: 20px; }
In this layout:
Prochaine étape : Dans la prochaine conférence, nous plongerons dans les Transformations et animations CSS, où vous apprendrez à animer facilement vos éléments Web. Préparez-vous à rendre vos créations dynamiques et interactives !
suivez-moi sur LinkedIn
Ridoy Hasan
Das obige ist der detaillierte Inhalt vonCSS-Positionierung – Absolut, Relativ, Fest und Sticky.. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!