最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
CSS中div高度内容垂直方向居中方法总结
时间:2012-09-23 编辑:简简单单 来源:一聚教程网
让div中的内容垂直居中,无非有以下几种方法,等我一一列举:
一、行高(line-height)法
如果要垂直居中的只有一行或几个文字,那它的制作最为简单,只要让文字的行高和容器的高度相同即可,比如:
代码如下 | 复制代码 |
p { height:30px; line-height:30px; width:100px; overflow:hidden; } |
这段代码可以达到让文字在段落中垂直居中的效果。
二、内边距(padding)法
另一种方法和行高法很相似,它同样适合一行或几行文字垂直居中,原理就是利用padding将内容垂直居中,比如:
代码如下 | 复制代码 |
p { padding:30px; } |
这段代码的效果和line-height法差不多。
三、模拟表格法
模拟表格法其实就是用CSS中对元素的声明让块元素像表格一样显示,用到的CSS属性有display、vertical-align等。 先看下面的Html代码:
代码如下 | 复制代码 |
居中显示 |
参照以上Html代码,让最外面名为box的Div呈表格样式显示,然后再让box中名为content的Div呈单元格显示,并利用vertical-align:middle让其垂直居中,这样就模拟出来和表格一样的显示方式,CSS代码如下:
代码如下 | 复制代码 |
#wrap { height:400px; display:table; } #content { vertical-align:middle; display:table-cell; border:1px solid #FF0099; background:#000; width:400px; } |
但这种方法有一个弊端,由于IE浏览器对高度理解会产生错误,所以这种方法仅对Firefox有效,对IE无效,既然这样,我们就需要找出对IE的修正方法,于是有了另外一种方法。
四、定位法
顾名思义,定位法是利用CSS定位属性position对元素进行定位的方法,也属于模拟方法,不过它对IE的支持还是不错的。 它的Html代码为:
代码如下 | 复制代码 |
垂直居中 |
这段代码比上一种方法中多出了一个名为sub的Div,它的作用是用来定位,原理就是:首先让box出于相对定位,sub相对box出于相对定位,位于box垂直方向的50%,再让content中的真正内容出于sub垂直方向的-50%,从而制作出content在box中垂直居中的效果,它们的CSS代码如下:
代码如下 | 复制代码 |
#wrap { border:1px solid #000; background:#F00; width:400px; height:400px; position:relative; } #subwrap { position:absolute; top:50%; } #content { border:1px solid #000; position:relative; top:-50%; color:#FFF; } |
这段代码无论是在IE中还是Firefox中,都能正常居中了
实例
方案一:
主要原理是标准浏览器下用table和table-cell布局,然后用vertical-align:middle居中元素,但IE67不支持table布局,所以用了用了css hake,就是带#开头的属性
代码如下 | 复制代码 |
方案二:
用负margin实现,但缺点是要知道居中内容的宽度和高度。
代码如下 | 复制代码 |
|
最后附上自己写的,所有居中布局的范例网页,大家不明白可以参考
代码如下 | 复制代码 |
Demo of middled vertical alignAuthor: Spenser Lee, Liberty Studio Originally posted on BlueIdea Forum Table of centents:
Case One: Single line countainer with/without a fixed heightIf you only want to display a container which only holds a single line of text, you can set line-height property to height property, then set overflow to hidden. Sample: Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Core code:
Case Two: Align multi-line container which does not have a fixed heightIn this case, we should simply set a pair of fixed equivalences to padding-top and padding-bottom attribute. It works on both IE and non-IE browsers. Sample: Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas dignissim diam eu sem. Proin nunc ante, accumsan sollicitudin, sodales at, semper sed, ipsum. Etiam orci. Vestibulum magna lectus, venenatis nec, tempus ac, dictum vel, lorem. Core code:
Case Three: Simulating table layout in container with a fixed heightCSS offers a set of very convenient display atrribute values called display: table, display: table-row, display: table-cell and other display values begin with table-. It offers a way to simulate table layout in all elements. As a result, any element with display: table-cell, vertical-align: middle and a fixed height will display exactly like a table cell. Sample: (You may not be able to view the effect on IE) Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas dignissim diam eu sem. Proin nunc ante, accumsan sollicitudin, sodales at, semper sed, ipsum. Etiam orci. Vestibulum magna lectus, venenatis nec, tempus ac, dictum vel, lorem. Core code:
Case Four: IE's solutionSince IE is a lame browser when it comes to ANYTHING, so you can't use the solution above simply because IE does not recognize it at all. However, there has been nothing yet you can not code with CSS if you have already coded it with table. It even offers you possibility to layout your page that tables can not do! So what's the solution for IE? Like what we always do: Hit IE's back with the BUGS it offers! Sample: (You are able to view the correct result only on IE) Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas dignissim diam eu sem. Proin nunc ante, accumsan sollicitudin, sodales at, semper sed, ipsum. Etiam orci. Vestibulum magna lectus, venenatis nec, tempus ac, dictum vel, lorem.
See? Don't ask me why it worked. This hack is based on years of study in IE's own stupid layout model and it works very well, even in IE5 given the box width hack. I won't tell you the theory behind all these. It's my top secret, with which I solved a lot of other cross browser layout problems, and I'm not going to share it with you. But you are free to use this IE hack It's kinda handful in most occassions.
Case Five: A perfect compounded sampleHere's the perfect compounded solution on vertical centering that works on almost all latest browsers. Sample: (This works on almost all browsers) Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas dignissim diam eu sem. Proin nunc ante, accumsan sollicitudin, sodales at, semper sed, ipsum. Etiam orci. Vestibulum magna lectus, venenatis nec, tempus ac, dictum vel, lorem. I'm not going to give you the full code of this one. But it's not difficult to write it yourself, with the solution of case 3 and 4, and a little knowledge in IE/non-IE CSS hackers. And actually the hacking style is not limited, so I don't want to force you to use my hacking style neither. Stop being lazy, and write the code yourself!
Copyright © Spenser Lee, 2006 Liberty Studio |
-
上一个: css实现各种导航菜单实现代码
相关文章
- css中不确定高度垂直居中2种方法 10-18
- CSS解决未知高度的DIV垂直居中 01-26
- CSS 鼠标点击拖拽效果代码展示 10-31
- CSS使用SVG实现动态分布的圆环发散路径动画教程 10-31
- css3中仿放大镜效果的几种方式原理介绍 10-26
- CSS3切割轮播图的代码展示 10-26