- 
							css实现垂直居中的问题普通类
- 
							- 支持
- 批判
- 提问
- 解释
- 补充
- 删除
 
- 
							- 
													css实现垂直居中的问题
 (1)单行内容的居中 
 只考虑单行是最简单的,无论是否给容器固定高度,只要给容器设置 line-height 和 height,并使两值相等,再加上 over-flow: hidden 就可以了.middle-demo-1{ 
 height: 4em;
 line-height: 4em;
 overflow: hidden;
 }优点: 1. 同时支持块级和内联极元素 2. 支持所有浏览器 缺点: 1. 只能显示一行 2. IE中不支持<img>等的居中 要注意的是: 1. 使用相对高度定义你的 height 和 line-height 2. 不想毁了你的布局的话,overflow: hidden 一定要 为什么? 请比较以下两个例子: <p style="background: #900; color: #00f; font: bold 12px/24px Helvertica,Arial,sans-serif; height:24px; width:370px;">Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</p> <br/> <br/> <p style="background: #090; color: #00f; font: bold 12px/2em Helvertica,Arial,sans-serif; height:2em; width:370px; overflow: hidden;">Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</p> 上一个高度是用的绝对单位px,并且没有隐藏溢出,下一个高度用的单位是相对单位em,并且隐藏了溢出。如果你的浏览器支持放大字体,那么尽情地放大字体,看看会出现什么效果。 (2)多行内容居中,且容器高度可变 
 也很简单,给出一致的 padding-bottom 和 padding-top 就行..middle-demo-2{ 
 padding-top: 24px;
 padding-bottom: 24px;
 }优点: 1. 同时支持块级和内联极元素 2. 支持非文本内容 3. 支持所有浏览器 缺点: 容器不能固定高度 (3)把容器当作表格单元 CSS 提供一系列diplay属性值,包括 display: table, display: table-row, display: table-cell 等,能把元素当作表格单元来显示。这是再加上 vertical-align: middle, 就和表格中的 valign="center" 一样了。 .middle-demo-3{ 
 display: table-cell;
 height: 300px;
 vertical-align: middle;
 }可惜IE不支持这些属性,不过在其他浏览器上显示效果非常完美。 要注意的是:和一个合法的<td>元素必须在<table>里一样,display: table-cell 元素必须作为 display: table 的元素的子孙出现。 优点: 不用说了吧,就是表格,效果和表格一模一样 缺点: IE下无效 (4)以毒攻毒!用 IE 的 bug 解决 IE 中的绝对居中 先不得不说一句,IE 真的是个很烂的浏览器,CSS1中的定义都不支持,害得要我们转个大圈子来造居中。不过就像我说的,凡是 table 布局可以实现的,CSS 一定可以实现,即使在 IE 里也不例外。我研究 IE layout 模式多年,还是找出了一个可以在 IE 中绝对居中的方法。这个方法就是基于 IE layout 的 bug,也可以算以毒攻毒。至于原理,不要问我,这是独门秘学,何况三言两语也讲不清楚,只要好用就行 .middle-demo-4{ 
 height: 300px;
 position: relative;
 }
 .middle-demo-4 div{
 position: absolute;
 top: 50%;
 left: 0;
 }
 .middle-demo-4 div div{
 position: relative;
 top: -50%;
 left: 0;
 }(5)整合三和四,写出支持所有浏览器的垂直居中容器! 
 思路是利用 IE 和 非IE 浏览器的 CSS hack, 整合三和四的CSS,写出兼容主流浏览器的垂直居中容器。具体代码就不给出了,大家权当作练习练习。例子可以在下面的附录中找到。
 最终实测支持的浏览器:IE6+, Mozilla 1.7, Netscape Navigator 8, Opera 8.0+, Firefox 1.0+ 和 Safari 1.0+IE5 下需要加上对合适模型的补正。
 推测支持的浏览器:Mozilla 1.5+, Netscape Navigator 7+, Opera 7+
 未测试浏览器:Konqueror1、单行垂直居中 
 文字在层中垂直居中vertical-align 属性是做不到的.我们这里有个比较巧妙的方法就是:设置height的高度与line-height的高度相同!
 <div style="line-height:500px;height:500;"></div>
 2、层水平居中
 设置div的宽度小于父div的宽度,设置 margin:0 auto;,即可让div居中。
 #parentdiv
 {
 width: 500px;
 }
 #childdiv {
 width: 200px;
 margin:0 auto;
 }
 3、层中的文字水平居中
 在childdiv的css加上text-align:center;
 #parentdiv
 {
 width: 500px;
 }
 #childdiv {
 width: 200px;
 margin:0 auto;
 text-align:center;
 }
 4、div层垂直居中
 <div style="width:275px;height:375px;border: solid red;">
 <div style=" background:green;height: 375px; width: 275px; position: relative; display: table-cell; vertical-align: middle;">
 <div style=" background:red;position:static;position:absolute\9; top: 50%;">
 <div style=" background:blue;position: relative; top: -50%;">
 51xuediannao.com </div>
 </div>
 </div>
 </div>
 5、div层垂直水平居中,英文超长换行
 <div style="float:left;width:275px;height:375px;border: solid red;">
 <div style=" height: 375px; width: 275px; position: relative; display: table-cell; vertical-align: middle;">
 <div style="position:static;position:absolute\9; top: 50%;">
 <div style="position: relative; top: -50%; text-align: center;">
 <div style="width: 85px;WORD-WRAP: break-word;TABLE-LAYOUT: fixed;word-break:break-all;margin:0 auto;">
 51xuediannao.com 51xuediannao.com 51xuediannao.com51xuediannao.com 51xuediannao.com51xuediannao.com </div>
 
 </div>
 </div>
 </div>
 </div>
 6、div垂直滚动
 <div style="width: 160px; height: 260px; overflow-y: scroll; border: 1px solid;">http://www.51xuedeiannao.com </div>7、垂直居中和使用text-align水平居中 
 <div style="float:left;width:275px;height:375px;border: solid red;">
 <div style=" height: 375px; width: 275px; position: relative; display: table-cell; vertical-align: middle;">
 <div style="position:static;position:absolute\9;top: 50%;">
 <div style="position: relative; top: -50%; text-align:center;">
 <div style="width: 275px;">
 <div style="width: 160px;WORD-WRAP: break-word;TABLE-LAYOUT: fixed;word-break:break-all;text-align:left;">
 懒人建站懒人建站懒人建站懒人建站懒人建站懒人建站懒人建站懒人建站懒人建站
 </div>
 </div>
 
 </div>
 </div>
 </div>
 </div>
 8、垂直居中和使用margin水平居中
 <div style="float:left;width:275px;height:375px;border: solid red;">
 <div style=" height: 375px; width: 275px; position: relative; display: table-cell; vertical-align: middle;">
 <div style="position:static;position:absolute\9; top: 50%;">
 <div style="position: relative; top: -50%; ">
 
 <div style="margin:0 auto;width: 160px;WORD-WRAP: break-word;TABLE-LAYOUT: fixed;word-break:break-all;">
 
 </div>
 
 
 </div>
 </div>
 </div>
 </div>
- 
													
- 
							- 标签:
- div
- 居中
- 浏览器
- display
- width
- 垂直
- style
- position
- 支持
- height
 
- 
				
				
				
 
			 
		 
	 
								 
								 
						
学习元评论 (0条)
聪明如你,不妨在这 发表你的看法与心得 ~