Hover over the text to reveal an image displayed in a separate div using CSS

I'm attempting to create a website similar to this one where hovering over text reveals an image in the background. Despite trying various methods suggested here, I haven't had any success so far. The closest I've come is following this solution, but it displayed the image within a small column next to the text. If achieving this effect with pure CSS is not feasible, I am open to using JavaScript as well. Below is the code snippet, and you can also view my JSFiddle here. Any assistance would be highly appreciated.

HTML

<div class="wrapper">
            <div class="menu">
                <div class="column">
                    <ul>
                        <li><span>column 1</span></li>
                        <li>filler 1</li>
                    </ul>
                </div>
                <div class="column">
                    <ul>
                        <li><span>column 2</span></li>
                        <li>filler 2</li>
                        <li><a id="preview1" href="#page1">link 1</a></li>
                        <li><a id="preview2" href="#page2">link 2</a></li>
                    </ul>
                </div>
                <div class="preview">
                <img id="preview1-show" src="http://via.placeholder.com/1000x500">
                <img id="preview2-show" src="http://via.placeholder.com/1000x500">
                </div>
            </div>
</div> 

CSS

.wrapper {
    width: 100%;
    height: 100vh;
    padding: 6em;
    position: fixed;
}

.menu {
    width: 100%;
    height: auto; 
    overflow: hidden;
}
.column {
    float: left;
    width: 20%;
    padding: 1em;
    text-align: center;
    z-index: 1;
}
.column ul {
    margin: 0;
    padding: 0;
}
.column li {
    position: relative; 
    display: block;
    padding: .3em;
}
.column li span {
    font-weight: bolder;
}
.column li a {
    display: inline-block;
    color: #000;
    text-decoration: none;
    cursor: pointer;
}
.preview {
    max-width: 100%;
    max-height: 100%;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    visibility: hidden;
}
#preview1:hover ~ #preview1-show {
  visibility: visible;
}
#preview2:hover ~ #preview2-show {
  visibility: visible;
}

Answer №1

When you hover over the text, I propose implementing the following CSS code:

{ 
    background-image: url("image url");
    background-repeat: no-repeat;
    background-position: your position;
}

Answer №2

I found the solution on my own. If anyone is interested, I decided to remove the css that targeted div id's and visibility:hidden; from .preview. Instead, I added display: none; z-index: -1; to .preview and utilized this JS code:

$(function(){
    $('#preview1').hover(function(){
        $('#preview1-show').show();
    },function(){
        $('#preview1-show').hide();
    });
    $('#preview2').hover(function(){
        $('#preview2-show').show();
    },function(){
        $('#preview2-show').hide();
    });
});

This method may not be perfect as it requires targeting each individual link (I have 13), but it functions exactly as I desired. If anyone knows of a more elegant solution, please feel free to share.

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

When interacting with elements with the "user-select: none" property, WkWebView still allows text selection

When using an iOS iPad app with a single fullscreen WKWebView, an issue arises when long-pressing on an area with user-select:none - the first text in the next available area without user-select:none gets selected instead of being ignored. Is there a way t ...

Node-modules CSS

In my React application, I am utilizing CSS modules. By configuring modules:true in my webpack.config.js, I can successfully apply styles from files within my src folder. However, when importing components from node-modules, the corresponding CSS is not be ...

Unable to generate a navigation panel using HTML/CSS and jQuery

I recently completed the basic courses in HTML/CSS, JavaScript, jQuery, and PHP on Codecademy. I'm currently working on creating a website using HTML/CSS and jQuery in Codecademy's codebits. However, I'm facing some issues with my navigation ...

How to align text to the right with ellipsis and button in a ReactJS component

In a specific scenario, I need to display a button on the right corner of the header. The selected filter value should appear next to the filter button like this: | Between 01/23/2018 and 07/30/2018 {button}| As the user changes the screen size, I want to ...

How come the centering of a text input field in an HTML form is proving to be so difficult using the text-align: center property?

