`

[转]css height:100%

    博客分类:
  • css
阅读更多

CSS布局不一定都非常的复杂,但是有些时候看上去简单但是实现起来还不是那么轻松。

在这篇文章中,我们通过CSS来实现跨浏览器支持的100%高度的布局。

这段代码用来创建一个头部和段落,非常的简单了。
接下来,我们需要给页面设置一些通用的样式:

* {padding: 0;margin: 0;} body {font-family: "lucida sans", verdana, arial, helvetica, sans-serif;font-size: 75%;} h1 {font-size: 1.4em;padding: 10px 10px 0;} p {padding: 0 10px 1em;} 
 



这些代码都是比较简单的,而且在大多数浏览器中都是没有问题的。
现在,我们想象需要主内容区域填充页面的700px,包括2px的边框,然后给页面填充背景。
给容器添加样式很简单,但是100%的高度经常会给开发者带来困惑。我们看看怎么解决呢。
首先我们需要给html和body都设置100%的高度,这个经常被忽略,但是非常的重要,如果父元素没有指定高度,那么子元素的百分比的高度设置了也没用。所以我们需要先设置父元素的高度:

html, body {height: 100%;} 
 



在那些需要设置高度或者需要给容器设置样式的地方,设置100%高度是可以的,但是当内容超过可视区域出现滚动条的时候,容器高度就消失了。所以这里就需要设置mini-height,这样当区域小的时候可以正确显示,当区域大了以后也不会有问题。

#container {min-height: 100%;background-color: #DDD;border-left: 2px solid #666;border-right: 2px solid #666;width: 676px;margin: 0 auto;} 
 



但是这样写IE6不支持,因为IE6不认识mini-height,我们继续看怎么办?
 
IE6怎么办

解决这个问题还是比较简单的,在IE6下面,浏览器会把你设置的高度认为就是mini-height,所以当容器增大的时候,也能跟其他浏览器一样适应。我们可以使用一些hack来做到:

* html #container {height: 100%;} 
 

 

分享到:
评论

相关推荐

    深入理解CSS的height:100%和height:inherit之间的使用区别

    inherit确实是个好东西,不仅节约代码,尤其与...1. 兼容性差异height:100% IE6+ √height:inherit IE8+ √ 2. 大多数情况作用是一样的除去兼容性,大多数情况下,两者作用是一样的,甚至都很难想出不一样的理由

    IE6 CSS高度height:100% 计算失效/无效

    好久没有写博文了,正好今天一个任务需求的修改,遇到了一个小小的问题(或者你已经遇到了),在这里分享...height:100%;background-color:#f00;} 结构 复制代码代码如下:”demo”> <div>position:absolute</div> </div>

    font-size:100%的目的和作用是什么

    看到一些css重设,请问设置font-size:100%的目的和作用是什么? ————————————————————————————— 假如你设置body{font-size:12px;} 但h1是不会继承这个12px,它会按照一定百分比增加...

    div背景半透明,覆盖整个可视区域的遮罩层效果

    1 、半透明效果可以使用 css3 中的 opacity 属性,在低版本的IE浏览器...html,body{ height:100%} .mask{height:100%;width:100%;} 但是这样做当内容超过一屏时只有IE6下显示的效果是我们所期望的,在其他浏览器中首屏

    media-queries

    /* CSS Document */ @media screen and (max-width: 1000px) { .header-inner,.banner-inner,.inner-box,.main-inner,.footer-inner,.banner-1-inner{ width:100%;} .header{ padding-bottom:12px;} .header-...

    【JavaScript源代码】JavaScript实现通讯录功能.docx

    JavaScript实现通讯录... height: 100%; margin: 0px; font-family: "PingFang SC", "微软雅黑", sans-serif; font-weight: 300; color: #333; } .header { width: 100%; padding: 32px; } h1 { margin:

    CSS完美解决前端图片变形问题的方法

    在头条IT学堂看到CSS完美解决前端图片变形... height: 200px; overflow: hidden; border: 2px solid red; position: relative; } img{ width: 100%; position: absolute; top: 50%; left: 50%; transform: tra

    右下角广告弹窗1.html

    style type="text/css"> * {margin:0px;padding:0px;} html,body {height:100%;} body {font-size:14px; line-height:24px;} #tip {position: absolute;right: 0px;bottom: 0px;height: 0px;width: 180px;border: 1...

    JQuery和html+css实现带小圆点和左右按钮的轮播图实例

    CSS代码: /*轮播图 左右按钮 小白点*/ #second_div{ margin-top: 160px; } .img_box{ overflow: hidden; width:100%; height:420px; border:1px solid; position:relative; } .img_box img{ width:100...

    scrollbar_js实现竖向滚动条

    .box-163css{position:relative;height:330px;border:solid 1px #ddd;width:600px;margin:20px auto;} .picul{ width:100%; float:left;} .picul li{ width:115px; height:115px; float:left; margin:0 7px 25px 7px...

    快速解决css使用@keyframes加载图片首次循环时出现白色间隙问题(闪屏)

    在使用css 的animations 属性时,首次加载循环会出现白色的间隙,看着有点像页面有刷新的感觉,后面每次循环就不会再有这个问题 问题演示   //html <div class=first> //less .container { width: 100vw; ...

    cssplot:纯 CSS 图表

    或者cssplot.base.css可以单独使用,并进行以下修改: 条形图:使用class="chart-column" style="height: 99.0%"而不是data-cp-size="99" 垂直条形图:使用class="chart-row" style="width: 99.0%"而不是data-cp-...

    js提示框替代系统alert,自动关闭alert对话框的实现方法

    自己写了个alert提示框。因为系统alert在苹果手机微信...height:100%; position:fixed; top:0px; left:0px; background-color:rgba(0,0,0,0.6); display:none; z-index:200; } .show_alert_div{ width:70%; position:ab

    component-css:将 @component 选择器引入 CSS

    组件 CSS (CCSS) 将 @component ... height: 100%; .content { width: 100%; }}你可以生成这个 CSS .app-component.app-example { width: 100%; height: 100%;}.app-component.app-example .app-example_content { widt

    【JavaScript源代码】Vue Element Sortablejs实现表格列的拖拽案例详解.docx

     .w-table{ height: 100%; width: 100%; float: left; } /* 拖动过程中,鼠标显示样式 */ .w-table_moving .el-table th .thead-cell{ cursor: move !important; } .w-table_moving .el-table__fixed{ ...

    【JavaScript源代码】Vue使用Echarts实现立体柱状图.docx

    Vue使用Echarts实现立体柱状图  本文实例为大家分享了Vue使用Echarts实现立体柱状图的具体代码,供大家参考,具体内容如下 预览: 代码: 页面部分: ... height: 100%; /*填满父级容器*/ } JS部分: import e

    div-css-漂亮的导航条

    <style type="text/css"> body { margin:0; padding:0; font: bold 11px/1.5em Verdana; } h2 { font: bold 14px Verdana, Arial, Helvetica, sans-serif; color: #000; margin: 0px; padding: 0px 0px 0px...

    动态改变左侧Div宽度

    body{ height:100%; overflow:hidden; margin: 0px; font-size:12px; background-color: #E1EEFD; } .wrapper{ width: 100%; height:100%;} .left { width:300px; height:100%; position:absolute; left:0; top:140...

Global site tag (gtag.js) - Google Analytics