【问题标题】:How can I display my navigation bar as sticky?如何将我的导航栏显示为粘性?
【发布时间】:2021-11-26 16:04:40
【问题描述】:
<body>
<div class="topnav">
  <!-- Centered link -->
  <div class="topnav-centered">
    <a href="#" class="logo">
      <img src="growmade2.png" style="height: 25px; width: 250px" alt="" />
    </a>
  </div>

  <!-- Left-aligned links (default) -->
  <a href="#news">SHOP</a>
  <a href="#contact">EXPLORE</a>

  <!-- Right-aligned links -->
  <div class="topnav-right">
    <a href="#search">MY CART</a>
  </div>
</div>

** CSS **

   .topnav {
    position: sticky;
    display: block;
    background-color: rgb(255, 255, 255);
    padding: 30px 80px 30px 50px;
  }

  .topnav a {
    font-family: "Roboto", sans-serif;
    font-family: "Roboto Condensed", sans-serif;
    font-family: "Source Sans Pro", sans-serif;
    font-weight: 900;
    display: inline-block;
    position: relative;
    color: #000;
    text-align: left;
    margin: 0 10px;
    padding-bottom: 2px;
    text-decoration: none;
  }

您好,我是 CSS 新手。我想创建一个带有静态导航栏的网站,但不能静态显示?任何人都可以解决这个问题吗?提前谢谢!!!

【问题讨论】:

    标签: javascript html css sticky


    【解决方案1】:

    好吧,您可以使用固定位置来使导航栏保持粘性。例如:

        .topnav {
        position: fixed;
        top: 0;
        z-index: 99;
      }
    

    【讨论】:

      【解决方案2】:

      您需要添加top: 0:

      body {
          margin: 0;
          padding:0;  
      }
      .topnav {
          position: sticky;
          top: 0;
          display: block;
          background-color: rgb(255, 255, 255);
          padding: 30px 80px 30px 50px;
        }
      
        .topnav a {
          font-family: "Roboto", sans-serif;
          font-family: "Roboto Condensed", sans-serif;
          font-family: "Source Sans Pro", sans-serif;
          font-weight: 900;
          display: inline-block;
          position: relative;
          color: #000;
          text-align: left;
          margin: 0 10px;
          padding-bottom: 2px;
          text-decoration: none;
        }
      <div class="topnav">
        <!-- Centered link -->
        <div class="topnav-centered">
          <a href="#" class="logo">
            <img src="growmade2.png" style="height: 25px; width: 250px" alt="" />
          </a>
        </div>
      
        <!-- Left-aligned links (default) -->
        <a href="#news">SHOP</a>
        <a href="#contact">EXPLORE</a>
      
        <!-- Right-aligned links -->
        <div class="topnav-right">
          <a href="#search">MY CART</a>
        </div>
      </div>
      
      <p>text</p>
      <p>text</p>
      <p>text</p>
      <p>text</p>
      <p>text</p>
      <p>text</p>
      <p>text</p>
      <p>text</p>
      <p>text</p>
      <p>text</p>
      <p>text</p>
      <p>text</p>
      <p>text</p>
      <p>text</p>

      【讨论】:

        【解决方案3】:

        试试这个:

        用于顶部位置的粘性导航栏

        .topnav 
        {
            position: sticky;
            top:0;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-08-23
          • 2018-11-02
          • 2017-07-21
          • 1970-01-01
          • 2014-04-27
          相关资源
          最近更新 更多