Constructing sprites with intersecting, peculiar rotations

For my current project, I am working on setting up a series of sidebar links that will create a finished design where the envelopes move and overlap based on their :hover state.

Originally, I believed each envelope would be its own PNG file, but I received a sprite that contains all the envelopes in one image.

Any suggestions for how I can achieve this effect? Typically, I would adjust the background position of each list element, but with the overlapping envelopes, this approach may not work. Should I ask to have the sprite exported differently?

Thank you in advance for your help...

Answer №1

It seems to me that the sprite is set up perfectly for this task. The different images correspond to different actions: hovering over the book, sharing on Twitter, sharing on Facebook, and emailing. The last image likely represents the default state. While using pure CSS and :hover can be a bit tricky (but possible!), achieving this effect with JavaScript would be very straightforward.

For the pure CSS approach, the div containing the sprite should be nested within all text elements so that the background changes based on the parent element being hovered over (the text). If this explanation isn't clear enough, I'd be happy to provide you with some example code.

Edit:

Although not flawless, it serves as a proof of concept.

Here's the JsFiddle link: http://jsfiddle.net/jp6fy/

CSS:

#side{
    position:relative;
    height:341px;
    width:250px;
}

#link1{
    top:0;
}

.link{
    position:absolute;
    left:0;
    top:85px;
    height:85px;
    padding-left:160px;
    width:90px;
}

#image{
    position:absolute;
    top:-255px;
    left:0;
    z-index:-1;
    background:url(https://i.sstatic.net/I2Y4k.png) -720px 0;
    height:341px;
    width:150px;
}

#link1:hover #image{
    background-position:-540px 0;
}

#link2:hover #image{
    background-position:-360px 0;
}

#link3:hover #image{
    background-position:-180px 0;
}

#link4:hover #image{
    background-position:0 0;
}

HTML:

<div id='side'>
    <div class='link' id='link1'>
        email
        <div class='link' id='link2'>
            facebook
            <div class='link' id='link3'>
                twitter
                <div class='link' id='link4'>
                    book
                    <div id='image'></div>
                </div>
            </div>
        </div>
    </div>
</div>

Answer №2

Yes, it can be done. (Although not the most visually appealing method.)

When using a :hover selector that only affects elements inside or adjacent to the triggering element, the workaround involves nesting the trigger elements: (view example)

<style>
div {
    width: 100px;
    height: 100px;
    position: absolute;
    left: 100px;
}
#image { background: black; }
#trigger1, #trigger1:hover #image { background: red; }
#trigger2, #trigger2:hover #image { background: green; }
#trigger3, #trigger3:hover #image { background: blue; }
</style>

<div id="trigger1">
  <div id="trigger2">
    <div id="trigger3">
      <div id="image"></div>
    </div>
  </div>
</div>

However, a more optimal approach would be to export the envelope sprites individually (CSS sprites can still be used). This will result in cleaner HTML and CSS, a smaller image file, and eliminates the need for complex nested positioning of elements with their own coordinate systems.

Answer №3

In an effort to maintain a simple markup, I utilized an approach involving just one additional non-semantic div per item:

<ul>
    <li id="email">
        <div class="background"></div>
        <em>Email</em> chris
    </li>
    <li id="facebook">
        <div class="background"></div>
        <em>Facebook</em> follow us
    </li>
    <li id="twitter">
        <div class="background"></div>
        <em>Twitter</em> your life away
    </li>
    <li id="book">
        <div class="background">
        </div><em>Book</em> a project
    </li>
</ul>

The background divs were strategically positioned at the same location, with varying background positions based on hover states:

/* Beginning with general styling for the document and list text */
body {
    background-color: black;
    color: white;
}
ul {
    width: 350px;
    margin-top: 40px;
    position: relative;
}
li {
    margin-right: 40px;
    font-family: "Century Gothic", Helvetica, sans-serif;
    text-align: right;
    margin-bottom: 0px;
    padding: 15px 4px 25px 0;
}
li em {
    text-transform: uppercase;
    display: block;
}
li:hover {
    color: red;
}

