如何在此布局中使内容 100% 高度和等高列? [英] How to Make the Content 100% height and equal height columns in this layout?

查看:19
本文介绍了如何在此布局中使内容 100% 高度和等高列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的布局,其中 my_menu 固定在顶部,然后是标题,然后是 960 像素的内容并居中,其中 3 列向左浮动,最后是粘性页脚.

  1. 问题是如何让内容拉伸到100%的高度即使任何一个子列中的内容不足?

  2. 无论任何列的高度如何,如何使 3 列的高度相等内容?

  3. 维护粘性页脚!纯 CSS,无 JavaScript,无jQuery!

HTML 标记

<html lang="zh-cn"><头><meta charset="utf-8"/><title>文档标题</title><link rel="stylesheet" href="main.css" type="text/css"/><body id="索引"><div id="包装器"><div id="my_menu">固定菜单宽度 100%

<div id="my_header">标题宽度 100%

<div id="内容"><p>内容 960px</p><div id="col1" class="content_columns">第 1 列宽度 320px 高度 100%第 1 列宽度 320px 高度 100%第 1 列宽度 320px 高度 100%第 1 列宽度 320px 高度 100%第 1 列宽度 320px 高度 100%第 1 列宽度 320px 高度 100%第 1 列宽度 320px 高度 100%第 1 列宽度 320px 高度 100%第 1 列宽度 320px 高度 100%第 1 列宽度 320px 高度 100%第 1 列宽度 320px 高度 100%第 1 列宽度 320px 高度 100%第 1 列宽度 320px 高度 100%第 1 列宽度 320px 高度 100%第 1 列宽度 320px 高度 100%第 1 列宽度 320px 高度 100%第 1 列宽度 320px 高度 100%第 1 列宽度 320px 高度 100%第 1 列宽度 320px 高度 100%第 1 列宽度 320px 高度 100%第 1 列宽度 320px 高度 100%第 1 列宽度 320px 高度 100%第 1 列宽度 320px 高度 100%第 1 列宽度 320px 高度 100%第 1 列宽度 320px 高度 100%第 1 列宽度 320px 高度 100%第 1 列宽度 320px 高度 100%

<div id="col2" class="content_columns">列 2 宽度 320px 高度 100%列 2 宽度 320px 高度 100%列 2 宽度 320px 高度 100%列 2 宽度 320px 高度 100%列 2 宽度 320px 高度 100%列 2 宽度 320px 高度 100%列 2 宽度 320px 高度 100%列 2 宽度 320px 高度 100%列 2 宽度 320px 高度 100%

<div id="col3" class="content_columns">第 3 列宽度 320px 高度 100%第 3 列宽度 320px 高度 100%第 3 列宽度 320px 高度 100%第 3 列宽度 320px 高度 100%第 3 列宽度 320px 高度 100%

<div class="clear_floats"></div><!-- 用于清除浮动 -->

<div class="push"></div><!-- 对于粘性页脚-->

<div id="my_footer">粘性页脚宽度 100%

CSS 样式

*/* 用于 CSS 重置 */{填充:0;边距:0;}html, 正文{宽度:100%;高度:100%;}div#包装器{宽度:100%;高度:100%;最小高度:100%;/* 对于粘性页脚 */高度:自动!重要;/* 对于粘性页脚 */边距:0 自动 -70px;/* 对于粘性页脚 */}div#my_menu{宽度:100%;高度:50px;轮廓:1px纯黑色;背景颜色:灰色;文本对齐:居中;位置:固定;}div#my_header{宽度:100%;高度:100px;轮廓:1px纯黑色;背景颜色:黄色;文本对齐:居中;填充顶部:50px;}div#内容{宽度:960px;边距:0 自动;轮廓:1px纯黑色;背景颜色:棕色;文本对齐:居中;}div.content_columns{宽度:320px;轮廓:1px纯黑色;背景颜色:金色;文本对齐:居中;向左飘浮;}div.clear_floats/* 用于清除浮动 */{清楚:两者;}div#my_footer{宽度:100%;高度:70px;轮廓:1px纯黑色;背景颜色:粉红色;文本对齐:居中;}div.push/* 对于粘性页脚 */{高度:70px;}

