html css布局与标题两列和页脚

时间:2014-03-09 23:08:13

标签: html css layout

我正在尝试创建一个高度为100px且宽度为100%的头部区域。 我需要2列,左边一列是250px宽,100%是高度,直到页脚。右列应为页脚宽度的100%和页脚高度的100%。页脚应位于页面底部,高度为100px,宽度为100%。 即使2列中没有内容,我也需要它们向下伸展到页脚并让页脚可见而不向下滚动。

这是我到目前为止所拥有的。

<div id="top"><p>Lorem ipsum dolor sit amet</p></div>
<div id="left"><p>Lorem ipsum dolor sit amet</p></div>
<div id="right"><p>Lorem ipsum dolor sit amet</p></div>
<div id="bot"><p>Lorem ipsum dolor sit amet</p></div>

body, html {
    height: 100%;
}
body {
    margin: 0px;
}
p {
    margin: 0px;
}
#top {
    height: 100px;
    background-color: #F4F4F4;
}
#left {
    width: 250px;
    height: 100%;
    background-color: #878787;
    float: left;
    margin-bottom: -100px;
}
#right {
    background-color: #323232;
    height: 100%;
    margin-bottom: -100px;
}
#bot {
    clear: right;
    height: 100px;
    background-color: #DCDCDC;
    margin-top: -100px;
    z-index: 100;
    position: relative;
}

这是表

的另一个例子

<table height="100%" width="100%" cellspacing="0" cellpadding="0" border="0" class="" id="">
	<tr>
		<td colspan="2" style="background-color: powderblue; height: 100px;">Header</td>
	</tr>
	<tr>
		<td style="background-color: gray; width: 350px;">Left Col</td>
		<td style="background-color: DarkSeaGreen">Right Col</td>
	</tr>
	<tr>
		<td colspan="2"  style="background-color: tomato; height: 100px;">Footer</td>
	</tr>
</table>

enter image description here

9 个答案:

答案 0 :(得分:4)

这是一个简单的CSS网格解决方案(我使用50px而不是100px,所以我们可以在简化的代码段中看到它)

html {
    height: 100%;
}
p {
    margin: 0px;
}
body {
  min-height:100%;
  margin:0;
  display:grid;
  grid-template-rows:50px 1fr 50px;
  grid-template-columns:250px 1fr;
  grid-template-areas:
    "top top"
    "left right"
    "bot bot";
}

#top {
    grid-area:top;
    background: linear-gradient(#F4F4F4,blue);
}
#left {
    grid-area:left;
    background: linear-gradient(#878787,red);
}
#right {
    grid-area:right;
    background: linear-gradient(#323232,green);
}
#bot {
    grid-area:bot;
    background: linear-gradient(#DCDCDC,purple);
}
<div id="top"><p>Lorem ipsum dolor sit amet</p></div>
<div id="left"><p>Lorem ipsum dolor sit amet</p></div>
<div id="right"><p>Lorem ipsum dolor sit amet</p></div>
<div id="bot"><p>Lorem ipsum dolor sit amet</p></div>

这是flexbox:

body, html {
    height: 100%;
}
p {
    margin: 0px;
}
body {
  margin:0;
  display:flex;
  flex-wrap:wrap;
}

#top,#bot {
    height:50px;
    width:100%;
    background: linear-gradient(#F4F4F4,blue);
}
#left {
    width:250px;
    min-height:calc(100% - 2 * 50px);
    background: linear-gradient(#878787,red);
}
#right {
    flex:1;
    min-height:calc(100% - 2 * 50px);
    background: linear-gradient(#323232,green);
}
<div id="top"><p>Lorem ipsum dolor sit amet</p></div>
<div id="left"><p>Lorem ipsum dolor sit amet</p></div>
<div id="right"><p>Lorem ipsum dolor sit amet</p></div>
<div id="bot"><p>Lorem ipsum dolor sit amet</p></div>

答案 1 :(得分:0)

您可以使用Sticky Footer Concept:Sticky Footer

* {
    margin: 0;
}
html, body {
    height: 100%;
}
.wrapper {
    min-height: 100%;
    height: auto !important;
    height: 100%;
    margin: 0 auto -155px; /* the bottom margin is the negative value of the footer's height */
}
.footer, .push {
    height: 155px; /* .push must be the same height as .footer */
}

结果如下所示:Working Fiddle

但如果没有javascript,则无法将内容拉伸到底部。你为什么要这样做呢?

答案 2 :(得分:0)

试试这个

#bot {
clear: right;
background-color: #DCDCDC;
position:absolute;
bottom:0;
width:100%;
z-index: 100;
margin-bottom: 0px;
}

Working Fiddle

答案 3 :(得分:0)

在这些情况下,我建议position: fixedabsolute。它就像calc()一样,但在旧的浏览器中也是如此!

Demo

#top, #left, #right, #bot {
    position: fixed;
}
#top {
    top: 0;
    left: 0;
    right: 0;
    height: 100px;
}
#left, #right {
    top: 100px;
    bottom: 100px;
}
#left {
    width: 250px;
    left: 0;
}
#right {
    left: 250px;
    right: 0;
}
#bot {
    height: 100px;
    bottom: 0;
}

