css - 我可以在 CSS 中同时声明 2 个背景图像吗?

标签 css

我想要 2 个随机背景图片用于我的 <body> 的顶部和底部.我的页面顶部有 4 个潜在背景图像,底部有 4 个。我将在我的 html 中使用 php 中的函数来选择随机顶部图像类和随机底部图像类。

我知道我能做到: background-image:url(/images/background/background-top-0.jpg),url(/images/background/background-bottom-0.jpg) ;

有没有办法将第二个背景图像添加到已声明的背景图像中?理想情况下,我想:

<body class="bg1 bg2">....</body>



.bg1{
    background-image:url(/images/background/background-top-0.jpg);
}
.bg2{
    background-image:inherit, url(/images/background/background-bottom-0.jpg);
}

目前我的 CSS 看起来像:

.bg1.bg2{
    background-image:url(/images/background/background-top-0.jpg),url(/images/background/background-bottom-0.jpg) ;
    min-height: 100%;
    background-size: contain;
    width:100%;
    background-position: center 110px, center bottom 80px;
    background-repeat: no-repeat;
    background-color: #128bd0;
}

最佳答案

我们可以将一张背景图片应用到 <html>和一个到<body> . 背景类可以用在 html 或正文上

例子

html,
body {
  height: 100%;
  margin: 0;
}
.bg1 {
  background: url(http://www.placehold.it/500X50/FF0000) no-repeat;
}
.bg2 {
  background: url(http://www.placehold.it/500X50/FF9900) no-repeat;
}
html.bg {
  background-position: center top;
  background-color: #EEE;
}
body.bg {
  background-position: center bottom;
}
<html class="bg bg1">

<body class="bg bg2">

</body>

</html>

关于css - 我可以在 CSS 中同时声明 2 个背景图像吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27837582/

相关文章:

javascript - 将单选按钮的值添加到 2 个不同的列中

html - 我怎样才能根据浏览器的宽度调整所有对象的大小?

php - 用户格式化文本的文本缩进

html - 如何在鼠标悬停时修改和编辑图像颜色?

html - Bootstrap 导航栏确实切换

javascript - 如何使div网格相互忽略

html - IE 11 和其他可能缺少的功能?

html - 从较低的媒体查询继承属性

html - 将旋转的 div 与另一个 div 对齐

css - LESS 将空格放在错误的位置