Transform this background into a transparent one, then ensure it becomes visible and remains that way

Here's what I've been working on:

My HTML code:

<div id="smith">
    <img class="top" src="http://i.imgur.com/D6Kohra.png"/>
</div>

And the CSS for it:

#smith {
    background: url("http://i.imgur.com/1ABXZ1y.png") no-repeat center;
    -webkit-transition: all .5s ease-in-out 0;
    -moz-transition: all .5s ease-in-out 0;
    -o-transition: all .5s ease-in-out 0;
    transition: all .5s ease-in-out 0;
}

#smith img {
  position:fixed;
  -webkit-transition: all .5s ease-in-out 180s;
  -moz-transition: all .5s ease-in-out 180s;
  -o-transition: all .5s ease-in-out 180s;
  transition: all .5s ease-in-out 180s;
}

#smith img.top {
    position:fixed;
    display:inline;
    left:50%;
}

#smith img.top:hover {
      opacity:0;
      transition:0;
}

#smith img.bottom:hover {
  transition:0;
}

In essence, I'm aiming to initially display the top image and then switch to the bottom image upon hovering. The challenge lies in freezing the bottom image while ensuring that it remains visible even after hovering off.

You can view the project on JsFiddle here: http://jsfiddle.net/mcKempt/gyn8fdt4/

I encountered an issue where the background image remained either constantly visible or hidden even during hover. I attempted separating it into its own image with an opacity of 0, but this approach didn't achieve the desired effect.

If you have alternative methods to accomplish this without setting the transition duration to '180s', please share your insights.

Additionally, I'm interested in discovering more resources related to CSS/HTML techniques.

Thank you!

Answer №1

As mentioned in my previous response, making permanent changes would necessitate the use of Javascript.

However, a clever workaround for achieving a semi-permanent effect involves setting a very long transition-duration on the base state and a short transition on the 'returning' :hover state.

#smith {
  background-color: transparent;
  height: 87px;
  width: 383px;
  border: 1px solid red;
  margin: 25px;
  background: url("http://lorempixel.com/400/200/sports/") no-repeat center;
}
#smith img.top {
  display: block;
  margin: 0 auto;
  opacity: 1;
  transition: opacity 999s ease;
}
#smith img.top:hover {
  opacity: 0;
  transition: opacity .5s;
}
<div id="smith">
  <img class="top" src="http://i.imgur.com/D6Kohra.png" />
</div>

The background-image itself cannot be smoothly transitioned, hence I propose using a pseudo-element solution.

#smith {
  background-color: transparent;
  height: 87px;
  width: 383px;
  border: 1px solid red;
  margin: 25px;
  position: relative;
}
#smith:before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: url("http://lorempixel.com/400/200/sports/") no-repeat center;
  z-index: -1;
  opacity: 0;
  transition: opacity 999s ease;
}
#smith:hover:before {
  z-index: -1;
  opacity: 1;
  transition: opacity .5s;
}
#smith img.top {
  display: block;
  margin: 0 auto;
  opacity: 1;
  transition: opacity 999s ease;
}
#smith img.top:hover {
  opacity: 0;
  transition: opacity .5s;
}
<div id="smith">
  <img class="top" src="http://i.imgur.com/D6Kohra.png" />
</div>

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

jQuery - Reveal one div while concealing another

I've been working on a script to toggle between two divs onClick - opening one while closing the other if it's already open. I'm fairly new to jQuery and Javascript, having mainly worked with HTML/CSS. Script I found: http://jsfiddle.net/J ...

Switching between tabs using Angular

Struggling with changing tabs in Angular based on new values? <ul class="nav nav-tabs"> <li class="active"> <a class="A" data-target="#A">New({{c.data.feil_nye_ant}})</a> </li> <li class="nav"> ...

Adjusting the opacity of a div while scrolling

I've searched multiple pages, but haven't found a solution that works for me. I'm trying to make the text in my div gradually become more transparent as I scroll. Can anyone help with this? Here's my code: <script src = "/js/titleSc ...

Extracting data from a database using PEAR for Excel: Enumerating columns

