Crop background image left and right.

huangapple go评论55阅读模式
英文:

Crop background image left and right

问题

我需要在div上应用背景图片,并且需要裁剪背景图片左右各50像素,但仍然希望以100%宽度显示,以填充整个屏幕宽度。我该如何做?

英文:

I am applying a background image to a div:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

div {
  background: white url(&quot;https://placekitten.com/1000/1000&quot;) no-repeat left top / 100% auto;
}

<!-- language: lang-html -->

&lt;div&gt;
  &lt;p&gt;Some text ...&lt;p&gt;
&lt;/div&gt;

<!-- end snippet -->

I need to crop 50 pixels left and right of the background image.

But I still want to display it with a 100% width to fill the entire screen width.

How can I do this?

答案1

得分: 1

你可以通过使用"calc(100% + 100px)"来将图像的大小增加100像素,并通过将其位置设置为-50px向左裁剪图像。

div {
  min-height: 200px;
  min-width: 200px;
  background: white url("https://placekitten.com/1000/1000") no-repeat -50px top / calc(100% + 100px) auto;
}
<div>
  <p>一些文本 ...</p>
</div>
英文:

You could add 100 pixel to the size of the image with "calc(100% + 100px)" and crop the image by positioning it -50px to the left.

<!-- begin snippet: js hide: false console: true babel: null -->

<!-- language: lang-css -->

div {
  min-height: 200px;
  min-width: 200px;
  background: white url(&quot;https://placekitten.com/1000/1000&quot;) no-repeat -50px top / calc(100% + 100px) auto;
}

<!-- language: lang-html -->

&lt;div&gt;
  &lt;p&gt;Some text ...&lt;p&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月13日 05:00:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76460302.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定