Having an excessive amount of CSS Sprites or none at all can be detrimental to your website's performance

UPDATE: Attempted to adjust the height to 27px as recommended by kennypu, but still facing issues.

I am working with a sprite containing multiple icons that I want to extract for navigation purposes. Unfortunately, I'm struggling to properly display portions of the sprite in my navigation bars. Despite reading numerous articles, watching tutorials, and browsing through Stack Overflow questions, I haven't been able to solve this issue.

You can find the JSFiddle link here: http://jsfiddle.net/m5kbX/

And the link to the sprite image is:

Here is the HTML code:

<div id="header-right">
            <ul id="navigation">
                <li>
                    <div class="nav-button">
                        <div id="schools">
                        </div>
                    </div>
                    <div class="nav-text">
                        Schools
                    </div>
                </li>
                <li>
                    <div class="nav-button">
                        <div id="professors">
                        </div>
                    </div>
                    <div class="nav-text">
                        Professors
                    </div>
                </li>
                <li>
                    <div class="nav-button">
                        <div class="Programs">
                        </div>
                    </div>
                    <div class="nav-text">
                        Programs
                    </div>
                </li>
                <li>
                    <div class="nav-button">
                        <div class="account">
                        </div>
                    </div>
                    <div class="nav-text">
                        My Account
                    </div>
                </li>
            </ul>
        </div>

And the CSS code:

#header-right{
    float: right;
    width: 381px;
    height: 64px;
    background-image:url('http://localhost/gradebyme/gradebyme/public/img/midtile2.png');
    background-repeat:repeat-x;
}

#navigation{
    position: relative;
    background: url('http://localhost/gradebyme/gradebyme/public/img/icontest.png') no-repeat;
    margin: 0;
    padding: 0;
    list-style: none;
}

#navigation li{
    width: 88px;
    height: 64px;
    display: inline-block;
    text-align: center;
}

.nav-button{
    width: 88px;
    height: 40px;
}

.nav-text{
    width: 88px;
    height: 24px;
    color:white;
}

#schools{
    float: left;
    width: 37px;
    height: 26.75px;
    background-position: 0 0;
}

#professors{
    float: left;
    width: 37px;
    height: 26.75px;
    background-position: 0 -27px;
}

#programs{
    float: left;
    width: 37px;
    height: 26.75px;
    background-color: green;
    background-position: 0 -55px;
}

#account{
    float: left;
    width: 37px;
    height: 26.75px;
    background-color: purple;
    background-position: 0 -83px;
}

The first section displaying the sprite is showing part of two icons and overflowing the container, while the remaining sections are not displaying anything. I would appreciate any help and advice regarding CSS sprites and general CSS techniques! Learning from experience is invaluable!

Answer №1

You haven't provided them with the necessary context.

    #schools{
        float: left;
        width: 37px;
        height: 26.75px;
        background-position: 0 0;
background: ????
    }

    #professors{
        float: left;
        width: 37px;
        height: 26.75px;
        background-position: 0 -27px;
background: ????
    }

    #programs{
        float: left;
        width: 37px;
        height: 26.75px;
        background-color: green;
        background-position: 0 -55px;
background: ????
    }

    #account{
        float: left;
        width: 37px;
        height: 26.75px;
        background-color: purple;
        background-position: 0 -83px;
background: ????
    }

Answer №2

Wow, there were so many issues with this code! From incorrect CSS selectors to the background image not being applied correctly, it was a mess...

I went in and debugged everything to make sure it works properly. You can check out the updated version on this Fiddle link. To get it to run smoothly, be sure to download the sprite provided by the original poster and place it in your 'localhost/img' directory while running a local server like WAMP.

Here's the HTML snippet:

<div id="header-right">
    <ul id="navigation">
        <li id="schools" class="nav-text">
            <div class="nav-button">
            </div>
            Schools
        </li>
        <li id="professors" class="nav-text">
            <div class="nav-button">
            </div>
            Professors
        </li>
        <li id="programs" class="nav-text">
            <div class="nav-button">
            </div>
            Programs
        </li>
        <li id="account" class="nav-text">
            <div class="nav-button">
            </div>
            My Account
        </li>
    </ul>
</div>​

And here's the corrected CSS section:

#header-right{
    float: right;
    width: 381px;
    height: 64px;
    background-image:url('http://localhost/img/midtile2.png');
    background-repeat:repeat-x;
    background-color: grey;
    border: 1px solid;
}

#navigation{
    position: relative;
    margin: 0;
    padding: 0;
    list-style: none;
}

#navigation li{
    display: inline-block;    
    float: left;
    text-align: center;    
    width: 88px;
    height: 64px;
}
/* More CSS rules... */

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

Display/Conceal content with JQuery on a PHP webpage

I am having trouble with the following code. My intention is to show/hide the content between the #info id when clicking buttons, but nothing seems to be happening. Could you help me identify the issue? echo '<script> $( "#show' . $r ...

