div中如何设置文字居中

再回首 1个月前 已收到1个回答 举报

香太郎 3星

共回答了365个问题采纳率:90.6% 评论

让div中的文字水平居中很容易,只需要将属性text-align设置为center或将margin设置为0 aotuo就可以了。而让div中的文字垂直居中还是要费一番周折的。

1.首先如果div中只有一行文字,那么可以通过将lineheight属性和height属性的值设为一样来实现。

2.如果div中有多行文字时,则需要通过position属性来实现垂直居中的效果了,代码如下:html:<div id="box"><div id="sub"><div id="content">垂直居中</div></div></div>css: #box{border:1px solid #000;background:#f00;position:relative;height:400px;width:400px} #sub{position:absolute;top:50%} #content{border:1px solid #fff;position:relative;top:-50%;color:#000}

1小时前

23
可能相似的问题

猜你喜欢的问题

热门问题推荐

Copyright © 2024 微短问答 All rights reserved. 粤ICP备2021119249号 站务邮箱 service@wdace.com