Home > database >  Why is the nav bar position=sticky not working?
Why is the nav bar position=sticky not working?

Time:03-31

<!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">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <nav>
        <navl>
        <ul>
            <li><img src="" alt="logo"></li>
            <li><input type="search"></li>
            <li><img src="" alt="hit"></li>
        </ul>
        </navl>

        <navr>
            <ul>
                <li><img src="" alt=""dp></li>
                <li>Name</li>
                <li>i1</li>
                <li>i2</li>
                <li>i3</li>
                <li>i4</li>
                <li>i5</li>
            </ul>
        </navr>
    </nav>
    <main>
        <left>
            <span>left</span>
        </left>
        <center>
            <span>center</span>
        </center>
        <right>
            <span>right</span>
        </right>
    </main>


</body>
</html>

*{
    padding: 0vw;
    margin: 0vw;
}

nav{
    background-color: rgba(52, 52, 146, 0.829);
    display: inline-flex;
    justify-content: space-around;
    align-items: center;
    width: 100vw;
    height: 45px;
    position:sticky;
}

ul {
    display: inline-flex;
    list-style-type:none;

}

navl{
    border-color:red ;
    border-style: dotted;
    align-items: center;
}
navr{
    border-color:red ;
    border-style: dotted;

}

main{
    justify-content: space-between;
    display: flex;
}
left{
    border-color: red;
    border-style: dotted;
    width: 30vw;
    height: 400px;
    display: inline-flex;
    position: sticky;
}
center{
    border-color: red;
    border-style: dotted;
    width: 35vw;
    height:5000px;
    display: inline-flex;
}
right{
    border-color: red;
    border-style: dotted;
    width: 30vw;
    height: 300px;
    display: inline-flex;
}

Why is the nav bar position:sticky attribute not working ? But if i set position to fixed, it works in that case. I want it to become fixed on the top as we scroll just as facebook. I am trying to create a clone-facebook. I am beginner trying to learn web-development. 1st code is HTML while 2nd is CSS Stylesheet

CodePudding user response:

Just add top:0; and z-index: 9999;

* {
  padding: 0vw;
  margin: 0vw;
}

nav {
  background-color: rgba(52, 52, 146, 0.829);
  display: inline-flex;
  justify-content: space-around;
  align-items: center;
  width: 100vw;
  height: 45px;
  position: sticky;
  z-index: 9999;
  top: 0;
}

ul {
  display: inline-flex;
  list-style-type: none;
}

navl {
  border-color: red;
  border-style: dotted;
  align-items: center;
}
navr {
  border-color: red;
  border-style: dotted;
}

main {
  justify-content: space-between;
  display: flex;
}
left {
  border-color: red;
  border-style: dotted;
  width: 30vw;
  height: 400px;
  display: inline-flex;
  position: sticky;
}
center {
  border-color: red;
  border-style: dotted;
  width: 35vw;
  height: 5000px;
  display: inline-flex;
}
right {
  border-color: red;
  border-style: dotted;
  width: 30vw;
  height: 300px;
  display: inline-flex;
}
<!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">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <nav>
        <navl>
        <ul>
            <li><img src="" alt="logo"></li>
            <li><input type="search"></li>
            <li><img src="" alt="hit"></li>
        </ul>
        </navl>

        <navr>
            <ul>
                <li><img src="" alt=""dp></li>
                <li>Name</li>
                <li>i1</li>
                <li>i2</li>
                <li>i3</li>
                <li>i4</li>
                <li>i5</li>
            </ul>
        </navr>
    </nav>
    <main>
        <left>
            <span>left</span>
        </left>
        <center>
            <span>center</span>
        </center>
        <right>
            <span>right</span>
        </right>
    </main>


</body>
</html>

  • Related