/* Important styling section below */

/* Sprite setup in all .background divs */
div.background { 
    background-image: url(https://i.sstatic.net/I2Y4k.png); 
    position: absolute;
    top: 0;
    left: 0;
    height: 341px;
    width: 160px;
}

/* Default state - turn off display for all divs */
div.background {
    display: none;
}

/* Default background shown for 'email' */
#email div.background  {
    display: block;
    background-position-x: -737px;
}

/* Toggle default background when hovering over entire list */
ul:hover #email div.background {
    display: none;
}

/* Override for email background on hover */
ul:hover #email:hover div.background {
    display: block;
    background-position-x: 0px;
}

/* Override for other items' backgrounds on hover */
#facebook:hover div.background {
    display: block;
    background-position-x: -375px;    
}

#twitter:hover div.background {
    display: block;
    background-position-x: -189px;

}

#book:hover div.background {
    display: block;
    background-position-x: -556px;    
}

View a working example on jsFiddle.

Note: It's fine to have multiple instances of the sprite in various divs; the browser caches one copy for use across all image displays.

Answer №4

Is it possible to generate an image map that changes the displayed image when hovering over different areas, showcasing the correct envelope design? Check out this fascinating demo at the following URL

explore a unique approach to image maps

Answer №5

Utilizing a clean HTML approach.

.nav { position: relative; }

.nav li {
  margin-left: 179.8px;
  list-style-type: none;
}

