div class="absolute-center">/div>/div>.parent { position: relative;}.absolute-center { position: absolute; margin: auto; top: 0; right: 0; bottom: 0; left: 0" /> div class="absolute-center">/div>/div>.parent { position: relative;}.absolute-center { position: absolute; margin: auto; top: 0; right: 0; bottom: 0; left: 0">
小樱知识 > 生活常识html字体垂直居中怎么设置(字体垂直居中设置教程)

html字体垂直居中怎么设置(字体垂直居中设置教程)

提问时间:2022-03-16 14:16:00来源:小樱知识网


1. 元素高度声明的情况下在父容器中居中:绝对居中法

<div class="parent"> <div class="absolute-center"></div></div>.parent { position: relative;}.absolute-center { position: absolute; margin: auto; top: 0; right: 0; bottom: 0; left: 0; height: 70%; width: 70%;}优点:1.跨浏览器,包括 IE8-102.无需其他冗余标记,CSS 代码量少3.完美支持图片居中4.宽度高度可变,可用百分比缺点:1.必须声明高度

2. 负外边距:当元素宽度高度为固定值时。设置 margin-top/margin-left 为宽度高度一 半的相反数,top:50%;left:50%

<div class="parent"> <div class="negative-margin-center"></div></div>.parent { position: relative;}.negative-margin-center { position: absolute; left: 50%; top: 50%; margin-left: -150px; margin-top: -150px; height: 300px; width: 300px;}优点:良好的跨浏览器特性,兼容 IE6-7代码量少缺点:不能自适应,不支持百分比尺寸和 min-/max-属性设置内容可能溢出容器边距大小域与 padding,box-sizing 有关

3. CSS3 Transform 居中:

<div class="parent"> <div class="transform-center"></div></div>.parent { position: relative;}.transform-center { position: absolute; left: 50%; top: 50%; margin: auto; width: 50%; -webkit-transform: translate(-50%, -50%); -moz-transform: translate(-50%, -50%); transform: translate(-50%, -50%);}优点:内容高度可变代码量少缺点:IE8 不支持属性需要浏览器厂商前缀可能干扰其他 transform 效果

4. table-cell 居中:

<div class="center-container is-table"> <div class="table-cell"> <div class="center-block"></div> </div></div>.center-container.is-table { display: table;}.is-table .table-cell { display: table-cell; vertical-align: middle;}.is-table .center-block { width: 50%; margin: 0 auto;}优点:高度可变内容溢出会将父元素撑开跨浏览器兼容性好缺点:需要额外 html 标记

以上内容就是为大家推荐的html字体垂直居中怎么设置(字体垂直居中设置教程)最佳回答,如果还想搜索其他问题,请收藏本网站或点击搜索更多问题

内容来源于网络仅供参考
二维码

扫一扫关注我们

版权声明:所有来源标注为小樱知识网www.xiaoyin02.com的内容版权均为本站所有,若您需要引用、转载,只需要注明来源及原文链接即可。

本文标题:html字体垂直居中怎么设置(字体垂直居中设置教程)

本文地址:https://www.xiaoyin02.com/shcs/121065.html

相关文章