缩小浏览器时响应式页脚显示在部分内容上

Responsive footer displayed over parts of content when shrinking browser

我是 CSS 和 HTML 的新手。

我知道这个问题已经被问过好几次了,但我似乎无法解决我当前的问题。

我一直在从事一些小项目,例如尝试重新创建具有额外功能的其他网站。

我当前的任务是重新创建首页搜索区域,例如google。

实施与发布

我一直关注浏览器调整大小时网站的响应能力,因此我一直尽可能多地使用 Flexbox

实施

具有容器 ID、最小高度为 100% 和相对位置的主 div。

在容器内 div 是 nav(#nav)、center(#center) 和 footer(#footer) 元素及其各自的 id。

我对页脚进行了一些研究,以及如何在缩小浏览器时保持响应的同时将其保留在页面底部,但我似乎停了下来或者可能我还没有完全理解如何让页脚停在某个元素。

问题

问题是我无法让页脚正常工作。我不希望它与主要内容中的按钮和 link 重叠。调整浏览器大小时它会停在搜索栏上,但我希望它在遇到按钮时停止。

正如您将从 JSbin link 中的代码片段块中看到的那样,我的页脚有问题。

当前 HTML + CSS 文件和输出JSBin

*{
    margin: 0px;
    padding: 0px;
}
html, body {
    height: 100%;  
}

#container {
    min-height:100%;
    position:relative;

}

a:visited{
    color: #609;
}

/* ----- Navigation Styling ----- */

#nav {
    display: flex;
    border: 10px solid  goldenrod;               /*Colourful Borders*/
    min-height:9vh;
    justify-content:flex-end;
    align-items: baseline;
    background:#ff0;
    padding:10px;
}

/*Nav Content*/

/* ----- Center Styling ----- */

#center {
    width: 100%;
    background-color: white;
    min-height:10vh;     
}

.ctr_img {
    height: 92px;
    width: 272x;
    padding-top: 20px;
    
}
.ctr_img:hover {
    -webkit-filter: invert(100%);
}

.img_mic {
    padding: 0 8px;
    float: right;
    display:inline-block;
    top: -30px;
    position: relative;
}

.srch_bx {
    border-style: hidden;
    box-shadow: 0 2px 2px 0 rgba(0,0,0,0.16), 0 0 0 1px rgba(0,0,0,0.08);
    max-width: 40%;
    min-height: 40px;
}

.srch_in {
    width: 100%;
    border-radius: 2px;
    border: none;
    overflow: auto;
    outline: none;
    left: 0px;
    background: url(data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw%3D%3D) transparent;
    font: 16px arial,sans-serif;
    display: inline-block;
    text-align: start;
}

.mic_set {
    background-size: 30px 30px;
    height: 100%;
    width: 26px;
}

.btn_sbmt {
    padding: 20px 50%;
    min-height: 20px;
    display: flex;
    justify-content: center;
}

.btn_srch, .btn_lcky {
    background-color: #f2f2f2;
    border: 1px solid #f2f2f2;
    border-radius: 2px;
    color: #757575;
    font-family: arial,sans-serif;
    font-size: 1vw;
    font-weight: bold;
    margin: 11px 4px;
    padding: 0 16px;
    height: 36px;
}
    
.lang_diff{
    max-height: 28px;
    font-size: 0.9vw;
    margin-bottom: 24px;
    font-family: arial,sans-serif;
}

/* Footer Styling */

#footer {
    position:absolute;
    bottom:0;
    height: 5.2em; /* Height of the footer */
    width: 100%;
    min-height: 10vh;
    background-color: #f2f2f2;
    line-height: 40px; /*Specifies the line height.*/
    min-width: 400px;


}

.ctry {
    display: flex;
    margin-left: 30px;
    text-align: left;
    color: rgba(0,0,0,.54);
    border-top: 1px solid #e4e4e4;
    max-width: 150px;
}

.ctry_ftr {
    font-size: 1vw;
}

.opt_ftr {
    display: flex;
    justify-content:space-between;
    border:  1px solid #e4e4e4;
}

span.blft_ftr a, span.brght_ftr a, span.brght_stg a {
    text-decoration: none;
    color: #666;
    margin-left: 16px;
    font-size: 1vw;
}

.brght_ftr{
    margin-right: 30px;
    float: right;
}

.adv_ftr {
    padding: 0 16px;
    text-decoration: none;
    display: inline-block;
}
<!DOCTYPE html>
<html>
    <head>
        <title>Testing Ground</title>
        <link rel="stylesheet" type="text/css" href="style.css" />
        <link rel="stylesheet" type="text/css" href="reset.css" />
        <link rel="icon" href="http://www.favicon.cc/logo3d/53653.png" />
        <meta charset="UTF-8">
    </head>
    <body>  <!--FLEX-->
        <div id="container"> <!-- Added New for Footer behaviour -->
            <nav id="nav" class="nav_cls">
                <!--Currently at work-->
                <p>Navigation Bar Content</p> 
            </nav>
            <center id="center">
                <a class="img_link" href="">
                    <img class="ctr_img" src="https://upload.wikimedia.org/wikipedia/commons/thumb/c/c4/PM5544_with_non-PAL_signals.png/250px-PM5544_with_non-PAL_signals.png" alt="image">
                </a>
                <div class="in_forms">
                    <div class="srch_bx">
                        <form>
                            <input class="srch_in" type="text" name="search"/>
                        </form>
                        <div class="img_mic">
                            <a href="https://youtu.be/Ye8mB6VsUHw ">
                                <img class="mic_set" src="https://cdn.sesamestreet.org/sites/default/files/1496328210/Cookie_Big.png" />
                            </a>
                        </div>
                    </div>
                </div>
                <div>
                    <div class="btn_sbmt">
                        <input class="btn_srch" type="submit" value="Don't Hide me!" name="search"/>
                        <input class="btn_lcky" type="submit" value="Seriously, Don't!" name="luck"/>
                    </div>
                </div>
                <div class="lang_diff">
                    Links:
                    <a class="lang" href="">Link1</a>
                    <a class="lang" href="">Link2</a>
                    <a class="lang" href="">Link3</a>
                    <a class="lang" href="">Link4</a>
                    <a class="lang" href="">Link5</a>
                </div>
            </center>
            <footer id="footer">
                <div class="ctry">
                    <span class="ctry_ftr">
                        First half of a footer 
                    </span>
                </div>
                <div class="opt_ftr">
                    <span class="blft_ftr">
                        <a class="adv_ftr" href="">Footer Link 1</a>
                        <a class="adv_ftr" href="">Footer Link 2</a>
                        <a class="adv_ftr" href="">Footer Link 3</a>
                    </span>
                    <span class="brght_ftr">
                        <a class="adv_ftr" href="">Footer Link 4</a>
                        <a class="adv_ftr" href="">Footer Link 5</a>
                    </span>
                </div>
            </footer>
        </div>
    </body>
</html>

访问过的网站

:)

首先恭喜你遵守了在SO上提问的规则。

您的问题来自将页脚置于文档流之外 (position:absolute)。看起来您希望将其置于文档流中。所以把position:absolute改成position:relative(或者static)。

要始终将其保持在屏幕底部,请将 #container 调整为 display:flex;flex-direction:column;min-height:100vh 并使 #center 增长:

#footer {
  position:static; /* this is default value of position
                    * so you could just remove `position:absolute`
                    * from your code.
                    */
}
#container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}
#center {
  flex-grow: 1;
}

看到它有效:

* {
  margin: 0px;
  padding: 0px;
}

html,
body {
  height: 100%;
}

#container {
  min-height: 100%;
  position: relative;
}

a:visited {
  color: #609;
}


/* ----- Navigation Styling ----- */

#nav {
  display: flex;
  border: 10px solid goldenrod;
  /*Colourful Borders*/
  min-height: 9vh;
  justify-content: flex-end;
  align-items: baseline;
  background: #ff0;
  padding: 10px;
}


/*Nav Content*/


/* ----- Center Styling ----- */

#center {
  width: 100%;
  background-color: white;
  min-height: 10vh;
}

.ctr_img {
  height: 92px;
  width: 272x;
  padding-top: 20px;
}

.ctr_img:hover {
  -webkit-filter: invert(100%);
}

.img_mic {
  padding: 0 8px;
  float: right;
  display: inline-block;
  top: -30px;
  position: relative;
}

.srch_bx {
  border-style: hidden;
  box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.16), 0 0 0 1px rgba(0, 0, 0, 0.08);
  max-width: 40%;
  min-height: 40px;
}

.srch_in {
  width: 100%;
  border-radius: 2px;
  border: none;
  overflow: auto;
  outline: none;
  left: 0px;
  background: url(data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw%3D%3D) transparent;
  font: 16px arial, sans-serif;
  display: inline-block;
  text-align: start;
}

.mic_set {
  background-size: 30px 30px;
  height: 100%;
  width: 26px;
}

.btn_sbmt {
  padding: 20px 50%;
  min-height: 20px;
  display: flex;
  justify-content: center;
}

.btn_srch,
.btn_lcky {
  background-color: #f2f2f2;
  border: 1px solid #f2f2f2;
  border-radius: 2px;
  color: #757575;
  font-family: arial, sans-serif;
  font-size: 1vw;
  font-weight: bold;
  margin: 11px 4px;
  padding: 0 16px;
  height: 36px;
}

.lang_diff {
  max-height: 28px;
  font-size: 0.9vw;
  margin-bottom: 24px;
  font-family: arial, sans-serif;
}


/* Footer Styling */

