【问题标题】:CSS - Align Content Below Fixed NavbarCSS - 在固定导航栏下方对齐内容
【发布时间】:2017-03-02 05:27:52
【问题描述】:

我正在尝试学习响应式CSS,但对一些基本的css 设计有困难。我已经创建了一个固定的导航栏,您可以在下面的代码中看到。但我面临的问题是我无法正确对齐其下方的内容。内容与导航栏重叠。你能建议一个适当的CSS 修复而不是像添加具有固定高度的空 div 这样的解决方法吗?代码如下:

  *{
        box-sizing: border-box;
    }
    
    body{
        margin:0px;
        padding: 0px;
        background-color: darkgrey;
    }
    
    .title-bar{
        position:fixed;
        margin-top:0px;
        z-index: 100;
        opacity: 1;
        background-color:white;
        width: 100%;
        height: 100px;
        border-bottom: solid 4px dodgerblue;
    }
    
    .page-container{
        padding: 0px;
    }
    
    .menu-options{
        list-style-type:none;
    }
    
    .menu-options li{
        display: inline;
        padding: 10px;
    }
    
    .menu-options li:hover{
        background-color: deepskyblue;
    }
    
    .menu-options li a{
        color: black;
        text-decoration:none;
    }
    
    .clear20{
        height: 40px;
        clear: both;
    }
    
    .clear10{
        height: 20px;
        clear: both;
    }
    
    .display-bar-1{
        background-color: deepskyblue;
        height: 200px;
        padding:40px;
        position: relative;
    }
    
    html {
        font-family: "Lucida Sans", sans-serif;
    }
    
    .caps {
        text-transform: uppercase;
    }
    
    .register_button{
        text-align:center;
        padding-top:20px;
        padding-bottom:10px;
        width: 200px;
        height: 70px;
        border: solid 3px white;
        font-style:bold;
        font-size:14pt;
    
    }
    
    .register_button:hover{
        background-color:navajowhite;
        color: white;
        cursor:pointer;
        border-color: black;
    }
<!DOCTYPE html>
    <html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
        <link href="responsive.css" rel="stylesheet">
    </head>
    
    <body>
        <div class="page-container">
    
            <div class="title-bar">
                <table width="100%">
                    <tr>
                        <td><h1> Multirater Surveys </h1></td>
                        <td>
                            <ul class="menu-options">
                                <li> <a href="#"> Home </a></li>
                                <li> <a href="#"> How It Works </a></li>
                                <li> <a href="#"> Features </a></li>
                                <li> <a href="#"> Surveys </a></li>
                                <li> <a href="#"> Plans and Pricings </a></li>
                                <li> <a href="#"> Webinars </a></li>
                                <li> <a href="#"> Blog </a></li>
                            </ul>
                        </td>
                    </tr>
                </table>
            </div>
    
    
            <div class="display-bar-1">
                <div class="row">
                    <div class="" style="width:60%;float:left;font-style:bold;font-size:22pt;color:white;padding:10px;">
                        World Class Service <br>
                        More Than 100 Clients
                    </div>
                    <div class="" style="padding:15px;float:left;width:40%;">
                        <div class="register_button" id="register"> <a style="text-decoration:none;color:black;" href="#"> Register Now </a> </div>
                    </div>
                </div>
            </div>
    
            <div class="display-bar-2">
                <div>
                    <h1> Some random block of texts </h1>
                    <p> Hello world how are you ? what is going on? </p>
                </div>
            </div>
    
            <div class="display-bar-1">
                <div class="row">
                    <div class="" style="width:60%;float:left;font-style:bold;font-size:22pt;color:white;padding:10px;">
                        World Class Service <br>
                        More Than 100 Clients
                    </div>
                    <div class="" style="padding:15px;float:left;width:40%;">
                        <div class="register_button" id="register"> <a style="text-decoration:none;color:black;" href="#"> Register Now </a> </div>
                    </div>
                </div>
            </div>
    
            <div class="display-bar-2">
                <div>
                    <h1> Some random block of texts </h1>
                    <p> Hello world how are you ? what is going on? </p>
                </div>
            </div>
    
            <div class="display-bar-1">
                <div class="row">
                    <div class="" style="width:60%;float:left;font-style:bold;font-size:22pt;color:white;padding:10px;">
                        World Class Service <br>
                        More Than 100 Clients
                    </div>
                    <div class="" style="padding:15px;float:left;width:40%;">
                        <div class="register_button" id="register"> <a style="text-decoration:none;color:black;" href="#"> Register Now </a> </div>
                    </div>
                </div>
            </div>
    
            <div class="display-bar-2">
                <div>
                    <h1> Some random block of texts </h1>
                    <p> Hello world how are you ? what is going on? </p>
                </div>
            </div>
    
            <div class="display-bar-2">
                <div>
                    <h1> Some random block of texts </h1>
                    <p> Hello world how are you ? what is going on? </p>
                </div>
            </div>
        </div>
    
        <input type="text" class="caps"/>
    
        <script>
        window.onload = function(){
            document.getElementById("register").onclick = function(){
                window.location = "https://www.google.com";
            }
        }
        </script>
    </body>
    
    </html>

