Switching background image when hovering over a link purely through CSS

I'm attempting to achieve a similar effect on my website, like this example:

(scroll down to "we specialize in")

I am fairly inexperienced with HTML and CSS, and I'm working within WordPress which adds an extra layer of complexity. I started my coding journey by asking this question on StackOverflow:

Change body background image on hover with pure html/css

However, I would like to include a transition between the images when they swap, and I'm also wondering where in the code I should adjust the font attributes.

div.link>a {
  displya: inline-block !important;
  position: relative !important;
  z-index: 100;
}

.bg1:hover+.background {
  background: url(https://aviel-albo.com/wp-content/uploads/2019/04/divorce-min.jpg);
}

.bg2:hover+.background {
  background: url(https://aviel-albo.com/wp-content/uploads/2019/04/Fotolia_37279445_Subscription_Monthly_M-min.jpg);
}

.background {
  background: transparent;
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
}
<div class=container>
  <div class="link">
    <a href="#" class="bg1">bg1</a>
    <div class=background></div>
    <a href="#" class="bg2">bg2</a>
    <div class=background>
    </div>
  </div>
</div>

Answer №1

One way to enhance the appearance of your code is by adding opacity transitions:

div.link>a {
  display: inline-block;
  position: relative;
  z-index: 100;
}

.bg1+.background {
  background: url(https://aviel-albo.com/wp-content/uploads/2019/04/divorce-min.jpg);
}
.bg1:hover +.background {
  opacity:1;
}

.bg2+.background {
  background: url(https://aviel-albo.com/wp-content/uploads/2019/04/Fotolia_37279445_Subscription_Monthly_M-min.jpg);
}
.bg2:hover +.background {
  opacity:1;
}

.background {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  transition:1s all;
  opacity:0;
}
<div class=container>
  <div class="link">
    <a href="#" class="bg1">bg1</a>
    <div class=background></div>
    <a href="#" class="bg2">bg2</a>
    <div class=background>
    </div>
  </div>
</div>

To optimize your code further, you can make use of this approach:

div.link>a {
  display: inline-block;
}

.bg1:before {
  background: url(https://aviel-albo.com/wp-content/uploads/2019/04/divorce-min.jpg);
}
.bg1:hover:before {
  opacity:1;
}

.bg2:before {
  background: url(https://aviel-albo.com/wp-content/uploads/2019/04/Fotolia_37279445_Subscription_Monthly_M-min.jpg);
}
.bg2:hover:before {
  opacity:1;
}

.bg:before {
  content:"";
  position: fixed;
  z-index:-1;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  transition:1s all;
  opacity:0;
}
<div class=container>
  <div class="link">
    <a href="#" class="bg1 bg">bg1</a>
    <a href="#" class="bg2 bg">bg2</a>
  </div>
</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

Button click initiates DataTables search rather than manually entering text in the input field

I am exploring the option of relocating the search function from an input to a button for a table that has been modified using DataTables. Currently, I have a customized input that triggers this function: <script> $(document).ready(function() { ...

substitute a variable

I am trying to update the value of a variable using an HTML form. Within my config.php file, I have declared a variable $num = 3; I would like the value 3 to be replaced with whatever I input in the form form.html. Below is the code for my HTML form : ...

The PHP implementation of Google reCAPTCHA is malfunctioning when combined with AJAX

I have a form on 100.php that makes an AJAX call to 200.php. <html> <head> <!-- Include the reCAPTCHA API JavaScript library provided by Google --> <script src='https://www.google.com/recaptcha/api.js'></script ...

Embedding a PDF file into an HTML form

For days, I have been on a relentless search for a solution to my problem to no avail. My ideal scenario involves embedding a fillable pdf form into an intranet html form for submission to the server, with the ability to parse field/values being a bonus b ...

Having trouble with my ReactJS application where click interactions are functioning properly on the header but not on my content

Objective: Implement an iframe displaying a YouTube video with play/pause functionality for users. Issue: Unable to interact with main content, but works correctly when placed in Navigation or Footer components. Attempted Solutions: Explored various de ...

Linking for default and French page internationalizations

Currently, I am working on SEO for a multi-language website that includes English, French, and Spanish versions of the web pages. For example, the English pages are located at example.com/en and have English content as default. <link rel="alternate" hr ...

JQuery-enabled button allows for the deletion of a dynamically generated list item

In my HTML file, there exists an ordered list: <ol id ="list"> </ol> As the user interacts with the page using JQuery and JavaScript, items are dynamically added to this list. Here's how it's done: $(function (){ $("#click-me") ...

Having trouble with the javascript dropdown functionality

I am currently facing an issue with my Javascript menu. The hover and click functionality for the main menu items is working perfectly fine, but I am struggling to make the dropdown feature work. Here is the code snippet from menu.js: /* When the user cl ...

An approach to modifying the value field within an HTML input tag using Python and Selenium

The html tag below displays an input field: <input id="input-value" title="Search" type="text" value=""> My goal is to modify the value from "" to "foo". <input id="input-value" ...

Guide on transferring a value from a Node.js document to an EJS file

var express = require('express'); var app = express(); app.use("/", function(request, response){ var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/"; MongoClient.connect(url, fu ...

Open Chrome in fullscreen mode directly from your Android home screen without the statusbar displayed

One of my clients is looking to have a specific website launched from an icon on an Android tablet. Since he is leasing the tablets, we have full control over the hardware. The goal is for these tablets to exclusively display his site (which is a slideshow ...

Error with Gmail displaying incorrect font

I've been struggling to change the font of my emails to Open Sans, especially with Gmail not rendering the correct font. However, I found a workaround for Outlook. Here is what I have: body { @font-face { font-family: 'O ...

When the browser window is resized to mobile view, a div is overlapped by an image

I've encountered an issue with the image size and position when resizing to mobile view in the browser. .extension { display: table; padding: 50px 0px 50px; width: 100%; height: auto; color: #fff; background-color: #558C89; ...

Why Isn't AJAX Displaying the Output from PHP?

Currently, I am implementing ajax to display a string received from a php file. This particular php file is generating rows for an html table. My goal is to have ajax place these rows within a with a specific id that resides inside a and I want this oper ...

Using PHP GD library to blend image hues with tint color

Is it possible to use GD in order to multiply a given color (RGB) with every pixel of an image resource (RGBA)? For instance, if the specified tint color is red (255, 0, 0), then a black pixel on the image resource should remain black (since 255 * 0 = 0) w ...

Leveraging an external Typescript function within Angular's HTML markup

I have a TypeScript utility class called myUtils.ts in the following format: export class MyUtils { static doSomething(input: string) { // perform some action } } To utilize this method in my component's HTML, I have imported the class into m ...

Can you explain the distinction between ' &:hover' and ' & :hover' pseudoclass selectors?

While styling a Material-UI Button, I encountered an unexpected behavior when adding hover effects. When using & :hover, only the inner span of the button was affected. However, when using &:hover, the desired style was achieved. What is the diff ...

Troubleshooting CSS Hover Not Displaying Properly in Angular 14

In the footer class of my HTML, there is a code snippet with chevrons: <div class="link-list"> <div *ngFor="let link of insideLinksLeft | originalOrderKeyValue" class="link"> <a [href]="link.val ...

Adjustable backdrop with CSS

How can I create a resizable dynamic background image in CSS that adapts to the size of the browser window? #mainPage { width: 100%; height: 100%; font-size: 3vw; text-transform: uppercase; background: black; font-weight: bold; ...

What is the best way to use regular expressions to match text with varied endings?

Here is my current situation. <h2>Details</h2>\n +<p>(.*)<br />|</p> ^ there's a tab space, wondering if there's a better way to show one or more (seems to be working) I am attempting to f ...