这也是一个好主意(如果浏览器窗口太小),也可以使用

html, body {
    height: 100%;
    position: relative;
    min-height: 300px; /* Minimum height */
    margin: 0;
}
#top, #left, #right, #bot {
    position: absolute;
}
#left, #right {
    overflow: auto; /* Enable scrollbars if necessary*/
}

Demo

答案 4 :(得分:0)

使用calc()。太棒了。

<强> DEMO * FullscreenDEMO *

<div class="head">header</div>
<div class="left">left</div>
<div class="right">right</div>
<div class="foot">footer</div>

   *{
    margin:0;
}
html,body{
    height:100%;
}
.head,.foot{
    width:100%;
    height:100px;
    background-color:red;
}
.left{
    width:250px;
    min-height:calc(100% - 200px);
    background-color:blue;
    display:inline-block;
    float:left;
}
.right{
    width:calc(100% - 250px);
    min-height:calc(100% - 200px);
    background-color:green;
    display:inline-block;
    float:left;
}
.foot{
    float:left;
}

答案 5 :(得分:0)

http://codepen.io/anon/pen/jhCed

奖励:拉伸左栏背景为100%。

  <div id="page">
    <div id="top"><p>Lorem ipsum dolor sit amet</p></div>
    <div id="content">
      <div id="left"><p>Lorem ipsum dolor sit amet</p></div>
      <div id="right"><p>Lorem ipsum dolor sit amet</p></div>
    </div>
  </div>
  <div id="bot"><p>Lorem ipsum dolor sit amet</p></div>

 html,body {
  margin: 0;
  padding: 0;
  height: 100%;
}
p {
    margin: 0px;
}
#page {
  position: relative;
  min-height: 100%; 
}
#page:before{
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  z-index: -1;
  height: 100%;
  width: 250px;
  background-color: #878787;
}
#page:after {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  z-index: -2;
  height: 100%;
  width: 100%;
  background-color: #323232;
}

#content {
  overflow: hidden;
  padding-bottom: 100px;
}
#top {
    height: 100px;
    position: relative;
    background-color: #F4F4F4;
}
#left {
    width: 250px;
    float: left;
    position: relative;
}
#right {
  margin-left: 260px;   
}
#bot {
    position: relative;
    margin: -100px auto 0 auto;
    height: 100px;
    background-color: #DCDCDC;
}

答案 6 :(得分:0)

好的,这是使用css表的替代方法。它的工作原理如下:

  • #top#bot始终100px高。
  • 如果可以的话,
  • #left将会250px
  • #right占据#left
  • 留下的所有水平空间
  • #left#right推送#bot
  • 如果有垂直剩余空间,#left#right将会增长,以便#bot位于窗口底部。

Demo

html, body {
    height: 100%;
    width: 100%;
    margin: 0;
}
body {
    display: table;
}
#top, #bot {
    display: table-row;
}
#top {
    height: 100px;
}
#middle {
    display: table-cell;
    overflow: hidden;
}
#left, #right {
    margin-bottom: -100000px;
    padding-bottom: 100000px;
}
#left {
    width: 250px;
    float: left;
}
#right {
    overflow: hidden;
}
#bot {
    height: 100px;
}

需要将html更改为

<div id="top"></div>
<div id="middle">
    <div id="left"></div>
    <div id="right"></div>
</div>
<div id="bot"></div>

使用大于100000px#left高度的值,而不是#right

由于它有点hacky,你可能更喜欢没有浮点数的css-tabular-only approach,但是在IE8上不起作用,尽管它只使用CSS2.1功能。

答案 7 :(得分:0)

您可以使用flex

以下是一个有效的例子:

http://jsfiddle.net/6Mp4g/2/

Flex是一种较新的 CSS3 解决方法,可为内容创建拉伸和响应式布局。它可能会有点复杂,所以我只会链接到源代码和一些语法 how-to

https://developer.mozilla.org/en-US/docs/Web/CSS/flex

<强> HTML:

<header>
    <h1>Header</h1>
</header>
<section class="Middle">
    <div class="LeftMenu">
        <h1>Left Menu</h1>
    </div>
    <div class="Content">
        <h1>Content</h1>
    </div>
</section>
<footer>
    <h1>Footer</h1>
</footer>

<强> CSS:

*
{
    margin: 0;
    padding: 0;
}
html, body
{
    height: 100%;
    text-align: center;
}
body
{
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    -webkit-flex-direction: column;
    -ms-flex-direction: column;
    flex-direction: column;
}
.Middle
{    
    -webkit-flex: 1 1 auto;
    -ms-flex: 1 1 auto;
    flex: 1 1 0;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    overflow: hidden;
}
.Content
{   
    -webkit-flex: 1 1 auto;
    -ms-flex: 1 1 auto;
    flex: 1 0 0;
    overflow: auto;
}
header
{
    background-color: #AAA;
    height:100px;
}
.LeftMenu
{
    background-color:  #CCA;
    width:250px;
}
.Content
{
    background-color: #CCF;
}
footer
{
    background-color: #AAA;
    height:100px;
}

答案 8 :(得分:0)

这是我使用网格的解决方案。 您需要有两个嵌套的网格才能始终显示底部的网格项,即使内容溢出也是如此。

这使用向后兼容的-ms代码

body, html {
  height: 100%;
}

body {
  height: 100%;
  /* Define a grid with top and bottom each 100 pixel height */
  /* MSIE */
  display: -ms-grid;
  -ms-grid-columns: 1fr;
  -ms-grid-rows: 100px 1fr 100px;
  /* Modern browsers */
  display: grid;
  grid-template-rows: 100px 1fr 100px;
}

#top {
  /* This grid item takes the first row */
  /* MSIE */
  -ms-grid-row: 1;
  -ms-grid-row-span: 1;
  /* Modern browsers */
  grid-row: 1 / span 1; /* Shorthand for grid-row-start: 1; grid-row-end: span 1;*/
}

#center {
  /* This grid item takes the second row */
  /* MSIE */
  -ms-grid-row: 2;
  -ms-grid-row-span: 1;
  /* Modern browsers */
  grid-row: 2 / span 1; /* Shorthand for grid-row-start: 2; grid-row-end: span 1;*/
  overflow: auto;

}
#bot {
  /* This grid item takes the third row */
  /* MSIE */
  -ms-grid-row: 3;
  -ms-grid-row-span: 1;
  /* Modern browsers */
  grid-row: 3 / span 1;  /* Shorthand for grid-row-start: 3; grid-row-end: span 1;*/
}

#center {
  /* Define a grid with 250px width column and auto width column */
  /* MSIE */
  display: -ms-grid;
  -ms-grid-rows: auto;
  -ms-grid-columns: 250px 1fr;
  /* Modern browsers */
  display: grid;
  grid-template-columns: 250px 1fr;
}

#left {
  /* This grid item uses first column */
  /* MSIE */
  -ms-grid-column: 1;
  -ms-grid-column-span: 1;
  /* Modern browsers */
  grid-column: 1 / span 1; /* Shorthand for grid-column-start: 1; grid-column-end: span 1;*/
}

#right {
  /* This grid item uses second column */
  /* MSIE */
  -ms-grid-column: 2;
  -ms-grid-column-span: 1;
  /* Modern browsers */
  grid-column: 2 / span 1; /* Shorthand for grid-column-start: 2; grid-column-end: span 1;*/
}

/*
 * The span 1 parts could be omitted, as that is the default value for grid-(row|column)-end 
 */


/* Styling */
body {
  margin: 0;
}

#top, #bot {
  background: blue;
}

#left {
  background: red;
}

#right {
  background: green;
}
<div id="cont"></div>
  <div id="top">#top</div>
  <div id="center">
    <div id="left"><p>Lorem, ipsum dolor.</p>
    <p>Nisi, ducimus laudantium.</p>
    <p>Alias, tempore sapiente.</p>
    <p>Deleniti, rerum harum.</p>
    <p>Impedit, fugiat accusantium?</p>
    <p>Nulla, ipsam placeat.</p>
    <p>Itaque, incidunt doloribus.</p>
    <p>Voluptas, saepe soluta?</p>
    <p>Cum, possimus repellendus.</p>
    <p>Quaerat, sed numquam!</p>
    <p>Soluta, illo qui.</p>
    <p>Officiis, id qui.</p>
    <p>Tempora, perspiciatis ab.</p>
    <p>Asperiores, cupiditate minus!</p>
    <p>Molestiae, aut voluptatem?</p>
    <p>Inventore, odit quisquam.</p>
    <p>Veritatis, impedit possimus.</p>
    <p>Quia, facilis facere.</p>
    <p>Aperiam, quaerat minus.</p>
    <p>Accusantium, quos voluptate.</p>
    <p>Deserunt, accusantium cum.</p>
    <p>Culpa, assumenda explicabo.</p>
    <p>Dolorem, optio? Esse?</p>
    <p>Doloremque, expedita libero!</p>
    <p>Vero, aperiam? Nulla.</p>
    <p>Ullam, blanditiis ad.</p>
    <p>In, suscipit quos.</p>
    <p>Dicta, voluptates quos?</p>
    <p>Et, nam dolores!</p>
    <p>Magni, dolorem aut.</p>
    <p>Tenetur, reiciendis in?</p>
    <p>Rerum, aliquam totam!</p>
    <p>Nihil, deserunt culpa.</p>
    <p>Maxime, eveniet tempora?</p>
    <p>Unde, corporis harum?</p>
    <p>Corporis, dolore voluptatem.</p>
    <p>Consequuntur, ipsam corporis!</p>
    <p>Aperiam, labore iste!</p>
    <p>Voluptatibus, illum quia.</p>
    <p>Facere, nostrum voluptatem.</p></div>
    <div id="right">#right</div>  
  </div>
  <div id="bot">#bot</div>  
<div></div>