Is it possible to change the text displayed when hovering over various images using just CSS and HTML?

I have created an image and text hover effect. There are four images and corresponding paragraphs for each image. When hovering over an image, the specific paragraph should replace the previous one using only HTML and CSS. Please note the following: The design should ensure that the discovery paragraph is active if no other text is hovered over. The code below shows my attempt:

HTML Code:

<div class="image-slide">
        <a href="#discovery"><img src="discovery.jpg" alt="discovery"></a>
        <a href="#recommendation"><img src="recommendation.jpg" alt="recommend"></a>
        <a href="#implementation"><img src="implementation.jpg" alt="implementation"></a>
        <a href="#review"><img src="review.jpg" alt="review"></a>
    </div>

    <div id="discovery" class="paragraph-slide">
        <h3>Discovery</h3>
        <p>This meeting is held about one week after the FIT Meeting and normally runs about two hours. This is where all the pertinent financial data is gathered and reviewed. We take a very comprehensive approach, therefore needing every detail to formulate the right financial plan to work towards your needs and goals.</p>
    </div>

    <div id="recommendation" class="paragraph-slide">
        <h3>Recommendation</h3>
        <p>Similar to Discovery, this meeting is crucial in gathering and reviewing financial data for developing the right plan suited to your individual needs and goals.</p>
    </div>

    <div id="implementation" class="paragraph-slide">
        <h3>Implementation</h3>
        <p>Once the recommendations are made, it's time to put them into action. This step involves executing the proposed financial plan to achieve your desired outcomes.</p>
    </div>

    <div id="review" class="paragraph-slide">
        <h3>Review</h3>
        <p>After implementing the plan, regular reviews and adjustments are essential to ensure it continues to align with your financial goals and aspirations.</p>
    </div>

CSS Code:

.image-slide{
    margin: 50px 0px 30px 50px;
}

.company-process h3{
    font-family: 'Cinzel-Regular';
    font-size: 22px;
    color: #1a1a1a;
    text-align: center;
    margin:50px 0 5px 0;
}

.company-process img{
    cursor: pointer;
}

.company-process .paragraph-slide p{
    font-family: 'Raleway-Regular';
    font-size: 16px;
    color: #666666;
    margin:20px 50px 20px 50px;
    padding: 0;
}

Answer №1

I tackled a challenging task and managed to grasp the essence of what you're looking for. Although I utilized different HTML tags, I believe my code is quite comprehensible. However, if you need any clarifications, feel free to ask :)

Here's a brief overview of my approach:

In the initial step within the HTML code, I enclosed all the images (contained in figure tags) in a div element with the "container" class. Additionally, I rearranged the sequence of images.

<div class="wrapper">
  <div class="container">
    <figure>
      <span>image 4 - review</span>
      <figcaption>This is review</figcaption>
    </figure>
    <figure>
       <span>image 3 - implementation</span>
      <figcaption>This is implementation</figcaption>
    </figure>
    <figure>
       <span>image 2 - recommendation</span>
      <figcaption>This is recommendation</figcaption>
    </figure>
    <figure>
       <span>image 1 - discovery</span>
      <figcaption>This is discovery</figcaption>
    </figure>
  </div>
</div>

But why reverse the order? Let's proceed to the next step below.

The second step involves the CSS aspect. I decided to reverse the image order based on the concept that the last image's caption should be displayed without hovering while remaining hidden when its siblings are hovered over. Initially, I applied "float: right" to ensure proper output order.

figure {
  display: inline-block;
  float: right;
  margin: 5px;
}

By default, all captions are set to "display: none" except for the last image's caption (which corresponds to image 1 in the HTML code sequence) which is set to "display: block".

figcaption {
  position: absolute;
  left: 0;
  display: none;
}

figure:last-child figcaption {
  display: block;
}

Subsequently, whenever the image captions, excluding the last one, are hovered over, they switch to "display: block".

figure:not(last-child):hover figcaption {
  display: block;
}

Finally, to hide the last image's caption (image 1 in the HTML code order), use the "~" selector (figure:not(last-child):hover ~ figure:last-child figcaption) in CSS.

figure:not(last-child):hover ~ figure:last-child figcaption {
  display: none;
}

The "~" selector essentially references all preceding elements.

Below is the complete code snippet:

.container {
  float: left;
}

figure {
  display: inline-block;
  float: right;
  margin: 5px;
}

figure span {
  display: block;
  width: 150px;
  height: 100px;
  border: 1px solid;
}

figcaption {
  position: absolute;
  left: 0;
  display: none;
}

figure:last-child figcaption {
  display: block;
}

figure:not(last-child):hover figcaption {
  display: block;
}

