What's the difference between a relative, fixed, absolute and statically positioned element?

In CSS, elements can be positioned using four different position values: relative, fixed, absolute, and static. Each of these position values affects how an element is positioned within the web page, and when to use them will depend on the specific use case.

Static position

A static positioned element is the default position for all HTML elements. A static element will be positioned according to the normal flow of the web page. It will take up space on the web page and will be affected by other elements around it. This position is not affected by the top, bottom, left and right properties.

Relative position

A relative positioned element is positioned relative to its normal position on the web page. This means that you can use the top, bottom, left and right properties to move the element away from where it would normally be. When an element is positioned relative, it still takes up space on the web page and will affect other elements around it. This position is commonly used to create a container for absolutely positioned elements.

Fixed position

A fixed positioned element is positioned relative to the browser window and it will not move even if the page is scrolled. This means that it will always stay in the same position on the web page, regardless of other elements around it. It does not take up space on the web page, and other elements will not be affected by it. This position is commonly used for elements like navigation bars and footers that should always be visible to the user.

Absolute position

An absolute positioned element is positioned relative to the nearest positioned ancestor element, instead of the viewport, but if an absolute positioned element has no positioned ancestors, it uses the document body, and moves along with page scrolling. However, if any of its ancestors have a position value other than static, it will be positioned relative to that ancestor. This position does not take up space on the web page and other elements will not be affected by it. This position is commonly used for elements that need to be positioned in a specific location on the web page, such as modals or tooltip.

In summary, when it comes to positioning elements in CSS, there are four different position values: relative, fixed, absolute and static. Each of them has its own advantages and disadvantages and will affect how an element is positioned within the web page. A static positioned element will be positioned according to the normal flow of the web page and takes up space, a relative positioned element will be positioned relative to its normal position and takes up space, a fixed positioned element is positioned relative to the browser window and does not take up space and an absolute positioned element is positioned relative to the nearest positioned ancestor and does not take up space. The choice between the four will depend on the specific use case.

Additional resources
  • Frontend web development courses

    Beginner-friendly courses focusing on HTML, CSS, and JavaScript.

    View Courses
  • Frontend web development projects

    Beginner-friendly projects focusing on HTML, CSS, and JavaScript.

    View Projects
  • Free website templates

    Collection of free, high-quality website templates for your next project.

    View Templates