对齐四列居中并响应

时间:2018-12-06 17:30:50

标签: javascript html css twitter-bootstrap

我有4列试图将它们居中对齐,并使其在Bootstrap 4上移动时能够响应,但是我在使它们正确对齐方面遇到一些问题。我要在各列之间留出一些空间,如下所示,但我也希望它们保持成行而不是堆叠。

这是当前正在执行的操作: enter image description here

这是我的代码:

.row-centered {
    text-align:center;
    display: inherit;
}
#businessDevTitle {
    text-align: left;
}
.col-centered {
    display:inline-block;
    float:none;
    /* reset the text-align */
    text-align:left;
    /* inline-block space fix */
    text-align: center;
    background-color: #ccc;
    border: 1px solid #ddd;
}
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<div id="ttr_main" class="row row-centered">
<h3 id="businessDevTitle">Business Development</h3>
    <div class="col-md-3 col-centered">
        <div class="col-*">
            <img src="https://i.imgur.com/zDpdA1M.png" />
        </div>
    </div>
    <div class="col-md-3 col-centered">
        <div class="col-*">
            <img src="https://i.imgur.com/1sjJ136.png" />
        </div>
    </div>
    <div class="col-md-3 col-centered">
        <div class="col-*">
            <img src="https://i.imgur.com/jaIojmI.png" />
        </div>
    </div>
    <div class="col-md-3 col-centered">
        <div class="col-*">
            <img src="https://i.imgur.com/CKd16El.png" />
        </div>
    </div>
</div>

1 个答案:

答案 0 :(得分:0)

在这里使用引导程序时,我们将尝试通过引导程序提供的类来实现该目的,因此将需要较少的自定义CSS。

通过以下方式构造HTML:

<div class="container-fluid">
    <div id="ttr_main" class="row">
        <h3 id="businessDevTitle">Business Development</h3>
        <div class="col-xs-3">
            <img src="https://i.imgur.com/zDpdA1M.png" class="img-responsive centered"/>
        </div>
        <div class="col-xs-3">
            <img src="https://i.imgur.com/1sjJ136.png" class="img-responsive centered"/>
        </div>
        <div class="col-xs-3">
            <img src="https://i.imgur.com/jaIojmI.png" class="img-responsive centered"/>
        </div>
        <div class="col-xs-3">
            <img src="https://i.imgur.com/CKd16El.png" class="img-responsive centered"/>
        </div>
    </div>
</div>

放弃col-centeredrow-centered的css,只需添加此类:

.centered {
    margin-left: auto;
    margin-right: auto;
}