[英]Vertically center-align a figcaption on an image
到目前为止,我有以下代码:
.portfolio-item figcaption{
position: relative;
top: -40px;
left: -20px;
width: 280px;
background: rgb(52,152,219);
color: white;
display: flex;
flex-direction: row;
justify-content: space-around;
padding: 10px 5px 10px 5px;
}
对于以下HTML:
<!-- PORTFOLIO IMAGE 1 -->
<div class="portfolio-item">
<figure>
<img src="assets/img/portfolio/folio01.jpg" alt="">
<figcaption>
<h5>UI DESIGN</h5>
<a data-toggle="modal" href="#myModal" >Take a Look</a>
</figcaption>
</figure>
</div>
问题是,居中,我使用的是left: -20px;
这绝对不是那么敏感。 任何想法? 谢谢
Flexbox可以做到这一点。 您不需要使用相对(或绝对)定位。
figure
元素成为具有column
direction的flex容器。 align-items: center
图像和标题align-items: center
。 order
属性切换图像和标题的order
。 figure { display: flex; flex-direction: column; align-items: center; } .portfolio-item figcaption { order: -1; /* default value for all flex items is 1 */ display: flex; flex-direction: row; justify-content: space-around; width: 280px; background: rgb(52, 152, 219); color: white; padding: 10px 5px 10px 5px; /* REMOVED position: relative; top: -40px; left: -20px; */ }
<div class="portfolio-item"> <figure> <img src="http://i.imgur.com/60PVLis.png" width="100" height="100" alt=""> <figcaption> <h5>UI DESIGN</h5> <a data-toggle="modal" href="#myModal">Take a Look</a> </figcaption> </figure> </div>
figure { position: relative; height: 400px; border: 1px dashed red; } figure > img { position: absolute; left: 50%; /* horizontal alignment */ transform: translateX(-50%); /* horizontal alignment (fine tuning) */ z-index: -1; /* keep image under figcaption always */ } figure > figcaption { position: absolute; top: 40%; /* vertical alignment */ left: 50%; transform: translateX(-50%); display: flex; flex-direction: row; justify-content: space-around; width: 280px; background: rgb(52, 152, 219); color: white; padding: 10px 5px 10px 5px; }
<div class="portfolio-item"> <figure> <img src="http://i.imgur.com/60PVLis.png" width="200" height="200" alt=""> <figcaption> <h5>UI DESIGN</h5> <a data-toggle="modal" href="#myModal">Take a Look</a> </figcaption> </figure> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.