css - 在 Div 中的伪元素中居中内容图像
问题描述
我在 :before 元素中有一个图像,并且内容图像不会居中。
我试着做
width: 314px;
height: 176px;
display: inline-block;
text-align: center;
margin-left: 50%;
transform: translate(-50%, 0);
但是此代码并未集中在所有设备上...
CSS
.bold_option_dropdown:before {
content: url(https://cdn.shopify.com/s/files/1/1595/2013/files/ENVISIONLEGGINGS-optii.png?3273);
display:block;
text-align: center;
width: 406px;
}
图像只是以它的父 div 为中心...不居中图像
解决方案
您可以在伪元素中添加图像作为背景
.bold_option_dropdown:before {
background: center center no-repeat;
background-size: contain;
background-image: url(https://cdn.shopify.com/s/files/1/1595/2013/files/ENVISIONLEGGINGS-optii.png?3273);
display:block;
width: 406px;
height: 406px;
}
推荐阅读
- java - java.lang.NoSuchFieldError dynamoDB android timeoffset
- vue.js - Vue路由器和setTimeout
- python - 为什么 Scipy 的 Convex Hull 算法会找到这么多共享同一点的单纯形?
- c# - 来自 db 的 C# Web 过滤器列表,具有多个参数作为数组
- dictionary - 地图功能中的“地图”是什么意思?
- flask - 如何将图像返回到img src
- javascript - 检测双击类型=“数字”的输入,但在单击微调器(箭头)时不检测
- c# - 在发布模式下编译错误 CS0246 找不到类型或命名空间名称“Dapper”
- android - 在 Kotlin 中单击当前图像时显示另一张图像
- python - 如何比较熊猫中的两个数据框?