是否可以使col-md-3更接近或彼此居中?

时间:2016-09-06 11:11:28

标签: html css twitter-bootstrap

是否可以使col-md-3彼此更接近? 这是我的代码:

<!DOCTYPE html>
<html>
  <head>
    <title>Center</title>
<style>

body {
  margin-top: 50px;
}

</style>

  </head>
  <body>

<div class="container">
  <div class="row">
      <div class="col-md-3">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eleifend imperdiet elit vel pretium. Donec auctor urna ut dui imperdiet ullamcorper.</p>
      </div>
      <div class="col-md-3">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eleifend imperdiet elit vel pretium. Donec auctor urna ut dui imperdiet ullamcorper.</p>
      </div>
      <div class="col-md-3">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eleifend imperdiet elit vel pretium. Donec auctor urna ut dui imperdiet ullamcorper.</p>
      </div>
  </div>
</div>
  </body>
</html>

不是中心有没有人知道如何解决它?

非常感谢您的帮助!

4 个答案:

答案 0 :(得分:0)

是的,可以将css应用于父div。在你的案例div中,class =&#34; row&#34;喜欢style =&#34; text-align:center;&#34;

答案 1 :(得分:0)

是的,您应该在col-md-offset-3 row中添加class课程,如下所示:

&#13;
&#13;
body {
      margin-top: 50px;
    }
&#13;
<!DOCTYPE html>
    <html lang="en">
    <head>
    <title>Center<title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js">
    </head>
    <body>
    
    <div class="container">
      <div class="row col-md-offset-3">
          <div class="col-md-3">
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eleifend imperdiet elit vel pretium. Donec auctor urna ut dui imperdiet ullamcorper.</p>
          </div>
          <div class="col-md-3">
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eleifend imperdiet elit vel pretium. Donec auctor urna ut dui imperdiet ullamcorper.</p>
          </div>
          <div class="col-md-3">
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eleifend imperdiet elit vel pretium. Donec auctor urna ut dui imperdiet ullamcorper.</p>
          </div>
      </div>
    </div>
      </body>
    </html>
    
    </body>
    </html>
&#13;
&#13;
&#13;

Bootstrap grid system

答案 2 :(得分:0)

更新HTML

<div class="container">
  <div class="row col-md-offset-1">
      <div class="col-md-3">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eleifend imperdiet elit vel pretium. Donec auctor urna ut dui imperdiet ullamcorper.</p>
      </div>
      <div class="col-md-3">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eleifend imperdiet elit vel pretium. Donec auctor urna ut dui imperdiet ullamcorper.</p>
      </div>
      <div class="col-md-3">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eleifend imperdiet elit vel pretium. Donec auctor urna ut dui imperdiet ullamcorper.</p>
      </div>
  </div>
</div>

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<style>

body {
  margin-top: 50px;
}

</style>

<body>

<div class="container">
  <div class="row col-md-offset-1">
      <div class="col-md-3">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eleifend imperdiet elit vel pretium. Donec auctor urna ut dui imperdiet ullamcorper.</p>
      </div>
      <div class="col-md-3">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eleifend imperdiet elit vel pretium. Donec auctor urna ut dui imperdiet ullamcorper.</p>
      </div>
      <div class="col-md-3">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eleifend imperdiet elit vel pretium. Donec auctor urna ut dui imperdiet ullamcorper.</p>
      </div>
  </div>
</div>

</body>
</html>

答案 3 :(得分:0)

是的,但为此你需要覆盖Bootstrap列的样式。默认情况下,Bootstrap的列的样式为float: left。我们需要将float替换为display: inline-block,然后我们可以使用text-align: center轻松将它们置于中心位置。

@import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css');

.center-align .row {
  letter-spacing: -4px;
  text-align: center;
  font-size: 0;
}
.center-align [class*='col-md'] {
  display: inline-block;
  vertical-align: top;
  letter-spacing: 0;
  text-align: left;
  font-size: 16px;
  float: none;
}
<div class="container center-align">
  <div class="row">
    <div class="col-md-3">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eleifend imperdiet elit vel pretium. Donec auctor urna ut dui imperdiet ullamcorper.</p>
    </div>
    <div class="col-md-3">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eleifend imperdiet elit vel pretium. Donec auctor urna ut dui imperdiet ullamcorper.</p>
    </div>
    <div class="col-md-3">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eleifend imperdiet elit vel pretium. Donec auctor urna ut dui imperdiet ullamcorper.</p>
    </div>
  </div>
</div>

注意 :不要忘记通过制作元素inline-block来删除额外的空间。