简体   繁体   English

如何制作背景尺寸:从左侧切割封面?

[英]How to make background-size: cover cut from left?

Consider this simple W3 School background image example, I changed its background-size to cover .考虑这个简单的 W3 School 背景图像示例,我将其background-size更改为cover

https://www.w3schools.com/cssref/tryit.asp?filename=trycss3_background-size https://www.w3schools.com/cssref/tryit.asp?filename=trycss3_background-size

If shrinking the page, you can see that the mountain image starts to cut from right to left.如果缩小页面,你可以看到山的图像开始从右向左切割。 I wonder if I could change this effect to cut from left to right?我想知道我是否可以将此效果更改为从左到右切割? Ie when cutting starts, the left side gets cut off first.即当切割开始时,左侧首先被切断。

You can use background-position: right;您可以使用background-position: right; to set the origin of the background-image to the right side of the container:将背景图像的原点设置在容器的右侧:

 #example1 { border: 2px solid black; padding: 25px; background: url(https://placehold.it/1000x100); background-repeat: no-repeat; background-size: cover; background-position: right; }
 <div id="example1"> <h2>Hello World</h2> <p>The background image is displayed in its original size.</p> </div>

You can also use the background shorthand property to solve this:您还可以使用background速记属性来解决此问题:

 #example1 { border: 2px solid black; padding: 25px; background: url(https://placehold.it/1000x100) no-repeat right / cover; }
 <div id="example1"> <h2>Hello World</h2> <p>The background image is displayed in its original size.</p> </div>

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

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