67

我有 html 作为: -

<footer>
<div class="container">
    <div class="row">
        <div class="col-xs-12 col-sm-6 text-left">
            <p>
                &copy; 2015 example.com. All rights reserved.
            </p>
        </div>
        <div class="col-xs-12 col-sm-6 text-right">
            <p>
                <a href="#"><i class="fa fa-facebook"></i></a> 
                <a href="#"><i class="fa fa-twitter"></i></a> 
                <a href="#"><i class="fa fa-google-plus"></i></a>
            </p>
        </div>
    </div>
</div>
</footer>

从代码中您可以看到列的文本分别左对齐和右对齐。但是在 xs 模式下,我喜欢它们居中对齐。如何使用 twitter bootstrap 执行此操作?

4

9 回答 9

76

Bootstrap V4 中添加了响应式文本对齐:

https://getbootstrap.com/docs/4.0/utilities/text/#text-alignment

对于左对齐、右对齐和居中对齐,可以使用响应式类,它们使用与网格系统相同的视口宽度断点。

<p class="text-sm-left">Left aligned text on viewports sized SM (small) or wider.</p>
于 2016-08-07T08:07:45.817 回答
28
<div class="text-lg-right text-center">
  center in xs and right in lg devices
</div>

text-lg-right仅适用于大尺寸,因此除大尺寸外text-center,其余尺寸均适用。

于 2018-07-15T06:32:04.663 回答
24

bootstrap 没有内置任何东西,但是一些简单的 css 可以修复它。像这样的东西应该工作。虽然没有测试

 @media (max-width: 768px) {
      .col-xs-12.text-right, .col-xs-12.text-left {
              text-align: center;
       } 
    }
于 2015-04-10T13:33:57.817 回答
18

引导程序 4

<div class="col-md-9 col-xs-12 text-md-left text-center">md left, xs center</div>
<div class="col-md-9 col-xs-12 text-md-right text-center">md right, xs center</div>
于 2019-02-14T08:28:23.573 回答
15
@media (max-width: 767px) {
    footer .text-right, 
    footer .text-left {
        text-align: center;
    } 
}

我更新了@loddn 的答案,做了两个更改

  • max-widthbootstrap 中的xs屏幕数是 767px(768px 是sm屏幕的开始)
  • (这是一个偏好问题)我使用footer而不是col-*这样,如果列宽发生变化,则不需要更新 CSS。
于 2015-04-10T13:44:53.663 回答
15

CSS部分是:

CSS:

@media (max-width: 767px) {

  // Align text to center.
  .text-xs-center {
    text-align: center;
  } 
}

而 HTML 部分将是(此文本中心仅在 767px 宽度以下工作)

HTML:

 <div class="col-xs-12 col-sm-6 text-right text-xs-center">
        <p>
            <a href="#"><i class="fa fa-facebook"></i></a> 
            <a href="#"><i class="fa fa-twitter"></i></a> 
            <a href="#"><i class="fa fa-google-plus"></i></a>
        </p>
 </div>
于 2016-10-03T11:58:09.707 回答
4

在 Bootstrap 5 中,-left分别-right被替换为-start-end

https://getbootstrap.com/docs/5.0/utilities/text/

于 2021-03-23T07:33:38.707 回答
1

这被标记为引导程序 3,但也许这会有所帮助:在引导程序 4.0.0(非测试版)中:在 col 类中,使用

<div class="col text-center text-md-left">

"text-center" 将所有窗口大小的文本居中,然后添加 "text-md-left" 或 "text-md-right" 以将其更改为 sm 上方。您可以将其设为“text-sm-left”、“text-lg-left”或其他任何内容。但是将其设置为中心总是首先设置它。

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
<body>
<footer>
  <div class="container">
    <div class="row">
      <div class="col-xs-12 col-sm-6 text-center text-md-left">
        <p>
          &copy; 2015 example.com. All rights reserved.
        </p>
      </div>
      <div class="col-xs-12 col-sm-6 text-center text-md-right">
        <p>
          <a href="#"><i class="fa fa-facebook"></i></a>
          <a href="#"><i class="fa fa-twitter"></i></a>
          <a href="#"><i class="fa fa-google-plus"></i></a>
        </p>
      </div>
    </div>

      </div>

</footer>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
于 2018-01-26T06:03:36.993 回答
1

使用 bs3-upgrade 库进行间距和文本对齐...

https://github.com/studija/bs3-upgrade

col-xs-文本中心 col-sm-文本-左

col-xs-文本中心 col-sm-text-right

<div class="container">
    <div class="row">
        <div class="col-xs-12 col-sm-6 col-xs-text-center col-sm-text-left">
            <p>
                &copy; 2015 example.com. All rights reserved.
            </p>
        </div>
        <div class="col-xs-12 col-sm-6 col-xs-text-center col-sm-text-right">
            <p>
                <a href="#"><i class="fa fa-facebook"></i></a> 
                <a href="#"><i class="fa fa-twitter"></i></a> 
                <a href="#"><i class="fa fa-google-plus"></i></a>
            </p>
        </div>
    </div>
</div>
于 2018-03-22T18:39:18.833 回答