简体   繁体   English

CSS repeat-x但只需要图像中的一部分

[英]CSS repeat-x BUT only a part I want in an image

I used background-size: cover for mobile screen sizes. 我使用了background-size: cover用于移动屏幕尺寸的background-size: cover

But, for desktop, if I use background-size: cover , I can't see the cat in my image. 但是,对于台式机,如果我使用background-size: cover ,则在图像中看不到猫。 Because, the original size of my image is that the height is much longer than the width. 因为,我图像的原始大小是高度远大于宽度。

So, I would like to repeat only a partial part of my image that a cat doesn't appear. 因此,我只想重复图像中没有出现猫的一部分。 But, if I use background-size: contain & background-repeat: repeat-x , what I see is the following: 但是,如果我使用background-size: containbackground-repeat: repeat-x ,则看到的是以下内容:

当前 (The cat in the image is appeared three times.) (图像中的猫出现了3次。)

What I want to do is something like the following: 我想要做的是如下所示: 我想要的是 (Repeat only the part on the left & right that cat doesn't appear.) (仅重复左右不出现猫的部分。)

I googled quite a bit & read several posts in StackOverflow. 我在Google上搜索了很多,并阅读了StackOverflow中的几篇文章。 But, I couldn't find an answer I want. 但是,我找不到想要的答案。 If you could give me any advice, I'd really appreciate it! 如果您能给我任何建议,我将不胜感激!

* Here are the links for the images: *以下是图片的链接:

- [1st] https://postimg.cc/image/6pku8wgcr/ -[第一] https://postimg.cc/image/6pku8wgcr/

- [2nd] https://postimg.cc/image/rz8gjr4d7/ -[第二] https://postimg.cc/image/rz8gjr4d7/

- [Original Cat Background] https://postimg.cc/image/hcenege97/ -[原始猫背景] https://postimg.cc/image/hcenege97/

And idea is to use multiple background above each other to simulate such effect: 并且想法是使用彼此之上的多个背景来模拟这种效果:

 body { margin:0; min-height:500px; height:100vh; background: /*the main background*/ url(https://picsum.photos/3744/5616?image=742) center, /*repeat the left part*/ url(https://picsum.photos/3744/5616?image=742) 40% 50%, url(https://picsum.photos/3744/5616?image=742) 30% 50%, url(https://picsum.photos/3744/5616?image=742) 20% 50%, url(https://picsum.photos/3744/5616?image=742) 10% 50%, url(https://picsum.photos/3744/5616?image=742) 0% 50%, /*repeat the right part*/ url(https://picsum.photos/3744/5616?image=742) 60% 50%, url(https://picsum.photos/3744/5616?image=742) 70% 50%, url(https://picsum.photos/3744/5616?image=742) 80% 50%, url(https://picsum.photos/3744/5616?image=742) 90% 50%, url(https://picsum.photos/3744/5616?image=742) 100% 50%; background-repeat:no-repeat; background-size:contain; } 

Depending on the image you can adjust the percentage and the number of backgrounds in order to control the repetition. 您可以根据图像调整背景的百分比和数量,以控制重复。 Here is an improvement of the first code to hide a non-needed part on the left by reducing the step of the repeat: 这是对第一个代码的改进,它通过减少重复步骤来隐藏左侧不需要的部分:

Run and compare this and the previous code on full screen 在全屏上运行并与之前的代码进行比较

 body { margin:0; min-height:500px; height:100vh; background: /*the main background*/ url(https://picsum.photos/3744/5616?image=742) center, /*repeat the left part more than the right*/ url(https://picsum.photos/3744/5616?image=742) 45% 50%, url(https://picsum.photos/3744/5616?image=742) 40% 50%, url(https://picsum.photos/3744/5616?image=742) 35% 50%, url(https://picsum.photos/3744/5616?image=742) 30% 50%, url(https://picsum.photos/3744/5616?image=742) 25% 50%, url(https://picsum.photos/3744/5616?image=742) 20% 50%, url(https://picsum.photos/3744/5616?image=742) 15% 50%, url(https://picsum.photos/3744/5616?image=742) 10% 50%, url(https://picsum.photos/3744/5616?image=742) 5% 50%, url(https://picsum.photos/3744/5616?image=742) 0% 50%, /*repeat the right part*/ url(https://picsum.photos/3744/5616?image=742) 60% 50%, url(https://picsum.photos/3744/5616?image=742) 70% 50%, url(https://picsum.photos/3744/5616?image=742) 80% 50%, url(https://picsum.photos/3744/5616?image=742) 90% 50%, url(https://picsum.photos/3744/5616?image=742) 100% 50%; background-repeat:no-repeat; background-size:contain; } 

UPDATE UPDATE

Here is the code with your image: 这是带有您的图片的代码:

 body { margin:0; min-height:500px; height:100vh; background: /*the main background*/ url(https://i.stack.imgur.com/76q2w.png) center, /*repeat the left part*/ url(https://i.stack.imgur.com/76q2w.png) 40% 50%, url(https://i.stack.imgur.com/76q2w.png) 30% 50%, url(https://i.stack.imgur.com/76q2w.png) 20% 50%, url(https://i.stack.imgur.com/76q2w.png) 10% 50%, url(https://i.stack.imgur.com/76q2w.png) 0% 50%, /*repeat the right part*/ url(https://i.stack.imgur.com/76q2w.png) 60% 50%, url(https://i.stack.imgur.com/76q2w.png) 70% 50%, url(https://i.stack.imgur.com/76q2w.png) 80% 50%, url(https://i.stack.imgur.com/76q2w.png) 90% 50%, url(https://i.stack.imgur.com/76q2w.png) 100% 50%; background-repeat:no-repeat; background-size:contain; } 

A gradient could also be used to paint the area where image is not supposed to repeat . 也可以使用渐变来绘制不应重复图像的区域。

 html { min-height:100vh; background-image: url(https://s25.postimg.cc/kw0l49gz3/original.png), linear-gradient(to top, rgb(116, 164, 61) 27.5%, gray 27.5%, gray 27.6%, rgb(67, 97, 32) 27.6%); background-position:center center; background-repeat:no-repeat; background-size: auto 100%; } 

part of the image itself can be used and stretch if it can match. 如果图像本身可以匹配,则可以使用图像的一部分并进行拉伸。

 html { min-height:100vh; background-image: url(https://s25.postimg.cc/kw0l49gz3/original.png), url(https://s25.postimg.cc/kw0l49gz3/original.png ); background-position:center center, 10% center ; background-repeat:no-repeat; background-size: auto 100%, 2000% 100%;/* stretch part of image behind the other one with a hudge value*/ } 

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

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