#footer {
  /* position: absolute; 
     bottom: 0; */
  height: 5.2em;
  /* Height of the footer */
  width: 100%;
  min-height: 10vh;
  background-color: #f2f2f2;
  line-height: 40px;
  /*Specifies the line height.*/
  min-width: 400px;
}

.ctry {
  display: flex;
  margin-left: 30px;
  text-align: left;
  color: rgba(0, 0, 0, .54);
  border-top: 1px solid #e4e4e4;
  max-width: 150px;
}

.ctry_ftr {
  font-size: 1vw;
}

.opt_ftr {
  display: flex;
  justify-content: space-between;
  border: 1px solid #e4e4e4;
}

span.blft_ftr a,
span.brght_ftr a,
span.brght_stg a {
  text-decoration: none;
  color: #666;
  margin-left: 16px;
  font-size: 1vw;
}

.brght_ftr {
  margin-right: 30px;
  float: right;
}

.adv_ftr {
  padding: 0 16px;
  text-decoration: none;
  display: inline-block;
}

#footer {
  line-height: 2.42em;
}
#container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

#center {
  flex-grow: 1;
}
<div id="container">
  <!-- Added New for Footer behaviour -->
  <nav id="nav" class="nav_cls">
    <!--Currently at work-->
    <p>Navigation Bar Content</p>
  </nav>
  <center id="center">
    <a class="img_link" href="">
      <img class="ctr_img" src="https://upload.wikimedia.org/wikipedia/commons/thumb/c/c4/PM5544_with_non-PAL_signals.png/250px-PM5544_with_non-PAL_signals.png" alt="image">
    </a>
    <div class="in_forms">
      <div class="srch_bx">
        <form>
          <input class="srch_in" type="text" name="search" />
        </form>
        <div class="img_mic">
          <a href="https://youtu.be/Ye8mB6VsUHw ">
            <img class="mic_set" src="https://cdn.sesamestreet.org/sites/default/files/1496328210/Cookie_Big.png" />
          </a>
        </div>
      </div>
    </div>
    <div>
      <div class="btn_sbmt">
        <input class="btn_srch" type="submit" value="Don't Hide me!" name="search" />
        <input class="btn_lcky" type="submit" value="Seriously, Don't!" name="luck" />
      </div>
    </div>
    <div class="lang_diff">
      Links:
      <a class="lang" href="">Link1</a>
      <a class="lang" href="">Link2</a>
      <a class="lang" href="">Link3</a>
      <a class="lang" href="">Link4</a>
      <a class="lang" href="">Link5</a>
    </div>
  </center>
  <footer id="footer">
    <div class="ctry">
      <span class="ctry_ftr">
                        First half of a footer 
                    </span>
    </div>
    <div class="opt_ftr">
      <span class="blft_ftr">
                        <a class="adv_ftr" href="">Footer Link 1</a>
                        <a class="adv_ftr" href="">Footer Link 2</a>
                        <a class="adv_ftr" href="">Footer Link 3</a>
                    </span>
      <span class="brght_ftr">
                        <a class="adv_ftr" href="">Footer Link 4</a>
                        <a class="adv_ftr" href="">Footer Link 5</a>
                    </span>
    </div>
  </footer>
</div>

另一个需要解决的小问题是使用 px 表示的 line-height 代替 #footer 而其高度用 em 表示,导致页面垂直发展滚动条。 line-height:2.42em 修复它。


换句话说,如果我给你这个答案而不提及你的示例在窄设备上降低字体大小所带来的主要可访问性问题,那将是不公平的。尝试在任何移动设备上打开示例页面,您就会明白我的意思。在任何时候,页面中的元素都应保持字体大小以供用户阅读。

网页的主要目的是呈现内容。如果您以禁止用户接收消息的形式呈现内容,您的页面将无法执行其功能。

#footer 是绝对定位的,这意味着 parent 容器不知道它有多大(即它在 "document flow" 之外)。 #container 的高度设置为 min-height: 100%,因此至少需要是 parent(在本例中为正文)的全高,但如果 child 内容较高,它会拉伸以适应额外的高度。

因为 #container 试图计算出它的高度时没有考虑 #footer#container 只是确保它可以适合其他 position: staticposition:relative child仁.

因此,当您缩小浏览器的高度使其非常短时,#container 太短了 - 它不会为 #footer 留下 space。

有几种方法可以解决这个问题,但如果 #footer 的高度根据内容和是否换行等而变化,从响应的角度来看这很棘手

如果你知道高度,你可以在 #container 的底部添加填充量(如果 box-sizing: border-box; 已打开,它不会添加到 height: 100% -它将被包括在内)并且它将为 #footer.

保留 space

如果您不知道高度,您可以利用 display: flex; 填充整个高度并将页脚 self-align 置于底部,而无需使用 position: absolute;。虽然 Flexbox 有一点学习曲线,但我建议您通读一些教程。 http://flexboxfroggy.com/ 是一个有趣的游戏,也可以帮助学习它。