Learning HTML has been quite the journey for me. One challenge I have faced is trying to center a search bar on my web page. Despite hours of searching and trying different tutorials, I still haven't been able to make it work. The CSS rule text-align ...

CSS: Applying a full-width background gradient works perfectly as long as the page remains stationary

Showing two screen captures demonstrating the effect with a small viewport that requires scrolling. This is how the HTML code appears: (excluding head and html tags) <body> <div id="grad1"></div> <div id="wrapper"> <header&g ...

Align an element to the right outside of its parent

I am facing an issue with my parent div and three children. I want one child to float left of the parent, one to be in the exact center of the parent, and one to float right of the parent. However, the floated right element is going outside of the parent d ...

Animation of shaking CSS circles

I have been working on creating a circle using CSS, but I've run into some issues that I can't seem to fix. There are two main problems I'm facing: When viewed in Chrome: The circles shake while rotating. When viewed in Firefox: A tail ...

The border in my HTML table is not displaying when I run my flask application

My flask application was running smoothly in a virtual environment until I encountered an issue with the html borders not showing up. Even after seeking help from a friend who knows some html, the problem still persists. app.py: from flask import Flask, r ...

Manipulating element height in AngularJS

My goal is to utilize the bootstrap style for a fixed navigation bar at the top of the page. By using navbar-fixed-top, I can fix an element to the top. The visibility of #subnav will be determined by the value of showsubnav. <div id="backnav"> ...

How can I make either the left or right section remain sticky for a certain period of time?

Can someone help me figure out how to fix a specific section in place temporarily, similar to the example provided below? In the link above, the Apple Watch product stays fixed while the content on the right moves partially, eventually causing the entire ...

Adjust the vertical alignment of spans within a div while ensuring that their height remains dynamic

Having a bit of trouble with my CSS. I have a single div with 3 columns inside: a span, an h3, and another span. Here's what I'm aiming for: - The div should adjust its height based on the content within the h3 tag ...

Retain the contents of the shopping cart even when the page is refreshed

For a course project, I am recreating a grocery store website and need assistance on how to retain the shopping cart values even after refreshing the webpage. Please inform me if more information is required... <button type="button" id= ...

Styles for Material UI 5 class names

After upgrading from Mui 4 to 5, I am facing a challenge in using class names effectively. While the SX property allows me to apply styles to individual components, I am struggling with applying the same class to multiple components. In version 4, my code ...

Using jQuery to add a class to an image element when the source attribute contains a specific

I have a bunch of text that looks like this <div id="justThisDIV"><p>Some paragraph <img src="http://mydomain.com/image.jpg"/> </p> <p><img src="http://mydomain.com/wow.png"/></p> <p><img src="http://notm ...

What are some effective techniques for personalizing Bootstrap?

Recently, I have found myself facing numerous challenges with Bootstrap. Although I used it to create a website in the past, I haven't touched it since. However, circumstances have now led me to incorporate it into my current project. I have downloade ...

access pictures from a different directory using JavaScript

I am working with an images array that contains three jpg files. I am trying to set the background image of a class called pic using images from the array. The issue I'm facing is that the images are stored in an images folder with the URL images/. I ...

Issues with CSS media queries not being responsive in Safari

Can anyone shed light on why media queries are not functioning properly in Safari? Here is an example: body { background-color:black; } @media screen and (min-width: 1024px) and (max-width: 1300px) { body { background-color:red; } } ...

What could be causing the malfunction of my Superfish menu in Firefox?

I am currently experimenting with the Superfish jQuery plugin to improve a drop-down menu on my website. Unfortunately, in Firefox browser (v. 21.0), the drop-down menu does not open when hovering over it as expected. However, it works fine in Chrome and O ...

Shifting the key of a choropleth map with R, rMaps, and datamaps

My challenge is to center the legend below a choropleth map of the United States. I don't have much experience with JS or CSS, but I decided to delve into the datamaps.all.min.js file in the R-3.2.1\library\rMaps\libraries\datamaps ...