How to Align an Image Sprite Using CSS within a Button

In an attempt to replicate the image below using CSS for button building and an image sprite for displaying the center content of the buttons, I am encountering difficulties with positioning my sprite images correctly. Any suggestions on improving the CSS to resemble the above image more closely would be greatly appreciated. Additionally, any insights on enhancing the HTML structure?

Below is a demonstration of the code I have worked on so far:

HTML

<div class="switch-wrapper">
    <div class="switcher left selected">
        <span id="left">...</span>
    </div>
    <div class="switcher right">
        <span id="right">...</span>
    </div>
</div>

CSS

/* begin button styles */

.switch-wrapper{
    width:400px;
    margin:220px;
}

.switcher {
    background:#507190;
    vertical-align: bottom;
    position: relative;
    display: inline-block;
    max-width: 100%;
    vertical-align: bottom;
    box-shadow: 1px 1px 1px rgba(0,0,0,.3);
}
#left{
    background-image: url(http://www.codedevelopr.com/assets/images/switcher.png);
    background-position: -0px -0px;
}


#right{
    background-image: url(http://www.codedevelopr.com/assets/images/switcher.png);
    background-position: -0px -17px;
}


.left{
    border-radius: 6px 0px 0px 6px;
    width: 45px; height: 38px;

}
.right{
    border-radius: 0 6px 6px 0;
    width: 45px; height: 38px;
    margin: 0 0 0 -6px

}

.switcher:hover,
.selected {
    background: #27394b;
    box-shadow: -1px 1px 0px rgba(255,255,255,.4),
     inset 0 4px 5px rgba(0,0,0,.6),
      inset 0 1px 2px rgba(0,0,0,.6);
}

.switcher::after {
    content: ' ';
    width: 100%;
    height: 100%;
    position: absolute;
    top: -1px;
    left: -1px;
    border: solid 0px #1B1B1B;
    border-radius: 6px;


}

Answer №1

  1. Technique of blocking and inline elements

Visit this link for more information

  1. Approach to absolute positioning

Check out this link for details

Answer №2

Hey there, it looks like the position absolute in #left & #right is not defined, so I took control of the sprite images using absolute positioning.

You have the flexibility to adjust your images by making changes to the top, left, bottom, right positions as needed...

Below is your updated CSS code:-

.switch-wrapper{
    width:400px;
    margin:220px;
}

.switcher {
    background:#507190;
    vertical-align: bottom;
    position: relative;
    display: inline-block;
    max-width: 100%;
    vertical-align: bottom;
    box-shadow: 1px 1px 1px rgba(0,0,0,.3);
}
#left{
    background-image: url(http://www.codedevelopr.com/assets/images/switcher.png);
    background-position: -0px -0px;
    background-position: 16px -16px;
    height: 15px;
    left: 15px;
    position: absolute;
    top: 12px;
}


#right{
    background-image: url(http://www.codedevelopr.com/assets/images/switcher.png);
    background-position: -0px -17px;
    background-position: 0 -29px;
    left: 15px;
    position: absolute;
    top: 13px;
}


.left{
    border-radius: 6px 0px 0px 6px;
    width: 45px; height: 38px;

}
.right{
    border-radius: 0 6px 6px 0;
    width: 45px; height: 38px;
    margin: 0 0 0 -6px

}

.switcher:hover,
.selected {
    background: #27394b;
    box-shadow: -1px 1px 0px rgba(255,255,255,.4),
     inset 0 4px 5px rgba(0,0,0,.6),
      inset 0 1px 2px rgba(0,0,0,.6);
}

.switcher::after {
    content: ' ';
    width: 100%;
    height: 100%;
    position: absolute;
    top: -1px;
    left: -1px;
    border: solid 0px #1B1B1B;
    border-radius: 6px;


}

Check out the live demo here:

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

Ways to conceal scrollbar when it is not in use

Typically, the scrollbar is visible only when scrolling through a page and then disappears immediately after. I attempted to modify the default scrollbar properties using basic CSS. While this made the scrollbar always visible, adding "overflow-y: auto" d ...

CSS problem causing issues with block display in basic gallery

I've created this code: <head> <style type="text/css"> div.img { margin: 2px; border: 1px solid #0000ff; height: auto; width: auto; float: left; text-align: center; } div.img img { display: inline; margin: 3px; border: 1 ...

php failure to execute included file

Hey there! I'm currently facing an issue with including a file that contains all of my 'head' information. My goal is to simplify and streamline my page by breaking it down. In my index.php file, here's what I did: <?php include & ...

What could be causing my HTML table to stretch all the way to the bottom and right?

https://i.sstatic.net/SpG52.png The layout appears to be extending to the right and bottom, despite my attempts to fix it. I am utilizing the Bootstrap table class in my code. Here is the code snippet: HTML: <head> <link rel="stylesheet" ...

Placing the cursor in front of a non-editable display block element using HTML5's contenteditable feature

Currently, I am faced with the challenge of inserting a span into a contenteditable element to act as a tag or separator. Here is my code snippet: .tag { display:block; background-color:red; } .edit-box{ border: 1px solid black; padding: 10px; ...

Flexbox isn't lining up items horizontally as expected

I have been researching flexbox and it seems like it should display in a horizontal row by default. However, no matter what I try, I can't seem to get it to work as expected. I've included the code below. When I remove "display: flex;" from the C ...

The combination of two equal height elements with absolutely positioned child elements

I have a website that features a side-bar navigation and a main content pane, both enclosed within a container element. The content has its own unique background styling, while the menu adopts the background of the parent container. In situations where th ...

Verify if the value of localStorage matches the specified value, then conceal the element

This is my second query and I'm hoping it covers everything. My knowledge of javascript is limited, which has made it difficult for me to get my code working properly. Despite trying various different approaches, I have been unable to resolve the issu ...

Row in the table exceeds page limit (print publication)

When I print my table (ctrl+p), it does not break the way I want. The last row of the first page splits with part on the second page. Is there a solution for this issue? The rows can vary in content and size, and I have tried using properties like page-bre ...

Ensure there is a blank space preceding a table only when the table is being physically printed

I am currently in the process of developing a report card system for students in grades K-6. The report card will display specific tables based on the student's grade level. For example, a 5th grader may not have a "Reading Stage" table displayed on t ...

Changing the height of SimpleBar in a component without using global styles in Angular

Is there a way to customize the height of my scrollbar without affecting other components? I tried using simplebar-scrollbar :: before in global style but it impacted other areas as well. I'm looking for a solution that only applies to my component. ...

Parent table surpassing limits

Can someone help me with an issue I'm having regarding a table that is exceeding the width of its container? I've been struggling to find out what's causing this problem, especially because it seems to only happen in Firefox. Here's the ...

Align the image at the center of the screen with an overlay

Whenever an image is clicked, a lightbox appears. Currently, the image overlay is positioned at the top of the screen, but ideally, it should be centered on the user's screen. See the code snippet below: // The following script creates a lightbo ...

Challenges with border-color and CSS input:focus

The dilemma Currently, I am immersed in a project focusing on constructing a front end using bootstrap4. In order to align with the company's main color scheme, I decided to alter the highlighting color of inputs. To achieve this, I made the followi ...

Crop base64 image data into a circular shape

I am looking to crop a portion of an image that takes up the entire screen. Specifically, I need to capture only a small section of the image, measuring 300px in width and height from the top 25%. Below is an example I came across: In the example, there i ...

Customizing text appearance with innerHTML in JavaScript: A guide to styling

Below is the code I have for a header: <div id="title-text"> The Cuttlefisher Paradise </div> <div id="choices"> <ul> <li id="home"><a href="#">Home</a></li> <li id="contact">&l ...

Conceal the Navigation Bar Upon the Pop Up's Appearance

I Designed a Landing Page with a Fixed Mobile Nav Bar at the Bottom. There is also a Pop-Up Form that appears when clicked. How can I make sure the Nav Bar is closed when the form pops up? This issue is blocking the Send Button. Is it possible to achiev ...

What is the best way to utilize an HTML form input in order to update a value on a website that will affect all users?

I am relatively new to web development and I am currently working on a website that features a leaderboard. The goal is to allow users to input the number of points they have earned since their last log-in, with this value being added to their existing sco ...

Expanding the header in Ionic 3 with a simple click event

I have successfully implemented an Expandable Header in Ionic 3 following a tutorial from Joshmorony. The header expands perfectly on scroll, as you can see in this GIF: However, I am facing an issue where I want to expand the header on click instead of o ...

Tips for making all images the same height while maintaining responsiveness:

Seeking assistance with achieving uniform height for images while maintaining their aspect ratio using CSS. How can I ensure all my images have the same height and remain responsive? Below is the code snippet containing the images: <div class="contain ...