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

2024-01-08

我对 CSS 和 HTML 还很陌生。

据我所知,这个问题已被问过多次,但我似乎无法解决当前的问题。

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

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

实施与发布

我一直关注浏览器调整大小时网站的响应能力,所以我一直在使用Flexbox尽可能经常地。

执行

一个带有容器 id 的主 div,最小高度为 100% 以及相对位置。

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

  • nav:柔性显示,基线对齐项目和最小高度为 9vh。
  • center:宽度为 100%,最小高度为 10vh;
  • footer:绝对位置,底部0,宽度为100%

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

Issue

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

正如您从 JSbin 链接中的代码片段块中看到的那样,我对页脚有疑问。

当前 HTML + CSS 文件和输出: JSBin http://jsbin.com/kojasimeco/edit?html,css,output

*{
    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>

访问过的网站

  • 页脚显示在内容之上。我需要它始终位于页面底部 https://stackoverflow.com/questions/19865705/footer-is-displayed-over-content-i-need-it-always-on-the-bottom-of-the-page

  • HTML、CSS - 粘性页脚显示在屏幕调整大小的内容上 https://stackoverflow.com/questions/10319377/html-css-sticky-footer-displays-over-content-on-screen-resize

  • 页脚显示在内容之上。我需要它始终位于页面底部 https://stackoverflow.com/questions/19865705/footer-is-displayed-over-content-i-need-it-always-on-the-bottom-of-the-page

  • 如何将页脚保留在页面底部 http://matthewjamestaylor.com/blog/keeping-footers-at-the-bottom-of-the-page

  • 粘页脚 https://css-tricks.com/snippets/css/sticky-footer/

  • 窗口缩小时页脚与主要内容重叠 http://doctype.com/footer-overlaps-main-content-when-window-shrunk


:)

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

您的问题来自于将页脚置于文档流之外(position:absolute)。看起来您希望将其放置在文档流中。所以改变position:absolute into position:relative (or static).

要使其始终保持在屏幕底部,请调整您的#container to display:flex;flex-direction:column;min-height:100vh和做#center grow:

#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>

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


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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

