179

Let's say I want a way to display just the the center 50x50px of an image that's 250x250px in HTML. How can I do that. Also, is there a way to do this for css:url() references?

I'm aware of clip in CSS, but that seems to only work when used with absolute positioning.

Hafthor
  • 16,358
  • 9
  • 56
  • 65
  • 1
    Use a sprite - see here http://www.w3schools.com/css/css_image_sprites.asp –  Jan 09 '11 at 14:50
  • A List Apart should be in anyones list of sites to visit with HTML/CSS/JS problems: [Here's one way of doing sprites](http://alistapart.com/articles/sprites), [and here's another using JS](http://alistapart.com/articles/sprites2). – graham.reeds Sep 11 '08 at 21:47

6 Answers6

184

As mentioned in the question, there is the clip css property, although it does require that the element being clipped is position: absolute; (which is a shame):

.container {
  position: relative;
}
#clip {
  position: absolute;
  clip: rect(0, 100px, 200px, 0);
  /* clip: shape(top, right, bottom, left); NB 'rect' is the only available option */
}
<div class="container">
  <img src="http://lorempixel.com/200/200/nightlife/3" />
</div>
<div class="container">
  <img id="clip" src="http://lorempixel.com/200/200/nightlife/3" />
</div>

JS Fiddle demo, for experimentation.

To supplement the original answer – somewhat belatedly – I'm editing to show the use of clip-path, which has replaced the now-deprecated clip property.

The clip-path property allows a range of options (more-so than the original clip), of:

  • inset — rectangular/cuboid shapes, defined with four values as 'distance-from' (top right bottom left).
  • circlecircle(diameter at x-coordinate y-coordinate).
  • ellipseellipse(x-axis-length y-axis-length at x-coordinate y-coordinate).
  • polygon — defined by a series of x/y coordinates in relation to the element's origin of the top-left corner. As the path is closed automatically the realistic minimum number of points for a polygon should be three, any fewer (two) is a line or (one) is a point: polygon(x-coordinate1 y-coordinate1, x-coordinate2 y-coordinate2, x-coordinate3 y-coordinate3, [etc...]).
  • url — this can be either a local URL (using a CSS id-selector) or the URL of an external file (using a file-path) to identify an SVG, though I've not experimented with either (as yet), so I can offer no insight as to their benefit or caveat.

div.container {
  display: inline-block;
}
#rectangular {
  -webkit-clip-path: inset(30px 10px 30px 10px);
  clip-path: inset(30px 10px 30px 10px);
}
#circle {
  -webkit-clip-path: circle(75px at 50% 50%);
  clip-path: circle(75px at 50% 50%)
}
#ellipse {
  -webkit-clip-path: ellipse(75px 50px at 50% 50%);
  clip-path: ellipse(75px 50px at 50% 50%);
}
#polygon {
  -webkit-clip-path: polygon(50% 0, 100% 38%, 81% 100%, 19% 100%, 0 38%);
  clip-path: polygon(50% 0, 100% 38%, 81% 100%, 19% 100%, 0 38%);
}
<div class="container">
  <img id="control" src="http://lorempixel.com/150/150/people/1" />
</div>
<div class="container">
  <img id="rectangular" src="http://lorempixel.com/150/150/people/1" />
</div>
<div class="container">
  <img id="circle" src="http://lorempixel.com/150/150/people/1" />
</div>
<div class="container">
  <img id="ellipse" src="http://lorempixel.com/150/150/people/1" />
</div>
<div class="container">
  <img id="polygon" src="http://lorempixel.com/150/150/people/1" />
</div>

JS Fiddle demo, for experimentation.

References:

ShreevatsaR
  • 38,402
  • 17
  • 102
  • 126
