【问题标题】:How to bring content/element down when using position fixed使用固定位置时如何降低内容/元素
【发布时间】:2020-09-04 20:47:59
【问题描述】:

使用导航栏上的固定位置时,如何将内容向下移动?因为我使用位置:固定,所以我的页面进入顶部。我可以使用margin-bottom,但我的导航栏并不完全位于屏幕顶部。 (是的,我在 * 内有 margin:0 和 padding:0)。

链接:https://codepen.io/Altinzzz/pen/ZEWawxV

代码

html

<div class="topnav" id="myTopnav">
    <a href="websitecar.html"> Home</a>
    <a href="aboutus.html" class="active"> About Us</a>
    <a href="contactus.html">Contact</a>
    <a href="#about">About</a>
</div>

<div class = "introduction">
    <span class = "introductionText"> Welcome </span>
    <span class = "introductionText2"> About us </span>
</div>

css

*{
font-family: Arial, Helvetica, sans-serif;
padding: 0;
margin: 0;
 }

.topnav {
   overflow: hidden;
   background-color: #312E2E;
   position: fixed;
   width: 100%;
   z-index: 2;
   float: left;
}

.topnav a {
   float: left;
   display: block;
   color: white;
   text-align: center;
   padding: 14px 16px;
   text-decoration: none;
   font-size: 17px;
 }

 .introduction{
    text-align:center;
    padding: 0;
 }

 .introduction span{
    display: block;
    animation-fill-mode: forwards;
 }

 .introductionText{
     font-size: 80px;
     background: white;
     color:black;
     letter-spacing: -50px;
     font-weight: 400;
     position: relative;
     animation: text 3s 1;

 }

 .introductionText2{
     font-size: 40px;
     background: white;
     font-weight: 400;
     margin-bottom: 50px;
 }

