CSS Positioning

The position property specifies the type of positioning method used for an element.

There are five different position values:

  • static
  • relative
  • fixed
  • absolute
  • sticky

We can center the text in css by:

class, id {
    text-align: center;
}

Position Static:

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.

For Example: 

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="styles.css">
    <title>Document</title>
</head>
<body>
	   <h2>position: static;</h2>

		<p>An element with position: static; is not positioned in any special way; it is always positioned according to the normal flow of the page:</p>

		<div class="static">
		This div element has position: static;
		</div>
</body>
</html>

CSS:

div.static {
  position: static;
  border: 3px solid #73AD21;
}

Check the output of this code by yourself.


Position relative:

An element with position relative is positioned relative to its normal position.

For Example:

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="styles.css">
    <title>Document</title>
</head>
<body>
   <h2>position: relative;</h2>

		<p>An element with position: relative; is positioned relative to its normal position:</p>

		<div class="relative">
		This div element has position: relative;
		</div>
</body>
</html>

CSS:

div.relative {
  position: relative;
  left: 30px;
  border: 3px solid #73AD21;
}

Position Absolute:

An element with position absolute is positioned relative to the nearest positioned ancestor (instead of positioned relative to the viewport).

Absolute positioned elements are removed from the normal flow and can overlap elements.

For Example:

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="styles.css">
    <title>Document</title>
</head>
<body>
   <h2>position: absolute;</h2>

		<p>An element with position: absolute; is positioned relative to the nearest positioned ancestor (instead of positioned relative to the viewport, like fixed):</p>

		<div class="relative">This div element has position: relative;
		  <div class="absolute">This div element has position: absolute;</div>
		</div>
</body>
</html>

CSS:

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 Fixed:

An element with position fixedis 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.


Position Sticky:

This position is very useful for websites. 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).

For Example:

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="styles.css">
    <title>Document</title>
</head>
<body>
   <p>Try to <b>scroll</b> inside this frame to understand how sticky positioning works.</p>

		<div class="sticky">I am sticky!</div>

	<div style="padding-bottom:2000px">
	  <p>In this example, the sticky element sticks to the top of the page (top: 0), when you reach its scroll position.</p>
	  <p>Scroll back up to remove the stickyness.</p>
	  <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
	  <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
	</div>
</body>
</html>

CSS:

div.sticky {
  position: -webkit-sticky;
  position: sticky;
  top: 0;
  padding: 5px;
  background-color: #cae8ca;
  border: 2px solid #4CAF50;
}
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

Comments

Post a Comment

Popular Posts