4

I'm trying to center horizontally an image in a div, and i don't understand why it isn't working... I've centered images so many times, and this time i just don't understand... Here is what i've tried...

CSS

#cabeca{
position: relative;
float: left;
width: 100%;
height: auto;
min-height: 290px;
margin: auto;
}

.cabecasize {
width:100%;
min-width: 968px;
height:auto;
min-height: 290px;
overflow:hidden;
left:0;
right: 0;
margin: auto;
}

.cabecafundo{
position: absolute;
width: 100vw;
left: calc(-50vw + 50%);
background-color: black;
height: 100%;
top:0;
z-index: -1;
}

.overcabeca{
overflow: hidden;
display: block;
margin: auto;
text-align: center;
}

HTML

<div id="cabeca">
    <div class="overcabeca">
        <img src="images/av61.png" alt="av61" class="cabecasize" />
    </div>
    <div class="cabecafundo">
    </div>
</div>  

Any ideas??

André Mata
  • 383
  • 1
  • 5
  • 15
  • everything is working the way it should... except the image, it just doesn't want to stay centered... It is already in a centered div, but when the page shrinks, an overflow happens and the img stay fixed, when it should go with the overflow and keep centered... thank for the help everybody! – André Mata Oct 08 '15 at 09:59
  • Why are you giving the image a width of 100%? Because you're basically telling the image to fill it's parent – Steyn Oct 08 '15 at 09:59
  • @SteynvanEsveld I need it to fill the parent, but i also have min-width, so that it stays the size I want... the size right now is perfect, i just need it to be in the center of the div (when the parent is smaller than 968px) – André Mata Oct 08 '15 at 10:03
  • Possible duplicate of [Horizontally center a div in a div](http://stackoverflow.com/questions/114543/horizontally-center-a-div-in-a-div) – Steyn Oct 08 '15 at 10:04
  • @SteynvanEsveld No the same... the solution there work for smaller inner divs... my inner div when I need it to be centered is bigger than the parent with overflow happening. – André Mata Oct 08 '15 at 10:06

3 Answers3

3

Check the below code, I hope it will helps you.

.wrap{
  position: relative;
  height: 100vh;
  width: 100vw;
}
.midImg{
  margin: auto;  
  position: absolute;
  left:0;
  right: 0;
  top: 0;
  bottom: 0;
  height: 150px;
  width: 150px;
}  
<div class="wrap">
  <img src="https://pbs.twimg.com/profile_images/473506797462896640/_M0JJ0v8.png" alt="av61" class="midImg" />
</div>
Mukul Kant
  • 7,074
  • 5
  • 38
  • 53
1

you can simply add this css to your image

img{
   margin-left: auto;
   margin-right: auto;
}
treb
  • 478
  • 1
  • 7
  • 16
0

.image {
  text-align: center;
  background: #c0c0c0;
  border: #a0a0a0 solid 1px;

}

.image:before {
  content: '\200B';
/*   content: '';
  margin-left: -0.25em; */
  display: inline-block;
  height: 100%; 
  vertical-align: middle;
 }

.centered {
  display: inline-block;
  vertical-align: middle;
  width: 300px;
  padding: 10px 15px;
   }

<div class="image" style="height: 600px;">
    <div class="centered">
        <img src="download.jpg"  alt="" />
    </div>
</div>
Razia sultana
  • 2,168
  • 3
  • 15
  • 20