【问题标题】:Keeping divs aligned vertically, and their content from overlapping保持 div 垂直对齐,并且它们的内容不会重叠
【发布时间】:2020-07-30 05:29:32
【问题描述】:

我将 div 按我想要的方式对齐,但中间 div 中的内容与页脚 div 的内容重叠。关于如何让中间 div 中的内容增加高度以使其不会与下面的 div 重叠的任何建议?

当 div 垂直对齐时,是否有办法永久防止它们与上方和下方的 div 重叠或重叠?

我正在为一个非常小的屏幕(Galaxy Fold)设计这个,我计划随着过程的继续扩大规模。

    body {
        font-family: Helvetica,Arial,sans-serif; /* styles the font of the entire page */
        margin: 0px; /* removes the margin from around the page */
        text-transform: capitalize; /* capitalizes the text on the page */
    }

    li {
        display: inline; /* makes the links lay horizontally */
        list-style-type: none; /* removes the markers for the list items behind the links */
    }

    #nav, #main, #footer {
        width: 100%;
        position: absolute; /* fixes the <div> elements to the page */
    }
    
    #nav {
        background-color: #393e46;
        padding-bottom: 16px; /* adds extra padding underneath the search bar */
        top: 0px;
        right: 0px;
        left: 0px;
    }

    #nav-container {
        margin: 0 auto; /* centers the 'nav-container' within the <nav> */
        max-width: 200px;
    }

    #rowA {
        background-color: red;
    }

    .nav-menu {
        margin-top: 0px; /* removes the extra margin above the links in the nav */
    }

    #rowB {
        background-color: green;
    }

    #nav-logo {
        font-size: 32px;
        text-align: center; /* centers the 'FGCPEDIA' logo */
    }

    #rowC {background-color: pink;}

    .nav-menu {
        float: right; /* makes the 'Sign Up/ Log In' links float to the right */
    }
    
    /* removes the highlight from the form when selected */
    input:focus, textarea:focus {
        outline: 0;
    }

    #main {
        background-color: orange;
        top: 26.4%; /* pushes the main directly under the nav */
        /* top: 32.9%; */
    }

    #main-container {
        margin: 0 auto; /* centers the elements in the main */
        max-width: 100px;
    }

    #footer {
        background-color: #222831;
        right: 0px;
        bottom: 0px;
        left: 0px;
    }

    #footer-container {
        margin: 0 auto;
        max-width: 200px;
    }

    .footer-row {
        padding-inline-start: 0px; /* removes the extra padding on the left side of links */
    }

    #copyright {
        color:#eeeeee; /* colors the footer text */
        font-size: 13px; /* the font size of the copyright text is '13' */
        text-align: center; /* centers the  */
    }
