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

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay