Is it possible to apply a PNG icon collection using CSS?

Throughout various examples, jQuery plugins, and CSS-styled HTMLElements, I have observed the setting of icons on an image with multiple options.

I am eager to implement this practice in my own apps.

https://i.sstatic.net/WDCH3.png

For instance, in the mentioned image, how can one choose a specific icon using CSS? What is the proper method for selecting one of them?

Are there any sample implementations available?

Thank you!

Answer №1

The key to using CSS for displaying images lies in manipulating their positioning and clipping within a parent element, whether by setting them as backgrounds or absolute positioned images with overflow: hidden.

Take this HTML for example:

<div class="sprite-images checkbox"></div>

Let's say the checkbox image we want is located at (32, 32) on the spritesheet, with dimensions of 32x32px.

Your CSS code would then resemble this:

.sprite-images{
    width: 32px;
    height: 32px;
    display: block; // can't be inline
}

.sprite-images.checkbox{
    background-image: url("images/my_spritesheet.png"); // specify sprite sheet URL
    background-position: 32px 32px; // coordinates for the desired icon
}

It's that straightforward. The background-image will automatically be clipped by the div according to its set size, translating it so only your intended sprite is visible.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to dynamically resize a text field based on the length of the typed text

I am looking to dynamically expand the width of an input text field when a user enters text into it. I am not sure how to achieve this functionality. Any guidance or help would be greatly appreciated. Is it possible to do this using Jquery or javascript? ...

Customize default zoom settings with Cascading Style Sheets (CSS)

body{ margin-bottom: 10%; margin-left: 10%; width:80%; color: #264653; position: relative; } #photo{ border-radius: 70%; position: absolute; left: 25%; top: 50px; } .left1{ margin-top: 10%; background-color: #264653; width : 30%; ...

Serve an HTML file with CSS/JS directly from the server disk to the client's browser

I'm currently facing a challenge that involves securely rendering and serving HTML files (along with CSS, JS) from the local disk on the server to a web application running in the client's browser. APPLICATIONS A front-end web app is built usi ...

Is there a way to split each foreach value into distinct variables?

I am looking to assign different variables to foreach values. I have fetched data from an API in JSON format, and then echoed those values using a foreach loop. My goal is to display the echoed value in an input box using JavaScript. I attempted the follow ...

What could be the reason behind my inability to successfully install and utilize Bootstrap Multiselect?

Hey everyone, I'm facing an issue while trying to implement Bootstrap Multiselect through NPM installation. I encountered an error stating $(...).multiSelect is not a function. Despite referring to the documentation, I am unable to utilize the library ...

What is the reason behind my button appearing beneath my links in React?

https://i.sstatic.net/Qmm4z.jpg Here is an image showcasing the current header render. The header consists of a HeaderMenu and 3 Links. While the links are functioning properly, the HeaderMenu is causing the links to be positioned below it. The HeaderMenu ...

Unable to execute ajax on dom ready in Internet Explorer 9

Here is some code that needs to be executed on DOM ready without any user interaction: if($.browser.msie){ console.log("Using getJSON"); $.getJSON(baseUrl,function(){ alert('hi'); }); }else{ setTimeou ...

The occurrence of an unforeseen symbol 'a' was encountered

After using jQuery to create JSON data, the following structure was produced: [{"name":"date","value":"24-05-2013"},{"name":"omschrijving","value":""}] Although this JSON is valid, I encountered an error when trying to process it with jQuery: An unexp ...

FullCalendar jQuery caught in an endless loop

After successfully implementing drag and drop deletion, I encountered a new issue. Whenever I delete an event, the removal process functions properly but then the code gets stuck in a loop within the eventDragStop function causing the calendar to freeze ...

Do you know the term for when JavaScript is utilized to display specific sections of a png image?

Imagine you have an image file in the format of a PNG which includes various icons intended for use on a website. How can JavaScript be utilized to choose and showcase a specific icon from that image? It's a technique I've observed in practice, b ...

Searching for a way to retrieve all information based on the parent in a dynamically dependent Laravel system?

Is there a way to fetch all child data by default in a dropdown list, but still have the option to select parent data when needed? I am using AJAX and I'm new to it. Edit: I have a 'category' dropdown with dynamically loaded parent data fro ...

Do window.location.href and window.open interfere with each other?

I'm attempting to open a PDF in a new URL and redirect the user to the homepage at the same time. However, these two conditions in the "if" block are conflicting with each other. The page successfully redirects to the homepage, but the window.open() f ...

Intrigued by the connection between background and calc()?

Hello everyone! I'm currently involved in a project and have a quick question regarding the calc function. As we all know, it functions perfectly on all browsers... ========== great! ============= background-color:#dfdfdf; background-image:url(..) ...

What is the best way to incorporate an opaque overlay onto an image along with text overlay on top of the image?

I'm struggling to overlay an opaque layer above an image with responsive text on top of it. I want the opaque layer to be positioned above the image but below the text, and not appear when hovering over the image. You can view my test page here: I a ...

Elements that are hidden or not displayed are still able to receive mouse over events

I am in the process of learning how to handle events within svgs and I have encountered a strange issue. Currently, I am working on an infovis project where I create an interface to display various column-graphs. This part is functioning well so far. Ho ...

Angular 12 experiencing CSS alignment issues

In my Angular component, I have the following CSS: .folders { border-left: 5px solid #b8744f; -moz-border-radius: 5px; -webkit-border-radius: 5px; -moz-box-shadow: inset 0 0 1px #fff; -webkit-box-shadow: inset 0 0 1px #fff; /*CORRECTION NEEDED ...

Accessing dropdown selection in Javascript-Json: A Comprehensive Guide

My dropdown list is being populated dynamically using json. The selected option from the first dropdown determines the options in a secondary dropdown. I need to use the selection from the second dropdown to automatically fill out two more fields. For exa ...

Problem with Bootstrap 3 navbar on mobile devices - not tappable or responsive

After years of using Bootstrap, I've come across a new issue with my implementation of a Bootstrap 3 Nav. While testing on a desktop browser with device emulation, the nav collapses and functions properly. However, when tapping on the header on an ac ...

Tips on positioning the navbar to the right edge of the screen

Currently using bootstrap, in the process of learning css and bootstrap. Looking to align links within ul to the far right of the screen. Assistance would be greatly appreciated <nav class="navbar navbar-expand-lg navbar-light bg-light"> ...

Turn off the feature that highlights links

Hi there! I'm curious to know if it's feasible to remove the highlighting effect when clicking on a link. I'd like the link to function more like an image, without the appearance of a highlighting box upon clicking. ...