需求 —— 在一个指定的div(cotainer)内,要实现:
- 图片撑满div
- 文字水平垂直居中
- 文字在图片上方
- 压缩时底下图片能等比例缩小 -> 可设置最小值
实现 —— 直接上代码:
<div class="cotainer">
<img src="@/../static/img/img_manage_logoBg.png" alt="bg">
<div class="textBox">
<p>标题</p>
<a href="" class="btn">按钮</a>
</div>
</div>
.container {
position: relative; /*父级容器相对定位*/
margin: 0 10px;
}
.container img {
/* 图片撑满父级div */
width: 100%;
height: 100%;
}
/* 文字包裹于div + 绝对定位 */
.textBox {
width: 100%;
position: absolute;
/* left: 50%; */
top: 50%;
/* transform: translateX(-50%); */
transform: translateY(-50%);
/* 文字div内用flex布局 */
display: flex;
flex-flow: column nowrap;
justify-content: center; /*垂直方向居中*/
align-items: center;
}
.textBox p {
width: 100%; /*宽度撑满,使用text-align: center水平居中*/
/* 以下根据需要设置 */
height: 32px;
font-size: 32px;
font-family: Microsoft YaHei;
font-weight: 400;
color: #000;
letter-spacing: 1px;
line-height: 32px;
text-align: center;
}
.textBox a {
display: block;
width: 134px; /*宽度固定,flex布局实现水平居中*/
height: 36px;
font-size: 18px;
font-family: Microsoft YaHei;
font-weight: 400;
color: #007BDB;
/* letter-spacing: 1px; */
line-height: 22px;
text-align: center;
text-decoration: none;
border: 1px solid #007BDB;
border-radius: 20px;
padding: 5px;
margin-top: 5%;
}