Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I center an image inside a Bootstrap panel nested inside another panel?

I have a page with a main bootstrap panel and inside this panel I have another panel. The objective is to have multiple nested panels inside the main one creating something that looks similar to an image gallery.

I was able to create the content and at this point it is responsive, adjusting the number of nested panels per row and its size when necessary.

However the inner panels have an image that should be centered. I tried placing the image inside a div with class container. However, when I do that, at certain screen sizes, the images end up outside the inner panels.

Here's my HTML (for the sake of simplicity with only 2 inner panels):

<div id="mainContainer" class="container">
    <div class="panel panel-default">
        <div class="panel-heading">Panel Title</div>
        <div class="panel-body">
            <div class="row">
                <!--BEGIN OF ITEM 1 -->
                <div class="col-lg-2 col-md-3 col-sm-4 col-xs-6">
                    <div class="panel panel-default">
                        <div class="panel-heading">Item 01</div>
                        <div class="panel-body"><img src="http://placehold.it/150x150" alt="" class="img-responsive" />
                    </div>
                </div>
                <!--END OF ITEM 1 -->
                <!--BEGIN OF ITEM 2 -->
                <div class="col-lg-2 col-md-3 col-sm-4 col-xs-6">
                    <div class="panel panel-default">
                        <div class="panel-heading">Item 02</div>
                        <div class="panel-body"><img src="http://placehold.it/150x150" alt="" class="img-responsive" />
                    </div>
                </div>
                <!--END OF ITEM 2 -->
            </div>
        </div>
    </div>
</div>

jsfiddle available here: http://jsfiddle.net/9LFKV/

If you resize your browser, you'll see that at some point the inner panel grows in width but the image is left aligned.

In that case, how do I put the image centered without breaking everything else?

like image 440
Rodrigo Lira Avatar asked Mar 08 '14 23:03

Rodrigo Lira


People also ask

How do I center an image in bootstrap grid?

Answer: Use the center-block Classcenter-block on it, along with the . img-responsive class in Bootstrap 3. The .

How can you center an image or other content within a parent element?

Step 1: Wrap the image in a div element. Step 2: Set the display property to "flex," which tells the browser that the div is the parent container and the image is a flex item. Step 3: Set the justify-content property to "center." Step 4: Set the width of the image to a fixed length value.

How do I center an image inline?

An <img> element is an inline element (display value of inline-block ). It can be easily centered by adding the text-align: center; CSS property to the parent element that contains it. To center an image using text-align: center; you must place the <img> inside of a block-level element such as a div .

How do I center a body in bootstrap?

1 — Vertical Center Using Auto Margins One way to vertically center is to use my-auto . This will center the element within it's flexbox container (The Bootstrap 4 . row is display:flex ). For example, h-100 makes the row full height, and my-auto will vertically center the col-sm-12 column.


2 Answers

Twitter bootstrap has a helper class of center-block, you could use that to align the element horizontally as follows:

<img src="http://placehold.it/150x150" class="img-responsive center-block">

WORKING DEMO.

You can also use text-center class for the parent element to align its inline elements.

like image 187
Hashem Qolami Avatar answered Oct 28 '22 12:10

Hashem Qolami


Add style margin:auto to your image,that will help you to horizontally center your image

like image 28
Maria Rose Avatar answered Oct 28 '22 11:10

Maria Rose