Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap: center any span

I have a problem when using a class to center my span :

.center {
    float:none;
    margin-left: auto;
    margin-right: auto;
}

<div class="row-fluid">
    <div class="center span5">
    <button></button>
    </div>
</div>

This does not work, apparently it is overridden by bootstrap row-* CSS. However if I include the same style in the div directly (style="...") it works.

I think it doesn't work because it doesn't knows the width, so I tried to use an offset, but then when the button isn't centered as well as with the .CSS. I can't specify a width since my button width is set by external JavaScript.

How can I make the .center class work in that case?

like image 625
Aki Avatar asked Apr 29 '13 05:04

Aki


People also ask

How do I center my span in Bootstrap 5?

Make sure you set a width on your col class, then text-align should work. Has to know where to center first. <span> elements will only be as wide as the content contained within. Changing to <div> will allow you to center the text because the <div> element will fill all of the space provided by it's parent.

How do I center align a span element?

To center an inline element like a link or a span or an img, all you need is text-align: center . For multiple inline elements, the process is similar. It's possible by using text-align: center .

How do I center data in Bootstrap?

Use d-flex justify-content-center on your column div. This will center everything inside that column. If you have text and image inside the column, you need to use d-flex justify-content-center and text-center .

How do you center an A in a div?

Using CSS, you can center text in a div in multiple ways. The most common way is to use the text-align property to center text horizontally. Another way is to use the line-height and vertical-align properties. The last way exclusively applies to flex items and requires the justify-content and align-items properties.


1 Answers

You can center it with:

.center {
   margin: 0 auto !important;
   float: none !important;
}

JSFiddle

Or you can use:

.row-fluid [class*="span"].center, .center {
    margin: 0 auto;
    float: none;
}

I tend to avoid !important because you never know when it may differ with some other CSS. Interestingly, the above affect differs on both .row and .row-fluid.

like image 95
pickypg Avatar answered Oct 02 '22 18:10

pickypg