Matthew
Matthew

Reputation: 4056

Center Image Vertically and Horizontally in Bootstrap Grid

I'm having trouble figuring out how to center an image vertically and horizontally. Essentially I have two rows of images, all have a width of 150 but heights vary.

CSS

.image-center {
  display: inline-block;
  vertical-align: middle;
  position: relative;
}

HTML

<div class="row">
        <div class="col">
            <img class="center-block image-center" width="150" src="imagery/1.png"/>
        </div>
        <div class="col">
            <img class="center-block image-center" width="150" src="imagery/2.png"/>
        </div>            <div class="col">
            <img class="center-block image-center" width="150" src="imagery/3.png"/>
        </div>            <div class="col">
            <img class="center-block image-center" width="150" src="imagery/4.png"/>
        </div>
    </div>

    <div class="row">
        <div class="col">
            <img class="center-block" width="150" src="imagery/5.png"/>
        </div>            <div class="col">
            <img class="center-block" width="150" src="imagery/6.png"/>
        </div>            <div class="col">
            <img class="center-block" width="150" src="imagery/7.png"/>
        </div>            <div class="col">
            <img class="center-block" width="150" src="imagery/8.png"/>
        </div>
    </div>

Upvotes: 14

Views: 39182

Answers (6)

vinoth s
vinoth s

Reputation: 198

This CSS will be responsive for any devices. It will center horizontally and vertically.

.col {
    margin: 0;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

Upvotes: 7

Arush Singh
Arush Singh

Reputation: 1

don't know about bootstrap but if you use normal CSS apply this class to the child container

position: absolute,
margin: auto,
top: 0,
right: 0,
bottom: 0,
left: 0,

Upvotes: -2

Brajesh Pandey
Brajesh Pandey

Reputation: 31

    HTML:

<div class="row imageCenterAlign topAlign">
        <div class="col">
            <img class="center-block image-center" width="150" src="imagery/1.png"/>
        </div>
        <div class="col">
            <img class="center-block image-center" width="150" src="imagery/2.png"/>
        </div>            <div class="col">
            <img class="center-block image-center" width="150" src="imagery/3.png"/>
        </div>            <div class="col">
            <img class="center-block image-center" width="150" src="imagery/4.png"/>
        </div>
    </div>

    <div class="row imageCenterAlign">
        <div class="col">
            <img class="center-block" width="150" src="imagery/5.png"/>
        </div>            <div class="col">
            <img class="center-block" width="150" src="imagery/6.png"/>
        </div>            <div class="col">
            <img class="center-block" width="150" src="imagery/7.png"/>
        </div>            <div class="col">
            <img class="center-block" width="150" src="imagery/8.png"/>
        </div>
    </div>

css:
.imageCenterAlign{
    display: flex;
    justify-content: center;
    align-items: center;
}
.topAlign{
    top:50%;
}

Upvotes: 1

GonzaloPani
GonzaloPani

Reputation: 170

I will recommend you to use the display:flex, you just need to create an horizontal class and a vertical class an assign it to the divs of the image depending of the orientation you want to give it.

It's easy and simple, I write you the snippet, you just need to change the margins, and I also changed the image.

div.horizontal {
    display: flex;
    justify-content: center;
    margin-bottom: 15px;
}

div.vertical {
    display: flex;
    flex-direction: column;
    justify-content: center;
    margin-right: 15px;
}
<div class="row horizontal">
        <div class="col vertical">
            <img class="center-block image-center" width="150" src="https://www.alliedmetrics.com/images/black-viton-seals.png"/>
        </div>

        <div class="col vertical">
            <img class="center-block image-center" width="150" src="https://www.alliedmetrics.com/images/black-viton-seals.png"/>
        </div>            <div class="col vertical">
            <img class="center-block image-center" width="150" src="https://www.alliedmetrics.com/images/black-viton-seals.png"/>
        </div>            <div class="col vertical">
            <img class="center-block image-center" width="150" src="https://www.alliedmetrics.com/images/black-viton-seals.png"/>
        </div>
    </div>

    <div class="row horizontal">
        <div class="col vertical">
            <img class="center-block" width="150" src="imagery/5.png"/>
        </div>            <div class="col vertical">
            <img class="center-block " width="150" src="imagery/6.png"/>
        </div>            <div class="col vertical">
            <img class="center-block" width="150" src="imagery/7.png"/>
        </div>            <div class="col vertical">
            <img class="center-block" width="150" src="imagery/8.png"/>
        </div>
</div>

Hope it helps you.

Upvotes: 0

kunambi
kunambi

Reputation: 772

The best way to accomplish this would be to use flexbox imho.

HTML:

<div class="group">
  <img src="https://fakeimg.pl/150x100/?text=Hello" />
  <img src="https://fakeimg.pl/150x110/?text=Hello" />
  <img src="https://fakeimg.pl/150x120/?text=Hello" />
  <img src="https://fakeimg.pl/150x80/?text=Hello" />
  <img src="https://fakeimg.pl/150x70/?text=Hello" />
  <img src="https://fakeimg.pl/150x100/?text=Hello" />
  <img src="https://fakeimg.pl/150x115/?text=Hello" />
  <img src="https://fakeimg.pl/150x90/?text=Hello" />
  <img src="https://fakeimg.pl/150x100/?text=Hello" />
  <img src="https://fakeimg.pl/150x120/?text=Hello" />
</div>

CSS:

DIV.group {
 display:flex;
 flex-flow:row wrap;
 justify-content:space-between;
 align-items:center;
 align-content:stretch;
}

Upvotes: 0

tius
tius

Reputation: 550

If you're using Bootstrap 4 (it seems you are), you may use flex alignment classes like align-items-center justify-content-center

 <div class="col d-flex align-items-center justify-content-center">

More info: https://getbootstrap.com/docs/4.1/layout/grid/#alignment

Upvotes: 39

Related Questions