>  기사  >  웹 프론트엔드  >  div的垂直居中 - dzxczx

div的垂直居中 - dzxczx

WBOY
WBOY원래의
2016-05-21 08:54:491255검색

之前一直不会垂直居中,看了一篇文章才知道  原地址 不详了..

<span style="color: #008080;"> 1</span> <span style="color: #800000;">.diva</span>{
<span style="color: #008080;"> 2</span> <span style="color: #ff0000;">      width</span>:<span style="color: #0000ff;">300px</span>;
<span style="color: #008080;"> 3</span> <span style="color: #ff0000;">      height</span>:<span style="color: #0000ff;">300px</span>;
<span style="color: #008080;"> 4</span> <span style="color: #ff0000;">      background-color</span>:<span style="color: #0000ff;">green</span>;
<span style="color: #008080;"> 5</span> <span style="color: #ff0000;">      position</span>:<span style="color: #0000ff;">absolute</span>;
<span style="color: #008080;"> 6</span> <span style="color: #ff0000;">      top</span>:<span style="color: #0000ff;">0</span>;
<span style="color: #008080;"> 7</span> <span style="color: #ff0000;">      right</span>:<span style="color: #0000ff;">0</span>;
<span style="color: #008080;"> 8</span> <span style="color: #ff0000;">      bottom</span>:<span style="color: #0000ff;">0</span>;
<span style="color: #008080;"> 9</span> <span style="color: #ff0000;">      left</span>:<span style="color: #0000ff;">0</span>;
<span style="color: #008080;">10</span> <span style="color: #ff0000;">      margin</span>:<span style="color: #0000ff;">auto</span>;
<span style="color: #008080;">11</span> }
<span style="color: #008080;">12</span> <span style="color: #800000;">这是一种垂直居中的方式。
</span><span style="color: #008080;">13</span> 
<span style="color: #008080;">14</span> <span style="color: #800000;">第二种兼容比较好
</span><span style="color: #008080;">15</span> 
<span style="color: #008080;">16</span> <span style="color: #800000;">.divb</span>{
<span style="color: #008080;">17</span> <span style="color: #ff0000;">      width</span>:<span style="color: #0000ff;">300px</span>;
<span style="color: #008080;">18</span> <span style="color: #ff0000;">      height</span>:<span style="color: #0000ff;">300px</span>;
<span style="color: #008080;">19</span> <span style="color: #ff0000;">      background-color</span>:<span style="color: #0000ff;">red</span>;
<span style="color: #008080;">20</span> <span style="color: #ff0000;">      position</span>:<span style="color: #0000ff;">absolute</span>;
<span style="color: #008080;">21</span> <span style="color: #ff0000;">      top</span>:<span style="color: #0000ff;">50%</span>;
<span style="color: #008080;">22</span> <span style="color: #ff0000;">      right</span>:<span style="color: #0000ff;">0</span>;
<span style="color: #008080;">23</span> <span style="color: #ff0000;">      left</span>:<span style="color: #0000ff;">0</span>;
<span style="color: #008080;">24</span> <span style="color: #ff0000;">      margin</span>:<span style="color: #0000ff;">auto</span>;
<span style="color: #008080;">25</span> <span style="color: #ff0000;">      margin-top</span>:<span style="color: #0000ff;">-150px</span>;<span style="color: #ff0000;">//一半的高度
</span><span style="color: #008080;">26</span> }

第一次写博客 不好之处大家见谅 有更好的方法或者错误欢迎指出。

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.