2018-10-12 15:37:13 -04:00
|
|
|
---
|
|
|
|
title: Lock an Element to the Browser Window with Fixed Positioning
|
|
|
|
---
|
2019-03-09 19:33:18 +05:30
|
|
|
|
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
# Lock an Element to the Browser Window with Fixed Positioning
|
2018-10-12 15:37:13 -04:00
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
|
|
|
|
---
|
|
|
|
## Hints
|
|
|
|
|
|
|
|
### Hint 1
|
2019-03-09 19:33:18 +05:30
|
|
|
|
|
|
|
Use `position: fixed;`.
|
|
|
|
|
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
### Hint 2
|
2019-03-09 19:33:18 +05:30
|
|
|
|
|
|
|
Use the `left` and `top` properties.
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
---
|
|
|
|
## Solutions
|
2019-03-09 19:33:18 +05:30
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
2019-03-09 19:33:18 +05:30
|
|
|
|
|
|
|
```html
|
|
|
|
<style>
|
|
|
|
#navbar {
|
|
|
|
position: fixed;
|
|
|
|
top: 0px;
|
|
|
|
left: 0px;
|
|
|
|
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>
|
|
|
|
```
|
2018-10-12 15:37:13 -04:00
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
#### Code Explanation
|
2018-10-12 15:37:13 -04:00
|
|
|
|
2019-03-09 19:33:18 +05:30
|
|
|
* `#navbar{}` selects all the elements with the ID of `navbar`.
|
|
|
|
* `position: fixed;` positions the element with respect to the viewport i.e. the output screen in this case.
|
|
|
|
* `top: 0px;` and `right: 0px;` offsets the element by `0px` to the top and left respectively and it is placed in the top-left corner of the screen.
|
2019-07-24 00:59:27 -07:00
|
|
|
|
|
|
|
</details>
|