css设置div内文字垂直,DIV+CSS如何让文字垂直居中?

满意答案

00e27ab806e4881f8254fe7ae8741834.png

dinahcat

2016.05.08

00e27ab806e4881f8254fe7ae8741834.png

采纳率:48%    等级:5

已帮助:205人

div+css实现文字垂直居中的五种方法:

1、把文字放到table中,用vertical-align property 属性来实现居中。

Content goes here

2、使用绝对定位的 div,把它的 top 设置为 50%,top margin 设置为负的 content 高度。这意味着对象必须在 CSS 中指定固定的高度。

#content {

position: absolute;

top: 50%;

height: 240px;

margin-top: -120px; /* negative half of the height */

}

Content goes here

3、在 content 元素外插入一个 div。设置此 div height:50%; margin-bottom:-contentheight;。

content 清除浮动,并显示在中间。

Content here

#floater {

float: left;

height: 50%;

margin-bottom: -120px;

}

#content {

clear: both;

height: 240px;

position: relative;

}

4、使用 position:absolute,有固定宽度和高度的 div。这个 div被设置为 top:0; bottom:0;。但是因为它有固定高度,其实并不能和上下都间距为 0,因此 margin:auto; 会使它居中。使用 margin:auto;使块级元素垂直居中即可

Content here

#content {

position: absolute;

top: 0;

bottom: 0;

left: 0;

right: 0;

margin: auto;

height: 240px;

width: 70%;

}

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

Content here

#content {

height: 100px;

line-height: 100px;

}

00分享举报

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值