Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Sprites - using just one piece of an image as a background for part of an element

I use the CSS Sprite Technique with a background image that looks something like this:

enter image description here

The CSS code for the icons:

div.icon {
    background-color: transparent;
    background-image: url("/images/icons.png");
    background-position: 0 0;
    background-repeat: no-repeat;
    display: inline-block;
    height: auto;
    vertical-align: text-top;
    width: auto;
}
div.icon:empty {
    width:16px;
    height:16px;
}
div.icon:not(:empty) {
    padding-left:20px;
}
div.icon.attenuation {
    background-position: 0 0;
}

My icons can be used like this:

<div class="icon warning"></div>

I want to put some text inside my icons like:

<div class="icon warning">There is a warning on this page</div>

But the problem is that the background image covers the entire text area:

enter image description here

The question is: how can I use only part of an image as a background image for part of my element?

Notes:

  • setting width to 16px for div.icon doesn't help.
like image 850
AlexStack Avatar asked May 07 '12 11:05

AlexStack


People also ask

How can I display just a portion of an image in HTML 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.

What is combine images using CSS Sprites?

Combining images using CSS sprites reduces the number of round-trip requests made, which can yield performance gains for your page. This method mostly refers to small images like icons, logos, and other graphical elements that can be combined into a single image and then positioned by CSS on the page.

How do I use an image as a sprite tag?

You create a defined area with a <a> with display:block; or <div> and use overflow hidden; to hide overflow and position:relative; . Then you place your <img> image sprite inside absolutely positioned, which is possible since you positioned the parent. Then use :hover on the image to change position.

How can I use one image as a background in HTML?

The most common & simple way to add background image is using the background image attribute inside the <body> tag. The background attribute which we specified in the <body> tag is not supported in HTML5. Using CSS properties, we can also add background image in a webpage.


2 Answers

Remember, where ever possible, you shouldn't change your markup just to achieve a design. It is possible using your markup.

div.icon:before {
    content: "";
    background-color: transparent;
    background-image: url("/images/icons.png");
    display: inline-block;
    height: 16px;
    vertical-align: text-top;
    width: 16px;
}

div.icon:not(:empty):before {
    margin-right: 4px;
}

div.icon.attenuation {
    background-position: 0 0;
}
like image 79
Alex Chamberlain Avatar answered Sep 30 '22 14:09

Alex Chamberlain


You have two ways:

1)Your markup must be like this:

<div class="icon warning"></div><div class="txt">There is a warning on this page</div>
.icon {width:10px(for ex.)}

2)You must change the image. Icons in the image must be below the another

like image 43
jumancy Avatar answered Sep 30 '22 13:09

jumancy