Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clip an element's background to display only a part of the image?

For clarification look at the following images.
The first one is what I intend to achieve. It's a text input element with two background images, one on each side.
The second image is the sprite image containing the icons I need.
Now my question is, is it possible to clip a background image to only display a portion of the image? Furthermore is it possible to use it with multiple backgrounds?

1st image:
enter image description here

2nd image:
enter image description here

like image 412
2hamed Avatar asked Aug 15 '12 07:08

2hamed


People also ask

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 do I add a background image to a section?

To insert an image in HTML, use the image tag and include a source and alt attribute. Like any other HTML element, you'll add images to the body section of your HTML file.

What is a backdrop clipping?

The background-clip CSS property sets whether an element's background extends underneath its border box, padding box, or content box.

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.


3 Answers

You're going to have to use two separate icon images to get this to work as you expect at the moment.

With CSS sprites, the background is clipped by the size of the element, there is an experimental CSS3 property called background-clip but it doesn't work the way you want (it will clip to the borders, padding or content of the box, not a specific dimension.)

So create two icons, use one on each side of the element with background-position.

As you can see here, with a spritesheet it will display the entire background image instead of the two icons you want. There is as yet no way to clip BG images in the way that you want. (one day, hopefully!)

like image 185
Kyle Avatar answered Sep 30 '22 04:09

Kyle


You will need to use separate icons OR you will need to have 2 additional elements (for showing the icons) overlaid on top of the input box. The latter will let you use the sprite itself.

like image 37
techfoobar Avatar answered Sep 30 '22 03:09

techfoobar


Set to your element (let's say div) yor big backgound picture and then adjust with background-position. Your image will be croped by your element size (ex. div).

in your case it will be around:

background-position: -87 -35;

and div size:

width: 28px; height: 30px
like image 36
Samich Avatar answered Sep 30 '22 03:09

Samich