What is position property?

Default Value: static.

CSS position values:

Static Position:

Here’s an example of how you can apply the static position to an element using CSS:

.element {
	position: static;
}

Sample Code:

<!--style.html-->
<!DOCTYPE html>
<html>
	<head>
		<link rel="stylesheet" type="text/css" href="blog.css">
	</head>
	<body>
		<h2>position: static;</h2>
		<div class="static"></div>
		<h2>position:relative;</h2>
		<div class="relative"></div>
		<div class="fi"><h2>position:fixed;</h2>
		<div class="fixed"></div></div>
		<div class="ab"><h2>position:absolute;</h2></div>
		<div class="absolute"></div>
		<h2>position:sticky;</h2>
		<div class="sticky"></div>
		<div class="content"><p>
	</body>
</html>
//style.css
div.static {
	position: static;
	border: 3px solid #73AD21;
	background-color:red;
	width: 50px;
	height: 50px;
}

Output:

Relative Position:

Here’s an example of how you can apply the relative position to an element using CSS:

.element {
	position: relative;
	top: 20px;
	left: 30px;
}

Sample Code:

//style.css
div.relative {
	position: relative;
        left: 30px;
        border: 3px solid #73AD21;
        width: 50px;
        height: 50px;
        background-color:rgb(236, 125, 125);
 }

Output:

Fixed Position:

Here’s an example of how you can apply the Fixed position to an element using CSS:

.element {
	position: fixed;
	top: 20px;
	left: 30px;
}

Sample Code:

//style.css
div.fixed {
	position: fixed;
        bottom:0 ;
        right: 0;
        width: 50px;
        height: 50px;
        border: 3px solid #73AD21;
        background-color:rgb(231, 173, 173);
  }

Output:

Absolute Position:

Here’s an example of how you can apply the absolute position to an element using CSS

.element {
	position: absolute;
	top: 50px;
	left: 100px;
}

Sample Code:

div.absolute {
        position: absolute;
        top: 290px;
        left:20;
        width: 200px;
        height: 100px;
        border: 3px solid #73AD21;
        background-color: crimson;
}

Output:

Sticky Position:

Here’s an example of how you can apply the absolute position to an element using CSS

.element {
	position: sticky;
	top: 20px;
}

Sample code:

div.sticky {
	position: -webkit-sticky;
	position: sticky;
	width: 50px;
	height: 50px;
	top: 0;
	padding: 5px;
	background-color: #7a0b0b;
	border: 2px solid #4CAF50;
}

Output:

Conclusion:

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.