CSS pro tips for the position property

Rahul Kaklotar
3 min readMar 30, 2023

--

CSS pro tips for the position property
  1. Absolute Positioning:

Absolute positioning is used to position an element relative to its nearest positioned ancestor. If no positioned ancestor is found, the element is positioned relative to the document body. To use absolute positioning, you need to set the position property to absolute. Here’s an example:

.container {
position: relative;
}

.absolute {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}

In this example, the .container element is set to position: relative to act as the positioned ancestor. The .absolute element is then positioned relative to the .container element by setting position: absolute. The top and left properties are set to 50% to center the element horizontally and vertically within its positioned ancestor. The transform property is used to further center the element by translating it back by 50% of its width and height.

2. Fixed Positioning:

Fixed positioning is used to position an element relative to the viewport, which means that the element stays in the same position even when the page is scrolled. To use fixed positioning, you need to set the position property to fixed. Here’s an example:

.fixed {
position: fixed;
top: 0;
left: 0;
width: 100%;
}

In this example, the .fixed element is set to position: fixed to position it relative to the viewport. The top and left properties are set to 0(zero) to position the element at the top-left corner of the viewport. The width property is set to 100% to make the element span the entire width of the viewport.

3. Relative Positioning:

Relative positioning is used to position an element relative to its normal position. This means that the element still takes up space in the document flow, but can be moved around without affecting other elements. To use relative positioning, you need to set the position property to relative. Here’s an example:

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

In this example, the .relative element is set to position: relative to enable relative positioning. The top and left properties are then used to move the element 20 pixels down and 20 pixels to the right from its normal position.

4. Sticky Positioning:

Sticky positioning is used to position an element relative to its scrolling container or the viewport, whichever is closer. This means that the element stays in its normal position until it reaches a certain point, after which it becomes sticky and scrolls with the page. To use sticky positioning, you need to set the position property to sticky. Here’s an example:

.sticky {
position: sticky;
top: 0;
}

In this example, the .sticky element is set to position: sticky to enable sticky positioning. The top property is set to 0(zero) to position the element at the top of its scrolling container. Once the element reaches the top of the scrolling container, it becomes sticky and stays in place while the rest of the page scrolls.

I hope these examples help you understand the position property better and give you some pro tips on how to use it effectively!

--

--

Rahul Kaklotar

I'm front-end developer with 4 years of expertise in HTML, CSS, JavaScript, React. Passionate about creating user-friendly websites with a sharp eye for detail.