David Thomas
  • 249,100
  • 51
  • 377
  • 410
  • 14
    amazing find, thanks a lot :) Just FYI, it is not as limited as one would think. If you have a image container div (id="img_container") around img tag, just make img_container position relative, and make img absolute, you can clip the image that way – FurtiveFelon Nov 25 '11 at 11:00
  • "position: absolute; (which is shame)" – Sanket Sahu Mar 19 '14 at 12:55
  • Not that I'm aware of, no. – David Thomas Oct 13 '14 at 20:44
  • 6
    `clip` is [deprecated](https://developer.mozilla.org/en-US/docs/Web/CSS/clip). Newer [`clip-path`](http://css-tricks.com/almanac/properties/c/clip/) doesn't require positioning – eagor Jan 15 '15 at 12:09
  • Good news, the "clip: rect" works in IE8, I have just checked. – Yevgeniy Afanasyev Jul 09 '15 at 00:51
  • 3
    while clip is deprecated many modern browsers do not yet support clip-path. https://developer.mozilla.org/en-US/docs/Web/CSS/clip-path – barrypicker Nov 09 '16 at 21:06
  • `clip` is deprecated and is not recommended to use by MDN https://developer.mozilla.org/en-US/docs/Web/CSS/clip – Ada Ge Jun 23 '19 at 20:01
  • @Ada: agreed, I quote from my answer: "*To supplement the original answer – somewhat belatedly – I'm editing to show the use of `clip-path`, which has replaced the now-deprecated `clip` property.*" – David Thomas Jun 23 '19 at 21:14
  • Note that the outside area is only hidden, not cropped, so it's not equivalent to a smaller image. – user202729 Jul 02 '19 at 11:58
130

One way to do it is to set the image you want to display as a background in a container (td, div, span etc) and then adjust background-position to get the sprite you want.

Matt
  • 74,352
  • 26
  • 153
  • 180
Espo
  • 41,399
  • 21
  • 132
  • 159
  • 2
    Just to clarify, you’d set the width and height of the container td, div, span or whatever to 50px to make this work. – Paul D. Waite Apr 27 '09 at 08:38
  • 7
    @Espo, This seems to be a fairly standard approach, however what if you have a sprite image which contains several individual images of size 32x32, and you want to display 1 of these on a div, span etc that is bigger than 32x32...setting the background-position no longer works. – Matthew Layton May 30 '13 at 12:01
  • 2
    @series0ne You could probably combine with [`background-size`](https://developer.mozilla.org/en-US/docs/Web/CSS/background-size) – Stijn de Witt Jun 19 '15 at 15:13
44

Another alternative is the following, although not the cleanest as it assumes the image to be the only element in a container, such as in this case:

<header class="siteHeader">
  <img src="img" class="siteLogo" />
</header>

You can then use the container as a mask with the desired size, and surround the image with a negative margin to move it into the right position:

.siteHeader{
    width: 50px;
    height: 50px;
    overflow: hidden;
}

.siteHeader .siteLogo{
    margin: -100px;
}

Demo can be seen in this JSFiddle.

Only seems to work in IE>9, and probably all significant versions of all other browsers.

thanksd
  • 54,176
  • 22
  • 157
  • 150
Vincent
  • 4,876
  • 3
  • 44
  • 55
  • 2
    Although it is a bitc hacky, it has the plus side that this works in all browsers (clip is deprecated and clip-path does not work in IE) and that it works when You try to print the webpage (background images are skipped by default) – Rauni Lillemets Dec 02 '15 at 09:45
  • This is the only truely accross browsers method. All other ways have issues. "Clip" has been depreciated and background-position is not read accurately in all browsers. – Kareem Jan 21 '16 at 14:12
3

Adjust the background-position to move background images in different positions of the div:

div { 
  background-image: url('image url');
  background-position: 0 -250px; 
}
Vincent
  • 4,876
  • 3
  • 44
  • 55
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
  • 1
    Type: `backgroud-image` -> `background-image`. I was wondering why is my browser not aware of such CSS property and finally I realised it was due to the type. – Eerik Sven Puudist Oct 20 '20 at 23:14
0

div{
width: 50px;
height: 50px;
background: no-repeat -100px -100px/500% url("https://qce.quantum.ieee.org/2022/wp-content/uploads/sites/6/2022/02/QCE22-250x250-website@1x.jpg")
    };
<html>
<head>
</head>
<body>
<div></div>
</body>
</html>
amateur
  • 37
  • 5
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Ethan Jul 11 '22 at 22:37
0

I ran into this problem just recently trying to work on one of my own projects for Codecademy. I found out that if you have an image and want to display only a portion of the picture you can position it by doing

'background-position: 95% 5%;'

Play around with the percentage to see what suits you best. But I believe that this works if you have already displayed your image as a background image.

LW001
  • 2,452
  • 6
  • 27
  • 36