CSS 处于 xs 模式时的 twitter 引导文本中心

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29562527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 03:09:44  来源:igfitidea点击:

twitter bootstrap text-center when in xs mode

csstwitter-bootstrap-3

提问by Dipendra Gurung

I have html as:-

我有 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>

From the code you can see that the column's text are left and right aligned respectively. However in the xs mode I like them to be center aligned. How can I do this using twitter bootstrap?

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

回答by Jarrod

Responsive text alignment has been added in Bootstrap V4:

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

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

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

For left, right, and center alignment, responsive classes are available that use the same viewport width breakpoints as the grid system.

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

<p class="text-sm-left">Left aligned text on viewports sized SM (small) or wider.</p>

回答by loddn

There is nothing built into bootstrap for this, but some simple css could fix it. Something like this should work. Not tested though

bootstrap 中没有为此内置任何内容,但是一些简单的 css 可以修复它。像这样的事情应该有效。虽然没有测试

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

回答by Prakash

html

html

<div class="text-lg-right text-center">
  center in xs and right in lg devices
</div>

回答by Marco Pietro Cirillo

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

I updated @loddn's answer, making two changes

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

  • max-widthof xsscreens in bootstrap is 767px (768px is the start of smscreens)
  • (this one is a matter of preference) I used footerinstead of col-*so that if the column widths change, the CSS doesn't need to be updated.
  • max-widthxsbootstrap 中的屏幕数为 767px(768px 是sm屏幕的开始)
  • (这是一个偏好问题)我使用了footer而不是col-*这样,如果列宽发生变化,则不需要更新 CSS。

回答by Sanjib Debnath

Css Part is:

CSS部分是:

CSS:

CSS:

@media (max-width: 767px) {

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

And the HTML part will be ( this text center work only below 767px width )

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

HTML:

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>

回答by Hitnux

Bootstrap 4

引导程序 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>

回答by grahamlander

This was tagged as a bootstrap 3, but maybe this will be helpful: In Bootstrap 4.0.0 (non beta) : Inside the col class, use

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

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

"text-center" centers the text for all window sizes, then adding the "text-md-left" or "text-md-right" to change it above sm. You could make it "text-sm-left", "text-lg-left" or whatever. But having it set to center always first sets up it up.

“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>

回答by TheAivis

Use bs3-upgrade library for spacings and text aligment...

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

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

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

col-xs-text-center col-sm-text-left

col-xs-text-center col-sm-text-right

col-xs-text-center col-sm-text-left

col-xs-text-center 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>