如何在父div中居中几个div?

时间:2016-01-25 23:51:38

标签: html css css3 flexbox centering

我在父div(class="row services")中包含3个div。

我怎样才能使三个col s12 l3 div在这个父div中集中居中?

我已尝试使用display:blocktext-align:center,但似乎没有效果。

HTML

<div class="row services">

    <div class="col s12 l3">
        <div class="divider"></div>
        <h5><p>Nelson Mazda Tulsa</p> <p>Tulsa, OK</p></h5>
        <p>9902 S. Memorial Dr.</p>
        <p>Tulsa, OK 74133</p>
        <p>866-612-0040</p>
        <a href="http://www.nelsonmazdaok.com/mazda-dealer-tulsa-ok/car-dealership-near.html" class="btn waves-effect waves-dark white black-text">Get Directions</a>
        <p>
        <a href="http://www.nelsonmazdaok.com/" class="btn waves-effect waves-dark white black-text">Visit Website</a>
    </div>

    <div class="col s12 l3">
        <div class="divider"></div>
        <h5><p>Nelson Mazda Hickory Hollow</p> <p>Antioch, TN</p></h5>
        <p>5300 Mount View Road</p>
        <p>Antioch, TN 37013</p>
        <p>877-708-4449</p>
        <a href="http://www.nelsonmazdahh.com/nelson-difference/car-dealership-near.html" class="btn waves-effect waves-dark white black-text">Get Directions</a>
        <p>
        <a href="http://www.nelsonmazdahh.com/" class="btn waves-effect waves-dark white black-text">Visit Website</a>
    </div>

    <div class="col s12 l3">
        <div class="divider"></div>
        <h5><p>Nelson Mazda Cool Springs</p> <p>Franklin, TN</p></h5>
        <p>7104 S Springs Drive</p>
        <p>Franklin, TN 37067</p>
        <p>877-708-4456</p>
        <a href="http://www.nelsonmazdacs.com/nelson-difference/car-dealership-near.html" class="btn waves-effect waves-dark white black-text">Get Directions</a>
        <p>
        <a href="http://www.nelsonmazdacs.com/" class="btn waves-effect waves-dark white black-text">Visit Website</a>
    </div>

</div>

CSS

enter image description here

2 个答案:

答案 0 :(得分:3)

CSS Flexbox可以轻松实现居中。

以下是您所需要的:

.row {
    display: flex;
    flex-direction: column;
    align-items: center;
}

DEMO

更多flexbox居中选项:https://stackoverflow.com/a/33049198/3597276

答案 1 :(得分:2)

试试这个:

.col s12 l3{
  margin: 0 auto;
} 

或者如果你想要它们连续:

.col s12 l3 {
 display: inline-block;
}

我希望这就是你要找的东西!