Having trouble creating a CSS Sprite for my project. Any solutions?

I'm having some trouble with creating CSS Sprites. This is my first attempt at using sprites, so I'm still learning.

Here is the HTML code snippet:

<div class="dfeature-box">
<div class="icon"><img class="multi" src="images/sprite.jpg"></div>
<div class="title">Title</div>
<div class="details">description.</div>
</div>

And below is the corresponding CSS code snippet:

.dfeature-box {
text-align: center;
margin-bottom: 30px;
display: inline-block;
width: 100%;
max-width: 272px;
}

.dfeature-box .icon {
display: flex;
display: -webkit-flex;
justify-content: center;
align-items: center;
width: 106px;
height: 106px;
background-color: #ffffff;
border-radius: 16px;
text-align: center;
margin-bottom: 35px;
margin-left: auto;
margin-right: auto;
}

.dfeature-box .icon .multi{
background-position:126px 9px; 
height:47px; 
width: 64px; 

This is the image I want to use (containing 3 icons in one image) - in my current codes, I'm using the last icon from the image (two people)

Now, I have a vital question: How can I utilize the image in CSS instead of embedding it directly in HTML (as a class within "icon")? Currently, you can see that I'm using the "img" tag inside the "icon" class.

==>> First update:

Please review these updated codes as per your recommendation: Check out the result below: enter image description here

CSS:

.sprite-bg{
background: url("images/sprite.jpg");
}

.bg-1{
width:62px;
height:62px;
background-position: 0px 0px;
}

HTML:

<div class="dfeature-box">
<div class="icon"><div class="sprite-bg bg-1"></div></div>
<div class="title">Title</div>
<div class="details">Description</div>
</div>

Take a look at this HTML code segment: Do you recommend any changes for the highlighted part? What should I modify there? enter image description here

Also, please review my initial post's codes. My goal is to integrate sprites into my existing code.

==>> Second update:

.dfeature-box {
text-align: center;
margin-bottom: 30px;
display: inline-block;
width: 100%;
max-width: 272px;
}

.dfeature-box .icon {
display: flex;
display: -webkit-flex;
justify-content: center;
align-items: center;
width: 106px;
height: 106px;

background-color: #ffffff;
border-radius: 16px;
text-align: center;
margin-bottom: 35px;
margin-left: auto;
margin-right: auto;
-webkit-transform-origin: center center;
-moz-transform-origin: center center;
-ms-transform-origin: center center;
transform-origin: center center;
-webkit-transform: scale(1);
-moz-transform: scale(1);
-ms-transform: scale(1);
transform: scale(1);
-webkit-box-shadow: 0 8px 12px rgba(31, 27, 90, 0.08);
box-shadow: 0 8px 12px rgba(31, 27, 90, 0.08);
-webkit-transition: all 0.3s ease;
transition: all 0.3s ease;
}

.sprite-bg{

background: url("https://i.imgur.com/VO1dBBA.jpg");

}
.bg-1{
    width:62px;
    height:62px;
    background-position: 0px 61px;
}
.bg-2{
    width: 64px;
    height: 62px;
    background-position: 62px 61px;
}
.bg-3{
    width: 64px;
    height: 62px;
    background-position: 127px;
}

.dfeature-box .icon i {
    font-size: 50px;
    background: -webkit-linear-gradient(to bottom, #45b35e, #6ad56a);
    background: linear-gradient(to bottom, #45b35e, #6ad56a);
    color: transparent;
    -webkit-text-fill-color: transparent;
    -webkit-background-clip: text;
    background-clip: text;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hustbee</title>

    <link rel="stylesheet" type="text/css" href="css/style.css">

</head>
<body>
<div class="dfeature-box">
                    <div class="icon"><div class="sprite-bg bg-1"></div></div>
                    <div class="title">TITLE</div>
                    <div class="details">long description.</div>
                </div>


                <div class="dfeature-box">
                    <div class="icon"><div class="sprite-bg bg-2"></div></div>
                    <div class="title">TITLE</div>
                    <div class="details">long description.</div>
                </div>

                <div class="dfeature-box">
                    <div class="icon"><div class="sprite-bg bg-3"></div></div>
                    <div class="title">TITLE</div>
                    <div class="details">long description.</div>
                </div>

</body>
</html>

Answer №1

Take a look at the following example:

Here are a few key points to consider:

  1. Always use a sprite image with the syntax backgound:url(path to image)
  2. Define 3 separate classes for different icons.
  3. Utilize various positions of the image in your CSS.
.sprite-bg{
 
  background: url(https://cdn.tutsplus.com/webdesign/uploads/legacy/tuts/373_sprites/angry_birds.png);

}
.bg-1{
 width:110px;
  height:110px;
  background-position: 0px 0px;
}
.bg-2{
 width: 58px;
    height: 60px;
    background-position: 194px 89px;
    //border: 2px solid red;
    float:right;
}
<div class="sprite-bg bg-1"></div>
<div class="sprite-bg bg-2"></div>

Answer №2

To utilize images individually, you must divide your image into 3 separate files as HTML/CSS does not support splitting images.

I also have a pressing query: How can I incorporate the image into CSS instead of within HTML (class=icon)? The current implementation shows "img" nested inside "class=icon".

A feasible solution would be to introduce the background property in your class and insert an image using url("...")

background: url("img/sprite.jpg");

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

Tips for aligning the menu content of a mobile view navbar to the center using bootstrap 4

When viewing on mobile, my text is left aligned instead of centered. I am using bootstrap4. How can I center the menu contents when it collapses? https://i.sstatic.net/VAs2O.png <ul class="navbar-nav ml-auto"> <li ...

Using an image as the background for a three.js scene may cause it to appear distorted

I am struggling with setting an image as a background for my scene without it becoming blurry. I have attempted to use the following code: textureLoader.load("images/background/space.png", function(t) { scene.background = t; }); The problem arises when ...

Place the child DIV at the bottom of its parent DIV

I've searched through open questions but couldn't find a solution. The code snippet in question looks like this: ... <div style="some height"> <div style="some width"> .......and so on.. .. < ...

A guide on dynamically accessing an image from the src directory using props in a Vite + React application

export default function Card(props) { return ( <card> <img className={cardCss.card__img} src={`../assets/imgs/card/${props.img}`} alt="" /> <div className={cardCss.card__stats}> ...

"Interaction with jQuery causes button element to shift position upon resizing of browser

I am encountering an issue with a div element that has jQuery appended to it for a bounce effect. The element is absolutely positioned within its relative parent. However, when the browser is resized, the child element does not remain centrally aligned dur ...

What is the best way to incorporate the information from my CSV file onto my website?

I am currently working on a project where I need to extract data from a CSV file and display it on my website. The idea is to use two values as x and y coordinates, similar to a graph, and retrieve the data at that specific location. Here is the HTML code ...

Ways to incorporate a border line between a pair of icons within a bootstrap card

Incorporating a border line within the card-header is my current goal. My approach involves positioning two icons - one on the left side and the other on the right. Illustrative example below: https://i.sstatic.net/BgxVu.png Utilizing HMTL and CSS to ac ...

Is it feasible to prevent a background image from being displayed within a specific element?

Is there a way to have a background image only display in specific elements? For instance, if there are several divs within a main div that has a background image set, and all of them are contained within a body tag with its own background image. Is it po ...

Locate the CSS selector for a popup menu item when using Selenium with Python 3

For my first Selenium project with Python 3, I am working on creating a DeviantART Llamabot for a friend. The bot is almost complete, except for the issue of locating the "give llama" button. The problem arises from the fact that the button appears under ...

Setting the variable to global does not apply styling to the element

Looking for help with styling an element created using document.createElement("img")? let fireball; //global variable fireball = document.createElement("img") //variable on local fireballArray.push(someFunction(fireball, { src: "img.png&qu ...

Tips on personalizing the checkbox by incorporating a custom background image

Looking to spruce up the standard checkbox? We're open to any method - whether it's a background image, CSS3, or some jQuery magic. Check out this example for inspiration! ...

IE9 is causing a bizarre problem where the entire page is suddenly jumping. It's

UPDATE: The client has requested testing to disable the click and drag feature in IE, so replicating the bug may be difficult at this time. I apologize if this hinders the community's ability to help fix the root issue. Here's the issue: It occu ...

Using jQuery's .text() function transforms my h1 element into regular text once a number is inputted

I am facing an issue with the following code snippet: <li id="machine" ><h1>Machine</h1></li> <li id="player"><h1>Player</h1></li> It displays as shown in the image below: https://i.sstatic.net/CR3Ut.png ...

Guide to implementing personalized CSS on WooCommerce product pages specific to mobile screen resolutions

As a beginner in wordpress, I am looking to customize the CSS of my single product page specifically to relocate the "Sale" icon. Despite trying the code below in custom CSS, no changes are being reflected. @media only screen and (max-width: 767px){ .sin ...

Determine if an HTML element contains a specific class using JavaScript

Is there a simple method to determine if an HTML element possesses a particular class? For instance: var item = document.getElementById('something'); if (item.classList.contains('car')) Remember, an element can have more than one clas ...

Table formatting problem

I am looking to add borders only below each row in my table. td{ border-bottom-style: solid;} However, I am noticing a visible border break between columns. Can anyone advise on how to remove that? ...

Angular 6 seems to be having issues loading Bootstrap

I've run into an issue while trying to incorporate Bootstrap into Angular 6. I have already installed Bootstrap using npm install bootstrap and added it to angular.json, but it's still not functioning. Is there something else I should be doing? A ...

Issue encountered after updating to version 2.0 of Datatables.net: Conflict between custom CSS and BS5

Having utilized Datatables.net v1.13.11 in conjunction with custom BS5 templates from Themesbrand (), I recently upgraded to v2.0 and came across the following observations: A border box appeared when hovering over a column header. Extra top and bottom bo ...

expanding menu options in a dynamic design

My webpage features a logo alongside a menu: Here's the HTML code: <div id="logomenuwrapper"> <div id="logo"><img src="img/logo_light.jpg"/></div> <div id="menu"> <ul> <li><a href="#">About ...

Attempting to align three sections horizontally

I'm having trouble with my current HTML code and I can't seem to figure out what the issue is. As a beginner in HTML, I am struggling to understand. My goal is to have three columns: one on the left taking up 20% of the space, one in the center t ...