position Property

DevJunctionPoint
0

 




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

There are five different position values:

  • static
  • relative
  • fixed
  • absolute
  • sticky

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.
    position: static;

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.
    position: relative;

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
    position: fixed;

position: absolute;

  • An element with position: absolute; is positioned relative to the nearest positioned ancestor (instead of positioned relative to the viewport, like fixed).
  • 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
    position: absolute;

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)

    position: sticky;

index.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>position property</title>
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <div class="container">
        <div class="box" id="red">Red Relative</div>
        <div class="box" id="green">Green Absolute</div>
        <div class="box" id="blue">Blue Fixed</div>
        <div class="box" id="yellow">yellow Sticky</div>
    </div>
</body>

</html>

style.css

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

.container {
    width: 500px;
    height: 500px;
    margin: auto;
    background-color: rgb(224, 221, 221);
}

.box {
    width: 70px;
    height: 70px;
    margin: 3px;
}

#red {
    background-color: red;
    position: relative;
    /* Position relative mean it's relative to it parent element we need to mention top bottom left right */
    top: 50px;
    left: 50px;
}

#green {
    background-color: green;
    position: absolute;
    /* position absolute it is relative to its parent element if parent element does not have relative then it will be relative
    towords the html root element. */
    top: 0px;
    right: 0px;
}

#blue {
    background-color: blue;
    position: fixed;
    bottom: 100px;
    right: 100px;

}

#yellow {
    background-color: yellow;
    position: sticky;
    /* sticky means it will stay at particular position if parent element scroll up then it move with parent element */
    top: 100px;
    bottom: 100px;
}


Output:


Tags

Post a Comment

0Comments

Do leave your comments

Post a Comment (0)

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Ok, Go it!
To Top