css学习总结】实现垂直水平居中的5种办法
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在编写HTML页面时,<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>垂直居中,可是今天给<span style="color: black;">大众</span>讲讲</p><a style="color: black;">css教程</a>
<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>,一共有5种思路,<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>单独介绍过,本文将介绍水平垂直<span style="color: black;">同期</span>居中的5种思路</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">思路一:text-align + line-height实现单行文本水平垂直居中</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><style></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.test{</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> text-align: center;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">line-height: 100px;}</style></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><div class="test" style="bac<span style="color: black;">公斤</span>round-color: lightblue;width: 200px;">测试文字</div></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">思路二: text-align + vertical-align</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">【1】在父元素设置text-align和vertical-align,并将父元素设置为table-cell元素,子元素设置为inline-block元素</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">[<span style="color: black;">重视</span>]若兼容IE7-浏览器,将结构改为<table>结构来实现table-cell的效果;用display:inline;zoom:1;来实现inline-block的效果</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><style></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.parent{</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> display: table-cell;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> text-align: center;</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;">.child{</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">display: inline-block;}</style></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><div class="parent" style="bac<span style="color: black;">公斤</span>round-color: gray; width:200px; height:100px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><div class="child" style="bac<span style="color: black;">公斤</span>round-color: lightblue;">测试文字</div></div></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">【2】若子元素是图像,可不<span style="color: black;">运用</span>table-cell,而是其父元素用行高替代高度,且字体<span style="color: black;">体积</span>设为0。子元素本身设置vertical-align:middle</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><style></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.parent{</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> text-align: center;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">line-height: 100px;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> font-size: 0;}</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.child{</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> vertical-align: middle;}</style></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><div class="parent" style="bac<span style="color: black;">公斤</span>round-color: gray; width:200px; "></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> <img class="child" src="</p><a style="color: black;"><span style="color: black;">http://</span></a>www.<span style="color: black;">maiziedu.com/uploads/rs</span><span style="color: black;">/26/ddzmgynp/img1.gif</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">" width="50%" alt="test"></div> </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">思路三: margin + vertical-align</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">想要</span>在父元素中设置vertical-align,须设置为table-cell元素;<span style="color: black;">想要</span>让margin:0 auto实现水平居中的块元素内容撑开宽度,须设置为table元素。而table元素是<span style="color: black;">能够</span>嵌套在tabel-cell元素里面的,就像一个单元格里<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>]若兼容IE7-浏览器,需将结构改为<table>结构</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><style></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.parent{</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">display:table-cell;</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;">.child{</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> display: table;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> margin: 0 auto;}</style></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><div class="parent" style="bac<span style="color: black;">公斤</span>round-color: lightgray; width:200px; height:100px; "></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> <div class="child" style="bac<span style="color: black;">公斤</span>round-color: lightblue;">测试文字</div></div> </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">思路四: <span style="color: black;">运用</span>absolute</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">【1】利用绝对定位元素的盒模型特性,在偏移属性为确定值的<span style="color: black;">基本</span>上,设置margin:auto</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><styl</p>
页:
[1]