text-align: center;
(无宽时使用)啦,当然你也可以像我下面一样,通过padding、margin(块级元素的居中)来使内容相对父元素居中,不过text-align: center;
更省事。
这里给设定最后盒子的span.font-test元素的宽是200px,高是100px;
通过上图可知,文字内容水平已经居中了,但是垂直上还没居中。
按道理,元素上的高=100-40×2=20px;是对的,那究竟漏了哪里呢?
👉👉👉字体除了大小,还存在着行高(默认的行高比字体size小)!!!!
🔹关于行高line-height知识的传送门: 🔹
①深入理解css之line-height;
②CSS line-height概念与举例;
③css行高(line-height)及文本垂直居中原理;
④为什么设置line-height 与height相等就可以垂直居中呢?
配合padding,设置为行高等于字体大小,即 line-hight=font-size=20px;或 line-hight=100%;单行文字就可以水平和垂直居中了。
代码部分:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>盒子模型——字体</title>
<style type="text/css">
body,p{padding: 0px;margin: 0px;
}
.box{background-color: #FFB6C1;width: 800px;height: 300px;
}/*200×100*/
.font-test{display: block;background-color: #ADD8E6;width: 80px;height: 20px;font-size: 20px;/*单行内容时,注意设置行高等于字体大小*/line-height: 100%; /*或者 line-height: 20px;*//* padding-right=padding-left=[200-(20×4)]/2=60px *//* padding-top=padding-bottom=(100-20)/2=40px */padding:40px 60px;
}
</style>
</head>
<body><div class="box"><span class="font-test">字体内容</span></div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>盒子模型——单行字垂直居中</title>
<style type="text/css">
body,p{padding: 0px;margin: 0px;
}
.box{background-color: #FFB6C1;width: 800px;height: 300px;
}/*200p×100*/
.font-test{display: block;background-color: #ADD8E6;width: 200px;height: 100px; /*<<<<<=====*//*此处行高等于该子元素的高*/line-height: 100px;}
</style>
</head>
<body><div class="box"><span class="font-test">字体内容</span></div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>盒子模型——单行字垂直居中</title>
<style type="text/css">
body,p{padding: 0px;margin: 0px;
}
.box{background-color: #FFB6C1;width: 800px;height: 300px; /*<<<<<=====*/
}/*200p×100*/
.font-test{display: block;background-color: #ADD8E6;width: 200px;height: 100px;/*此处行高等于父元素的高*/line-height: 300px;}
</style>
</head>
<body><div class="box"><span class="font-test">字体内容</span></div>
</body>
</html>
display: table-cell;
变成表格单元格属性,且子元素中加入vertical-align:middle;
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>盒子模型——单行字垂直居中</title>
<style type="text/css">
body,p{padding: 0px;margin: 0px;
}
.box{background-color: #FFB6C1;width: 800px;height: 300px;display: table-cell;
}/*200p×100*/
.font-test{display: block;background-color: #ADD8E6;width: 200px;height: 100px;display: table-cell;vertical-align:middle;
}
</style>
</head>
<body><div class="box"><span class="font-test">字体内容</span></div>
</body>
</html>
display: table;
变为块级表格元素,而子元素设置display: table-cell;
为表格单元格属性,且加入vertical-align:middle;
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>盒子模型——单行字垂直居中</title>
<style type="text/css">
body,p{padding: 0px;margin: 0px;
}
.box{background-color: #FFB6C1;width: 800px;height: 300px;display: table;
}/*200p×100*/
.font-test{display: block;background-color: #ADD8E6;width: 200px;height: 100px;display: table-cell;vertical-align:middle;
}
</style>
</head>
<body><div class="box"><span class="font-test">字体内容</span></div>
</body>
</html>
display: table
和display: table-cell
的区别和作用的对比(用哪种看你想要什么效果了)
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>多行段落垂直居中</title>
<style type="text/css">body,p{padding: 0px;margin: 0px;}/*800×500*/.box{background-color: #FFB6C1;width: 800px;height: 500px;display: table-cell;}/*600×300*/.paragraph{border: 1px solid #000000;background-color: #ADD8E6;width: 598px;height: 298px;color: #ffffff;display: table-cell;vertical-align:middle;}
</style>
</head><body><div class="box"><p class="paragraph">东湖岩壁千姿百态,无奇不有。真可谓怪石嶙峋,形状万千。山岩累累,危峰兀立。抬眼上望,山峰像是用绿色染过似的,处处苍翠欲滴。还有气势壮丽的峻峭石壁,像是那样的高,那样的陡,昂首仰视,真使人感慨万千。</p></div>
</body>
</html>
vertical-align: middle;
来垂直居中注意:vertical-align: middle;
只对行内元素有效。<td>默认自带,所以不用设置。(这样会多很多无意义的标签)
(看到这估计很多人跟我一样想:既然如此,将块级元素行内化,再加入vertical-align: middle;
不就行了?嗯,很好,欢迎入坑,下面我会接着说明)
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>多行段落垂直居中</title>
<style type="text/css">body,p{padding: 0px;margin: 0px;}/*800×500*/.box{background-color: #FFB6C1;width: 800px;height: 500px;}/*600×300*/.paragraph{border: 1px solid #000000;background-color: #ADD8E6;width: 598px;height: 298px;color: #ffffff;}
</style>
</head><body>
<div class="box">
<table><tbody><tr><td class="paragraph"><p>东湖岩壁千姿百态,无奇不有。真可谓怪石嶙峋,形状万千。山岩累累,危峰兀立。抬眼上望,山峰像是用绿色染过似的,处处苍翠欲滴。还有气势壮丽的峻峭石壁,像是那样的高,那样的陡,昂首仰视,真使人感慨万千。</p>
</td></tr></tbody></table>
</div>
</body>
</html>
vertical-align: middle;
带来的坑vertical-align: middle;
这么好用,且只对行内元素有效,那么岂不是直接给行内元素的css样式添加或者将块级元素行内化就好了?然后内容就能垂直居中了?
上上标标,显示在同级左上方或右上方。
下下标标,显示在同级左下方或右下方。
中中标标,当然,没有中标,这里只是用small标签取巧,来说这种效果。
而middle,所体现的,就是“中标”的效果,必须旁边有参照,在参照物旁边,起到垂直居中效果,但是对于整个段落来说,并不是居中(所以,你的参照标签,height就必须等于容器高)。
👇👇👇(下面是码子,可以自行感受这个坑所带来的“魅力”):
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>多行段落垂直居中</title>
<style type="text/css">body,p{padding: 0px;margin: 0px;}/*800×500*/.box{background-color: #FFB6C1;width: 800px;height: 500px;}/*600×300*/.paragraph{border: 1px solid #000000;background-color: #ADD8E6;width: 598px;/*height: auto; 这里可以不写*/color: #ffffff;display: inline-block; /*因为是要对准旁边元素居中,又必须有宽高,所以只能行内块元素(共享一行)*/vertical-align: middle;}.help{width: 0; /*因为不需要内容,所以宽值为0*/height: 100%; /*因为起到参考效果,所以高值必须要,因为是对准这个高的中间位置垂直居中的,所以必须等于父元素。*/display: inline-block;/*因为是要给旁边元素对准居中,又必须有宽高,所以只能行内块元素(共享一行)*/vertical-align: middle;}
</style>
</head><body><div class="box"><p class="paragraph">东湖岩壁千姿百态,无奇不有。真可谓怪石嶙峋,形状万千。山岩累累,危峰兀立。抬眼上望,山峰像是用绿色染过似的,处处苍翠欲滴。还有气势壮丽的峻峭石壁,像是那样的高,那样的陡,昂首仰视,真使人感慨万千。</p><div class="help"></div></div>
</body>
</html>
本文发布于:2024-02-01 07:17:38,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170674306034829.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |