- How to Make a Div Stick to the Top of Screen when Scrolling with CSS and Javascript
- Create HTML
- Add CSS
- Example of making a stick to the top of the screen using CSS:
- Example of making a stick to the top of the screen using CSS and JavaScript:
- CSS Layout — The position Property
- The position Property
- position: static;
- Example
- position: relative;
- Example
- position: fixed;
- Example
- position: absolute;
- Example
- position: sticky;
- Example
- Positioning Text In an Image
- CSS top Property
- Definition and Usage
- Browser Support
- CSS Syntax
- Property Values
- More Examples
- Example
- Related Pages
How to Make a Div Stick to the Top of Screen when Scrolling with CSS and Javascript
Here, we’ll demonstrate examples where we use HTML and CSS, and another example with Javascript added.
First, let us show an example where we use CSS. We’ll start with creating HTML.
Create HTML
p>Scroll down this page. p> div class="sticky">Box Div div> h2>Scroll again to the top to "remove" the sticky position. h2> p> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. p>
Add CSS
- Set the position to “sticky”. For Safari, use -webkit-sticky.
- Set the top property to 0.
- Add the background-color, padding, and font-size properties.
div.sticky < position: -webkit-sticky; position: sticky; top: 0; background-color: #666; padding: 40px; font-size: 25px; >
Now, let’s see the result of our code.
Example of making a stick to the top of the screen using CSS:
html> html> head> title>Title of the document title> style> div.sticky < position: -webkit-sticky; position: sticky; top: 0; background-color: #666; padding: 40px; font-size: 25px; > style> head> body> p>Scroll down this page. p> div class="sticky">Sticky Div div> h2> Scroll again to the top to "remove" the sticky position. h2> p> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. p> body> html>
As you can see, the sticks to the top of the screen, but it goes back to its original position when scrolling back to the top of the page.
The element with position: sticky; is positioned regarding the user’s scroll position. Depending on the scroll position, the sticky element switches between relative and fixed.
Next, we’ll demonstrate an example where we also use Javascript.
Example of making a stick to the top of the screen using CSS and JavaScript:
html> html> head> title>Title of the document title> style> #boxThis < padding: 5px; background-color: #80c294; font-size: 1.5em; width: 300px; text-align: center; font-weight: bold; border: 1px solid #666; -webkit-border-radius: 10px; border-radius: 10px; > #boxThis.box < margin-top: 0; position: fixed; top: 0; z-index: 9999; -webkit-border-radius: 0 0 10px 10px; border-radius: 0 0 10px 10px; > style> script src="https://code.jquery.com/jquery-3.4.1.min.js"> script> head> body> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> div id="boxHere"> div> div id="boxThis">Box div> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> lorem br /> script> function boxtothetop( ) < var windowTop = $(window) .scrollTop(); var top = $('#boxHere') .offset() .top; if(windowTop > top) < $('#boxThis') .addClass('box'); $('#boxHere') .height($('#boxThis') .outerHeight()); > else < $('#boxThis') .removeClass('box'); $('#boxHere') .height(0); > > $(function( ) < $(window) .scroll(boxtothetop); boxtothetop(); >); script> body> html>
CSS Layout — The position Property
The position property specifies the type of positioning method used for an element (static, relative, fixed, absolute or sticky).
The position Property
The position property specifies the type of positioning method used for an element.
There are five different position values:
Elements are then positioned using the top, bottom, left, and right properties. However, these properties will not work unless the position property is set first. They also work differently depending on the position value.
position: static;
HTML elements are positioned static by default.
Static positioned elements are not affected by the top, bottom, left, and right properties.
An element with position: static; is not positioned in any special way; it is always positioned according to the normal flow of the page:
Here is the CSS that is used:
Example
position: relative;
An element with position: relative; is positioned relative to its normal position.
Setting the top, right, bottom, and left properties of a relatively-positioned element will cause it to be adjusted away from its normal position. Other content will not be adjusted to fit into any gap left by the element.
Here is the CSS that is used:
Example
position: fixed;
An element with position: fixed; is positioned relative to the viewport, which means it always stays in the same place even if the page is scrolled. The top, right, bottom, and left properties are used to position the element.
A fixed element does not leave a gap in the page where it would normally have been located.
Notice the fixed element in the lower-right corner of the page. Here is the CSS that is used:
Example
position: absolute;
An element with position: absolute; is positioned relative to the nearest positioned ancestor (instead of positioned relative to the viewport, like fixed).
However; if an absolute positioned element has no positioned ancestors, it uses the document body, and moves along with page scrolling.
Note: Absolute positioned elements are removed from the normal flow, and can overlap elements.
Here is the CSS that is used:
Example
div.relative <
position: relative;
width: 400px;
height: 200px;
border: 3px solid #73AD21;
>
div.absolute position: absolute;
top: 80px;
right: 0;
width: 200px;
height: 100px;
border: 3px solid #73AD21;
>
position: sticky;
An element with position: sticky; is positioned based on the user’s scroll position.
A sticky element toggles between relative and fixed , depending on the scroll position. It is positioned relative until a given offset position is met in the viewport — then it «sticks» in place (like position:fixed).
Note: Internet Explorer does not support sticky positioning. Safari requires a -webkit- prefix (see example below). You must also specify at least one of top , right , bottom or left for sticky positioning to work.
In this example, the sticky element sticks to the top of the page ( top: 0 ), when you reach its scroll position.
Example
div.sticky <
position: -webkit-sticky; /* Safari */
position: sticky;
top: 0;
background-color: green;
border: 2px solid #4CAF50;
>
Positioning Text In an Image
How to position text over an image:
CSS top Property
Set the top edge of the positioned element 50px down from the top edge of its nearest positioned ancestor:
More «Try it Yourself» examples below.
Definition and Usage
The top property affects the vertical position of a positioned element. This property has no effect on non-positioned elements.
- If position: absolute; or position: fixed; — the top property sets the top edge of an element to a unit above/below the top edge of its nearest positioned ancestor.
- If position: relative; — the top property makes the element’s top edge to move above/below its normal position.
- If position: sticky; — the top property behaves like its position is relative when the element is inside the viewport, and like its position is fixed when it is outside.
- If position: static; — the top property has no effect.
Default value: | auto |
---|---|
Inherited: | no |
Animatable: | yes. Read about animatable Try it |
Version: | CSS2 |
JavaScript syntax: | object.style.top=»100px» Try it |
Browser Support
The numbers in the table specify the first browser version that fully supports the property.
CSS Syntax
Property Values
Value | Description | Demo |
---|---|---|
auto | Lets the browser calculate the top edge position. This is default | Demo ❯ |
length | Sets the top edge position in px, cm, etc. Negative values are allowed. Read about length units | Demo ❯ |
% | Sets the top edge position in % of containing element. Negative values are allowed | Demo ❯ |
initial | Sets this property to its default value. Read about initial | |
inherit | Inherits this property from its parent element. Read about inherit |
More Examples
Example
Use the top property with a negative value and for an element with no positioned ancestors:
div.b <
position: absolute;
top: -20px;
border: 3px solid blue;
>
div.c position: absolute;
top: 150px;
border: 3px solid green;
>