解决方案

完全支持 IE6+ 的解决方案.我会抽出并解释相关的代码部分.

现代支持

div.content_columns {宽度:320px;轮廓:1px纯黑色;背景颜色:金色;文本对齐:居中;显示:表格单元格;/* 没有浮点数,而是这个 */}

所以,由于现代浏览器使这项任务变得简单,我们需要做的就是使用 display: table-cell 来让它工作.它使列等高,并充当表格单元格.简单的柠檬汁.

IE 6 &7 支持

<!--[if lte IE 7]><风格>div#内容{溢出:隐藏;}div.content_columns {垂直对齐:顶部;显示:内联;缩放:1;填充底部:9999px;底边距:-9999px;}</风格><![endif]-->

现在,对于 IE 6 和 7 的支持,我们将使用一些技巧.首先,应该注意的是,这不必在有条件的评论中完成,但我更喜欢那样做.对我来说更干净.但是您可以使用 CSS hacks 使这些值仅在 IE 中有效.

我们正在做的是获得 IE 6 &7 将每一列视为 inline-block 元素,但由于 他们不'不支持(至少对于块级元素)我们使用 inline 显示和 zoom: 1; 修复来触发 IE 的 hasLayout 属性.这会将其视为 inline-block 元素.然后,我们将它们全部设置为在顶部彼此对齐,并使用另一个小技巧.我们使用 padding: 9999px 将元素底部扩展到其他元素不可能比它更长的位置,我们使用 margin: -9999px;当我们这样做时不改变页面的呈现.我们不想扩展页面,只想扩展背景.9999px 是任意值,可以是任何值,只要它足够高以大于最长列和最短列之间的差值即可.最后,我们在容器元素上设置了 overflow: hidden,这样背景就不会通过渗出底部来扩展页面.

有了它,IE 6+ 完全支持使用纯 CSS 的多列固定宽度布局 2(加上 MS zoom).

I have this layout where the my_menu is fixed at the top, followed by header, followed by content with 960px and centered, in which 3 columns have been floated left and finally followed by a sticky footer.

  1. The problem is How to make the content stretch to the 100% height even if there is not enough content in any one of the child columns?

  2. How make the 3 columns equal height regardless of any column's content?

  3. Maintain the sticky footer! Pure CSS, No JavaScript, No JQuery!

The HTML Markup

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="utf-8" /> 
<title>Document Title</title> 
<link rel="stylesheet" href="main.css" type="text/css" /> 
</head> 
<body id="index"> 
<div id="wrapper"> 
    <div id="my_menu"> 
    FIXED MENU WIDTH 100% 
    </div> 
    <div id="my_header"> 
    HEADER WIDTH 100% 
    </div> 
    <div id="content"> 
        <p>CONTENT 960px</p> 
        <div id="col1" class="content_columns"> 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        COLUMN 1 WIDTH 320px HEIGHT 100% 
        </div> 
        <div id="col2" class="content_columns"> 
        COLUMN 2 WIDTH 320px HEIGHT 100% 
        COLUMN 2 WIDTH 320px HEIGHT 100% 
        COLUMN 2 WIDTH 320px HEIGHT 100% 
        COLUMN 2 WIDTH 320px HEIGHT 100% 
        COLUMN 2 WIDTH 320px HEIGHT 100% 
        COLUMN 2 WIDTH 320px HEIGHT 100% 
        COLUMN 2 WIDTH 320px HEIGHT 100% 
        COLUMN 2 WIDTH 320px HEIGHT 100% 
        COLUMN 2 WIDTH 320px HEIGHT 100% 
        </div> 
        <div id="col3" class="content_columns"> 
        COLUMN 3 WIDTH 320px HEIGHT 100% 
        COLUMN 3 WIDTH 320px HEIGHT 100% 
        COLUMN 3 WIDTH 320px HEIGHT 100% 
        COLUMN 3 WIDTH 320px HEIGHT 100% 
        COLUMN 3 WIDTH 320px HEIGHT 100% 
        </div> 
        <div class="clear_floats"></div> <!-- For Clearing Floats --> 
    </div> 
    <div class="push"></div> <!-- For Sticky Footer --> 
