DEV Community

Cover image for How to Position Elements Using CSS
TheDevSpace
TheDevSpace

Posted on β€’ Originally published at thedevspace.io

1

How to Position Elements Using CSS

This post was originally published at thedevspace.io. Everything you need to master web development, all in one place.

The position property controls how you wish to position an element. There are five different position methods available, including static, relative, fixed, absolute, and sticky. static is the default positioning method, meaning the element is not positioned in any special way.

Things get more interesting starting from the relative position.

Relative position

With the position set to relative, the element will be placed relative to its default position. It can be adjusted by setting the left, right, top, or bottom properties.

<div class="static">Static</div>
<div class="relative">Relative</div>
Enter fullscreen mode Exit fullscreen mode
.static {
  position: static;
}

.relative {
  position: relative;
  top: 10px;
  left: 20px;
}
Enter fullscreen mode Exit fullscreen mode

Visit Code Demo πŸ”—

Fixed position

fixed position means that the element is positioned relative to the viewport. This method can be useful when you need to create a component, such as a navigation bar, that always stays at the top of the webpage, no matter the user's scrolling position. Or when you need a pop-up window that stays at the bottom right corner.

<div class="fixed">Fixed</div>
Enter fullscreen mode Exit fullscreen mode
.fixed {
  position: fixed;
  bottom: 10px;
  right: 10px;
}
Enter fullscreen mode Exit fullscreen mode

Visit Code Demo πŸ”—

Together, the right and bottom properties will anchor the <div> element to the bottom right corner of the webpage.

Absolute position

If an element has the absolute position, it will be placed according to its closest positioned ancestor, which should have a positioning method other than static.

<div class="relative">
  Relative
  <div class="absolute">Absolute</div>
</div>
Enter fullscreen mode Exit fullscreen mode
.absolute {
  position: absolute;
  right: 20px;
  bottom: 20px;
}
Enter fullscreen mode Exit fullscreen mode

Visit Code Demo πŸ”—

In this example, .relative is a 500x500px box with relative position, and .absolute is a smaller box placed inside .relative. As you can see from the demonstration, the .absolute box is positioned relative to the .relative box instead of the viewport.

If the absolute element does not have a positioned ancestor, it will be positioned relative to the viewport, making it behave like a fixed element.

Sticky position

The sticky option is similar to fixed, except the element will be fixed only when a specific scroll threshold is reached.

<p>Lorem ipsum. . .</p>

<p>. . .</p>

<div class="sticky">Sticky</div>

<p>. . .</p>
Enter fullscreen mode Exit fullscreen mode
.sticky {
  position: sticky;
  top: 10px;

  border: solid orange 2px;
  background-color: bisque;
  width: 100%;
  height: 100px;
}
Enter fullscreen mode Exit fullscreen mode

Visit Code Demo πŸ”—

Read More

If you found this guide helpful, follow us on social media for more tips, tutorials, and updates on web development:

πŸ”Ή TheDevSpace | LinkedIn

πŸ”Ή TheDevSpace | X

πŸ”Ή TheDevSpace | Threads

Let's stay connected! πŸš€

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo πŸ“Šβœ¨

Top comments (0)

πŸ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay