html css layout with header two columns and footer

2019-04-25 03:49发布

I am trying to create a head area that is 100px in height and spans 100% in width. I need 2 columns with the left one being 250px wide and 100% in height down to the footer. The right column should be 100% of the remaining page width and 100% in height to the footer. The footer should be at the bottom of the page and 100px in height and 100% in width. Even if there is no content in the 2 columns, I need them to stretch down to the footer and have the footer visible without scrolling down to it.

Here is what I have so far.

<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;
}

Here is another example with a table

<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

标签: html css layout
9条回答
来,给爷笑一个
2楼-- · 2019-04-25 04:26

Try This

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

Working Fiddle

查看更多
对你真心纯属浪费
3楼-- · 2019-04-25 04:28

Here is a simple CSS-grid solution (I used 50px instead of 100px so we can see it in the reduced snippet)

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>

And here is with 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>

查看更多
乱世女痞
4楼-- · 2019-04-25 04:31

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

Bonus: stretched left column background of 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;
}
查看更多
Juvenile、少年°
5楼-- · 2019-04-25 04:33

You could use the 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 */
}

The Result would look like this: Working Fiddle

But stretching the columns without content to the bottom is not possible without javascript. Why would you want to do that anyways?

查看更多
对你真心纯属浪费
6楼-- · 2019-04-25 04:33

You could use flex

Here's a working example:

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

Flex is a newer CSS3 workaround to creating stretched and responsive-like layouts for content. It can get a bit complicated, so I'll just link to the source and some syntax 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;
}
查看更多
叼着烟拽天下
7楼-- · 2019-04-25 04:35

This is my solution using grid. You need to have two nested grids to always show bottom grid item, even when content overflows.

This uses backward compatible -ms-code

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>

查看更多
登录 后发表回答