0

I am trying to center an image inside a div in the exact middle - horizontally and vertically, but this is not working for me:

#col_as_table
{
    float:left;
    position:relative;
    width:14%;
    min-height:100px;
    height:auto;
    display: table;
    display: table-cell;
    text-align: center;
    vertical-align: middle;
}

HTML

<div id="col_as_table"><img src="test.png"></div>
disinfor
  • 10,865
  • 2
  • 33
  • 44
user3765800
  • 27
  • 1
  • 8
  • 2
    And the rest of the code? could be helpfull to find the problem.. a jsfiddle example maybe – gmo Jul 10 '14 at 20:53
  • what kind of HTML element is the parent? – Adjit Jul 10 '14 at 20:54
  • I have only that and inside i put the image – user3765800 Jul 10 '14 at 20:55
  • You put the image inside *what*? Not your CSS, I'd imagine. – Andrew Jul 10 '14 at 20:56
  • Remove `float:left;` Why would you have float: left if you want to center the image ? – Ani Jul 10 '14 at 20:56
  • The div go to the left i have 2 cols and this it´s the col of left , only want show the image in the center and in the middle , i put the code i have – user3765800 Jul 10 '14 at 20:57
  • This is a possible duplicate of: http://stackoverflow.com/questions/7273338/how-to-vertically-align-an-image-inside-div – disinfor Jul 10 '14 at 21:03
  • You likely will not get any suitable answer untill someone makes the good guess. Just bring a fiddle or a codepen, witj 2 cols and some fake image and fake content with , eventually, a sketch of what you try to do. As said , there many ways to center content – G-Cyrillus Jul 10 '14 at 21:03
  • possible duplicate of [How to vertically center image inside div](http://stackoverflow.com/questions/2237636/how-to-vertically-center-image-inside-div) – Paulie_D Jul 11 '14 at 12:49

4 Answers4

3

Give this a shot. You'll need an extra div.

Fiddle!

HTML:

<div class="divTable">
    <div class="innerDivTable">
        <img src="test.png"/>
    </div>
</div>

CSS:

.divTable {
    width: 200px; /* purely for demonstration */
    height: 200px; /* purely for demonstration */
    display: table;
    border: 1px solid black;
}

.innerDivTable {
    display: table-cell;
    vertical-align: middle;
    text-align: center;
}
SethGunnells
  • 1,269
  • 1
  • 12
  • 19
0

Fiddle: http://jsfiddle.net/4mYj3/1/

CSS:

#col_as_table
{
   position:relative;
   margin: auto;
   text-align: center;
   vertical-align: middle;
   display: table;
   height:500px;
}
img{
  position: absolute;
  top: 50%; 
  left: 0; 
  bottom: 0; 
  right: 0;
}
Ani
  • 4,473
  • 4
  • 26
  • 31
0

I wrote a Pen some time ago for the same purpose. It shows 3 methods on how you can center an image vertically as well as horizontally.

I'd personally use absolute positioning or table cell depending upon the situation.

#div3{
    display:table-cell;
    vertical-align:middle;
}

#div1 img{
    height:100px;
    position:absolute;
    top:50%;
    left:50%;
    -webkit-transform:translateY(-50%) translateX(-50%);
}
Lokesh Suthar
  • 3,201
  • 1
  • 16
  • 28
-1

Here's a fiddle:

http://jsfiddle.net/C4DC7/

There are many ways to accomplish this. My example expands on the idea that you can afford your images to expand the parent div element.

Onion
  • 1,714
  • 1
  • 23
  • 42
  • This answer isn't correct. It fails in that the image is never in the exact center as the OP requested. – disinfor Jul 10 '14 at 20:59
  • Yes also i can do that with padding but i want get the same as when i use for example in tables valign , i read time ago one way for do this in css with table but i can find – user3765800 Jul 10 '14 at 21:00
  • The question actually doesn't really deserve an answer. @disinfor I would not downvote answer but the question, maybe this is the right size of the picture of the op :) – G-Cyrillus Jul 10 '14 at 21:07
  • @GCyrillus Fair enough! I think I can remove my down vote on the answer in a few minutes. – disinfor Jul 10 '14 at 21:09