Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting just the middle section of an image with CSS

Tags:

html

css

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.

Example of image cropping desired

like image 307
user2924019 Avatar asked Apr 21 '15 12:04

user2924019


People also ask

How do I select the center of an image in CSS?

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.

How do I take part of an image in CSS?

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.

How can I display just a portion of an image in HTML CSS?

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.

How do I hide part of an image in CSS?

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.


4 Answers

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.

like image 94
FoxInFlame Avatar answered Sep 28 '22 08:09

FoxInFlame


Put a negative margin-top to your image, the parent will need an overflow: hidden;

like image 37
romuleald Avatar answered Sep 28 '22 09:09

romuleald


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/

like image 26
Jivings Avatar answered Sep 28 '22 09:09

Jivings


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

like image 40
Aravind Bharathy Avatar answered Sep 28 '22 10:09

Aravind Bharathy