DEV Community

Faisal Ahmed
Faisal Ahmed

Posted on

Fixed vs Sticky Positioning in css

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

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

<body>
    <div class="container">
        <nav class="nav1">
            <span>Go to  website</span>
        </nav>
        <nav class="nav2">
            <li>Home</li>
        </nav>
        <div class="phone">
            <span>phone</span>
        </div>
        <div class="first"></div>
        <div class="second"></div>
        <div class="third"></div>
    </div>
</body>

</html>
Enter fullscreen mode Exit fullscreen mode

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
.container {
    position: relative;
    height: 100vh;
}
.nav1 {
    background: lightcyan;
    height: 20px;
}
.nav2 {
    width: 100%;
    background: lightblue;
    height: 40px;

    /* position: fixed; */
    position: sticky;
    top: 0;
}
.phone {
    position: fixed;
    right: 0;
    bottom: 50px;
    background: blue;
    width: 50px;
    height: 50px;

    display: flex;
    justify-content: center;
    align-items: center;
}
.first {
    height: 500px;
    background: greenyellow;
}
.second {
    height: 500px;
    background: violet;
}
.third {
    height: 400px;
    background: yellow;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)