CSS居中的几种方式
<h3 style="color: black; text-align: left; margin-bottom: 10px;">Grid布局的三种实现方式</h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">办法</span>1&2:为<span style="color: black;">必须</span>居中的元素的父级添加 :</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;">"box"</span><span style="color: black;">></span>
<span style="color: black;"><</span><span style="color: black;">p</span><span style="color: black;">></span> <span style="color: black;">123</span> <span style="color: black;"></</span><span style="color: black;">p</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;">style</span><span style="color: black;">></span>
<span style="color: black;">.</span><span style="color: black;">box</span><span style="color: black;">{</span>
<span style="color: black;">display</span> <span style="color: black;">:</span> <span style="color: black;">grid</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;">justify-items</span><span style="color: black;">:</span> <span style="color: black;">center</span><span style="color: black;">;</span>
<span style="color: black;">}</span>
<span style="color: black;"></</span><span style="color: black;">style</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;"><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;">"box"</span><span style="color: black;">></span>
<span style="color: black;"><</span><span style="color: black;">p</span><span style="color: black;">></span> <span style="color: black;">123</span> <span style="color: black;"></</span><span style="color: black;">p</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;">style</span><span style="color: black;">></span>
<span style="color: black;">.</span><span style="color: black;">box</span><span style="color: black;">{</span>
<span style="color: black;">display</span> <span style="color: black;">:</span> <span style="color: black;">grid</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;">justify-content</span><span style="color: black;">:</span> <span style="color: black;">center</span><span style="color: black;">;</span>
<span style="color: black;">}</span>
<span style="color: black;"></</span><span style="color: black;">style</span><span style="color: black;">></span>
</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>justify-items:center,在检查元素时会<span style="color: black;">发掘</span>子项的边框是占满<span style="color: black;">全部</span>格子的;而justify-content:center,<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>三: 为子元素添加margin:auto;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">该方式与Flex布局实现居中的方式类似,利用margin吸收空白空间,使元素居中。</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;">Flex布局的两种实现方式:</h3>
<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>
<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;">"box"</span><span style="color: black;">></span>
<span style="color: black;"><</span><span style="color: black;">p</span><span style="color: black;">></span> <span style="color: black;">123</span> <span style="color: black;"></</span><span style="color: black;">p</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;">style</span><span style="color: black;">></span>
<span style="color: black;">.</span><span style="color: black;">box</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;">align-items</span><span style="color: black;">:</span> <span style="color: black;">center</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;">}</span>
<span style="color: black;"></</span><span style="color: black;">style</span><span style="color: black;">></span>
</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">办法</span>二:为子元素添加margin:auto;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">该方式与Grid布局实现居中的方式类似,利用margin吸收空白空间,使元素居中。</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;">Table-cell布局实现居中:</h3>
<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>的内容撑开;或再设置一个盒子包裹父级盒子,设置width=100%,才<span style="color: black;">能够</span>和浏览器同宽。</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;">"box"</span><span style="color: black;">></span>
<span style="color: black;"><</span><span style="color: black;">p</span><span style="color: black;">></span> <span style="color: black;">123</span> <span style="color: black;"></</span><span style="color: black;">p</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;">style</span><span style="color: black;">></span>
<span style="color: black;">.</span><span style="color: black;">box</span><span style="color: black;">{</span>
<span style="color: black;">width</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;">display</span><span style="color: black;">:</span><span style="color: black;">table</span><span style="color: black;">-</span><span style="color: black;">cell</span><span style="color: black;">;</span>
<span style="color: black;">vertical-align</span><span style="color: black;">:</span><span style="color: black;">center</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>
<span style="color: black;"></</span><span style="color: black;">style</span><span style="color: black;">></span>
</div>
<h3 style="color: black; text-align: left; margin-bottom: 10px;">绝对定位实现居中:</h3>
<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;">"box"</span><span style="color: black;">></span>
<span style="color: black;"><</span><span style="color: black;">p</span><span style="color: black;">></span> <span style="color: black;">123</span> <span style="color: black;"></</span><span style="color: black;">p</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;">style</span><span style="color: black;">></span>
<span style="color: black;">.</span><span style="color: black;">box</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;">width</span><span style="color: black;">:</span><span style="color: black;">200</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;">200</span><span style="color: black;">px</span><span style="color: black;">;</span>
<span style="color: black;">}</span>
<span style="color: black;">p</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;">margin</span><span style="color: black;">:</span><span style="color: black;">0</span><span style="color: black;">;</span>
<span style="color: black;">}</span>
<span style="color: black;"></</span><span style="color: black;">style</span><span style="color: black;">></span>
</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>要为父级元素添加相对定位来<span style="color: black;">做为</span>参考点。 <span style="color: black;">同期</span>,<span style="color: black;">倘若</span><span style="color: black;">无</span>清除p标签的外边距,<span style="color: black;">那样</span>translate会将margin值计算在内,<span style="color: black;">引起</span>视觉上并<span style="color: black;">无</span>居中。</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;">line-height实现居中:</h3>
<div style="color: black; text-align: left; margin-bottom: 10px;"><div class="box">
<p> 123 </p>
</div>
<style>
.box{
width:200px;
height:200px;
text-align:center;
}
.box::after{
content:;
line-height:200px;
}
</style></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">利用伪元素的文本基线与容器中心对齐,line-height的值<span style="color: black;">必须</span>与容器等高,<span style="color: black;">才可</span>实现垂直居中效果。但当父级容器height不<span style="color: black;">知道</span>,或子元素内存在块级元素便<span style="color: black;">没</span>法<span style="color: black;">运用</span>。</p>
你的留言真是温暖如春,让我感受到了无尽的支持与鼓励。 seo常来的论坛,希望我的网站快点收录。 谷歌外链发布 http://www.fok120.com/ 我深受你的启发,你的话语是我前进的动力。 谷歌网站排名优化 http://www.fok120.com/
页:
[1]