【问题讨论】:

    标签: html css css-position


    【解决方案1】:

    当您使用position:fixed 时,它会将元素从流中取出。因此,页面的其余部分从顶部开始,就好像固定元素甚至不存在一样。这意味着对其应用边距或任何其他 CSS 不会影响任何其他元素。

    相反,您需要在页面的其余部分为其添加空间,例如在下一个元素的顶部添加填充或边距,例如

     .introduction{
        text-align:center;
        padding: 50px 0 0;
     }
    

    为了便于维护,最好为所有页面添加外部容器以添加此空间,特别是如果每​​个页面中的第一个元素不相同,例如

    .page-container{
        padding-top: 50px;
    }
    

    HTML 会是这样的:

    <div class="topnav" id="myTopnav"> ... </div>
    
    <div class="page-container">
         ... whatever elements you have...
    </div>
      
    

    响应式设计的注意事项:请注意,如果 nav 换行,那么固定填充是不够的 - 在这种情况下,您可能需要查看媒体查询或使用汉堡菜单来保持导航栏相同的高度来克服这个问题。

    工作示例:

    * {
      font-family: Arial, Helvetica, sans-serif;
      padding: 0;
      margin: 0;
    }
    
    .topnav {
      overflow: hidden;
      background-color: #312E2E;
      position: fixed;
      width: 100%;
      z-index: 2;
      float: left;
    }
    
    .topnav a {
      float: left;
      display: block;
      color: white;
      text-align: center;
      padding: 14px 16px;
      text-decoration: none;
      font-size: 17px;
    }
    
    .page-container {
      padding-top: 50px;
    }
    
    .introduction {
      text-align: center;
      padding: 0;
    }
    
    .introduction span {
      display: block;
      animation-fill-mode: forwards;
    }
    
    .introductionText {
      font-size: 80px;
      background: white;
      color: black;
      letter-spacing: -50px;
      font-weight: 400;
      position: relative;
      animation: text 3s 1;
    }
    
    .introductionText2 {
      font-size: 40px;
      background: white;
      font-weight: 400;
      margin-bottom: 50px;
    }
    
    .contentforscroll {
      background: lightblue;
      height: 800px;
    }
    <div class="topnav" id="myTopnav">
      <a href="websitecar.html"> Home</a>
      <a href="aboutus.html" class="active"> About Us</a>
      <a href="contactus.html">Contact</a>
      <a href="#about">About</a>
    </div>
    
    <div class="page-container">
      <div class="introduction">
        <span class="introductionText"> Welcome </span>
        <span class="introductionText2"> About us </span>
      </div>
      <div class="contentforscroll">
        <p>More content to make the page scroll so you see the fixed element</p>
      </div>
    </div>

    【讨论】:

    • 应该是padding: 48px 0 0,而不是padding: 50px 0 0 ....你的答案在我面前...哈哈
    • @DanMullin 感谢您的评论!如果导航不是一个固定的高度并且基于字体大小,我通常允许一个或两个额外的像素用于跨浏览器的变化——尤其是当用户的 Windows 显示比例不是 100% 时——浏览器需要重新计算位置,并且可以从一个到下一个:) 当然,如果 2 个 div 必须 在没有空间的情况下相遇,那么你最好使用固定高度(不幸的是!)
    【解决方案2】:

    在简介中添加padding-top

    【讨论】:

      【解决方案3】:

      请给身体顶部填充(大于顶部导航的高度) 并且 topnav 应该坚持到顶部,所以应该添加 top: 0

      请检查以下

      *{
          font-family: Arial, Helvetica, sans-serif;
          padding: 0;
          margin: 0;
      }
      
      body {
        padding-top: 50px;
      }
      
      .topnav {
          overflow: hidden;
          background-color: #312E2E;
          position: fixed;
          top: 0;
          width: 100%;
          z-index: 2;
          float: left;
      }
      
      .topnav a {
          float: left;
          display: block;
          color: white;
          text-align: center;
          padding: 14px 16px;
          text-decoration: none;
          font-size: 17px;
      }
      
      .topnav a:hover {
          background-color: black;
          color: white;
      }
      
      .topnav a.active {
          background-color: #4CAF50;
          color: white;
      }
      
      .topnav .icon {
          display: none;
      }
        
      @media screen and (max-width: 600px) {
          .topnav a:not(:first-child) {
              display: none;
          }
          .topnav a.icon {
              float: right;
              display: block;
          }
      }
        
      @media screen and (max-width: 600px) {
          .topnav.responsive {
              position: relative;
          }
      
          .topnav.responsive .icon {
              position: absolute;
              right: 0;
              top: 0;
          }
      
          .topnav.responsive a {
              float: none;
              display: block;
              text-align: left;
          }
      }
      
      .introduction{
          text-align:center;
          padding: 0;
      }
      
      .introduction span{
          display: block;
          animation-fill-mode: forwards;
      }
      
      .introductionText{
          font-size: 80px;
          background: white;
          color:black;
          letter-spacing: -50px;
          font-weight: 400;
          position: relative;
          animation: text 3s 1;
      
      }
      
      .introductionText2{
          font-size: 40px;
          background: white;
          font-weight: 400;
          margin-bottom: 50px;
      }
      
      
      @keyframes text {
          0%{
              color: black;
              margin-bottom: -80px;
              letter-spacing: -10px;
          }
          25%{
              margin-bottom: -80px;
              letter-spacing: 25px;
          }
          80%{
              letter-spacing: 0px;
          }
          100%{
              letter-spacing: 0;
              margin-bottom: -3px;
          }
      }
      <div class="topnav" id="myTopnav">
              <a href="websitecar.html"> Home</a>
              <a href="aboutus.html" class="active"> About Us</a>
              <a href="contactus.html">Contact</a>
              <a href="#about">About</a>
              <a href="javascript:void(0);" class="icon" onclick="myFunction()">
                <i class="fa fa-bars"></i>
              </a>
          </div>
          
          <div class = "introduction">
              <span class = "introductionText"> Welcome </span>
              <span class = "introductionText2"> About us </span>
          </div>

      【讨论】:

        【解决方案4】:

        感谢各位推荐。这三个都是正确的。

        添加 padding-top: 50px;到 .introduction 有效,添加 padding-top: 50px; 也是如此到 body 然后把 top: 0;, 放在 topnav 里面。

        【讨论】:

          猜你喜欢
          • 2018-12-12
          • 2021-06-28
          • 1970-01-01
          • 2011-06-25
          • 1970-01-01
          • 2020-08-17
          • 2013-06-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多