html - 将 2 个背景图像应用到来自不同 CSS 类的一个 HTML 元素
问题描述
我将 2 个 CSS 类应用于div
在线示例:
<div class="img1 img2">
</div>
使用 LESS,我尝试将 2 个背景图像应用于div
每个类中的一个:
div {
border: 1px solid red;
height: 200px;
&.img1 {
background+: url('https://via.placeholder.com/100/09f/fff') no-repeat left top;
} // img1
&.img2 {
background+: url('https://via.placeholder.com/100/09f/fff') no-repeat right bottom;
} // img2
}
但只应用了第二个背景。
Less的合并功能不就是为了这个吗?
解决方案
这是一种将多个背景包含到单个元素中的方法: https ://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Backgrounds_and_Borders/Using_multiple_backgrounds
.multi-bg-example {
width: 100px;
height: 400px;
background-image: url('https://via.placeholder.com/100/09f/fff'),
url('https://via.placeholder.com/100/09f/fff');
background-repeat: no-repeat,
no-repeat;
background-position: bottom left,
top right;
}
<div class="multi-bg-example" />
所以你一个一个设置道具,它们适用于相应的背景。
推荐阅读
- java - 未映射扩展 MongoRepository 的存储库的 REST 端点
- excel - VBA 中的 FIND 函数 - 对象不支持此属性或方法
- php - 按钮内的 HTML href(如 chrome 标签) 1 个按钮中有 2 个不同的点击功能
- navigation - Here-Maps Android SDK 没有 WaypointParameterType 添加为航点
- python - 如果熊猫中两个日期之间的日期,则查找每个 ID 的值总和
- azure - Azure 自定义视觉:是否可以在自定义视觉环境中存储图像元数据(不是标签)?
- windows - 为什么flutter.dev无法访问?
- apache - 如何针对安全漏洞强化 Apache
- javascript - 在 forEach 中查找特定的 div
- java - 将浮点值格式化为特定格式 - Java 与 C# 数字格式