<!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <title></title>
            <!-- <link href="../../css/stylesheet2.css" rel="stylesheet"> -->
            <link href="stylesheet2.css" rel="stylesheet">
        </head>
        
        <body>

            <nav id="nav">
                <div id="nav-container">

                    <!-- nav-row-1 -->
                    <div id="rowA">
                        <ul class="nav-menu">
                            <li>
                                <a href="">Sign Up</a>
                            </li>

                            <li>
                                <a href="">Log In</a>
                            </li>
                        </ul>
                    </div>

                    <!-- nav-row-1 -->
                    <div id="rowB">
                        <h3 id="nav-logo">DICTIONARY</h3>
                    </div>

                    <!-- nav-row-1 -->
                    <div id="rowC">
                        <form action="" method="get">
                            <input id="nav-search-bar" type="search" placeholder="Search">
                        </form>
                    </div>


                </div>
            </nav>


            <!-- main section -->
            <main id="main">
                <div id="main-container">

                    <!-- header -->
                    <div id="btd-masthead">
                        <h2>Browse the Dictionary</h2>
                    </div>

                    <hr>

                    <!-- btd links -->
                    <div id="btd-links">
                        
                        <div id="btd-links-1">
                            <a href="a.html">a</a>
                            <a href="b.html">b</a>
                            <a href="c.html">c</a>
                            <a href="d.html">d</a>
                            <a href="e.html">e</a>
                            <a href="f.html">f</a>
                            <a href="g.html">g</a>
                            <a href="h.html">h</a>
                            <a href="i.html">i</a>
                            <a href="j.html">j</a>
                            <a href="k.html">k</a>
                            <a href="l.html">l</a>
                            <a href="m.html">m</a>
                            <a href="n.html">n</a>
                            <a href="o.html">o</a>
                            <a href="p.html">p</a>
                            <a href="q.html">q</a>
                            <a href="r.html">r</a>
                            <a href="s.html">s</a>
                            <a href="t.html">t</a>
                            <a href="u.html">u</a>
                            <a href="v.html">v</a>
                            <a href="w.html">w</a>
                            <a href="x.html">x</a>
                            <a href="y.html">y</a>
                            <a href="z.html">z</a>
                        </div>
                    </div>

                    <hr>

                    <div>

                    </div>

                    <!-- terms list row -->
                    <div id="terms-container">

                        <!-- column 1 -->
                        <div class="terms-col">

                            <a href="">Abare</a>
                            <a href="../terms/a/adaptation.html">Adaptation</a>
                            <a href="../terms/a/air_dash.html">Air Dash</a>
                            <a href="">Air Throw</a>
                            <a href="../terms/a/air_to_air.html">Air-to-Air</a>
                            <a href="../terms/a/anti_air.html">Anti-Air</a>
                            <a href="../terms/a/armor.html">Armor</a>
                            <a href="">Assist</a>
                            <a href="../terms/a/auto_correct.html">Auto Correct</a>

                        </div>
                    </div>
                </div>
            </main>

            <!-- the footer stays the same on every page -->
            <footer id="footer">
                <div id="footer-container">

                    <!-- row 1 -->
                    <ul class="footer-row">
                        <li>
                            <a href="a.html" style="text-transform: none;">Browse the Dictionary:</a>
                        </li>
                        <li>
                            <a href="a.html">a</a>
                        </li>
                        <li>
                            <a href="b.html">b</a>
                        </li>
                        <li>
                            <a href="c.html">c</a>
                        </li>
                        <li>
                            <a href="d.html">d</a>
                        </li>
                        <li>
                            <a href="e.html">e</a>
                        </li>
                        <li>
                            <a href="f.html">f</a>
                        </li>
                        <li>
                            <a href="g.html">g</a>
                        </li>
                        <li>
                            <a href="h.html">h</a>
                        </li>
                        <li>
                            <a href="i.html">i</a>
                        </li>
                        <li>
                            <a href="j.html">j</a>
                        </li>
                        <li>
                            <a href="k.html">k</a>
                        </li>
                        <li>
                            <a href="l.html">l</a>
                        </li>
                        <li>
                            <a href="m.html">m</a>
                        </li>
                        <li>
                            <a href="n.html">n</a>
                        </li>
                        <li>
                            <a href="o.html">o</a>
                        </li>
                        <li>
                            <a href="p.html">p</a>
                        </li>
                        <li>
                            <a href="q.html">q</a>
                        </li>
                        <li>
                            <a href="r.html">r</a>
                        </li>
                        <li>
                            <a href="s.html">s</a>
                        </li>
                        <li>
                            <a href="t.html">t</a>
                        </li>
                        <li>
                            <a href="u.html">u</a>
                        </li>
                        <li>
                            <a href="v.html">v</a>
                        </li>
                        <li>
                            <a href="w.html">w</a>
                        </li>
                        <li>
                            <a href="x.html">x</a>
                        </li>
                        <li>
                            <a href="y.html">y</a>
                        </li>
                        <li>
                            <a href="z.html">z</a>
                        </li>
                    </ul>

                    <!-- row 2 -->
                    <ul class="footer-row">
                        <li>
                            <a href="../../index.html">home</a>
                        </li>
                        <li>
                            <a href="../about_us.html">About Us</a>
                        </li>
                        <li>
                            <a href="../contact_us.html">Contact Us</a>
                        </li>
                        <li>
                            <a href="../disclaimer.html">Disclaimer</a>
                        </li>
                    </ul>

                    <!-- row 3 -->
                    <div class="footer-row">
                        <p id="copyright">&copy; 2020 copyright</p>
                    </div>
                </div>
            </footer>

            <script src="js/script.js"></script>
        </body>
    </html>

