CSS position Property

You are Here:

Using absolute

In the following example, we will demonstrate how CSS position:absolute works.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> div{ position: relative; border: 1px solid black; height: 250px; } img{ position: absolute; top: 20%; } </style> </head> <body> <h1>CSS position Property</h1> <p>This is first paragraph.</p> <p>This is second paragraph.</p> <p>This is third paragraph.</p> <div> <img src="apple.png"> </div> </body> </html>

Syntax

Using CSS

element{ position: sticky; }

Using Javascript

object.style.position="sticky";

Animatable

No, position property is not animatable. CSS Animatable Properties Reference.

Default Value

Default value for CSS position property is static.

Property Value

The following table provides a list of values for CSS position property.

ValueExplanation
relativeThe element is positioned according to the normal flow of the document, and then offset relative to itself based on the values of top, right, bottom, and left.
absoluteThe element is positioned relative to its first positioned (not static) ancestor element.
fixedThe element is positioned relative to the browser window.
staticThe element is positioned according to the normal flow of the document. The top, right, bottom, left, and z-index properties have no effect.
stickyThe element is positioned according to the normal flow of the document, and then offset relative to its nearest scrolling ancestor and containing block.

Using fixed

In the following example, we will demonstrate how CSS position:fixed works.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> body{ height: 800px; } img{ position: fixed; } </style> </head> <body> <h1>CSS position Property</h1> <p>This is first paragraph.</p> <p>This is second paragraph.</p> <p>This is third paragraph.</p> <div> <img src="apple.png"> </div> </body> </html>

Using static

In the following example, we will demonstrate how CSS position:static works.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> div{ position: relative; border: 1px solid black; height: 250px; } img{ position: static; } </style> </head> <body> <h1>CSS position Property</h1> <p>This is first paragraph.</p> <p>This is second paragraph.</p> <p>This is third paragraph.</p> <div> <img src="apple.png"> </div> </body> </html>

Using sticky

In the following example, we will demonstrate how CSS position:sticky works.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> body{ height: 1500px; } div{ position: -webkit-sticky; position: sticky; top: 0; padding: 5px; background-color: #bfbfbf; border: 2px solid black; } </style> </head> <body> <h1>CSS position Property</h1> <p>This is first paragraph.</p> <p>This is second paragraph.</p> <p>This is third paragraph.</p> <div> I will stick, when I reach the top of the page. </div> </body> </html>

Using JavaScript

In the following example, we will demonstrate how to change the CSS position property of an element using JavaScript.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> body{ height: 1500px; } div{ position: -webkit-sticky; position: sticky; top: 0; padding: 5px; margin-bottom: 15px; background-color: #bfbfbf; border: 2px solid black; } </style> </head> <body> <h1>CSS position Property</h1> <p>This is first paragraph.</p> <p>This is second paragraph.</p> <p>This is third paragraph.</p> <div> I will stick, when I reach the top of the page. </div> <button onclick="myFunction()">Click Me</button> <script> var x = document.getElementsByTagName("div")[0]; function myFunction(){ x.style.position = "static"; } </script> </body> </html>

Reminder

Hi Developers, we almost covered 98.7% of CSS Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in CSS.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author