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.
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.may be you can write like this:
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.