Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I center Glyphicons horizontally using Twitter Bootstrap

Tags:

I am trying to center my FontAwesome icons inside my Twitter Bootstrap code.

This is my HTML:

<div id="frontpage-content" class="content">
    <div class="container">
        <div class="row">
            <div class="col-lg-4">
                <span>
                    <i class="fa fa-camera-retro fa-5x"></i>
                </span>
                <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Pariatur, inventore, ipsa dolorum laborum sit alias iusto nam quibusdam ad distinctio rerum expedita autem itaque delectus iste mollitia perferendis sint libero accusamus
                        in. Enim, natus necessitatibus pariatur optio explicabo consequuntur quod!</p>
            </div>
            <div class="col-lg-4">
                <i class="fa fa-camera-retro fa-5x"></i>
                <p>Ut, aliquid, aperiam, veniam modi voluptates maiores nesciunt libero fugiat illum recusandae cum similique et alias possimus error ex tenetur quasi sint eius dicta officia earum eveniet suscipit corporis autem deleniti nihil sed!
                        Earum blanditiis vel similique nisi fugit reprehenderit?</p>
            </div>
            <div class="col-lg-4">
                <i class="fa fa-camera-retro fa-5x"></i>
                <p>Quisquam eos aperiam autem atque minus modi similique earum! Ab, laboriosam odit non quo officiis asperiores atque dolorum omnis vitae in qui officia sequi molestias quisquam velit exercitationem aperiam. Voluptatum, unde, nesciunt
                        temporibus voluptates sint ab architecto at quod dolore.</p>
            </div>
        </div>
    </div>

And here is what I have already tried:

#frontpage-content {
    background-color: $bgDefault;
    i {
        text-align:center;
    }
    span {
        text-align:center;
    }
}

Works perfectly for the text, but doesn't work for the i elements.

I know I can center it, if I give to the surrounding div a static width and then position the i element with...

 margin-right: auto;
 margin-left: auto;
 display: block;

...but I don't want to give the surrounding div a static width.

So, how do I solve this problem, without applying static width?

Edit: Also I know of the center HTML element, but this wouldn't be CSS, and therefore not very handy.

like image 662
LoveAndHappiness Avatar asked Apr 22 '14 20:04

LoveAndHappiness


People also ask

How do I use Glyphicons in bootstrap 5?

In lines 15 and 17 the <span> and <a> element has the bootstrap glyphicon glyphicon-pencil classes, so to use any glyphicon in bootstrap: Use the <span> or <a> element. Set the class attribute of the <span> or <a> element as glyphicon then a space and then glyphicon-iconname.

What is the main usage of Glyphicons in bootstrap?

The bootstrap glyphicons are an icon used for form, text, navigation and many other web components. This helps to reduce text and give meaningful information in short.

What are Glyphicons in bootstrap?

What are Glyphicons? Glyphicons are icon fonts which you can use in your web projects. Glyphicons Halflings are not free and require licensing, however their creator has made them available for Bootstrap projects free of cost.


2 Answers

All you need is to define display:block with text-align:center in the i inside your container div and you'll have it:

.col-lg-4 i {
    display:block;
    text-align:center
}

Here is a demo

EDIT #1: As this answer seems to get some traction, it is worth to note that the more "twitter-bootstrapy" way of doing this would be what @SimoneMelloni suggested in their answer to this question, ie. the use of the text-center class.

Note that this is basically the same as my solution, considering all text-center does is set text-align:center, it is just making use of a twitter bootstrap feature.

Also note that text-align:center only works on block-level elements or ones where you explicitly set display:block, as in my original answer above. There I needed to specify that because I used it on the i tag, which is an inline element.

EDIT #2: I just saw your [OP] edit on the center element: while it is indeed not CSS but HTML, that's less of an issue, the bigger issue with it is that it's obsolete. See more info on it on MDN's doc on the <center> element

like image 66
benomatis Avatar answered Oct 28 '22 14:10

benomatis


<div class="text-center">
    <i class="fa fa-camera-retro fa-5x></i>
</div>

Did the trick without opening the css file

like image 44
Simone Melloni Avatar answered Oct 28 '22 15:10

Simone Melloni