gpt4 book ai didi

html - 如何在背景颜色之上显示背景图像?

转载 作者:行者123 更新时间:2023-12-03 23:04:08 26 4
gpt4 key购买 nike

我想在背景颜色之上显示背景图像。难点在于背景图片必须在两个框以上,不能在框之间进行剪切。我找到了一种通过在每个框中定义背景图像来在颜色上显示图像的方法,但它会剪切图像。
这是我的原始代码,其中背景颜色位于背景图像上方并将其隐藏:

.wrapper {
background-image: url(https://i.ibb.co/gD7vNSs/fleur-cerf.png), url(https://i.ibb.co/Nn2Wt9Q/fleur-renard.png);
background-position: top left, top right;
background-repeat: repeat-y;
}
.box1 {
padding: 50px;
padding-left: 100px;
background: #f6f7f9;
}

.box2 {
padding: 50px;
padding-left: 100px;
background: #e89d8c;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="test.css">
</head>
<body>
<div class="wrapper">
<div class="box1">
<h1>This is a heading</h1>
<p>
This is paragraph
</p>
</div>
<div class="box2">
<h1>This is a heading</h1>
<p>
This is paragraph
</p>
</div>
</div>
</body>
</html>

任何的想法?

最佳答案

一个伪元素和很少的 z-index 可以做到:

.wrapper {
position:relative;
}

.wrapper::after {
content:"";
position:absolute;
top:0;
left:0;
right:0;
bottom:0;
background-image: url(https://i.ibb.co/gD7vNSs/fleur-cerf.png), url(https://i.ibb.co/Nn2Wt9Q/fleur-renard.png);
background-position: top left, top right;
background-repeat: repeat-y;
}

.box1 {
padding: 50px;
padding-left: 100px;
background: #f6f7f9;
}

.box2 {
padding: 50px;
padding-left: 100px;
background: #e89d8c;
}
/* to make the content always on the top */
.box1 *,
.box2 * {
position:relative;
z-index:2;
}
<div class="wrapper">
<div class="box1">
<h1>This is a heading</h1>
<p>
This is paragraph
</p>
</div>
<div class="box2">
<h1>This is a heading</h1>
<p>
This is paragraph
</p>
</div>
</div>

关于html - 如何在背景颜色之上显示背景图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63731688/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com