html - 使图像适合父级并在其上添加铭牌
问题描述
我正在尝试插入一些图像并在它们上面添加铭牌。图像可以是垂直的、水平的和方形的。它们是静态的并手动添加。
我的期望:
- 长图像被缩放以适应最大 750px 宽的父级
- 高图像缩放为不超过 80vh,否则适合父宽度
- 铭牌始终覆盖图像宽度的 100%
它应该是什么样子。蓝色是文章,父级,1000px 宽。红色框架是容器.art
,橙色是img
,黑色是span
纯文本。
到目前为止我所做的。
html:
<div class="art">
<img src="./some/path">
<span>Image name</span>
</div>
CSS(CSS):
.art {
max-width: 75%;
margin: 0 auto;
position: relative;
img {
object-fit: contain;
width: 100%;
max-height: 80vh;
}
span {
box-sizing: border-box;
position: absolute;
left: 0;
width: 100%;
bottom: 1rem;
padding: 1rem;
background-color: rgba(0,0,0,0.85);
}
}
问题:适用于长图像,不适用于高。铭牌贴在图像之外。
解决方案
请检查此代码。这是您的解决方案。
为 Image 父级添加一个 DIV 并为此添加 CSS。
.art {
max-width: 75%;
margin: 0 auto;
position: relative;
text-align:center;
}
.art-container{
display:inline-block;
position:relative;
}
img {
object-fit: cover;
height: 80vh;
max-width:100%;
}
span {
box-sizing: border-box;
position: absolute;
color:white;
left: 0;
width: 100%;
bottom: 1rem;
padding: 1rem;
background-color: rgba(0,0,0,0.85);
}
}
<div class="art">
<div class="art-container">
<img src="https://i.pinimg.com/originals/af/8d/63/af8d63a477078732b79ff9d9fc60873f.jpg">
<span>Image name</span>
</div>
</div>
<!-- https://cdn57.androidauthority.net/wp-content/uploads/2015/11/00-best-backgrounds-and-wallpaper-apps-for-android.jpg -->
推荐阅读
- github - 如何使用 Python 从 github 存储库中采样随机提交
- javascript - 我可以使用 graphql 分页光标作为唯一标识符吗?
- javascript - 如果我返回一个对象,它是不是引用?
- python - 是否可以使用 .split() 在 python 中使用多个分隔符参数?
- google-bigquery - 在欧盟位置找不到数据集(ga_sessions 数据)
- android - 如何从通知中打开 Jetpack 导航片段?
- pip - 部署 Huggingface 模型进行推理 - pytorch-scatter 问题
- vue.js - Nuxt / VueJS 页面在桌面和连接 WiFi 的移动设备上渲染良好,但在 4G/5G 设备上却不行。知道如何解决这个问题吗?
- css - 如何根据子 SVG 内容对齐父 div 高度
- c# - 如何在 Xamarin 的 firebase 中更新用户显示名称?