使用 CSS 剪辑/裁剪背景图像
我有这个 HTML:
<div id="graphic">lorem ipsum</div>
使用这个 CSS:
#graphic { background-image: url(image.jpg); width: 200px; height: 100px;}
我应用的背景图像是 200x100 像素,但我只想显示 200x50 像素背景图像的裁剪部分。
background-clip
似乎不是正确的 CSS 属性。我可以用什么来代替?
不应使用 background-position
,因为我在精灵上下文中使用上述 CSS,其中我想要显示的图像部分小于定义 CSS 的元素。
I have this HTML:
<div id="graphic">lorem ipsum</div>
with this CSS:
#graphic { background-image: url(image.jpg); width: 200px; height: 100px;}
The background image I'm applying is 200x100 px, but I only want to display a cropped portion of the background image of 200x50 px.
background-clip
does not appear to be the right CSS property for this. What can I use instead?
background-position
should not be used, because I'm using the above CSS in a sprite context where the image part I want to show is smaller than the element on which the CSS is defined.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将图形放入具有自己的维度上下文的伪元素中:
浏览器支持很好,但是 如果您需要支持 IE8,请使用单冒号
:before
. IE 在此之前的版本中不支持这两种语法。You can put the graphic in a pseudo-element with its own dimensional context:
Browser support is good, but if you need to support IE8, use a single colon
:before
. IE has no support for either syntax in versions prior to that.也许你可以这样写:
may be you can write like this:
另一种选择是使用 Linear-gradient() 来覆盖图像的边缘。请注意,这是一个愚蠢的解决方案,所以我不会花太多精力来解释它......
我在此页面上使用此方法: https://championmains.github.io/dynamicflairs/riven/ 并且不能使用
::before
或::after
元素,因为我已经将它们用于另一个黑客。Another option is to use
linear-gradient()
to cover up the edges of your image. Note that this is a stupid solution, so I'm not going to put much effort into explaining it...I'm using this method on this page: https://championmains.github.io/dynamicflairs/riven/ and can't use
::before
or::after
elements because I'm already using them for another hack.