How can I align text center on small screens with Bootstrap?
CssBootstrap 4Twitter Bootstrap-3Bootstrap 5Css Problem Overview
I have html as:-
<footer>
<div class="container">
<div class="row">
<div class="col-xs-12 col-sm-6 text-left">
<p>
© 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?
Css Solutions
Solution 1 - Css
Responsive text alignment has been added in Bootstrap V4:
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>
Solution 2 - Css
<div class="text-lg-right text-center">
center in xs and right in lg devices
</div>
text-lg-right
applies for large size only, so except large size text-center
will be applied for the rest of the sizes.
Solution 3 - Css
There is nothing built into bootstrap for this, but some simple css could fix it. Something like this should work. Not tested though
@media (max-width: 768px) {
.col-xs-12.text-right, .col-xs-12.text-left {
text-align: center;
}
}
Solution 4 - Css
Bootstrap 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>
Solution 5 - Css
@media (max-width: 767px) {
footer .text-right,
footer .text-left {
text-align: center;
}
}
I updated @loddn's answer, making two changes
max-width
ofxs
screens in bootstrap is 767px (768px is the start ofsm
screens)- (this one is a matter of preference) I used
footer
instead ofcol-*
so that if the column widths change, the CSS doesn't need to be updated.
Solution 6 - Css
Css Part is:
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:
<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>
Solution 7 - Css
In Bootstrap 5, -left
and -right
have been replaced with -start
and -end
respectively.
Solution 8 - Css
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
<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.
<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>
© 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>
Solution 9 - Css
Use bs3-upgrade library for spacings and text aligment...
https://github.com/studija/bs3-upgrade
> 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>
© 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>
Solution 10 - Css
In Bootstrap version Bootstrap v5, you'll not find text-lg-left
or text-md-right
.
-left
and -right
has been updated with -start
and -end
<div class="text-center text-sm-center text-md-start text-lg-end">
<p>text-center: for <576px</p>
<p>text-sm-center: for ≥576px</p>
<p>text-md-start: for ≥768px</p> < for left align
<p>text-lg-end: for ≥992px</p> < for right align
</div>