我有以下带有图片的HTML,并在悬停时显示标题:
<div style="width: 100%; border: 1px solid #cccccc; padding: 20px; overflow: hidden;">
<div class="cap-bot">
<img src="http://appcenter.clickmeeting.com/uploads/2014/05/google-calendar-logo1.gif" alt="" />
<figcaption>CALENDAR EVENTS</figcaption>
</div>
</div>
CSS:
.cap-bot {
display: block;
position: relative;
float: left; /* removing this centers the image */
overflow: hidden;
margin: 0 0 0 0;
text-align: center;
}
figcaption {
width: 100%;
position: absolute;
background: #e55302;
background: rgba(229,83,2,0.90);
color: white;
padding: 10px 0;
opacity: 0;
-webkit-transition: all 0.6s ease;
-moz-transition: all 0.6s ease;
-o-transition: all 0.6s ease;
}
div:hover figcaption {
opacity: 1;
}
.cap-bot:before { bottom: 10px; left: 10px; }
.cap-bot figcaption { left: 0; bottom: -30%;}
.cap-bot:hover figcaption { bottom: 0; }
我想将图像和标题居中显示在图像的顶部,而不是整个DIV。
float: left
仅在图片上显示标题,但删除图片时图片居中,但标题会显示在整个DIV上。
JSFidle:http://jsfiddle.net/69td39ha/3/
请帮我修改代码,使图像居中,图标宽度显示(没有固定尺寸)。
答案 0 :(得分:2)
演示 - http://jsfiddle.net/victor_007/69td39ha/4/
display:inline-block
用于.cap-bot
和父{4}}
移出你的风格
text-align:center
&#13;
.cap {
width: 100%;
border: 1px solid #cccccc;
padding: 20px;
overflow: hidden;
text-align: Center;
}
.cap-bot {
display: inline-block;
position: relative;
overflow: hidden;
margin: 0 0 0 0;
text-align: center;
}
figcaption {
width: 100%;
position: absolute;
background: #e55302;
background: rgba(229, 83, 2, 0.90);
color: white;
padding: 10px 0;
opacity: 0;
-webkit-transition: all 0.6s ease;
-moz-transition: all 0.6s ease;
-o-transition: all 0.6s ease;
}
div:hover figcaption {
opacity: 1;
}
.cap-bot:before {
bottom: 10px;
left: 10px;
}
.cap-bot figcaption {
left: 0;
bottom: -30%;
}
.cap-bot:hover figcaption {
bottom: 0;
}
&#13;