在bootstrap响应页面中如何居中div

时间:2013-11-22 10:30:37

标签: css css3 twitter-bootstrap responsive-design

position a div at centre of a responsive page

我需要使用bootstrap创建一个响应式页面,在页面中心放置一个div,就像下面提到的布局一样。

17 个答案:

答案 0 :(得分:159)

Bootstrap 4的更新

使用弹性框更简单的垂直网格对齐

@import url('https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css');
html,
body {
  height: 100%
}
<div class="h-100 row align-items-center">
  <div class="col" style="background:red">
    TEXT
  </div>
</div>

Bootstrap 3的解决方案

@import url('http://getbootstrap.com/dist/css/bootstrap.css');
 html, body, .container-table {
    height: 100%;
}
.container-table {
    display: table;
}
.vertical-center-row {
    display: table-cell;
    vertical-align: middle;
}
<script src="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.1.0/bootstrap.min.js"></script>

<div class="container container-table">
    <div class="row vertical-center-row">
        <div class="text-center col-md-4 col-md-offset-4" style="background:red">TEXT</div>
    </div>
</div>

这是一个以所有屏幕尺寸为中心的水平和垂直div的简单示例。

答案 1 :(得分:33)

<强> CSS:

html,
body {
    height: 100%;
}

.container {
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
}

<强> HTML:

<div class="container">
  <div>
    example
  </div>
</div>

Example fiddle

答案 2 :(得分:10)

bootstrap 4

水平居中的孩子,使用bootstrap-4 class

justify-content-center

垂直居中,使用bootstrap-4 class

 align-items-center

但请记住不要忘记使用d-flex类 它是一个bootstrap-4实用程序类,就像这样

<div class="d-flex justify-content-center align-items-center" style="height:100px;">
    <div class="bg-primary">MIDDLE</div>    
</div>

注意:如果此代码不起作用,请务必添加bootstrap-4实用程序

答案 3 :(得分:9)

Bootstrap 4更新

现在Bootstrap 4是flexbox,垂直对齐更容易。给定一个完整高度的flexbox div,只需我们my-auto即可获得顶部和底部边距......

<div class="container h-100 d-flex justify-content-center">
    <div class="jumbotron my-auto">
      <h1 class="display-3">Hello, world!</h1>
    </div>
</div>

http://codeply.com/go/ayraB3tjSd/bootstrap-4-vertical-center

Vertical center in Bootstrap 4

答案 4 :(得分:3)

没有显示表而没有引导程序,我宁愿那样做

<div class="container container-table">
    <div class="row vertical-center-row">
        <div class="text-center col-md-4 col-md-offset-4" style="background:red">TEXT</div>
    </div>
</div>


 html, body, .container-table {
    height: 100%;
}
.container-table {
    width:100vw;
  height:150px;
  border:1px solid black;
}
.vertical-center-row {
   margin:auto;
  width:30%;
  padding:63px;
  text-align:center;

}

http://codepen.io/matoeil/pen/PbbWgQ

答案 5 :(得分:3)

您无需更改CSS中的任何内容,您可以直接在类中编写此代码,您将获得结果。

<div class="col-lg-4 col-md-4 col-sm-4 container justify-content-center">
  <div class="col" style="background:red">
    TEXT
  </div>
</div>

enter image description here

答案 6 :(得分:2)

不是最好的方法,但仍然可以使用

<div class="container-fluid h-100">
<div class="row h-100">
<div class="col-lg-12"></div>
<div class="col-lg-12">
<div class="row h-100">
  <div class="col-lg-4"></div>
  <div class="col-lg-4 border">
    This div is in middle
  </div>
  <div class="col-lg-4"></div>
</div>

</div>
<div class="col-lg-12"></div>
</div>

</div>

答案 7 :(得分:1)

很好的回答ppollono。我只是在玩耍,我得到了一个更好的解决方案。 CSS将保持不变,即:

html, body, .container {
    height: 100%;
}
.container {
    display: table;
    vertical-align: middle;
}
.vertical-center-row {
    display: table-cell;
    vertical-align: middle;
}

但对于HTML:

<div class="container">
    <div class="vertical-center-row">
        <div align="center">TEXT</div>
    </div>
</div>

这就足够了。

答案 8 :(得分:1)

