使用background-clip属性将背景限制在before伪元素内部,并将background-origin属性设置为padding-box以使背景图片放置在padding区域内。
示例代码:
.element:before {
content: "";
display: block;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: -1;
background-image: url("image.png");
background-repeat: no-repeat;
background-size: cover;
background-clip: content-box;
background-origin: padding-box;
}