5种 CSS 实现DIV中内容垂直居中的方法

5种 CSS 实现DIV中内容垂直居中的方法

使用 CSS 实现DIV中内容垂直居中并不容易,有些方法在一些浏览器中无效。

今天中国网页设计给大家分享使DIV中对象垂直集中的5种不同方法,以及它们各自的优缺点。

CSS 实现DIV中内容垂直居中方法一

这个方法把一些 div 的显示方式设置为表格,因此我们可以使用表格的 vertical-align property 属性。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<div class="wrapper">
<div class="cell">
<div class="content"> 中国网页设计-最好的网页制作学习网站</div>
</div>
</div>
<div class="wrapper"> <div class="cell"> <div class="content"> 中国网页设计-最好的网页制作学习网站</div> </div> </div>
<div class="wrapper"> 
 <div class="cell">
 <div class="content"> 中国网页设计-最好的网页制作学习网站</div>
 </div>
</div>
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
.wrapper {display:table;}
.cell {
display:table-cell;
vertical-align:middle;
}
.wrapper {display:table;} .cell { display:table-cell; vertical-align:middle; }
.wrapper {display:table;} 
.cell {
    display:table-cell; 
    vertical-align:middle;
}

优点:
content 可以动态改变高度(不需在 CSS 中定义)。当 wrapper 里没有足够空间时, content 不会被截断

CSS 实现DIV中内容垂直居中方法二

使用绝对定位的 div,把它的 top 设置为 50%,top margin 设置为负的 content 高度。这意味着对象必须在 CSS 中指定固定的高度。
因为有固定高度,或许你想给 content 指定 overflow:auto,这样如果 content 太多的话,就会出现滚动条,以免content 溢出。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<div class="content">Content goes here</div>
<div class="content">Content goes here</div>
<div class="content">Content goes here</div>
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
.content {
position:absolute;
top:50%; height:240px;
margin-top:-120px; /* negative half of the height */
}
.content { position:absolute; top:50%; height:240px; margin-top:-120px; /* negative half of the height */ }
.content {
    position:absolute; 
    top:50%; height:240px; 
    margin-top:-120px; /* negative half of the height */ 
}

优点:
适用于所有浏览器、不需要嵌套标签
缺点:
没有足够空间时,content 会消失(类似div 在 body 内,当用户缩小浏览器窗口,滚动条不出现的情况)

CSS 实现DIV中内容垂直居中方法三

这个方法使用了一个 position:absolute,有固定宽度和高度的 div。这个 div 被设置为 top:0; bottom:0;。但是因为它有固定高度,其实并不能和上下都间距为 0,因此 margin:auto; 会使它居中。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<div class="content">Content here</div>
<div class="content">Content here</div>
<div class="content">Content here</div>
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
.content {
position:absolute;
top:0;
bottom:0;
left:0;
right:0;
margin:auto;
height:240px;
width:70%;
}
.content { position:absolute; top:0; bottom:0; left:0; right:0; margin:auto; height:240px; width:70%; }
.content {
    position:absolute; 
    top:0; 
    bottom:0; 
    left:0; 
    right:0; 
    margin:auto; 
    height:240px; 
    width:70%; 
}

优点:简单
缺点:IE(IE8 beta)中无效,无足够空间时,content 被截断,但是不会有滚动条出现。

CSS 实现DIV中内容垂直居中方法四

这个方法只能将单行文本置中。只需要简单地把 line-height 设置为那个对象的 height 值就可以使文本居中了。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<div id="content">Content here</div>
<div id="content">Content here</div>
<div id="content">Content here</div>
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
.content {
height:100px;
line-height:100px;
}
.content { height:100px; line-height:100px; }
.content {
    height:100px; 
    line-height:100px;
}

优点:
适用于所有浏览器、无足够空间时不会被截断
缺点:只对文本有效(块级元素无效)、多行时,断词比较糟糕

这个方法在小元素上非常有用,例如使按钮文本或者单行文本居中。