Target the first element within a tree structure using CSS

Trying to target the initial blockquote in an email body with varying formats, such as: <div class="myclass"> <br><p></p> <div><div> <!--sometimes with less or more div--> <blockquote&g ...

Change the css code to override the color of the background material

Hey there, I'm facing an issue with overriding the background color of a material element. I've tried several methods but can't seem to get it to change. After inspecting the element on the web browser, I found the CSS code that controls th ...

Ensure that any modifications made to an Angular service are reflected across all components that rely on that service

I am currently in the process of replicating a platform known as Kualitee.com, which serves as a test-management tool utilized by QA Engineers. Within Kualitee, users can access multiple projects, each containing various test cases and team members. The ab ...

What's preventing me from invoking this object's function through an inline anchor?

CSS: <div class="box"> <p>This is a box</p> </div> Javascript: var box = { content : (function() {return ("This is the content of the box")}) }; alert("Box content: \n" + box.content()); $("output").text( +"<br/ ...

Transitioning from one CSS id to another during page loading

Wondering how to fade in one CSS id on page load and then smoothly transition to another after a few seconds? Here are the ids: #background { background: url("images/am_photography_bg.jpg") no-repeat center -25px fixed; background-size: 100%; ...

After applying the rotateY function, the z-index appears to be malfunctioning

My current project involves creating a page flip effect, which requires two overlapping div elements. Initially, both divs are side by side with the second div having a -webkit-translateY(180deg) property applied to it. The first div has a z-index of 2, wh ...

When attempting to render HTML containing multiple CSS classes within a JSON request parameter, the styles are not being applied as expected

In my API, I'm dealing with a JSON request parameter that includes an HTML string. This HTML string references CSS styles from a separate .css file. However, when an HTML element has two CSS classes assigned to it, neither of the classes' styles ...

How can I make a sticky element appear behind a different element?

https://jsfiddle.net/30suam6z/1/ .first { height: 100vh; background-color: red; position: relative; z-index: 2; /* Increase z-index to ensure it covers .test */ } .second { height: 100vh; background-color: yellow; position: relative; z- ...

Steps for designing a single-column content-focused layout

Creating a single column fixed-width layout with a header and footer, the order of view should be as follows: header navigation dynamic content 1 dynamic content 2 main content footer The dynamic contents (3, 4, 5) will expand vertically with unknown he ...

Using a single jQuery script, implement multiple modals on a webpage that are responsive on both desktop and mobile devices

My Modal is functioning well on desktop, but I'm facing an issue on mobile where the screen doesn't close when clicking outside the modal area. Is there a way to solve this for mobile without adding the 'X' button to close it? I attem ...

What is the best way to center a grid item in Material-UI within a React application?

I'm struggling with centering a grid element in MUI v5 and aligning a long paragraph to the left side while adding horizontal margin for big screens. Can anyone help me out with this? Thank you. <Box style={{ backgroundColor:" ...

Steps to display a JSX component stored in a variable

Presently, I am implementing an if/else statement within my code to determine the content that will be displayed in the JSX element named 'signupMessage'. Subsequently, I render the contents of this 'signupMessage' element. render() { ...

"Seamless responsiveness in design with jQuery, but encountering compatibility issues

My usage of jQuery is quite basic to ensure that elements are properly positioned when they are moved: function ipad() { var width = jQuery(window).width(); if (width < 1200 && width >= 768){ //if tablet jQuery('.mobbut').css(&apo ...

Firebase issue: The function ref.once is throwing an Uncaught TypeError and is not recognized

I am attempting to utilize Firebase for both uploading a file and storing it in my database simultaneously. I want to track the number of uploads so that I can rename each file uniquely. While I have successfully uploaded and stored a copy in the database, ...

What is the best way to showcase an array of objects in a table using AngularJS that updates

My technology stack includes angular.js for the front-end, node.js for server-side operations, and PostgreSQL for managing my database. Currently, I have a list of values stored in the database: Upon checking the controller code, I obtained the following ...

Align Text Center inside a Magical H1 Heading Tag

I'm having a bit of trouble with something that should be simple. I want to center the text inside my h1 tag on a wizard, and I've added this CSS to my stylesheet.css: .h1textalign { text-align:center; } Here's how I'm trying to apply ...

Acquiring inner HTML content with PHP

Looking to optimize meta tags for search and open graph. My goal is to dynamically generate the meta description tag by combining content from two specific elements in the HTML. For instance, let's say we have this snippet of code: <div class="r ...

Are Bootstrap Input groups inconsistent?

Hey there! I've been working on the sign-in example, but I seem to have hit a roadblock. In my local setup, the top image is what I see in my browser after running the code, while the desired layout that I found on the Bootstrap site is the one below ...

Incorporate a backdrop to enhance a material icon

I'm working with the following Material Icon code but I want to enhance it by adding a white background for better contrast. Any suggestions on how to achieve this? <a id="lnk_quick_print" href="javascript:;" title="Quick P ...