figure:not(last-child):hover ~ figure:last-child figcaption {
  display: none;
}
<div class="wrapper">
  <div class="container">
    <figure>
      <span>image 4 - review</span>
      <figcaption>This is review</figcaption>
    </figure>
    <figure>
       <span>image 3 - implementation</span>
      <figcaption>This is implementation</figcaption>
    </figure>
    <figure>
       <span>image 2 - recommendation</span>
      <figcaption>This is recommendation</figcaption>
    </figure>
    <figure>
       <span>image 1 - discovery</span>
      <figcaption>This is discovery</figcaption>
    </figure>
  </div>
</div>

Answer №2

If you want to incorporate some JavaScript into your website, take a look at the following code snippet:

$('.photo-gallery div').on('click', function() {
  $('.description').hide();
  var target = $(this).attr('data-target');
  $(target).show();
});
.photo-gallery {
  margin: 20px;
}

.description h3 {
  font-family: "Roboto", sans-serif;
  font-size: 18px;
  color: #333;
  text-align: center;
  margin: 10px;
}

.description p {
  font-family: "Open Sans", sans-serif;
  font-size: 14px;
  color: #666;
  margin: 5px;
}

.description {
  display: none;
}

.description.active {
  display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="photo-gallery">
  <div data-target="#nature-info">Nature</div>
  <div data-target="#city-info">City</div>
  <div data-target="#beach-info">Beach</div>
</div>
<div id="nature-info" class="description active">
  <h3>Nature</h3>
  <p>Beautiful natural scenery captured in stunning photographs.</p>
</div>
<div id="city-info" class="description">
  <h3>City</h3>
  <p>Vibrant cityscapes from all around the world.</p>
</div>

<div id="beach-info" class="description">
  <h3>Beach</h3>
  <p>Relaxing beach views that transport you to paradise.</p>
</div>

Answer №3

Here is a suggestion you could consider.

Place the text within the anchor tag. Create a panel with relative positioning to place the text inside. Initially hide the text, and when the anchor is hovered over, display the text.

.pane {position:relative;}
.pane .image-slide img {height:100px;}
.pane a {float:left;}
.pane .paragraph-slide
{
  position:absolute;
  display:none;
  top: 150px;
  left: 0;
}

.pane a:hover .paragraph-slide {
  display:block;
}


.image-slide {
  margin: 50px 0px 30px 50px;
}

.company-process h3 {
  font-family: 'Cinzel-Regular';
  font-size: 22px;
  color: #1a1a1a;
  text-align: center;
  margin: 50px 0 5px 0;
}

.company-process img {
  cursor: pointer;
}

.company-process .paragraph-slide p {
  font-family: 'Raleway-Regular';
  font-size: 16px;
  color: #666666;
  margin: 20px 50px 20px 50px;
  padding: 0;
}
<div class="pane">
  <div class="image-slide">
    <a href="#discovery"><img src="https://www.fillmurray.com/200/300" alt="discovery">
      <div id="discovery" class="paragraph-slide">
        <h3>Discovery</h3>
        <p>This meeting is held about one week after the FIT Meeting and normally runs about two hours. This is where all the pertinent financial data is gathered and reviewed. We take a very comprehensive approach,and therefore need every itty bitty detail
          to formulate the right financial plan to work towards your needs and goals.</p>
      </div>
    </a>
    <a href="#recommendation"><img src="https://www.fillmurray.com/300/300" alt="recommend">
      <div id="recommendation" class="paragraph-slide">
        <h3>Recommendation</h3>
        <p>This meeting is held about one week after the FIT Meeting and normally runs about two hours. This is where all the pertinent financial data is gathered and reviewed. We take a very comprehensive approach,and therefore need every itty bitty detail
          to formulate the right financial plan to work towards your needs and goals.</p>
      </div>

    </a>
    <a href="#implementaion"><img src="https://www.fillmurray.com/400/300" alt="implementation">
      <div id="implementaion" class="paragraph-slide">
        <h3>Implementaion</h3>
        <p>This meeting is held about one week after the FIT Meeting and normally runs about two hours. This is where all the pertinent financial data is gathered and reviewed. We take a very comprehensive approach,and therefore need every itty bitty detail
          to formulate the right financial plan to work towards your needs and goals.</p>
      </div>
    </a>
    <a href="#review"><img src="https://www.fillmurray.com/200/300" alt="review">
      <div id="review" class="paragraph-slide">
        <h3>Review</h3>
        <p>This meeting is held about one week after the FIT Meeting and normally runs about two hours. This is where all the pertinent financial data is gathered and reviewed. We take a very comprehensive approach,and therefore need every itty bitty detail
          to formulate the right financial plan to work towards your needs and goals.</p>
      </div>
    </a>
  </div>
</div>

Answer №4

Employ the title attribute in img tags like this:

<img src="...." alt="this text will display if image fails to load" title="this text appears when hovering"/>

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

Issue with input width percentage not functioning as expected

Is there a special method to specify the width of an input field in CSS without it extending beyond the container? I want my input field to be 98% of the container's width, but when I set it to that, it goes off the screen. This is the HTML code I am ...

Tips for optimizing HTML and CSS for better browser performance

Just starting to learn HTML, CSS through an online course and currently working on a simple website. The table of contents on my website is centered, but when I resize the browser on my 27-inch iMac, it doesn't stay centered. Any tips? Here's a s ...

I'm looking to create a unit test for my AngularJS application

I am currently working on a weather application that utilizes the to retrieve weather data. The JavaScript code I have written for this app is shown below: angular.module('ourAppApp') .controller('MainCtrl', function($scope,apiFac) { ...

Enhancing the functionality of a bootstrap button with the addition of an

My goal is to have a button that opens a URL in a new tab. I've managed to achieve this using window.location.href, but it doesn't open the URL in a new tab. The code I'm working with is written in jQuery and Javascript, and it's part ...

hover effect with fading transition while mouse is still hovering

Is there a way to create a fade-in/fade-out effect on a div without the mouse needing to leave the area? Let me provide a clearer explanation: When the mouse enters the object The object gradually fades in Then, after a delay, the object fades out while ...

Using JavaScript, conceal a specific Div by examining the content within another Div

I am attempting to implement some logic using JavaScript. The goal is to hide the "cart-button" div if the innerHTML value of a div with the class "b-format" is set to Audio, otherwise hide the "more-button" div. However, for some reason this functionality ...

PHP error: Index not defined

One challenge I’m encountering is with a dating site form that includes the following categories: firstname,lastname,username,password,email,mysex,yoursex,relationship,date of birth, and country I've completed the PHP code to send information to a ...

Adjust the height of the box based on the content within its flexbox

I have a flexbox div nested inside another div, and I'm trying to adjust the height of the outer box based on the content of the inner flexbox. Here is the link to the current fiddle. The issue I am facing is that the text in the second box overflows ...

Tips on automatically adjusting the width of a div to fit the content, maintaining center alignment, and ensuring the contents float to the left

I am facing an issue with a div element that has multiple children. My goal is to have the children neatly fit to the bottom left of the grid when the window expands, utilizing the next available space efficiently. However, as the div expands with the wind ...

Hierarchy in CSS: The battle between author and user styling

Following a hierarchy of precedence: User agent declarations User normal declarations Author normal declarations Author important declarations User important declarations The CSS specification distinguishes between author and user: Author. The author s ...

Creating responsive images in Bootstrap columns

I've found a solution to my issue with large images, but I am still struggling with smaller images not fitting well into larger spaces. Essentially, I created a CMS for a user who required templates with various bootstrap columns as content areas. Cu ...

Issue with closing collapsed menu on click in Bootstrap version 3

Here is the code snippet: <li> <a href="#" id="statics" class="main-bar"> <span class="glyphicon glyphicon-stats" aria-hidden="true"></span> </a> </li> I am trying to toggle the responsive menu on eleme ...

Notify user with a Javascript alert if there are no search results found

I have developed a search index for Chicago employees and want to create an alert if no matching records are found. However, I am struggling to determine the value that needs to be inserted in case of an empty result set. Ideally, upon submission of the fu ...

HTML/JavaScript: Embrace the Power of Dynamic Page

I have a unique element in my HTML code: <image src="http://..." style='...'> Using Python-Flask, I pass on a dynamic source address and save it as window.dynamicEmbedding. Now, during page load, I want to change the image's ...

Dynamic sliding effect in CSS for seamless showing and hiding of div elements

I stumbled upon a fantastic solution in these forums How to create sliding DIV on click? However, what I really wanted was for the content to fade in and out with just a click of a button. Here is the code snippet I am currently working with: <html> ...

The container's :before pseudo-element features a sleek white border

I have been experimenting with using the :before and :after pseudo-elements to generate diagonal lines within a container. However, I am encountering an issue where these pseudo-elements appear to have a white bottom border, and I am unable to determine t ...

Anticipated spatial glitch problem involving the gadicc/meteor-reactive-window package for Meteor

Utilizing the gadicc/meteor-reactive-window Meteor Package to switch templates based on screen size. This file is named pictureDisplatSection.html <template name="pictureDisplaySection"> <div class="display"> ...

Switch up the appearance of a document by manipulating the stylesheet using a select tag

I'm currently facing an issue with the implementation of a drop-down box on my website for selecting different themes. Despite having the necessary javascript code, I am unable to get it working correctly. Here's the snippet: //selecting the sele ...

keep the color of the link after clicking until a different link is clicked

At the bottom of every post on my website, there are 3 buttons: "Written By", "Related Post," and "In This Category": SoCatchy! I'm looking to have each button change color when clicked, and stay that way until another link is clicked. Here is the c ...

Getting the value of dynamically generated buttons when they are clicked in PHP - a simple guide

In my PHP code, I have successfully created dynamic buttons. However, I am facing an issue where I need to retrieve the value of a specific button when it is clicked, and populate all the information in a form. Below is the code snippet for handling the b ...