CSS 水平垂直居中?晓得这几种就够了!
<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;"><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;">"parent"</span><span style="color: black;">></span>
<span style="color: black;"><</span><span style="color: black;">span</span> <span style="color: black;">class</span><span style="color: black;">=</span><span style="color: black;">"child"</span><span style="color: black;">></</span><span style="color: black;">div</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;">1、flex 布局水平垂直居中</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"> <span style="color: black;">.</span><span style="color: black;">parent</span> <span style="color: black;">{</span>
<span style="color: black;">display</span><span style="color: black;">:</span> <span style="color: black;">flex</span><span style="color: black;">;</span>
<span style="color: black;">justify-content</span><span style="color: black;">:</span> <span style="color: black;">center</span><span style="color: black;">;</span>
<span style="color: black;">align-items</span><span style="color: black;">:</span> <span style="color: black;">center</span><span style="color: black;">;</span>
<span style="color: black;">}</span>
</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2、transform + position 水平垂直居中</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"> <span style="color: black;">.</span><span style="color: black;">parent</span> <span style="color: black;">{</span>
<span style="color: black;">position</span><span style="color: black;">:</span> <span style="color: black;">relative</span><span style="color: black;">;</span>
<span style="color: black;">}</span>
<span style="color: black;">.</span><span style="color: black;">child</span> <span style="color: black;">{</span>
<span style="color: black;">position</span><span style="color: black;">:</span> <span style="color: black;">absolute</span><span style="color: black;">;</span><span style="color: black;">left</span><span style="color: black;">:</span> <span style="color: black;">50</span><span style="color: black;">%</span><span style="color: black;">;</span><span style="color: black;">top</span><span style="color: black;">:</span> <span style="color: black;">50</span><span style="color: black;">%</span><span style="color: black;">;</span>
<span style="color: black;">transform</span><span style="color: black;">:</span> <span style="color: black;">translate</span><span style="color: black;">(</span><span style="color: black;">-50</span><span style="color: black;">%</span><span style="color: black;">,</span> <span style="color: black;">-50</span><span style="color: black;">%</span><span style="color: black;">);</span>
<span style="color: black;">}</span>
</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3、定宽块级元素水平居中</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">.</span><span style="color: black;">child</span><span style="color: black;">{</span>
<span style="color: black;">display</span><span style="color: black;">:</span> <span style="color: black;">block</span><span style="color: black;">;</span>
<span style="color: black;">width</span><span style="color: black;">:</span> <span style="color: black;">40</span><span style="color: black;">px</span><span style="color: black;">;</span>
<span style="color: black;">margin</span><span style="color: black;">:</span> <span style="color: black;">0</span> <span style="color: black;">auto</span><span style="color: black;">;</span>
<span style="color: black;">}</span>
</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">4、内联元素水平居中</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">.</span><span style="color: black;">parent</span> <span style="color: black;">{</span>
<span style="color: black;">text-align</span><span style="color: black;">:</span> <span style="color: black;">center</span><span style="color: black;">;</span>
<span style="color: black;">}</span>
</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">5、内联元素垂直居中</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">.</span><span style="color: black;">parent</span> <span style="color: black;">{</span>
<span style="color: black;">height</span><span style="color: black;">:</span><span style="color: black;">100</span><span style="color: black;">px</span><span style="color: black;">;</span>
<span style="color: black;">line-height</span><span style="color: black;">:</span><span style="color: black;">100</span><span style="color: black;">px</span><span style="color: black;">;</span>
<span style="color: black;">}</span>
</div>
<h2 style="color: black; text-align: left; margin-bottom: 10px;">顺便介绍三种布局:</h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1、float布局(还在<span style="color: black;">运用</span>float布局的<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>设置属性 float:left 或 float:right 让元素从左往右流动或从右往左流动,<span style="color: black;">经过</span>设置 margin 属性<span style="color: black;">调节</span>元素的位置,最后清除浮动。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2、绝对定位</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">给父元素设置 display:relative 给子元素设置 display:absolute 使子元素相<span style="color: black;">针对</span>父元素定位,<span style="color: black;">经过</span>设置 left/right/top/bottom 属性<span style="color: black;">调节</span>元素的位置。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3、flex布局(<span style="color: black;">不可</span><span style="color: black;">运用</span>flex布局的<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;">给父元素(Flex容器)设置 display:flex;子元素(Flex项目)会默认排成一行,<span style="color: black;">经过</span>设置容器或项目的<span style="color: black;">各样</span>属性来改变项目布局的方式。</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;">接下来用代码演示三种布局方式</h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">HTML:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><</span><span style="color: black;">body</span><span style="color: black;">></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;">"father"</span><span style="color: black;">></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;">"a"</span><span style="color: black;">></span>left<span style="color: black;"></</span><span style="color: black;">div</span><span style="color: black;">></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;">"b"</span><span style="color: black;">></span>mid<span style="color: black;"></</span><span style="color: black;">div</span><span style="color: black;">></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;">"c"</span><span style="color: black;">></span>right<span style="color: black;"></</span><span style="color: black;">div</span><span style="color: black;">></span>
<span style="color: black;"></</span><span style="color: black;">div</span><span style="color: black;">></span>
<span style="color: black;"></</span><span style="color: black;">body</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;"><span style="color: black;">div</span> <span style="color: black;">{</span>
<span style="color: black;">box-sizing</span><span style="color: black;">:</span> <span style="color: black;">border-box</span>
<span style="color: black;">}</span>
<span style="color: black;">.</span><span style="color: black;">father</span> <span style="color: black;">{</span>
<span style="color: black;">height</span><span style="color: black;">:</span> <span style="color: black;">300</span><span style="color: black;">px</span><span style="color: black;">;</span>
<span style="color: black;">width</span><span style="color: black;">:</span> <span style="color: black;">600</span><span style="color: black;">px</span><span style="color: black;">;</span>
<span style="color: black;">border</span><span style="color: black;">:</span> <span style="color: black;">1</span><span style="color: black;">px</span> <span style="color: black;">solid</span> <span style="color: black;">black</span><span style="color: black;">;</span>
<span style="color: black;">}</span>
<span style="color: black;">.</span><span style="color: black;">father</span> <span style="color: black;">></span> <span style="color: black;">div</span> <span style="color: black;">{</span>
<span style="color: black;">border</span><span style="color: black;">:</span> <span style="color: black;">1</span><span style="color: black;">px</span> <span style="color: black;">solid</span> <span style="color: black;">red</span><span style="color: black;">;</span>
<span style="color: black;">width</span><span style="color: black;">:</span> <span style="color: black;">150</span><span style="color: black;">px</span><span style="color: black;">;</span>
<span style="color: black;">height</span><span style="color: black;">:</span> <span style="color: black;">150</span><span style="color: black;">px</span><span style="color: black;">;</span>
<span style="color: black;">text-align</span><span style="color: black;">:</span> <span style="color: black;">center</span><span style="color: black;">;</span>
<span style="color: black;">line-height</span><span style="color: black;">:</span> <span style="color: black;">150</span><span style="color: black;">px</span><span style="color: black;">;</span>
<span style="color: black;">}</span>
</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://pic2.zhimg.com/80/v2-29b4a63e7d4408fa94e0bff1c0b5bf5d_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;">1、浮动布局</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">.</span><span style="color: black;">a</span> <span style="color: black;">{</span>
<span style="color: black;">float</span><span style="color: black;">:</span> <span style="color: black;">left</span><span style="color: black;">;</span>
<span style="color: black;">}</span>
<span style="color: black;">.</span><span style="color: black;">b</span> <span style="color: black;">{</span>
<span style="color: black;">float</span><span style="color: black;">:</span> <span style="color: black;">left</span><span style="color: black;">;</span>
<span style="color: black;">margin-left</span><span style="color: black;">:</span> <span style="color: black;">75</span><span style="color: black;">px</span><span style="color: black;">;</span>
<span style="color: black;">}</span>
<span style="color: black;">.</span><span style="color: black;">c</span> <span style="color: black;">{</span>
<span style="color: black;">float</span><span style="color: black;">:</span> <span style="color: black;">right</span><span style="color: black;">;</span>
<span style="color: black;">}</span>
<span style="color: black;">.</span><span style="color: black;">father</span><span style="color: black;">::</span><span style="color: black;">after</span> <span style="color: black;">{</span>
<span style="color: black;">content</span><span style="color: black;">:</span> <span style="color: black;">;</span>
<span style="color: black;">display</span><span style="color: black;">:</span> <span style="color: black;">block</span><span style="color: black;">;</span>
<span style="color: black;">clear</span><span style="color: black;">:</span> <span style="color: black;">both</span><span style="color: black;">;</span>
<span style="color: black;">}</span><span style="color: black;">/* 清除浮动 */</span>
</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2、绝对定位</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">.</span><span style="color: black;">father</span> <span style="color: black;">{</span>
<span style="color: black;">position</span><span style="color: black;">:</span> <span style="color: black;">relative</span><span style="color: black;">;</span>
<span style="color: black;">}</span>
<span style="color: black;">.</span><span style="color: black;">a</span> <span style="color: black;">{</span>
<span style="color: black;">position</span><span style="color: black;">:</span> <span style="color: black;">absolute</span><span style="color: black;">;</span>
<span style="color: black;">}</span>
<span style="color: black;">.</span><span style="color: black;">b</span> <span style="color: black;">{</span>
<span style="color: black;">position</span><span style="color: black;">:</span> <span style="color: black;">absolute</span><span style="color: black;">;</span>
<span style="color: black;">left</span><span style="color: black;">:</span> <span style="color: black;">225</span><span style="color: black;">px</span><span style="color: black;">;</span>
<span style="color: black;">}</span>
<span style="color: black;">.</span><span style="color: black;">c</span> <span style="color: black;">{</span>
<span style="color: black;">position</span><span style="color: black;">:</span> <span style="color: black;">absolute</span><span style="color: black;">;</span>
<span style="color: black;">right</span><span style="color: black;">:</span> <span style="color: black;">0</span><span style="color: black;">;</span>
<span style="color: black;">}</span>
</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3、flex:我只要两行代码</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">.</span><span style="color: black;">father</span><span style="color: black;">{</span>
<span style="color: black;">display</span><span style="color: black;">:</span> <span style="color: black;">flex</span><span style="color: black;">;</span>
<span style="color: black;">justify-content</span><span style="color: black;">:</span><span style="color: black;">space</span><span style="color: black;">-</span><span style="color: black;">between</span><span style="color: black;">;</span>
<span style="color: black;">}</span>
</div>
这夸赞甜到心里,让我感觉温暖无比。
页:
[1]