Problem
Let’s say I want to use HTML to display only the middle 50x50px of a 250x250px image. I’m not sure how I’m going to do it. Is there a method to do this with css:url() references, as well?
I’m aware of the clip property in CSS, however it only appears to function with absolute placement.
Asked by Hafthor
Solution #1
There is the clip css property, as noted in the question, albeit it does require that the element being clipped is position: absolute; (which is a shame):
Experiment with the JS Fiddle demo.
I’m amending to show the use of clip-path, which has replaced the now-deprecated clip property, to augment the original answer — somewhat belatedly.
The clip-path attribute (more so than the original clip) provides a variety of options, including:
Experiment with the JS Fiddle demo.
References:
Answered by David Thomas
Solution #2
Setting the picture you want to use as a background in a container (td, div, span, etc.) and then adjusting background-position to get the sprite you want is one way to achieve it.
Answered by Espo
Solution #3
Another option is the following, which isn’t the cleanest because it implies that the picture is the sole element in a container, like in this case:
<header class="siteHeader">
<img src="img" class="siteLogo" />
</header>
The container can then be used as a mask with the necessary size, and the picture can be moved into the correct location by surrounding it with a negative margin:
.siteHeader{
width: 50px;
height: 50px;
overflow: hidden;
}
.siteHeader .siteLogo{
margin: -100px;
}
This JSFiddle has a demonstration.
Only IE>9 and possibly all significant versions of all other browsers appear to operate.
Answered by Vincent
Solution #4
To shift background pictures in different positions of the div, adjust the background-position:
div {
background-image: url('image url');
background-position: 0 -250px;
}
Answered by Codemaker
Post is based on https://stackoverflow.com/questions/57725/how-can-i-display-just-a-portion-of-an-image-in-html-css