简体   繁体   English

带有背景重复的 div:无重复; 重复

[英]div with background-repeat: no-repeat; repeats

I've got a div with a CSS which states background-repeat: no-repeat;我有一个带有 CSS 的 div,它指出background-repeat: no-repeat; , but if the div is bigger then the image, the image starts to repeat itself. ,但如果 div 比图像大,图像就会开始重复。

Any ideas, suggestions etc are welcome.欢迎任何想法、建议等。 Thank you谢谢

.background-image{
    position: fixed;
    top:0px;
    background-repeat: no-repeat;
    overflow: hidden;
    z-index: -1;    
    background-size: 1700px, 819px; 
    background: url('../assets/kafa.jpg');  
    border: 0px;
    background-position: -1050px -550px;
    width: 100%;
    height: 100%;
}

https://jsfiddle.net/ohymeft0/ https://jsfiddle.net/ohymeft0/

You are using the background shorthand property which sets many values, including background-repeat .您正在使用设置许多值的background速记属性,包括background-repeat Either change background to background-image , or place your background-repeat property after the background rule.要么将background更改为background-image ,要么将background-repeat属性放在background规则之后。

尝试将background更改为background-image ,这应该可以工作

Try this:尝试这个:

.background-image{
    position: fixed;
    top:0px;
    overflow: hidden;
    z-index: -1;    
    background: url('https://cdn.pixabay.com/photo/2017/02/20/18/03/cat-2083492__340.jpg') no-repeat;   
    width: 100%;
    height: 100%;
}

As @chriskirknielsen said, your overriding your properties.正如@chriskirknielsen 所说,你覆盖了你的属性。 This solves that.这就解决了。

The property background is shorthand for several CSS background properties (see https://developer.mozilla.org/en-US/docs/Web/CSS/background ).属性background是几个 CSS 背景属性的简写(参见https://developer.mozilla.org/en-US/docs/Web/CSS/background )。

Where you've set that after background-repeat: no-repeat it overrides the previous property and uses its default repeat style.您在background-repeat: no-repeat之后设置的位置会覆盖上一个属性并使用其默认重复样式。

If you replace background: url('https://cdn.pixabay.com/photo/2017/02/20/18/03/cat-2083492__340.jpg');如果更换background: url('https://cdn.pixabay.com/photo/2017/02/20/18/03/cat-2083492__340.jpg'); in your example with background-image (see the amended code snippet below) it should give the result you're expecting.在您带有background-image示例中(请参阅下面的修改后的代码片段),它应该会给出您期望的结果。

 .background-image { position: fixed; top: 0px; background-repeat: no-repeat; overflow: hidden; z-index: -1; background-size: 1700px, 819px; background-image: url('https://cdn.pixabay.com/photo/2017/02/20/18/03/cat-2083492__340.jpg'); border: 0px; background-position: -1050px -550px; width: 100%; height: 100%; }
 <div class="background-image"></div>

  .background-image{ 
    position: fixed;
    top:0px;
    overflow: hidden;
    z-index: -1;
    background: url('https://cdn.pixabay.com/photo/2017/02/20/18/03/cat-2083492__340.jpg')    no-repeat;
    width: 100%;
    height: 100%;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM