我遇到的问题是将一列放在一行中。这是我的html的样子:
<div class="row">
<div class="col-sm-12 parent-col">
<div class="col-sm-2 child-col">itemOne</div>
<div class="col-sm-2 child-col">itemTwo</div>
<div class="col-sm-2 child-col">itemThree</div>
<div class="col-sm-2 child-col">itemFour</div>
<div class="col-sm-2 child-col">itemFive</div>
</div>
</div>
我已经知道了:
.parent-col { float: none; margin: 0 auto; } //This did not work
.child-col { float: none; display: inline-block; } //This works, but I then lose responsiveness.
.col-sm-offset-1 on parent-col. //This also works. However, it is not EXACTLY centered. One side is about 4px larger than the other.
关于如何或可能为什么我无法集中或保持响应能力的任何其他想法?
答案 0 :(得分:1)
如果您尝试将子列置于父列的中心,则可以将偏移量添加到第一个子列:
<div class="row">
<div class="col-sm-12">
<div class="row">
<div class="col-sm-offset-1 col-sm-2">itemOne</div>
<div class="col-sm-2">itemTwo</div>
<div class="col-sm-2">itemThree</div>
<div class="col-sm-2">itemFour</div>
<div class="col-sm-2">itemFive</div>
</div>
</div>
</div>
将偏移量放在父列上会有效地在该行中放置13列,因此您希望将子列添加到12(1个偏移量,列中总共10个,然后剩余部分为1 = 12)< / p>
答案 1 :(得分:1)
Bootstrap的网格基于12列。 12/5 = 2.4,并且bootstrap实际上没有那种列宽:)
选项#1 - 每侧添加1列(使用col-sm-1
)。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="row">
<div class="col-sm-12 parent-col">
<div class="col-sm-1 child-col"></div>
<div class="col-sm-2 child-col">itemOne</div>
<div class="col-sm-2 child-col">itemTwo</div>
<div class="col-sm-2 child-col">itemThree</div>
<div class="col-sm-2 child-col">itemFour</div>
<div class="col-sm-2 child-col">itemFive</div>
<div class="col-sm-1 child-col"></div>
</div>
</div>
选项#2 - 在第一列添加偏移量:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="row">
<div class="col-sm-12 parent-col">
<div class="col-sm-2 child-col col-md-offset-1">itemOne</div>
<div class="col-sm-2 child-col">itemTwo</div>
<div class="col-sm-2 child-col">itemThree</div>
<div class="col-sm-2 child-col">itemFour</div>
<div class="col-sm-2 child-col">itemFive</div>
</div>
</div>
选项#3 - 如果你真的想要我建议反对它你可以覆盖网格系统:
@media (min-width: 768px) {
.grid-of-10 .col-sm-2 {
width: 20%;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="row">
<div class="col-sm-12 parent-col grid-of-10">
<div class="col-sm-2 child-col">itemOne</div>
<div class="col-sm-2 child-col">itemTwo</div>
<div class="col-sm-2 child-col">itemThree</div>
<div class="col-sm-2 child-col">itemFour</div>
<div class="col-sm-2 child-col">itemFive</div>
</div>
</div>
注意 - 应全屏查看所有摘要。