One aspect of my website allows users to extract an Excel file using data from a database, with the help of the PEAR library. The Excel table that is generated currently has 32 columns. My main goal is to insert a 33rd column that assigns a number startin ...

Utilize the nth-child() selector to style list items within a specific tag

How can I style li tags using nth-child when these li tags are nested within a tag like this: <ul> <a href=""><li>this is a test</li></a> <a href=""><li>this is a test</li></a&g ...

Is tabIndex being used as a css property?

I'm trying to understand how to utilize the HTML attribute tabindex using CSS. Specifically, I want to assign tabIndex=0 to all div elements that have the className='my-checkbox'. This is my current approach: <div tabIndex="0" classNam ...

Having problems with OS X causing issues with my CSS, any solutions to resolve this?

After creating a form and styling it with CSS, I noticed that the form appears differently on my Mac than on Windows. The elements are smaller and the colors have changed. Why is OS X affecting my CSS? Is there a solution to this issue? Here is an image s ...

Unexpected lack of parameters in a function triggered by an event listener's callback

I am currently working with a functional react component where I am attempting to attach events to multiple elements simultaneously. However, I am encountering issues with passing a trigger element to a function. export default function Header() { cons ...

Create an input box featuring elongated lines

I'm currently working on coding an input box that resembles the design shown in the image below. https://i.sstatic.net/Wrpo0.png However, I've encountered an issue where only part of the lines are showing up with my current code. Interestingly, ...

What is the best way to showcase specific information from a JSON file with the help of

Our corporation is looking to showcase job listings that we promote on a prominent job platform. They provide us with JSON data so we can exhibit our company's job openings on our website. I aim to present essential information in a summary format, w ...

"Troubleshoot: jQuery UI accordion not functioning properly in response to

I've encountered an issue with the accordion functionality while working with dynamic data. To address this, I'm incorporating JavaScript to generate <div> and <h3> tags for the accordion. Here is the code snippet I am using: $(&ap ...

A step-by-step guide on changing the class of a focused contenteditable element with a button click using jQuery

I need assistance changing the class of an element within a contenteditable div. Here is my current code: <button>swap class</button> <div contenteditable="true"> <h1 class="line-height-1">Your Headline Here</h1> </di ...

Execute PHP and HTML code stored in a string

I have a string stored in a variable within my PHP code that looks like this: <?php $string = " <?php $var = 'John'; ?> <p>My name is <?php echo $var; ?></p> "; ?> When using the $string variable elsewhere, I ...

The alert box is not displaying, only the text within the tags is visible

Trying to implement an alert message for logged-in users. A successful login will trigger a success message, while incorrect username or password will display an error. function showMessage(response) { if (response.statusLogged == "Success login") { ...

Browser now replacing attribute translate="yes" with translate="translate"

We have encountered an issue with a translation library that is affecting the functionality of our page. <html lang="en" class="notranslate translated-ltr"> <meta name="google" content="notranslate"> As ...

A newline within the source code that does not display as a space when rendered

Written in Chinese, I have a lengthy paragraph that lacks spaces as the language's punctuation automatically creates spacing. The issue arises when inputting this paragraph into VSCode - there's no seamless way to incorporate line breaks in the s ...

Ways to rearrange items in the navigation bar?

I am working with a Bootstrap template and I am trying to adjust the layout to be right-to-left (RTL). Currently, when I set the site title to RTL in the navbar, the buttons in the navbar also switch to RTL alignment. This is not the desired layout I want. ...

Choose from a range of options using a dropdown menu with the ability to make multiple selections

Looking for a way to have a simple city drop down with multiple selection options? I tried using the 'multiple' attribute of the select tag, but it changed the dropdown into a box shape with multiple select options. What I really want is for the ...

What could be causing the incorrect execution of this MySQL update within PHP?

Recently, I've been trying to update a specific row in my database table. To locate the exact row for updating, I use an email address entered into a form as a search key before inputting the remaining data needed for the update process. However, ther ...

Struggling to make HTML5 geolocation coordinates function properly

I've attempted to work with examples in this code snippet, but I'm struggling to make the html5 geolocation coordinates function properly. Everything works fine when I hardcode the coordinates. var infowindow; var map; function initialize() ...