【问题讨论】:

    标签: html css


    【解决方案1】:

    问题是您将position:absolute 用于所有您的主要块!我猜这是学生的作品,所以值得阅读我在下面的解释中添加的链接。

    发生了什么: Absolute positioning 将元素从流中取出,因此它们独立于页面的其余部分工作,因此您必须使用 CSS 来准确地指定每个元素出现的位置。当一个 div 增长时,它将与下一个重叠,因为它仍会出现在您在 CSS 中指定的确切位置。

    如何解决:只需使用默认的normal flow!在下面的 sn-p 中,我进行了以下更改:

    1. 删除了所有与绝对定位相关的CSS,恢复正常流动
    2. 将display:flex 添加到您的主要块中以防止collapsing margin problem

    li {
      display: inline;
      list-style-type: none;
    }
    
    #nav, #main, #footer {
      display:flex;
    }
    
    #nav {
      background-color: #393e46;
      padding-bottom: 16px;
    }
    
    #nav-container {
      margin: 0 auto;
      max-width: 200px;
    }
    
    #rowA {
      background-color: red;
    }
    
    .nav-menu {
      margin-top: 0px;
    }
    
    #rowB {
      background-color: green;
    }
    
    #nav-logo {
      font-size: 32px;
      text-align: center;
    }
    
    #rowC {
      background-color: pink;
    }
    
    .nav-menu {
      float: right;
    }
    
    
    #main {
      background-color: orange;
    }
    
    #main-container {
      margin: 0 auto;
      max-width: 100px;
    }
    
    #footer {
      background-color: #222831;
    }
    
    #footer-container {
      margin: 0 auto;
      max-width: 200px;
    }
    
    .footer-row {
      padding-inline-start: 0px;
    }
    
    #copyright {
      color: #eeeeee;
      font-size: 13px;
      text-align: center;
    }
    <nav id="nav">
      <div id="nav-container">
    
        <!-- nav-row-1 -->
        <div id="rowA">
          <ul class="nav-menu">
            <li>
              <a href="">Sign Up</a>
            </li>
    
            <li>
              <a href="">Log In</a>
            </li>
          </ul>
        </div>
    
        <!-- nav-row-1 -->
        <div id="rowB">
          <h3 id="nav-logo">DICTIONARY</h3>
        </div>
    
        <!-- nav-row-1 -->
        <div id="rowC">
          <form action="" method="get">
            <input id="nav-search-bar" type="search" placeholder="Search">
          </form>
        </div>
    
    
      </div>
    </nav>
    
    
    <!-- main section -->
    <main id="main">
      <div id="main-container">
    
        <!-- header -->
        <div id="btd-masthead">
          <h2>Browse the Dictionary</h2>
        </div>
    
        <hr>
    
        <!-- btd links -->
        <div id="btd-links">
    
          <div id="btd-links-1">
            <a href="a.html">a</a>
            <a href="b.html">b</a>
            <a href="c.html">c</a>
            <a href="d.html">d</a>
            <a href="e.html">e</a>
            <a href="f.html">f</a>
            <a href="g.html">g</a>
            <a href="h.html">h</a>
            <a href="i.html">i</a>
            <a href="j.html">j</a>
            <a href="k.html">k</a>
            <a href="l.html">l</a>
            <a href="m.html">m</a>
            <a href="n.html">n</a>
            <a href="o.html">o</a>
            <a href="p.html">p</a>
            <a href="q.html">q</a>
            <a href="r.html">r</a>
            <a href="s.html">s</a>
            <a href="t.html">t</a>
            <a href="u.html">u</a>
            <a href="v.html">v</a>
            <a href="w.html">w</a>
            <a href="x.html">x</a>
            <a href="y.html">y</a>
            <a href="z.html">z</a>
          </div>
        </div>
    
        <hr>
    
        <div>
    
        </div>
    
        <!-- terms list row -->
        <div id="terms-container">
    
          <!-- column 1 -->
          <div class="terms-col">
    
            <a href="">Abare</a>
            <a href="../terms/a/adaptation.html">Adaptation</a>
            <a href="../terms/a/air_dash.html">Air Dash</a>
            <a href="">Air Throw</a>
            <a href="../terms/a/air_to_air.html">Air-to-Air</a>
            <a href="../terms/a/anti_air.html">Anti-Air</a>
            <a href="../terms/a/armor.html">Armor</a>
            <a href="">Assist</a>
            <a href="../terms/a/auto_correct.html">Auto Correct</a>
    
          </div>
        </div>
      </div>
    </main>
    
    <!-- the footer stays the same on every page -->
    <footer id="footer">
      <div id="footer-container">
    
        <!-- row 1 -->
        <ul class="footer-row">
          <li>
            <a href="a.html" style="text-transform: none;">Browse the Dictionary:</a>
          </li>
          <li>
            <a href="a.html">a</a>
          </li>
          <li>
            <a href="b.html">b</a>
          </li>
          <li>
            <a href="c.html">c</a>
          </li>
          <li>
            <a href="d.html">d</a>
          </li>
          <li>
            <a href="e.html">e</a>
          </li>
          <li>
            <a href="f.html">f</a>
          </li>
          <li>
            <a href="g.html">g</a>
          </li>
          <li>
            <a href="h.html">h</a>
          </li>
          <li>
            <a href="i.html">i</a>
          </li>
          <li>
            <a href="j.html">j</a>
          </li>
          <li>
            <a href="k.html">k</a>
          </li>
          <li>
            <a href="l.html">l</a>
          </li>
          <li>
            <a href="m.html">m</a>
          </li>
          <li>
            <a href="n.html">n</a>
          </li>
          <li>
            <a href="o.html">o</a>
          </li>
          <li>
            <a href="p.html">p</a>
          </li>
          <li>
            <a href="q.html">q</a>
          </li>
          <li>
            <a href="r.html">r</a>
          </li>
          <li>
            <a href="s.html">s</a>
          </li>
          <li>
            <a href="t.html">t</a>
          </li>
          <li>
            <a href="u.html">u</a>
          </li>
          <li>
            <a href="v.html">v</a>
          </li>
          <li>
            <a href="w.html">w</a>
          </li>
          <li>
            <a href="x.html">x</a>
          </li>
          <li>
            <a href="y.html">y</a>
          </li>
          <li>
            <a href="z.html">z</a>
          </li>
        </ul>
    
        <!-- row 2 -->
        <ul class="footer-row">
          <li>
            <a href="../../index.html">home</a>
          </li>
          <li>
            <a href="../about_us.html">About Us</a>
          </li>
          <li>
            <a href="../contact_us.html">Contact Us</a>
          </li>
          <li>
            <a href="../disclaimer.html">Disclaimer</a>
          </li>
        </ul>
    
        <!-- row 3 -->
        <div class="footer-row">
          <p id="copyright">&copy; 2020 copyright</p>
        </div>
      </div>
    </footer>

    绝对定位对于需要独立于页面其余部分工作的偶尔元素很有用,但很少(如果有的话)需要将其用于主要布局,尤其是在响应式设计中。

    【讨论】:

    • 谢谢。我按照你的建议做了,效果很好。
    • @LAF_OUT_LOUD 如果此答案对您有用,您可以考虑接受它,以便将问题标记为已解决,其他有类似问题的人会知道它可能对他们有用。另外我们都得到了一些代表点:) 见What to do when someone answers
    【解决方案2】:

    如果您的问题是希望您的网页/网站具有响应性,请使用 Viewport Units,例如 vw 表示 width 和 vh 表示 height 而不是 px 和 %,因为它可以帮助您使您的网页/网站具有响应性。

    试试吧,它肯定会解决你的问题,但如果它没有让我知道在 cmets 中,我会尽力帮助你。

    我还将您的 CSS 文件转换为 Viewport Units,以便您更容易理解。

    body {
            font-family: Helvetica,Arial,sans-serif; /* styles the font of the entire page */
            margin: 0; /* removes the margin from around the page */
            text-transform: capitalize; /* capitalizes the text on the page */
        }
    
        li {
            display: inline; /* makes the links lay horizontally */
            list-style-type: none; /* removes the markers for the list items behind the links */
        }
    
        #nav, #main, #footer {
            width: 100vw;
            position: absolute; /* fixes the <div> elements to the page */
        }
        
        #nav {
            background-color: #393e46;
            padding-bottom: 2.435vw; /* adds extra padding underneath the search bar */
            top: 0;
            right: 0;
            left: 0;
        }
    
        #nav-container {
            margin: 0 auto; /* centers the 'nav-container' within the <nav> */
            max-width: 14.641vw;
        }
    
        #rowA {
            background-color: red;
        }
    
        .nav-menu {
            margin-top: 0; /* removes the extra margin above the links in the nav */
        }
    
        #rowB {
            background-color: green;
        }
    
        #nav-logo {
            font-size: 2.342vw;
            text-align: center; /* centers the 'FGCPEDIA' logo */
        }
    
        #rowC {background-color: pink;}
    
        .nav-menu {
            float: right; /* makes the 'Sign Up/ Log In' links float to the right */
        }
        
        /* removes the highlight from the form when selected */
        input:focus, textarea:focus {
            outline: 0;
        }
    
        #main {
            background-color: orange;
            top: 26.4vh; /* pushes the main directly under the nav */
            /* top: 32.9%; */
        }
    
        #main-container {
            margin: 0 auto; /* centers the elements in the main */
            max-width: 100vw;
        }
    
        #footer {
            background-color: #222831;
            right: 0;
            bottom: 0;
            left: 0;
        }
    
        #footer-container {
            margin: 0 auto;
            max-width: 14.641vw;
        }
    
        .footer-row {
            padding-inline-start: 0; /* removes the extra padding on the left side of links */
        }
    
        #copyright {
            color:#eeeeee; /* colors the footer text */
            font-size: 0.951vw; /* the font size of the copyright text is '13' */
            text-align: center; /* centers the  */
        }

    【讨论】:

    • 这不是一个答案,它只是一个建议。如果您需要更多信息,请在 cmets 中询问。否则,请向 OP 展示 如何 使用这些来解决问题,以便您的回答有所帮助。
    • 感谢我能得到的任何帮助。有关如何解决此问题的任何建议?
    • @LAF_OUT_LOUD 我的第一个建议是暂时不要开始担心视口单位!问题实际上比这更简单。您在主布局中使用绝对定位,并且您将永远无法使用绝对定位获得有效的响应式设计。它真的很容易修复,但看到没有其他人回答,我猜你还在学习,我添加了一个答案来表明你想做!
    • @FluffyKitten 现在不担心视口单位是不正确的,因为让网站具有响应性是最重要的事情之一,帖子所有者应该尽快了解这一点。您的回答也是正确的,可能比我更好,我非常感谢您的建议,但是对某人的回答投反对票也可能会降低他们的道德,您应该理解这一点并帮助新的贡献者。否决别人的答案以将您的答案置于首位以使其被接受并获得更多赞成票是不道德的,如果您因此获得赞成票,那么您成千上万的声誉就浪费了。
    • @LAF_OUT_LOUD 查看您帖子中的其他答案,它将解决您的问题。如果您有任何其他问题,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 2016-11-20
    • 2016-02-03
    • 1970-01-01
    • 1970-01-01
    • 2022-06-27
    • 2014-10-18
    • 2015-09-09
    • 1970-01-01
    相关资源
    最近更新 更多