`responsive.html`

  

【问题讨论】:

    标签: html css navbar fixed


    【解决方案1】:

    适当的方法是在 div 中添加 padding-top,与固定标题的高度相同。

    【讨论】:

      【解决方案2】:

      我对您的 responsive.css 文件进行了一些更改,结果对我来说很有效。

      以下是我所做的更改:

      标题栏

      .title-bar{
          position:fixed;
          top: 0;
          z-index: 100;
          opacity: 1;
          background-color:white;
          width: 100%;
          height: 100px;
          border-bottom: solid 4px dodgerblue;
      }
      

      我把margin-top:0px;改成了top: 0;

      为什么?按照herehere

      的说明,将固定定位导航栏的顶部边缘设置为低于其最近定位祖先的顶部边缘的 0

      display-bar-1

      .display-bar-1{
          background-color: deepskyblue;
          height: 200px;
          margin-top: 100px;
          padding:40px;
          position: relative;
      }
      

      我加了margin-top: 100px;

      为什么?您的导航栏的高度为 100 像素。所以我给你的 display-bar-1 类设置了 100px 的边距。

      希望这能解决您的问题。

      【讨论】:

        【解决方案3】:

        您的navbar 的高度为 100 像素。所以你需要从下面的divs 100px 开始。 在navbar 之后添加到第一个div 这个:

        style="top:100px;"
        

        那么它应该可以工作。它看起来像这样:

            <div class="display-bar-1" style="top:100px;">
                <div class="row">
                    <div class="" style="width:60%;float:left;font-style:bold;font-size:22pt;color:white;padding:10px;">
                        World Class Service <br>
                        More Than 100 Clients
                    </div>
                    <div class="" style="padding:15px;float:left;width:40%;">
                        <div class="register_button" id="register"> <a style="text-decoration:none;color:black;" href="#"> Register Now </a> </div>
                    </div>
                </div>
            </div>
        

        其他方法是为navbar 之后的第一个div 创建一个自己的css 类。然后你也可以使用

        padding-top:100px;
        

        margin-top:100px;
        

        					*{
            box-sizing: border-box;
        }
        
        body{
            margin:0px;
            padding: 0px;
            background-color: darkgrey;
        }
        
        .title-bar{
            position:fixed;
            margin-top:0px;
            z-index: 100;
            opacity: 1;
            background-color:white;
            width: 100%;
            height: 100px;
            border-bottom: solid 4px dodgerblue;
        }
        
        .page-container{
            padding: 0px;
        }
        
        .menu-options{
            list-style-type:none;
        }
        
        .menu-options li{
            display: inline;
            padding: 10px;
        }
        
        .menu-options li:hover{
            background-color: deepskyblue;
        }
        
        .menu-options li a{
            color: black;
            text-decoration:none;
        }
        
        .clear20{
            height: 40px;
            clear: both;
        }
        
        .clear10{
            height: 20px;
            clear: both;
        }
        
        .display-bar-1{
            background-color: deepskyblue;
            height: 200px;
            padding:40px;
            position: relative;
        }
        
        html {
            font-family: "Lucida Sans", sans-serif;
        }
        
        .caps {
            text-transform: uppercase;
        }
        
        .register_button{
            text-align:center;
            padding-top:20px;
            padding-bottom:10px;
            width: 200px;
            height: 70px;
            border: solid 3px white;
            font-style:bold;
            font-size:14pt;
        
        }
        
        .register_button:hover{
            background-color:navajowhite;
            color: white;
            cursor:pointer;
            border-color: black;
        }
        <!DOCTYPE html>
        <html>
        <head>
            <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
            <link href="responsive.css" rel="stylesheet">
        </head>
        
        <body>
            <div class="page-container">
        
                <div class="title-bar">
                    <table width="100%">
                        <tr>
                            <td><h1> Multirater Surveys </h1></td>
                            <td>
                                <ul class="menu-options">
                                    <li> <a href="#"> Home </a></li>
                                    <li> <a href="#"> How It Works </a></li>
                                    <li> <a href="#"> Features </a></li>
                                    <li> <a href="#"> Surveys </a></li>
                                    <li> <a href="#"> Plans and Pricings </a></li>
                                    <li> <a href="#"> Webinars </a></li>
                                    <li> <a href="#"> Blog </a></li>
                                </ul>
                            </td>
                        </tr>
                    </table>
                </div>
        
        
                <div class="display-bar-1" style="top:100px;">
                    <div class="row">
                        <div class="" style="width:60%;float:left;font-style:bold;font-size:22pt;color:white;padding:10px;">
                            World Class Service <br>
                            More Than 100 Clients
                        </div>
                        <div class="" style="padding:15px;float:left;width:40%;">
                            <div class="register_button" id="register"> <a style="text-decoration:none;color:black;" href="#"> Register Now </a> </div>
                        </div>
                    </div>
                </div>
        
                <div class="display-bar-2">
                    <div>
                        <h1> Some random block of texts </h1>
                        <p> Hello world how are you ? what is going on? </p>
                    </div>
                </div>
        
                <div class="display-bar-1">
                    <div class="row">
                        <div class="" style="width:60%;float:left;font-style:bold;font-size:22pt;color:white;padding:10px;">
                            World Class Service <br>
                            More Than 100 Clients
                        </div>
                        <div class="" style="padding:15px;float:left;width:40%;">
                            <div class="register_button" id="register"> <a style="text-decoration:none;color:black;" href="#"> Register Now </a> </div>
                        </div>
                    </div>
                </div>
        
                <div class="display-bar-2">
                    <div>
                        <h1> Some random block of texts </h1>
                        <p> Hello world how are you ? what is going on? </p>
                    </div>
                </div>
        
                <div class="display-bar-1">
                    <div class="row">
                        <div class="" style="width:60%;float:left;font-style:bold;font-size:22pt;color:white;padding:10px;">
                            World Class Service <br>
                            More Than 100 Clients
                        </div>
                        <div class="" style="padding:15px;float:left;width:40%;">
                            <div class="register_button" id="register"> <a style="text-decoration:none;color:black;" href="#"> Register Now </a> </div>
                        </div>
                    </div>
                </div>
        
                <div class="display-bar-2">
                    <div>
                        <h1> Some random block of texts </h1>
                        <p> Hello world how are you ? what is going on? </p>
                    </div>
                </div>
        
                <div class="display-bar-2">
                    <div>
                        <h1> Some random block of texts </h1>
                        <p> Hello world how are you ? what is going on? </p>
                    </div>
                </div>
            </div>
        
            <input type="text" class="caps"/>
        
            <script>
            window.onload = function(){
                document.getElementById("register").onclick = function(){
                    window.location = "https://www.google.com";
                }
            }
            </script>
        </body>
        
        </html>

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-07-29
          • 1970-01-01
          • 2016-03-01
          • 1970-01-01
          • 1970-01-01
          • 2020-07-01
          相关资源
          最近更新 更多