CSS中怎么设置元素水平垂直居中?
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">记录怎么<span style="color: black;">运用</span>text-align与vertical-align属性设置元素在容器中垂直居中对齐。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">text-align与vertical-align虽然都是设置元素内部对齐方式的,但两者的用法还是有略微<span style="color: black;">区别</span>的。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在讨论这两者的用法之前,<span style="color: black;">咱们</span><span style="color: black;">首要</span><span style="color: black;">必须</span><span style="color: black;">认识</span>元素的<span style="color: black;">归类</span>。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://pic4.zhimg.com/80/v2-cde008d4c42af51ef129f6ad4aa3e287_720w.webp" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">块元素:<span style="color: black;">独霸</span>一行、可设宽高、标准盒模型、<span style="color: black;">通常</span>当容器<span style="color: black;">运用</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">内联元素:多个元素是排在一行的,当它遇到边缘或换行符时才会换行,它的宽高<span style="color: black;">咱们</span>是定义不了的,它随内容变化。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">内联块元素:既有块元素特点,又有内联元素特点。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">text-align是设置元素中文字的水平对齐方式。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">它的<span style="color: black;">功效</span>对象是文本,<span style="color: black;">掌控</span>文本,对块状元素等不<span style="color: black;">生效</span>果,只能让块元素里的内容(例如p标签内的文字:让文字在p标签内居中)相对块元素居中</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">vertical-align是设置元素的垂直对齐方式。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">它的<span style="color: black;">功效</span>对象是元素;它只能<span style="color: black;">功效</span>于内联或内联块元素。该属性相对基线去进行对齐的,介绍一下基线,上图:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://pic3.zhimg.com/80/v2-901beef93dcde10100fe01ce597ab22e_720w.webp" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">亦</span><span style="color: black;">便是</span>说基线是在元素中间靠下的位置的,而<span style="color: black;">运用</span>vertical-align属性的时候,<span style="color: black;">由于</span>它定义内联元素本身的基线与它所在行的基线做相应对齐的,<span style="color: black;">因此</span>在设置元素相对父元素垂直居中对齐的时候,<span style="color: black;">倘若</span>在父元素中<span style="color: black;">仅有</span>它一个元素,是没办法在父元素中进行垂直居中对齐的。<span style="color: black;">此时</span>就<span style="color: black;">必须</span>一个标尺(既同级元素),让它相<span style="color: black;">针对</span>标尺对齐来达到居中效果。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">怎样</span>设置一个元素在父元素中水平垂直居中:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1.给它的父元素写text-align属性;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2.要居中的元素将其类型转为inline-block;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3.要居中的元素加vertical-align属性;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">4.添加一个“标尺”,既同级元素(span等),要居中的元素与其互相垂直居中</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">重视</span>在编辑时标尺与<span style="color: black;">必须</span>居中的元素之间<span style="color: black;">不可</span>有空格回车;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">标尺须加:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">display:inline-block;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">width:0;(目的是<span style="color: black;">隐匿</span>标尺)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">height:100%(与父元素等高,中线位置既是居中位置);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">vertical-align:middle;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">例如:让div1-1在div1水平垂直对齐,加上背景颜色以便区分。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><</span><span style="color: black;">div</span> <span style="color: black;">class</span><span style="color: black;">=</span><span style="color: black;">"div1"</span><span style="color: black;">></span><span style="color: black;">div1</span>
<span style="color: black;"><</span><span style="color: black;">div</span> <span style="color: black;">class</span><span style="color: black;">=</span><span style="color: black;">"div1-1"</span><span style="color: black;">></span><span style="color: black;">div2</span><span style="color: black;"></</span><span style="color: black;">div</span><span style="color: black;">><</span><span style="color: black;">span</span><span style="color: black;">></</span><span style="color: black;">span</span><span style="color: black;">></span>
<span style="color: black;"></</span><span style="color: black;">div</span><span style="color: black;">></span>
</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">CSS部分:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;">*{
margin: 0;
padding: 0;
}
.div1{
width: 200px;
height: 150px;
bac<span style="color: black;">公斤</span>round: blue;
margin: 20px 20px;
text-align: center;
}
.div1-1{
width: 100px;
height: 100px;
bac<span style="color: black;">公斤</span>round: red;
display: inline-block;
vertical-align: middle;
}
.div1 span{
display: inline-block;
width: 0px;
height: 100%;
bac<span style="color: black;">公斤</span>round: #0681D0;
vertical-align: middle;
}</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">结果图:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://pic1.zhimg.com/80/v2-b2d569166201496c6c32b647e4424bf0_720w.webp" style="width: 50%; margin-bottom: 20px;"></div>
我深感你的理解与共鸣,愿对话长流。 我深受你的启发,你的话语是我前进的动力。 你的话语如春风拂面,温暖了我的心房,真的很感谢。
页:
[1]