Center image vertically within a div

Asked

Viewed 97,880 times

12

I need to center images inside a div. I can do it horizontally with text-align:center;, but I want to centralize vertically. I tried to vertical-align: middle; and display: table-cell; and it didn’t work.

Is there any other solution? If possible, only using CSS.

  • I predict that this will be a very common question in the future, so this question is a good candidate to be protected.

  • You could add some more information. For example, does your div have fixed width and height?. Some snippets of html/css would also be good.

  • Is this question resolved? If the answer that solves it is accepted.

11 answers

11

That should solve your problem:

div img {
    display: block;
    margin-left: auto;
    margin-right: auto;
}
  • 2

    Dei voteup because that is the right way to do it, I know it is obvious, but it will only work if the width div is larger than the image.

  • 1

    The horizontal alignment is really the best way to do, but vertiacal alignment has to be in 2 level, one level with 'display:table;' and another level with 'displau:table-Cell' then can be used the 'vertical-align: Middle;'

9


If Internet Explorer 8 support is not required or the project is more flexible, you can also use the following code:

.vertical-align {
    position: relative;
    top: 50%;
    transform: translateY(-50%); /* Adicionar os prefixos dos navegadores */
}

Source - http://zerosixthree.se/vertical-align-anything-with-just-3-lines-of-css/

It is also worth remembering that the use of display:table does not work with float.

Another solution, for those who need to support under Internet Explorer 8:

.vertical-align {
    display: block;
}

.vertical-align > div {
    display: block;
    position: relative;
    margin-top: inherit;
    clear: ~'expression(style.marginTop = "" + (offsetHeight < parentNode.offsetHeight ? parseInt((parentNode.offsetHeight - offsetHeight) / 2) + "px" : "0"), style.clear = "none", 0)';
}

Another solution if Internet Explorer 6 support is required:

.vertical-align {
    position: relative;
}

.vertical-align > div {
    position: absolute;
    top: 50%;
}

.vertical-align > div > div {
    position: relative;
    top: -50%;
}

Source - http://css-tricks.com/vertically-center-multi-lined-text/

8

Although it has been some time since this question was asked; I will be showing an easy way to center images using flex-box, to help those in need.

Enter a class in div and tag img within the div, as shown below:

<div class="centralizarImagem">
    <img src="algumaImagem.jpg" alt="Alguma Imagem" width="400" height="190" />
</div>

In the CSS insert:

.centralizarImagem { 
    display:         flex;
    display: -webkit-flex; /* Garante compatibilidade com navegador Safari. */

    justify-content: center;
    align-items: center;
}

If you want to study more about flex-box, on the website CSS-Tricks a more complete guide is available.

  • The only answer/suggestion that worked for me (March/2020)

5

What’s missing is the display:table; in the container of div who is with display: table-cell; Putting this will work perfectly. take the example: CSS:

#container {
 width: 200px; 
 height: 150px; 
 border: 1px solid #c30;
 background: #ffe;
 position: relative;
 display:table; 
 }
#container p {
 *position: absolute; 
 top: 50%; 
 display: table-cell; 
 vertical-align: middle;
 }
#container span {
 display:block; 
 *position: relative; 
 top: -50%;
 }

HTML:

<div id="container">
    <p><span>Texto no meio da DIV</span></p>
</div>

4

The simplest way I know and I’m sure it doesn’t fail is this

#container {
  position: relative;
  width: 200px;
  height: 200px;
}
#container img {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  margin: auto;
  max-width: 100%;
  max-height: 100%;
}

This is a cross-browser way of making the independent alignment of the image to be paisegem or portrait.

2

To center an image within a div, you need to first put the div with position: relative and the image with position: absolute.

After that,transform: translate(-50%,-50%), along with top 50% and left 50%, as can be seen in the example below:

div{  
    position: relative;  
    width: 170px; // Largura da DIV  
    height: 155px; // Altura da DIV  
    overflow: hidden; // Para que as partes da imagem maior que as widht e height não apareçam  
}

img{  
    position: absolute;  
    overflow: hidden;  
    top: 50%;  
    left: 50%;  
    -webkit-transform: translate(-50%,-50%);  
    -moz-transform: translate(-50%,-50%);  
    -ms-transform: translate(-50%,-50%);  
    -o-transform: translate(-50%,-50%);  
    transform: translate(-50%,-50%);  
}

1

Try with line-height:.

Example:

line-height: 40px;

0

  • This will also work with images of different sizes?

  • 1

    Yes, just set the image size and put the negative margin, for example if set width: 500px and height: 100px. Then the margin-left must be half negative width in the magin-left case: -250px and the margin-top must be half negative height size in the margin-top case: -50px

  • Buddy, when I use negative margins, I feel like I’m doing something wrong, hehe.

  • 1

    All right, I just mentioned one of N’s ways of doing what you want.

0

I believe that with display:grid be the easy way! I left the comments in CSS

OBS: Works from IE10 forward! https://www.w3.org/TR/css-grid-1/

html, body {
    width: 100%;
    height: 100%;
    margin: 0;
}
.grid {
    width: 100%;
    height: 100%;
    display: grid;
    place-items: center; /* centraliza na vertical e horizontal */
    /* align-items: center; se quiser centralizar apenas na vertical */
}
.centro {
    width: 100px;
    height: 100px;
    background-color: aqua;
}
<div class="grid">
    <div class="centro">margin: auto;</div>
</div>

0

If your div and your image have a heigth set, you can do this:

.img { 
 height: 200px; /* Altura da imagem */
 position:relative;
 margin-top: 50%;
 top: -100px; /* Negatico da metade da altura da imagem */
}

0

Thank you all for your help.

I got it this way.

.produtosListagemImagemFix {
display: table-cell;
height: 286px;
vertical-align: middle;
width: 253px;}

So it worked. 100% aligned!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.