在Bootstrap 4中,使用:

<div class="d-flex justify-content-center">...</div>

您还可以根据需要更改位置:

<div class="d-flex justify-content-start">...</div>
<div class="d-flex justify-content-end">...</div>
<div class="d-flex justify-content-between">...</div>
<div class="d-flex justify-content-around">...</div>

参考here

答案 9 :(得分:1)

尝试一下,例如,我使用固定高度。我认为这不会影响响应方案。

<html lang="en">
<head>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
    <div class="d-flex justify-content-center align-items-center bg-secondary w-100" style="height: 300px;">
        <div class="bg-primary" style="width: 200px; height: 50px;"></div>
    </div>
</body>
</html>

答案 10 :(得分:0)

为了使 div 居中,请将 bootstrap 的三个类名分配到 div 中:ml-0 mr-0 mx-auto

看看下面的简单例子,它是如何居中的

<div class="bg-primary w-50 ml-0 mr-0 mx-auto text-center">
      Center
</div>

答案 11 :(得分:0)

body{
    height: 100vh;
} 
.container{
    height: 100%;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/>


<div class="container text-center d-flex align-items-center justify-content-center">
        <div>
            <div>
                <h1>Counter</h1>
            </div>
            <div>
                <span>0</span>
            </div>
            <div>
                <button class="btn btn-outline-primary">Decrease</button>
                <button class="btn btn-danger">Reset</button>
                <button class="btn btn-outline-success">Increase</button>
            </div>
        </div>
    </div>

您也可以这样做。

答案 12 :(得分:0)

对于实际版本的Bootstrap 4.3.1使用

样式

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<style type="text/css">
html, body {
    height: 100%;
}
</style>

代码

<div class="h-100 d-flex justify-content-center">
<div class="jumbotron my-auto">
<!-- example content -->
<div class="alert alert-success" role="alert">
<h4 class="alert-heading">Title</h4>
<p>Desc</p>
</div>
<!-- ./example content -->
</div>
</div

答案 13 :(得分:0)

我认为使用bootstrap完成布局的最简单方法是这样的:

<section>
<div class="container">
    <div class="row">
        <div align="center">
            <div style="max-width: 200px; background-color: blueviolet;">
                <div>
                    <h1 style="color: white;">Content goes here</h1>
                </div>
            </div>
        </div>
    </div>
</div>

我所做的只是添加允许我居中div的div层,但由于我没有使用百分比,你需要指定div的最大宽度为中心。

您可以使用相同的方法来居中多个列,只需添加更多div图层:

<div class="container">
    <div class="row">
        <div align="center">
            <div style="max-width: 400px; background-color: blueviolet;">
                <div class="col-md-12 col-sm-12 col-xs-12" style="background-color: blueviolet;">
                    <div class="col-md-8 col-sm-8 col-xs-12" style="background-color: darkcyan;">
                        <h1 style="color: white;">Some content</h1>
                    </div>
                    <div class="col-md-4 col-sm-4 col-xs-12" style="background-color: blue;">
                        <p style="color: white;">More content</p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

注意:我为md,sm和xs添加了第12列的div,如果不这样做,第一个具有背景颜色的div(在这种情况下为“blueviolet”)会崩溃,你将能够看到孩子divs,但不是背景颜色。

答案 14 :(得分:-1)

最简单的解决方案是在两侧添加一个空白列,如下所示:

  <div class="row form-group">

    <div class="col-3"></div>

    <ul class="list-group col-6">
      <li class="list-group-item active">Yuvraj Patil</li>
    </ul>

    <div class="col-3"></div>
  </div>

答案 15 :(得分:-1)

这是简单的CSS规则,将任何div放在中心

.centered {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

https://css-tricks.com/quick-css-trick-how-to-center-an-object-exactly-in-the-center/

答案 16 :(得分:-2)

jsFiddle

<强> HTML

<div class="container" id="parent">
  <div class="row">
    <div class="col-lg-12">text
      <div class="row ">
        <div class="col-md-4 col-md-offset-4" "id="child">TEXT</div>
      </div>
    </div>
  </div>
</div>

<强> CSS

#parent {    
    text-align: center;
}
#child {
    margin: 0 auto;
    display: inline-block;
    background: red;
    color: white;
}