div中的中心图像

时间:2015-10-08 09:56:34

标签: html css image center

我试图将图像水平居中于div,我不明白为什么它不起作用......我已经多次居中图像了,这个时间我只是不明白...... 这是我尝试过的......

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>  

任何想法??

2 个答案:

答案 0 :(得分:1)

检查以下代码,希望它能帮到你。

&#13;
&#13;
.wrap{
  position: relative;
  height: 100vh;
  width: 100vw;
}
.midImg{
  margin: auto;  
  position: absolute;
  left:0;
  right: 0;
  top: 0;
  bottom: 0;
  height: 150px;
  width: 150px;
}  
&#13;
<div class="wrap">
  <img src="https://pbs.twimg.com/profile_images/473506797462896640/_M0JJ0v8.png" alt="av61" class="midImg" />
</div>
&#13;
&#13;
&#13;

答案 1 :(得分: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>