如何在 Bootstrap 的 div 行中居中调整大小的 div 列?

杰克恩
  • 我的行占用了大量房地产,没有我提供的 css 样式。我喜欢那部分很好。
  • 我的列 div 有一个宽度,所以在大显示器上它们不会拉伸。我也喜欢那个。
  • 我想让那些 div 列在 div 行内居中。在我开始应用我自己的样式来做到这一点之前,我想知道是否有一种 Bootstrap 方法来实现它。我在网上找到了text-centercenter-box但在我的情况下似乎都不起作用。

<meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css"> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>

例子:

<div class="container" style="border: 2px #000 solid;">
    <div class="row text-center center-box" style="text-align: center;  border: 3px red solid"> 
        <div class="col-sm-4" style="background-color: yellow; border: 1px green solid; width: 50px; height: 100px;">text</div>
        <div class="col-sm-4" style="background-color: yellow; border: 1px green solid; width: 50px; height: 100px;">text</div>
        <div class="col-sm-4" style="background-color: yellow; border: 1px green solid; width: 50px; height: 100px;">text</div>
    </div>
</div>

红色边框显示 div 行拉伸得很好。div 列的宽度限制为大约 150 像素。它们保持与左侧对齐。我们可以使用 Bootstrap 技巧将它们居中吗?

由 sachMati 推荐但对我不起作用

<div class="container" style="border: 2px #000 solid;">
    <div class="row justify-content-center" style="text-align: center; border: 3px red solid;">
        <div class="col-sm-4" style="background-color: yellow; border: 1px green solid; width: 50px; height: 100px;">text</div>
        <div class="col-sm-4" style="background-color: yellow; border: 1px green solid; width: 50px; height: 100px;">text</div>
        <div class="col-sm-4" style="background-color: yellow; border: 1px green solid; width: 50px; height: 100px;">text</div>
    </div>
</div>
伊万S95

不知道为什么width要为col-*定义宽度,因为这col-*首先的重点但是,如果您使用的是Bootstrap 4,您只需将justify-content-center加到rowdiv 中,它就会将其内容居中(如果它有可用空间)。

您可以为较大的设备指定一个较小的列,例如如果您col-sm-4用于小屏幕,则可以col-lg-2用于较大的屏幕,避免设置特定的宽度,如果这当然是您需要的外观,因为设置显式会width破坏列类。

全屏试试这个

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet" />
<div class="container" style="border: 2px #000 solid;">
  <div class="row justify-content-center" style="text-align: center;  border: 3px red solid">
    <div class="col-sm-4" style="background-color: yellow; border: 1px green solid; width: 50px; height: 100px;">text</div>
    <div class="col-sm-4" style="background-color: yellow; border: 1px green solid; width: 50px; height: 100px;">text</div>
    <div class="col-sm-4" style="background-color: yellow; border: 1px green solid; width: 50px; height: 100px;">text</div>
  </div>
</div>

对于Bootstrap 3,您可以尝试添加 flex 行为,它的工作原理几乎相同;或者您可以使用这些offset-*坚持使用 Bootstrap

.flex {
  display: flex;
  justify-content: center;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css" rel="stylesheet" />

<div class="container" style="border: 2px #000 solid;">
  <div class="row flex" style="text-align: center;  border: 3px red solid">
    <div class="col-sm-4" style="background-color: yellow; border: 1px green solid; width: 150px; height: 100px;">text</div>
    <div class="col-sm-4" style="background-color: yellow; border: 1px green solid; width: 150px; height: 100px;">text</div>
    <div class="col-sm-4" style="background-color: yellow; border: 1px green solid; width: 150px; height: 100px;">text</div>
  </div>
</div>

<!-- Offset  -->

<div class="container" style="border: 2px #000 solid; margin-top: 20px">
  <div class="row" style="text-align: center;  border: 3px red solid">
    <div class="col-xs-2 col-xs-offset-3" style="background-color: yellow; border: 1px green solid; height: 100px;">text</div>
    <div class="col-xs-2" style="background-color: yellow; border: 1px green solid; height: 100px;">text</div>
    <div class="col-xs-2" style="background-color: yellow; border: 1px green solid; height: 100px;">text</div>
  </div>
</div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Bootstrap div列居中

来自分类Dev

如何在 Bootstrap Div 中居中

来自分类Dev

如何在Bootstrap网格列中居中对齐按钮(或任何div)?

来自分类Dev

如何在Bootstrap中将div垂直居中

来自分类Dev

如何在HTML Bootstrap中居中2个div?

来自分类Dev

Bootstrap:如何在液体容器内部正确居中放置6列DIV

来自分类Dev

如何在Bootstrap中删除不同列中两个div之间的空间

来自分类Dev

如何在列中的div内设置div

来自分类Dev

Bootstrap - 如何在一个 div 行中以相等的宽度容纳 7 列并且没有浪费

来自分类Dev

如何调整div的大小

来自分类Dev

如何使用Bootstrap将div的内容居中

来自分类Dev

Bootstrap 将 div 居中

来自分类Dev

在Bootstrap中向下滚动时如何在顶部附加div

来自分类Dev

如何在Twitter Bootstrap 3中更改div col顺序

来自分类Dev

如何在Bootstrap 3中对SelectedIndex显示/隐藏Div?

来自分类Dev

如何在Twitter Bootstrap中的div之间链接

来自分类Dev

在Bootstrap中向下滚动时如何在顶部附加div

来自分类Dev

div中的background在bootstrap中没有调整大小

来自分类Dev

如何使用Bootstrap在固定高度的div中居中显示图像?

来自分类Dev

如何在<div>中使用flexigrid调整<div>元素的大小

来自分类Dev

html / CSS如何在调整窗口大小时使div始终居中

来自分类Dev

如何根据div中的文本调整div的大小

来自分类Dev

如何根据div中的文本调整div的大小

来自分类Dev

bootstrap div 不会逐步调整大小

来自分类Dev

如何在Bootstrap div中对齐内容,使其触及父div的边距

来自分类Dev

如何在Bootstrap div中对齐内容,使其触及父div的边距

来自分类Dev

Bootstrap 4 Order div 列

来自分类Dev

如何在div中居中对齐文本

来自分类Dev

如何在div中居中导航

Related 相关文章

热门标签

归档