.nav li:before {
  position: absolute;
  left: 0; top: 0;
  content: url(https://i.sstatic.net/I2Y4k.png);
  clip: rect(0 899px 341px 719.2px);
  margin-left: -719.2px;
  z-index: 1;
}

.nav li:hover:before { z-index: 2; }

.email:hover:before {
  clip: rect(0 179.8px 341px 0);
  margin-left: 0;
}

.facebook:hover:before {
  clip: rect(0 359.6px 341px 179.8px);
  margin-left: -179.8px;
}

.twitter:hover:before {
  clip: rect(0 539.4px 341px 359.6px);
  margin-left: -359.6px;
}

.book:hover:before {
  clip: rect(0 719.2px 341px 539.4px);
  margin-left: -539.4px;
}
<ul class="nav">
  <li class="email">Email</li>
  <li class="facebook">Facebook</li>
  <li class="twitter">Twitter</li>
  <li class="book">Book</li>
</ul>

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

Is it possible to utilize retina images with CSS3's :before selector?

Can I adjust the size of an image inserted using :before? For example, if I want to use a high resolution image in the content below - how can I specify the dimensions? .buy_tickets_btn:before{ content: url(../images/buttons/pink_ticket_btn_bg.pn ...

Integrating PHP form validation alongside JavaScript validation, including a success message displayed at the top of

Currently, I have a functional JavaScript validation code that verifies my contact form and submits it. My objective is to enhance this by incorporating server-side validation with PHP. This will enable the display of a success message upon correct form su ...

As the Page Expands, Elements Shift into New Positions

I'm currently focused on enhancing the accessibility of my web pages. A challenge I've encountered is that when I expand the width of my page, the elements appear to shift further down the screen. It seems like this issue may be related to settin ...

Determine all the child nodes using Xpath

How can I extract all child nodes (option tags) from the following HTML using Html Agility Pack? <select name="akt-miest" id="onoffaci"> <option value="a_0">Všetci</option> <option value="a_1">Iba prihlásení</option> <o ...

Error Message: Undefined Constructor for Firebase Google Authentication

Hey there! I've been working on integrating Firebase google authentication into my project. Unfortunately, I encountered an error while testing it out. Here's the error message that appeared in the console: Uncaught (in promise) TypeError: Cannot ...

HTML: Trim the perimeter borders

My goal is to recreate the design shown in the image. I attempted to achieve this using the border-radius property, but unfortunately I could not replicate it accurately. ...

Leveraging jQuery to extract the value from a concealed form field

Seeking assistance with a jQuery issue... I am attempting to use jQuery to retrieve the values of hidden fields in a form. The problem I am facing is that there are multiple forms displayed on the same page (result set items for updating), and the jQuery ...

Adjustable div heights for text within inline elements

How can I make two inline-table divs (content and sidebar) have the same height regardless of their content? I've experimented with various attributes, but nothing seems to be working. You can view my website here. ...

Automatically refresh a table within a webpage with updated database information without the need to manually refresh the

I'm a beginner in PHP and AJAX. The issue I am currently encountering is displaying a table on a JSP page and wanting to auto-update the table without refreshing the page every 10 seconds. I retrieve values from a database using a PHP page Below is t ...

Troubleshooting the issue of CSS animations activating twice and causing a flickering effect specifically in the Firefox browser

I am facing an issue with CSS animations in Firefox. When I try to slide in some radio buttons upon clicking a button, the animation seems to be firing twice in Firefox while it works fine in Chrome. I have attempted several solutions but haven't been ...

Styling HTML elements for Google custom search API json results: Best practices

I have been working with the Google Custom Search API to retrieve JSON callback results for my project. I have been experimenting with the JSON callback variables based on the recommendations from Google's documentation available here: https://github ...

Tips for adding information into a designated DIV using JQUERY

Having a bit of trouble with my JQUERY at the moment. Basically, I have this Facebook-style layout for displaying posts. Each post has an <input> box where members can leave comments. When a user presses <enter>, my jQuery (AJAX) will fetch th ...

Receiving time slots on a bootstrap schedule

I recently developed a calendar using Bootstrap that allows users to select a start date and automatically sets the end date within a 7-day range from the chosen start date. However, I am facing a challenge in enabling users to also pick minutes along with ...

HTML textarea content will update automatically whenever there is an input change in JavaScript

Everything seems to be working as expected, but I noticed that the onmouseover event only triggers once. Is there a way to make it work multiple times, i.e., every time the text array changes? <html> <head> <script> function myFunct ...

Challenges with adjusting the dimensions of varying-sized fluid squares that are floated

I've been facing a roadblock in my coding project since yesterday afternoon. I'm trying to transform a classic unordered list menu into a tiled "gallery" within a 12 column grid, with fluid square tiles. When the squares are floated, the first s ...

Continuously fade out existing content using jQuery and seamlessly fade in new content

I am looking to create a continuous fading effect between two pieces of content in the same position. Currently, the fade in and fade out transitions work, but both contents are visible below each other when the document loads. Basically, the content will ...

Is it feasible for nested div to remain unaffected by bleeding when the container is positioned relatively and has an overflow hidden

Is it possible to bleed a nested div even when the container is positioned relative with hidden overflow? In this case, giving the nested div a fixed position is not an option. Please check out this unique example: http://jsfiddle.net/s7nhw/11/. If you ...

Retrieving user information from the database and adding it to the specified element

I've been grappling with what seems like a simple question for the past hour. Below is a script that I have which pulls all active reservations from the reservations table. require("../includes/connect.php"); function dispAllReservations(){ $i= ...

What is causing the black part in the output from rasterizeHTML.drawHTML when using canvas.toDataURL?

Whenever the button is clicked, it triggers the function common_save_project(). Currently, the canvas displays correctly on the screen after the function call, but the image generated by canvas.toDataURL turns out black where rasterizeHTML.drawHTML was inv ...

When I utilize position absolute, the elements start overlapping each other

I am currently working on displaying sidebar content within tabs, and I need to adjust the height dynamically based on the content. However, I encountered an issue where the elements above and below the tabs are overlapping. To address this problem, I remo ...