</div> 
    <div id="my_footer"> 
    STICKY FOOTER WIDTH 100% 
    </div> 
</body> 
</html> 

The CSS Styles

* /* For CSS Reset */ 
{ 
padding: 0; 
margin: 0; 
} 

html, body 
{ 
width: 100%; 
height: 100%; 
} 

div#wrapper 
{ 
width: 100%; 
height: 100%; 
min-height: 100%; /* For Sticky Footer */ 
height: auto !important; /* For Sticky Footer */ 
margin: 0 auto -70px; /* For Sticky Footer */ 
} 

div#my_menu 
{ 
width: 100%; 
height: 50px; 
outline: 1px solid black; 
background-color: grey; 
text-align: center; 
position: fixed; 
} 

div#my_header 
{ 
width: 100%; 
height: 100px; 
outline: 1px solid black; 
background-color: yellow; 
text-align: center; 
padding-top: 50px; 
} 

div#content 
{ 
width: 960px; 
margin: 0 auto; 
outline: 1px solid black; 
background-color: brown; 
text-align: center; 
} 

div.content_columns 
{ 
width: 320px; 
outline: 1px solid black; 
background-color: gold; 
text-align: center; 
float: left; 
} 

div.clear_floats /* For Clearing Floats */ 
{ 
clear: both; 
} 

div#my_footer 
{ 
width: 100%; 
height: 70px; 
outline: 1px solid black; 
background-color: pink; 
text-align: center; 
} 

div.push /* For Sticky Footer */ 
{ 
height: 70px; 
} 

解决方案

A solution with full IE6+ support. I'll pull out and explain the relevant bits of code.

Modern support

div.content_columns {
    width: 320px;
    outline: 1px solid black;
    background-color: gold;
    text-align: center;
    display: table-cell; /* No floats, this instead */
}

So, since modern browsers make this task easy, all we need to do is use display: table-cell to get this to work. It makes the columns equal height, and acts as a table-cell. Easy peasy lemon squeezy.

IE 6 & 7 support

<!--[if lte IE 7]>
<style>
    div#content {
        overflow: hidden;
    }
    div.content_columns {
        vertical-align: top;
        display: inline;
        zoom:1;
        padding-bottom: 9999px;
        margin-bottom: -9999px;
    }
</style>
<![endif]-->

Now, for IE 6 and 7 support we're going to use a few tricks. First off, it should be noted that this doesn't have to be done in a conditional comment, but I like it better that way. Cleaner to me. But you can use CSS hacks to get these values to only work in IE.

What we're doing is getting IE 6 & 7 to treat each column as an inline-block element, but since they don't support that (for block level elements at least) we use inline display, and a zoom: 1; fix to trigger IE's hasLayout property. This will treat it like it is an inline-block elmenent. Then, we set them all to be aligned at the top with each other, and use another little trick. We use the padding: 9999px to extend the bottom of the element so far that the other elements are not likely to be longer than it, and we use margin: -9999px; to not change the rendering of the page when we do so. We don't want to extend the page, just the background. 9999px is arbitrary and can be any value as long as it is high enough to be greater than the difference between the longest and shortest columns. And for the final touches, we set overflow: hidden on the container element so that the backgrounds don't extend the page by bleeding out the bottom.

And there you have it, full IE 6+ support for multicolumn fixed width layout using pure CSS 2 (plus MS zoom).

这篇关于如何在此布局中使内容 100% 高度和等高列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