在xs模式下的twitter bootstrap文本中心

Dip*_*ung 45 css twitter-bootstrap-3

我有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>
Run Code Online (Sandbox Code Playgroud)

从代码中可以看到列的文本分别左右对齐.但是在xs模式中我喜欢它们居中对齐.我怎么能用twitter bootstrap做到这一点?

Jar*_*rod 60

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>
Run Code Online (Sandbox Code Playgroud)


lod*_*ddn 20

为此,没有内置到引导程序中,但是一些简单的css可以修复它.这样的事情应该有效.虽然没有测试过

 @media (max-width: 768px) {
      .col-xs-12.text-right, .col-xs-12.text-left {
              text-align: center;
       } 
    }
Run Code Online (Sandbox Code Playgroud)


小智 17

引导程序 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>
Run Code Online (Sandbox Code Playgroud)


Mar*_*llo 13

@media (max-width: 767px) {
    footer .text-right, 
    footer .text-left {
        text-align: center;
    } 
}
Run Code Online (Sandbox Code Playgroud)

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

  • max-widthxsbootstrap中的屏幕是767px(768px是sm屏幕的开始)
  • (这是一个偏好的问题)我使用footer而不是col-*如果列宽度改变,CSS不需要更新.


San*_*ath 12

Css部分是:

CSS:

@media (max-width: 767px) {

  // Align text to center.
  .text-xs-center {
    text-align: center;
  } 
}
Run Code Online (Sandbox Code Playgroud)

并且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>
Run Code Online (Sandbox Code Playgroud)

  • 这是最优雅的答案,因为它不会破坏现有的类,但会添加一个新的,有意义的类. (2认同)

小智 12

HTML

<div class="text-lg-right text-center">
  center in xs and right in lg devices
</div>
Run Code Online (Sandbox Code Playgroud)


小智 8

对于任何正在寻找使用 Bootstrap 5 的解决方案的人:

<p class="text-center text-md-start">
Run Code Online (Sandbox Code Playgroud)

Boostrap4 和 Boostrap5 之间有一个小变化,更多内容请参见链接:https ://getbootstrap.com/docs/5.0/utilities/text/


anj*_*esh 6

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

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