CSS 实现DIV中内容垂直居中方法五

这种方法,在 content 元素外插入一个 div。设置此 div height:50%; margin-bottom:-contentheight;。content 清除浮动,并显示在中间。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<div class="floater"></div>
<div class="content">Content here</div>
<div class="floater"></div> <div class="content">Content here</div>
<div class="floater"></div>
<div class="content">Content here</div>
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
.floater {
float:left;
height:50%;
margin-bottom:-120px;
}
.content {
clear:both;
height:240px;
position:relative;
}
.floater { float:left; height:50%; margin-bottom:-120px; } .content { clear:both; height:240px; position:relative; }
.floater {
    float:left; 
    height:50%; 
    margin-bottom:-120px;
}
.content {
    clear:both; 
    height:240px; 
    position:relative;
}

优点:适用于所有浏览器
没有足够空间时(例如:窗口缩小) content 不会被截断,滚动条出现

缺点:唯一我能想到的就是需要额外的空元素了

总结:

我最喜欢的是方法五,缺点不多。因为 content 会清除浮动,所以可以在它上面放置别的元素,并且当窗口缩放时,
居中的 content 不会把另外的元素盖住。

CSS 实现DIV中内容垂直居中的例子http://douglasheriot.com/tutorials/css_vertical_centre/demo5.html

125jz网原创文章。发布者:江山如画,转载请注明出处:http://www.125jz.com/2678.html

(0)
江山如画的头像江山如画管理团队
上一篇 2018年9月23日 下午4:29
下一篇 2018年9月27日 上午8:44

99%的人还看了以下文章

  • 美观简洁的bootstrap 表单验证代码

    美观简洁的bootstrap 表单验证代码,如上图所示,点击登录按钮,在输入框下方显示错误提示信息 <!DOCTYPE html> <html> <head> <meta charset=”utf-8″ /> <title>ipput</title> <!– 新 Bootstra…

    2020年2月9日
    4.2K0
  • 跟永哥学HTML5(1):认识html5

    HTML5(简称H5),是超文本标记语言(HTML)的第五次重大修改,是最新一代的超文本标记语言,它不单单是一种标记语言,更为下一代 Web 提供了全新功能 。

    2018年1月21日
    2.2K0
  • js实现两个页面表单传值并接收

    网页制作时,需要两个页面之间传值,通过JS就可轻松实现。 js 实现两个页面表单传值并接收源码 A页面: <input type=”text” id=”txt”> <input type=”button” value=”测试” onclick=”test()”/> <SCRIPT LANGUAGE=”JavaScript”&gt…

    2019年7月10日
    3.3K0
  • 实现网页全屏和退出全屏JS代码,多浏览器兼容

    实现网页全屏和退出全屏JS代码,兼容主流浏览器如:谷歌、火狐、360等。   <!DOCTYPE html> <html> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <t…

    2019年2月15日
    3.3K0
  • 手把手教你做响应式网页,响应式布局实例入门(精)

    刚接触响应式布局的童鞋会感觉响应式布局很高大上,很难,但实际上只用CSS就能实现响应式布局。今天中国网页设计通过一个响应式布局实例,手把手教你如何制作响应式网页。 响应式布局(Responsive Layout) 响应式布局分别为不同的屏幕分辨率定义布局,同时,在每个布局中,应用流式布局的理念,即页面元素宽度随着窗口调整而自动适配。 响应式布局实例 当窗口大…

    2018年7月28日
    4.5K0
  • 第8课:组织元素(span和div)

    pan和div元素用于组织和结构化文档,并经常联合class和id属性一起使用。 在这一课中,我们将进一步探究span和div的用法,因为这两个HTML元素对于CSS是很重要的。 用span组织元素 用div组织元素 用span组织元素 span元素可以说是一种中性元素,因为它不对文档本身添加任何东西。但是与CSS结合使用的话,span可以对文档中的部分文本…

    2020年2月25日
    3.1K0

发表回复

登录后才能评论