I have an image that has some CSS: width:100%;
Now the image is around 560px
high, but I wish to only select 300px of the image, cutting off the top and bottom of the image without squashing it.
Is it possible to do this?
I have looked at crop
but you have to select the portions to do this, and so it won't work when trying to get just the middle.
CSS helps us to control the display of images in web applications. The centering of images or texts is a common task in CSS. To center an image, we have to set the value of margin-left and margin-right to auto and make it a block element by using the display: block; property.
Wrap the image in a div The markup to set up CSS-only cropping is super basic. All you need to do is wrap the img tag in a div . The pug image is 750 pixels wide and 500 pixels high. Let's make it portrait-oriented by maintaining the 500 pixel height, but chopping the width in half to 375 pixels.
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. Just to clarify, you'd set the width and height of the container td, div, span or whatever to 50px to make this work.
The first is to use the clip property. This allows you to specify a rectangular area of an element to be visible. Any part of the element outside of the specified area will be hidden. Another way to hide part of an image is to use the overflow property.
Try this. This is using "clip-path" CSS value. This was mentioned in @Sam Jacob's post.
<style>
.clip-me {
position: absolute;
clip: rect(110px, 160px, 170px, 60px);
/* values describe a top/left point and bottom/right point */
clip-path: inset(10px 20px 30px 40px); /* or "none" */
/* values are from-top, from-right, from-bottom, from-left */
}
</style>
<img class="clip-me" src="thing-to-be-clipped.png">
Note: This CSS property does not work in IE.
Put a negative margin-top
to your image, the parent will need an overflow: hidden;
You can use clip-path
:
/* values are from-top, from-right, from-bottom, from-left */
.clip {
clip-path: polygon(5% 5%, 80% 5%, 80% 60%, 90% 60%, 5% 60%);
-webkit-clip-path: polygon(5% 5%, 80% 5%, 80% 60%, 90% 60%, 5% 60%);
}
Here's a jsfiddle example: http://jsfiddle.net/q08g3948/1/
Use Clip Path
img {
position: absolute;
clip: rect(0, 100px, 100px, 0); /* clip: shape(top, right, bottom, left);*/
}
Edit: As mentioned in the comment, the position should be absolute or fixed for this to work
If you don't want to make the position absolute or fixed, 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.
As the mozilla page here says that using clip is not a web standard, considering using clip-path or background-position to achieve this.
img{
/* deprecated version */
position: absolute; /* absolute or fixed positioning required */
clip: rect(110px, 160px, 170px, 60px); /* or "auto" */
/* values descript a top/left point and bottom/right point */
/* current version (doesn't require positioning) */
clip-path: inset(10px 20px 30px 40px); /* or "none" */
/* values are from-top, from-right, from-bottom, from-left */
}
A detailed explanation here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With