Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
615 views
in Technique[技术] by (71.8m points)

html - Why my sticky navigation only works when my header is set to be inline?

This is my HTML file:

    .jumbotron {
       font-size: 20px;
       padding: 60px;
       background-color: #00a8ec;
       text-align: center;
       color: white;
    }
    
    header {
       display: inline;
    } 
    
    nav {
       background-color: #00b2a6;
       padding: 5px;
       position: sticky;
       top: 0;
    }
    
    footer {
        padding: 20px;
        color: white;
        background-color: #00b2a6;
        text-align: center;
        font-weight: bold;
     }
<!DOCTYPE html>
    <html>
        <head>
            <title>TITLE</title>
            <link rel="stylesheet" href="tes.css">
        </head>
        <body>
            <header>
                <div class="jumbotron">
                    <h1>TITLE</h1>
                    <p>DESCRIPTION</p>
                </div>
                <nav>
                    <ul>
                        <li><a href="#sejarah">Sejarah</a></li>
                        <li><a href="#geografis">Geografis</a></li>
                        <li><a href="#wisata">Wisata</a></li>
                    </ul>
                </nav>
            </header>
    
            <main>
                <div id="content">
                    <pre>Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                    </pre>
                        
                </div>
    
              
            <footer>
                <p>FOOTER</p>
            </footer>
            
        
        </body>
    
    </html>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You find a hack to force it to work. When using inline you will have block level elements inside inline level element one which is invalid and it's like the inline element no more exist (this is a simplified explanation, in reality it's a bit more complex1).

Your code is exactly like not having header:

.jumbotron {
  font-size: 20px;
  padding: 60px;
  background-color: #00a8ec;
  text-align: center;
  color: white;
}

nav {
  background-color: #00b2a6;
  padding: 5px;
  position: sticky;
  top: 0;
}

footer {
  padding: 20px;
  color: white;
  background-color: #00b2a6;
  text-align: center;
  font-weight: bold;
}
<div class="jumbotron">
    <h1>TITLE</h1>
    <p>DESCRIPTION</p>
  </div>
  <nav>
    <ul>
      <li><a href="#sejarah">Sejarah</a></li>
      <li><a href="#geografis">Geografis</a></li>
      <li><a href="#wisata">Wisata</a></li>
    </ul>
  </nav>


<main>
  <div id="content">
    <pre>Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                        Hahahaha
                    </pre>

  </div>


  <footer>
    <p>FOOTER</p>
  </footer>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...