css的盒子的居中

2016-09-07  本文已影响19人  huhu502
      .one{
        height: 300px;
        width: 577px;
        border: 1px solid black;
        position: relative;
        margin: 0 auto;
    }
    .two{
        width: 100px;
        height: 100px;
        border: 1px solid green;
        position: absolute;
        margin: 0 auto;
    }   
  </style>
   </head>
  <body>
    <div class="one">
        <div class="two">   
        </div>
    </div>

这种情况下子盒子不居中,父盒子居中

例子:

 .two{
        width: 100px;
        height: 100px;
        border: 1px solid green;
        position: absolute;
        left: 50%;        父盒子的一半
        margin-left: -50px;子盒子向左走自己的一半width: 100px/2
    }
      margin-left: auto;    先跑到最右边
        margin-right: auto;       跑到中间
上一篇 下一篇

猜你喜欢

热点阅读