1 Star 0 Fork 0

rexkny/web_tests

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
单行文字垂直居中.html 2.15 KB
一键复制 编辑 原始数据 按行查看 历史
rexkny 提交于 2022-04-17 18:05 . hine-height
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
padding: 0;
margin: 0;
}
#font-center{
height: 100px;
background-color: aqua;
border: 1px dashed #007acc;
text-align: center;
}
h1{
text-align: center;
height: 100px;
line-height: 100px;
}
#test{
line-height: 100px;
border: #007acc 1px solid;
}
#yuanLi{
height: 200px;
background-color: aqua;
border: 1px dashed #007acc;
text-align: center;
}
#shang{
background-color: bisque;
height: 90px;
width: 50%;
position: relative;
left:25%;
text-align: center;
color: #007acc;
font-family:'Gill Sans';
padding:0 20px;
line-height:45px;
}
#xia{
background-color: bisque;
height: 90px;
width: 50%;
position: relative;
left:25%;
color: #007acc;
font-family:'Gill Sans';
padding:0 20px;
line-height:90px;
text-align: center;
}
</style>
</head>
<body>
<h1>显示样式</h1>
<div id="font-center">
<div id="inner">
<span id="test">垂直居中</span>
</div>
</div>
<h1>单行文字垂直居中原理,忽略边框宽度</h1>
<div id="yuanLi">
<div id="shang">假定文字本身高20px,设置line-height为父容器的高度(这里为200px)后,浏览器会在文字的上方和下方添加高度为[(200-20)/2]像素的空白,从而使文字在视觉效果上垂直于父容器居中</div>
<span>垂直居中</span>
<div id="xia">本部分蓝色div中的上下两处肉色div模拟的就是line-height中用于填充的空白</div>
</div>
</body>
</html>
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
HTML
1
https://gitee.com/rexkny/web_tests.git
git@gitee.com:rexkny/web_tests.git
rexkny
web_tests
web_tests
master

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385