freeCodeCamp/curriculum/challenges/english/01-responsive-web-design/applied-visual-design/lock-an-element-to-the-brow...

121 lines
2.7 KiB
Markdown
Raw Normal View History

---
id: 587d781e367417b2b2512acc
title: Lock an Element to the Browser Window with Fixed Positioning
challengeType: 0
videoUrl: 'https://scrimba.com/c/c2MDNUR'
forumTopicId: 301061
dashedName: lock-an-element-to-the-browser-window-with-fixed-positioning
---
# --description--
The next layout scheme that CSS offers is the `fixed` position, which is a type of absolute positioning that locks an element relative to the browser window. Similar to absolute positioning, it's used with the CSS offset properties and also removes the element from the normal flow of the document. Other items no longer "realize" where it is positioned, which may require some layout adjustments elsewhere.
One key difference between the `fixed` and `absolute` positions is that an element with a fixed position won't move when the user scrolls.
# --instructions--
The navigation bar in the code is labeled with an id of `navbar`. Change its `position` to `fixed`, and offset it 0 pixels from the `top` and 0 pixels from the `left`. After you have added the code, scroll the preview window to see how the navigation stays in place.
# --hints--
The `#navbar` element should have a `position` set to `fixed`.
```js
assert($('#navbar').css('position') == 'fixed');
```
Your code should use the `top` CSS offset of 0 pixels on the `#navbar` element.
```js
assert($('#navbar').css('top') == '0px');
```
Your code should use the `left` CSS offset of 0 pixels on the `#navbar` element.
```js
assert($('#navbar').css('left') == '0px');
```
# --seed--
## --seed-contents--
```html
<style>
2020-01-03 00:41:36 +00:00
body {
min-height: 150vh;
}
#navbar {
width: 100%;
background-color: #767676;
}
nav ul {
margin: 0px;
padding: 5px 0px 5px 30px;
}
nav li {
display: inline;
margin-right: 20px;
}
a {
text-decoration: none;
}
</style>
<body>
<header>
<h1>Welcome!</h1>
<nav id="navbar">
<ul>
<li><a href="">Home</a></li>
<li><a href="">Contact</a></li>
</ul>
</nav>
</header>
<p>I shift up when the #navbar is fixed to the browser window.</p>
</body>
```
# --solutions--
```html
<style>
2020-01-03 00:41:36 +00:00
body {
min-height: 150vh;
}
#navbar {
position: fixed;
top: 0;
left: 0;
width: 100%;
background-color: #767676;
}
nav ul {
margin: 0px;
padding: 5px 0px 5px 30px;
}
nav li {
display: inline;
margin-right: 20px;
}
a {
text-decoration: none;
}
</style>
<body>
<header>
<h1>Welcome!</h1>
<nav id="navbar">
<ul>
<li><a href="">Home</a></li>
<li><a href="">Contact</a></li>
</ul>
</nav>
</header>
<p>I shift up when the #navbar is fixed to the browser window.</p>
</body>
```