I do a catalog using Bootstrap 3. When displayed on tablets, the product images look ugly because of their small size (500x500) and a width of 767 pixels in the browser. I want to put the image in the center of the screen, but for some reason I can not. Who be will help solve the problem?
-
5I like the design of your site and products - nice work `:)` – Bojangles Aug 27 '13 at 10:25
-
7Another example of "Why we need place code into question body". – LEQADA Oct 25 '15 at 08:02
-
Try this Boostrap Class in parent element: img d-flex justify-content-center border border-light p-5 – JWC May Feb 09 '21 at 11:37
19 Answers
There is .center-block
class in Twitter Bootstrap 3 (Since v3.0.1), so use:
<img src="..." alt="..." class="img-responsive center-block" />

- 97,268
- 26
- 150
- 164

- 12,958
- 4
- 20
- 25
-
29This should be the accepted answer, as you do not need to add any CSS to use it. – user2602152 Jan 03 '14 at 15:22
-
17`class="img-responsive" style="margin: 0 auto;"` works for me `class="img-responsive center-block"` does not (bootstrap 3, but a version a few month old) – mxro Jan 09 '14 at 03:46
-
9
-
1@DHlavaty can you explain the difference between using .img-responsive AND .center-block and just using .img-responsive with an auto margin like the accepted answer suggests? is one method more robust? – user137717 Mar 16 '15 at 22:57
-
1You might not want to use this if your image is supposed to be clickable. That is, if it is wrapped inside a ` ` pair, the clickable area will get expanded to the full width of the enclosing container which might be quite a bit larger than your image. This may confuse users who click on what they think is "empty" space. – CXJ Aug 01 '17 at 00:23
If you're using Bootstrap v3.0.1 or greater, you should use this solution instead. It doesn't override Bootstrap's styles with custom CSS, but instead uses a Bootstrap feature.
My original answer is shown below for posterity
This is a pleasantly easy fix. Because .img-responsive
from Bootstrap already sets display: block
, you can use margin: 0 auto
to center the image:
.product .img-responsive {
margin: 0 auto;
}
-
37It's probably better to create a new class with `margin: 0 auto`, rather than changing .img-responsive. – knite Sep 17 '13 at 21:02
-
4That's a thought, although personally I can't think of anywhere I'd want a responsive image not centered, especially with this design. It all depends on the use case, and what's going to happen in the future – Bojangles Sep 17 '13 at 21:12
-
Please see this answer if you are using Bootstrap v4 or higher: https://stackoverflow.com/a/43293957/4102515 – snorberhuis Aug 06 '18 at 21:07
Add only the class center-block
to an image, this works with Bootstrap 4 as well:
<img src="..." alt="..." class="center-block" />
Note: center-block
works even when img-responsive
is used

- 3,611
- 2
- 36
- 34
Just use .text-center
class if you're using Bootstrap 3.
<div class="text-center">
<img src="..." alt="..."/>
</div>
Note: This doesn't work with img-responsive

- 1,149
- 3
- 15
- 30
-
1
-
For img-responsive add img-center (tested on 3.3.6) for those that might see this on google searches – DardanM Jun 12 '16 at 23:19
This should center the image and make it responsive.
<img src="..." class="img-responsive" style="margin:0 auto;"/>

- 8,637
- 2
- 54
- 49
I would suggest a more "abstract" classification. Add a new class "img-center" which can be used in combination with .img-responsive class:
// Center responsive images
.img-responsive.img-center {
margin: 0 auto;
}

- 2,966
- 3
- 30
- 33
Simply put all the images thumbnails inside a row/col divs like this:
<div class="row text-center">
<div class="col-12">
# your images here...
</div>
</div>
and everything will work fine!

- 713
- 14
- 26
You can use property of d-block here or you can use a parent div with property 'text-center' in bootstrap or 'text-align: center' in css.
Image by default is displayed as inline-block, you need to display it as block in order to center it with .mx-auto. This can be done with built-in .d-block:
<div>
<img class="mx-auto d-block" src="...">
</div>
Or leave it as inline-block and wrapped it in a div with .text-center:
<div class="text-center">
<img src="...">
</div>

- 312
- 1
- 3
- 11
You can still work with img-responsive
without impacting other images with this style class.
You can precede this tag with the section id/ div id/class to define a order within which this img
is nested. This custom img-responsive
will work only in that area.
Suppose you have a HTML area defined as:
<section id="work">
<div class="container">
<div class="row">
<img class="img-responsive" src="some_image.jpg">
</div>
</div>
</section>
Then, your CSS can be:
section#work .img-responsive{
margin: 0 auto;
}
Note: This answer is in relation to the potential impact of altering img-responsive
as a whole. Of course, center-block
is the simplest solution.

- 1,593
- 20
- 35
-
This answer is under appreciated, wrapping the image in the row class is crucial in centering the image when there is a text under the image. – Glenn Plas Jul 07 '16 at 12:13
Try this code it will work for small icons too with bootstrap 4 because there is no center-block class is bootstrap 4 so try this method it will be helpful. You can change the position of the image by setting the .col-md-12
to .col-md-8
or .col-md-4
, it's upto you.
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="text-xs-center text-lg-center">
<img src="" class="img-thumbnail">
</div>
</div>
</div>
</div>
Try this:
.img-responsive{
display: block;
height: auto;
max-width: 100%;
margin:0 auto;
}
.Image{
background:#ccc;
padding:30px;
}
<div class="Image">
<img src="http://minisoft.com.bd/uploads/ourteam/rafiq.jpg" class="img-responsive" title="Rafique" alt="Rafique">
</div>

- 931
- 11
- 14
@media (max-width: 767px) {
img {
display: table;
margin: 0 auto;
}
}

- 45
- 2
-
4Why `display: table`? It's already `display: block` which is enough to get `margin: 0 auto` to work – Bojangles Aug 27 '13 at 10:29
To add to the answers already given, having the img-responsive
in combination with img-thumbnail
will set display: block
to display: inline block
.

- 781
- 5
- 13
<div class="col-md-12 text-center">
<img class="img-responsive tocenter" />
</div>
.
<style>
.tocenter {
margin:0 auto;
display: inline;
}
</style>

- 5,316
- 3
- 40
- 50
<div class="text-align" style="text-align: center; ">
<img class="img-responsive" style="margin: auto;" alt="" src="images/x.png ?>">
</div>
you can try this.

- 151
- 9
You can fix it with defining margin:0 auto
or you can use col-md-offset also
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<style>
.img-responsive{
margin:0 auto;
}
</style>
<body>
<div class="container">
<h2>Image</h2>
<div class="row">
<div class="col-md-12">
<p>The .img-responsive class makes the image scale nicely to the parent element (resize the browser window to see the effect):</p>
<img src="http://www.w3schools.com/bootstrap/cinqueterre.jpg" class="img-responsive" alt="Cinque Terre" width="304" height="236">
</div>
</div>
</div>
</body>
</html>

- 1
- 1

- 2,622
- 2
- 19
- 26
The more exact way applied to all Booostrap objects using standard classes only would be to not set top and bottom margins (as image can inherit these from parent), so I am always using:
.text-center .img-responsive {
margin-left: auto;
margin-right: auto;
}
I have also made a Gist for that, so if any changes will apply because of any bugs, update version will be always here: https://gist.github.com/jdrda/09a38bf152dd6a8aff4151c58679cc66

- 43
- 10
So far the best solution to accept seems to be <img class="center-block" ... />
. But no one has mentioned how center-block
works.
Take Bootstrap v3.3.6 for example:
.center-block {
display: block;
margin-right: auto;
margin-left: auto;
}
The default value of dispaly
for <img>
is inline
. Value block
will display an element as a block element (like <p>
). It starts on a new line, and takes up the whole width. In this way, the two margin settings let the image stay in the middle horizontally.

- 3,847
- 30
- 32
2021.09 from a project:
<div class="d-flex" style="height: 60px; width: 60px;">
<img alt="ddd" src="myurl" class="m-auto"/>
</div>

- 3,672
- 2
- 41
- 29