缩小浏览器时,响应式页脚显示在部分内容上 的相关文章

  • 如何向 CSS 形状添加偏移轮廓?

    我在创建带有斜角边缘的块时遇到了一些问题 此外我需要一个斜角的边框并稍微偏离主块 问题是这个块可以根据屏幕响应 不知道具体的方法 希望大家帮忙 这就是我现在所做的 box display flex padding 20px height 2
  • JavaScript 中带前导零的数字发生变化

    我使用 print 语句从 php 调用 javascript 函数来打印 html 代码 并且传入一个整数 但是 在 php 中传递的值与 javascript 函数接收到的数字不匹配 我不知道为什么 这是调用 javascript 函数
  • Bootstrap 中的旋转字形 / Font Awesome

    我试图让引导站点中的字形在悬停时旋转 除了更改颜色之外 这是我的尝试 http jsfiddle net young greedo17 88g5P http jsfiddle net young greedo17 88g5P 使用此代码 d
  • 使用绝对定位时文本被破坏

    我有一个小挑战 我在 Stack Overflow 上没有找到任何解决方案 这就是我得到的 这就是我想要的 为了产生这个标题效果 我使用绝对位置 我什至不知道我的标题的宽度和高度 因此 使用此解决方案时 大文本会中断 My HTML div
  • 滚动时将菜单栏固定在顶部[关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我见过一些网站 当用户向下滚动页面时 会在右侧或左侧弹出一个框 另外 注意到这个模板 http www mvpthemes com m
  • Internet Explorer 中的锯齿状按钮边缘

    如何去除 Internet Explorer 中宽按钮的锯齿状边缘 例如 您还可以通过设置来消除 Windows XP 的按钮样式 以及 Windows 的所有其他版本 background color and or border colo
  • 我应该使用哪种文档类型?

    如果我想使用可定制的 div 我应该使用哪种文档类型 具有div动画 图像移动 设置div不透明度等 我尝试通过 javascript 创建一个 div 设置其背景颜色 位置 宽度和高度 并向其添加 onmouseover 事件 一切正常
  • Django:使用条件 {% extends %} 使 {% block "div" %} 成为条件

    我想分享一个 AJAX 和常规 HTTP 调用之间的模板 唯一的区别是一个模板需要扩展 base html html 而另一个则不需要 我可以用 extends request is ajax yesno app base ajax htm
  • 如何在 Lift 框架中添加新页面

    如何在 lift 中的 webapp 目录中添加一个可供用户访问的新页面 目前只能通过index html访问http localhost 8080 com http localhost 8080 or http localhost 808
  • 如何使用 HTML 5 实现类似 gmail 的文件上传/附件

    我记得一些支持 Ajax 之类的选项 无回发世界 文件上传 隐藏的 iframe 使用 flash 对象 尽管我仍然好奇为什么使用 SWF 以及它提供什么优势 然而 通过查看博客 HTML 5 似乎很有前途 我尝试了一些小示例 它确实有效
  • 为什么我的反应路由器链接将我带到页面中间?

    我的网站上有很多链接 但只有一个可以执行此操作 它没有将我带到导航栏的顶部 而是转到内容的中间 知道为什么会这样吗 这是一个页面 其中有一个 map经历一些JSON 这是渲染div页面下方的文本元素 如果重要的话 这是一些可能相关的代码 路
  • 如何在通过 .ajaxForm() 提交表单之前执行一些操作?

    我正在使用 ajaxForm 框架来发送我的数据 而无需重新加载我的页面 ReplayForm ajaxForm success function data alert Success 现在 我想在提交表单之前检查一些条件 如果条件为假 则
  • 如何让Gmail像加载进度条一样

    我想在页面的中心和顶部创建一个像 Gmail 一样的加载进度条 并适用于所有浏览器 这是基本代码
  • 为不同的字体系列指定不同的字体大小

    有没有办法为不同的字体系列指定不同的字体大小 我想要使 用的字体 出于产品品牌目的 是一种有点罕见的字体 FlashDLig 并非所有 PC 和浏览器都支持 我的一台带有 IE 9 的 Windows 7 PC 不显示它 现在 对于我使用
  • HTML 代码中的 PHP [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我用 HTML 代码编写了 PHP div div 但这出现在输出页面中 else print 我怎样才能让PHP执行 你的文件有一个 p
  • 如何通过 jQuery 中的类获取特定 html 元素的innerHTML?

    我有这样的 HTML 代码 div class a html value 1 div div class a html value 2 div 我怎样才能访问html value 1 and html value 2使用jquery 分别地
  • 如何使整个跨度落入新行?

    这个片段显示了我想要的 http jsfiddle net 945Df 3 http jsfiddle net 945Df 3 div class sup strong a href Rosario Santa Fe Argentina a
  • 如何删除标题中的粗体?

    我有一个标题 h1 THIS IS A HEADLINE h1 如何使短语 THIS IS 不加粗 其余部分不做任何更改 我在文本装饰中找不到任何相关标签 标题看起来很粗体 因为它大尺寸 如果您已应用粗体或想要更改行为 您可以执行以下操作
  • 如何在画布上所有其他内容后面绘制图像? [复制]

    这个问题在这里已经有答案了 我有一块画布 我想用drawImage在画布上当前内容后面绘制图像 由于画布上已经有内容 我正在使用字面上的画布来创建包含图像的画布 因此我无法真正先绘制图像 所以我无法使用drawImage在我呈现其余内容之前
  • 如何在数据列表 HTML PHP 中设置选择

    您好我想知道是否有一种方法可以在数据列表中设置选定的值 我想要这样的东西

随机推荐