Lesson info

Lesson info

The position property allows you to move elements relative to: their current place in the document (relative), a parent element (absolute), or the viewport (fixed). Here we’ll be covering fixed position.

Fixed-positioned elements are completely removed from the document flow — meaning they no long touch or push other elements around them. They're pinned to the viewport, so they stay in place as the user scrolls, unlike absolute elements which are pinned to one of their parent elements.

We’ll be covering these concepts in this lesson:

  1. Enabling fixed position‍
  2. ‍Positioning values
  3. ‍Z-index

And then bring it all together with an example.

Clone this projectDownload project assetsDownload lesson assets

Transcript

Just like absolute Positioning, Fixed Positioning completely removes an element from the document flow (and doesn't leave a seat warmer). But where absolute Positioning can take cues from one of its parent elements, Fixed means our element Position is based around the viewport itself.

Here's a navbar on a page. And we can scroll through the page to see that our elements scroll with the document.

Let's select the navbar. And sometimes clicking the navbar gets us one of its child elements, so we can use the navigation at the bottom to make sure the actual navbar is selected.

From here, let's set the Position to Fixed. And right away, we see something happen: the navbar collapses a bit. There are two ways to deal with this:

1. We can set the width of our navbar to 100%. And we can set our Position to 0 pixels from the top. If we scroll, it works fine.

And 2. We can undo what we just did, and instead, set the top, left, and right to 0 pixels by using this nifty button.

Now why are we doing 0 pixels from the top? We keep saying that, why is it important? Let's remove that for a second. And let's go into the Navigator and move our navbar down on the page a bit, maybe into a section here. And if we scroll around, we see that it's not quite what we intended.

Adding 0 pixels from the top (or simply using the preset button) makes everything behave as expected.

So, fixed Positioning removes an element from the document flow, and Positions it around the viewport.