Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

html - center image in a div

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??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...