【发布时间】:2016-01-23 05:40:53
【问题描述】:
鉴于以下设计元素,我尝试将图像包含在 html 中,以便可以使用 css 转换(悬停效果)来操作不透明度。
到目前为止,这是我的解决方案:http://codepen.io/roachdesign/pen/VeQKJK/
这里的主要缺点是我使用手动垂直居中(绝对/顶部:40%),这在缩小浏览器时会变得很明显。
在使用绝对定位时,是否可以使用 flexbox 或表格应用垂直居中?
HTML
<div class="badge-container">
<a href="">
<div class="badge">
<img src="http://www.placehold.it/400x400/FF9999">
<h2><strong>Cumberland</strong>County</h2>
</div>
</a>
<a href="">
<div class="badge">
<img src="http://www.placehold.it/400x400/99FF99">
<h2><strong>Dauphin</strong>County</h2>
</div>
</a>
<a href="">
<div class="badge">
<img src="http://www.placehold.it/400x400/9999FF">
<h2><strong>Lancaster</strong>County</h2>
</div>
</a>
<a href="">
<div class="badge">
<img src="http://www.placehold.it/400x400/9F9F99">
<h2><strong>Lebanon</strong>County</h2>
</div>
</a>
<a href="">
<div class="badge">
<img src="http://www.placehold.it/400x400">
<h2><strong>York</strong>County</h2>
</div>
</a>
</div>
SCSS
.badge-container {display:flex; flex-direction:row;
.badge {position:relative;}
h2 {position:absolute;
top:36%;
left:0;
right:0;
text-align:center;
strong {display:block;}
}
}
img {max-width:100%;}
【问题讨论】:
-
我建议看看this answer here。它涵盖了垂直/水平居中文本的多种不同方法。
-
对于图像上的文字,请尝试以下解决方案:geekgirllife.com/…
-
@JoshCrozier 我尝试了每一个,但边缘/翻译方法,当然它在列表中排名第一,哈哈