【问题标题】:Div(s) between two fixed elements (header and footer)两个固定元素(页眉和页脚)之间的 Div(s)
【发布时间】:2016-07-28 21:12:54
【问题描述】:

这是我想做的:

我希望我已经用图片很好地解释了它。 我在这个主题上搜索了很多,但我真的不想进入大量的功能。我想尽可能简单地解决它。

问题在于,如果用户更改浏览器大小或在不同屏幕上显示,我希望所有人都能实时响应。

另外,关于在左侧菜单中添加这些图像;它们是 1:1 的边比,但我想将它们缩放在一起,它们将是左侧菜单的 100% 高度(图片中的蓝色)。

这是我目前所拥有的:

HTML/CSS:

header {
   background-color: red;
   position: relative;
   width: 100%;
}

#allContents {
   position: relative; /*minus širina headerja in footerja!!*/
   height: 100%;
   width: 100%;
   float: left;
   clear: both;
   z-index: 10;
}

#leftMenu {
   position: relative;
   background-color: green;
   width: 10%;
   height: 100%;
   z-index:10; 
}

#mainContent {
   background-color: yellow;
   size: 20%;
   float: left;
   position: relative;
}

#rightMenu {
   background-color: orange;
   float: left;
   position: relative;
   top: 0;
   right: 0;
   width: 25%;
}

footer {
   clear: both;
   background-color: blue;
   position: fixed;
   bottom: 0px;
   width: 100%;
}
<header>
   Fixed. Some header content... also image on the left.
</header>

<div id="allContents">
   <div id="leftMenu">On left, between header and footer, but never behind. Always between.</div>
   <div id="mainContent">Between all 4 elements. Here will be main content with scrolling.</div>
   <div id="rightMenu">Div with some news.</div>
</div>

<footer>
   Fixed footer.
</footer>

【问题讨论】:

    标签: html css position fixed


    【解决方案1】:

    听起来您会从使用 Flexbox 中受益匪浅。它们允许您使用 CSS 格式化您想要的确切结构,并且它们响应窗口大小。 Flexbox Froggy 是学习如何使用它们的绝佳资源。

    【讨论】:

    • 这确实很棒 :D 看起来很有希望,谢谢。我也找到了这个指南 [link]css-tricks.com/snippets/css/a-guide-to-flexbox
    • 其他元素按我的意愿工作,但我需要滚动才能看到页脚
    • @HelloDarkness 那是因为我在 CSS 第 3 行的body, html 中添加了height: 100vh。你可以把它拿出来,那些 div 只会和它们中的内容一样“高”。此外,如果这对你有用,那么你能把它作为答案吗?
    • 但现在,如果标题变高,您需要滚动到内容的结尾。我需要它是不滚动的页面。我希望所有元素的高度组合为 100vh .. 不仅是 3 个中间元素
    【解决方案2】:

    你在找这个吗?

    header {
       background-color: red;
       position: relative;
       width: 100%;
    }
    
    #allContents {
       position: relative; /*minus širina headerja in footerja!!*/
       height: 100%;
       width: 100%;
       float: left;
       clear: both;
       z-index: 10;
        display: -webkit-flex;
        display: flex;
    }
    
    #leftMenu {
       position: relative;
       background-color: green;
       width: 10%;
       height: 100%;
       z-index:10; 
     
    }
    
    #mainContent {
       background-color: yellow;
       size: 20%;
       float: left;
       position: relative;
     
    }
    
    #rightMenu {
       background-color: orange;
       float: left;
       position: relative;
       top: 0;
       right: 0;
       width: 25%;
      
    }
    <header>
       Fixed. Some header content... also image on the left.
    </header>
    
    <div id="allContents">
       <div id="leftMenu">On left, between header and footer, but never behind. Always between.</div>
       <div id="mainContent">Between all 4 elements. Here will be main content with scrolling.</div>
       <div id="rightMenu">Div with some news.</div>
    </div>
    
    <footer>
       Fixed footer.
    </footer>

    【讨论】:

    • 页脚仍然不在屏幕底部,如果窗口大于(大约)500px,页眉比其他元素更靠右。无论如何,我感谢您的帮助:)
    • 是的,您可以通过添加此样式页脚将其固定在底部{ position: fixed;bottom:0;background: yellow;宽度:100%;文本对齐:居中;}
    • 这会让其他元素在它下面(如果页脚有固定位置)?
    • ok 使用位置:绝对;并把 body{ /* padding-bottom: 你的页脚高度放在这里 */ }
    • 问题 #1:页脚没有固定高度和 #2:中间 3 个元素不能拉伸以填充所有可用空间
    猜你喜欢
    • 2022-01-06
    • 2023-03-16
    • 2014-11-05
    • 1970-01-01
    • 1970-01-01
    • 2014-07-07
    • 2013-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多