How to Align Text to The Middle of an Element with CSS in Twitter Bootstrap

How do you get centered content using Twitter Bootstrap?

This is for Text Centering (which is what the question was about)

For other types of content, see Flavien's answer.

Update: Bootstrap 2.3.0+ Answer

The original answer was for an early version of bootstrap. As of bootstrap 2.3.0, you can simply give the div the class .text-center.


Original Answer (pre 2.3.0)

You need to define one of the two classes, row or span12 with a text-align: center. See http://jsfiddle.net/xKSUH/ or http://jsfiddle.net/xKSUH/1/

How do I align text to the middle of an element with CSS in Twitter Bootstrap?

Make the line-height of the div the same as the height of the div, eg:

#UploadSuccess { height: 20px; line-height: 20px; }

This will only work if you have one line of text.

How can I center elements horizontally or vertically with Twitter Bootstrap?

Update: while this answer was likely correct back in early 2013, it should not be used anymore. The proper solution uses offsets, like so:

class="mx-auto"

As for other users suggestion there are also native bootstrap classes available like:

class="text-center"
class="pagination-centered"

Center a column using Twitter Bootstrap 3

There are two approaches to centering a column <div> in Bootstrap 3:

Approach 1 (offsets):

The first approach uses Bootstrap's own offset classes so it requires no change in markup and no extra CSS. The key is to set an offset equal to half of the remaining size of the row. So for example, a column of size 2 would be centered by adding an offset of 5, that's (12-2)/2.

In markup this would look like:

<div class="row">
<div class="col-md-2 col-md-offset-5"></div>
</div>

Now, there's an obvious drawback for this method. It only works for even column sizes, so only .col-X-2, .col-X-4, col-X-6, col-X-8, and col-X-10 are supported.


Approach 2 (the old margin:auto)

You can center any column size by using the proven margin: 0 auto; technique. You just need to take care of the floating that is added by Bootstrap's grid system. I recommend defining a custom CSS class like the following:

.col-centered{
float: none;
margin: 0 auto;
}

Now you can add it to any column size at any screen size, and it will work seamlessly with Bootstrap's responsive layout:

<div class="row">
<div class="col-lg-1 col-centered"></div>
</div>

Note: With both techniques you could skip the .row element and have the column centered inside a .container, but you would notice a minimal difference in the actual column size because of the padding in the container class.


Update:

Since v3.0.1 Bootstrap has a built-in class named center-block that uses margin: 0 auto, but is missing float:none, you can add that to your CSS to make it work with the grid system.

Bootstrap text align top center

I believe you probably wanted to have this instead:

<label class="text-align-top text-align-center"></label>

(adding quotes around the CSS class names)

This would assume, however, that you have defined both, text-align-top and text-align-center classes in CSS, as no such classes exist in Twitter-Bootstrap.

If you want to do this the Twitter-Bootstrap-way, you can simply use the Alignment classes, which will take care of horizontal alignments. In your case you'd use text-center.

There are no vertical alignment classes in twitter-bootstrap, so you're going to have to set it up yourself using vertical-align: middle; on your targeted element, or create your custom CSS class having that, ie...

CSS:

.vertical-align-top {
vertical-align: top
}

HTML (with twitter-bootstrap):

<label class="vertical-align-top text-center"></label>

Vertically middle align text in a bootstrap column

Try this. Use display: table; for parent class and display: table-cell; vertical-align: middle; for child elements. This will give you vertical align correctly.

<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <style> a { display: block; } .customclass { display: table; } .customclass img, .customclass a{ display: table-cell; vertical-align: middle; } </style></head>
<body> <div class="row"> <div class="col-xs-4 text-center customclass" style="background:red"> <img src="http://placehold.it/75"> <a style="background: yellow">Some text red-yellow</a> </div>
<div class="col-xs-4 text-center customclass" style="background:lightgreen"> <img src="http://placehold.it/75"> <a style="background: gray">some other text long text some other long text lightgreen-gray</a> </div> <div class="col-xs-4 text-center customclass" style="background:skyblue"> <img src="http://placehold.it/75"> <a style="background: violet">this is another long long long text that should be here skyblue-violet</a> </div> </div></body>
</html>

Centering text in a table in Twitter Bootstrap

The .table td 's text-align is set to left, rather than center.

Adding this should center all your tds:

.table td {
text-align: center;
}

@import url('https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css');table,thead,tr,tbody,th,td {  text-align: center;}
.table td { text-align: center;}
<table class="table">  <thead>    <tr>      <th>1</th>      <th>1</th>      <th>1</th>      <th>1</th>      <th>2</th>      <th>2</th>      <th>2</th>      <th>2</th>      <th>3</th>      <th>3</th>      <th>3</th>      <th>3</th>    </tr>  </thead>  <tbody>    <tr>      <td colspan="4">Lorem</td>      <td colspan="4">ipsum</td>      <td colspan="4">dolor</td>    </tr>  </tbody></table>

how can I center my text in bootstrap?

Remove the max-width from the h2 and p.

Add a class of text-center to the container.

How to center form in twitter bootstrap?

Both answers posted are valid and work so I upvoted them. Here is another method i did not see posted.

You can center your form by setting it to display:inline-block and center it within the .center container using text-align:center. This way the form will center regardless of width within that container:

CSS

.center {
text-align:center;
}

.center form {
display:inline-block;
}


Related Topics



Leave a reply



Submit