Connecting images and text from a distance

After days of researching this topic, I have not found any solutions that align exactly with what I am trying to achieve. I believe it should be a simple task using just HTML and CSS, but I seem to be facing some difficulties.

Initially, my goal was to make text appear when an image is clicked, pushing all other content out of the way. However, this proved too complicated and required strange positioning tricks or leaving empty space for the text until it appeared (which is not desired). Most suggestions pointed towards using JavaScript, but I prefer sticking to pure HTML and CSS.

Now, I have given up on that approach and instead, I am focusing on:

  • Creating a hover effect (that is also touch-friendly) on an image where the accompanying text gets highlighted or stylized.

HTML:

<div class="left-photos">
    <a href="diane">
    <img id="diane-photo" src="diane.jpg" alt="diane profile photo" /></a>
</div>

<div class="us-profile-text">
    <h4>Diane Adamec - Board President</h4>
    <p>TEXT HERE TEXT HERE TEXT HERE</p>
</div>

CSS:

.left-photos {
        width: 143px;
        float: left;
        margin-right: 10px;

    }

.us-profile-text p {
        padding: 0 5px 10px 5px;
        margin: 0;
    }

.us-profile-text {
    position: relative;
    font-size: .8em;
{

a:hover img {
        border: 5px solid #ff0000;
    }

Your assistance is greatly appreciated!

Answer №1

A great solution is to use just CSS for this task, but be sure to nest the hidden content within a parent div. If not possible, jQuery is always an option and still works quite well.

Pure HTML/CSS - Try This CodePen

HTML (Simplified classes for clarity)

<div class="container">
    <img id="profile-pic" src="http://animalia-life.com/data_images/dog/dog5.jpg" alt="profile picture" />
  <div class="details"> <!-- Ensure nesting -->
      <h4>John Doe - CEO</h4>
      <p>Lorem ipsum dolor sit amet.</p>
  </div>
</div>

CSS

.container {
  height:300px;
  width:100%;
}
.container img {
  width:100%;
  height:auto;
}
.details {
  display:none;
}
.container:hover .details {
  display:block;
}

If you need functionality on mobile devices, using a hover state won't suffice. In that case, consider implementing a click event with jQuery:

$(".container").click(function() {
  $(".details").css("display", "block")
})

There are multiple approaches to achieve this, so keep experimenting! Best of luck!

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

Achieving a scrolling body with a fixed header in a Vue b-table

Currently, I have implemented a b-table element on a webpage to display data from a database. The table is paginated, but feedback suggests that users prefer all information displayed in a single scrolling view. However, the issue arises when I attempt to ...

Remove the option to delete without making any changes to the flash file

Utilizing the DataTable javascript tool to export a grid, I have obtained the following HTML generated code: <div class="DTTT_container"> <a class="DTTT_button DTTT_button_copy" id="ToolTables_example_0" tabindex="0" aria-controls="e ...

My DIV elements are not responding to the widths I set for them. What could be the issue?

Recently, I've been delving into the world of Flex to experiment with some unique layouts. However, I encountered an issue where my divs were not recognizing widths as expected. I attempted to set the first div of each row to 5% width so they would ...

Custom-designed scroll bars that perfectly match the size of the content window

Currently attempting to adjust the layout of my webpages in order to enable scrolling for dynamic content across various pages. I have made adjustments to the header, footer, and sidebar elements, but am facing issues with implementing proper overflow prop ...

Tips for invoking a JavaScript class method from an HTML document

I've created a Typescript class that dynamically inserts an HTML element to a page after it's loaded. Here is the code snippet: const calcElement: string = ` <div class="container"> <span class="cc-title">Field</span> ...

Implement a scrolling feature to the tooltip when using "data-toggle"

I'm struggling with a piece of code that showcases a tooltip when the user hovers over the question circle icon. The tooltip's content can't exceed 1000 characters, but I need to figure out how to make the overflow scroll by adjusting the to ...

Activate the function within the same class only for the selected item

Hello there, I'm struggling to grasp how to make this particular functionality work. Basically, I have 8 divs, each containing an image used as a button with the same class (tm-img), and hidden divs with additional information. My goal is to initiall ...

What could be the reason for the hover attribute not functioning properly in NextJS

Why isn't my CSS recognizing the hover attribute? Here is my code: I would like the class "mega" to appear when hovering over the li tag. <nav className="mobile-hide"> <ul className="flex-item second-links&quo ...

Can you explain the distinction between <P> and <p> tags in HTML?

Currently, I am examining HTML documents by parsing them through Java and I have noticed that the p tag is one of the most frequently utilized tags. While I understand that it is used to create a new line, I am puzzled by the fact that in some documents I ...

Unable to use document.write when extracting code from the internet

I am currently developing a game using emulatorjs and ruffle. The goal is to have it all contained in a single downloadable file that can be run locally. I attempted to create another file for auto-updating purposes, but encountered issues with some of the ...

"Efficiently setting up individual select functions for each option in a UI select menu

I've integrated UI Selectmenu into my current project UI selectmenu includes a select option that allows for setting select behavior across all selectmenu options, as shown in the code snippet below: $('.anything'). selectmenu({ ...

Transparent dropdown elements using Bootstrap

I'm trying to incorporate a transparent bootstrap dropdown into my form. Specifically, I want the button itself to be transparent, not the dropdown list that appears when the button is clicked. Here's an example of what I currently have: https: ...

Rebel Spirit on a Wordpress Platform

I've encountered a strange issue on my Wordpress blog. For some reason, the code "&nbsp" is being inserted right after the opening <body> tag. I have checked the header.php file and there is no instance of this code present there. It's ...

The html-docx-js package generates incomprehensible text

Has anyone successfully utilized html-docx-js recently? I attempted the following code snippet: var newHTML = "<!DOCTYPE html><html><head lang='en'><meta charset='UTF-8'><title>Report</title& ...

A list containing various checkboxes

I have created a list with checkboxes in each item. I want to ensure that if any of the child checkboxes are checked, the parent checkbox is also automatically checked. Here's the HTML code: <input type="checkbox" id="parent"> <ul> ...

Having trouble making "jQuery.inArray" function properly in my code

Recently, I made a small interaction where list items are displayed and rotated when clicked - check it out here: http://jsfiddle.net/S79qp/430/ Lately, due to compatibility issues with IE8, I had to switch from using .indexOf() to jQuery.inArray. However ...

Failure to Present Outcome on Screen

Seeking assistance! I attempted to create a mini loan eligibility web app using JavaScript, but encountered an issue where the displayed result did not match the expected outcome upon clicking the eligibility button. Here is the HTML and JavaScript Code I ...

What advantages come with using PHP comments instead of HTML comments within HTML code?

Word on the street is that there might be, so I wanted to confirm. CSS comment: <!-- Comment your styles here. --> JavaScript comment: <script> // Add your comments here </script> ...

The Benefits of Semantic Class Names compared to Microdata

As I strive to use semantic class names, my exploration of microdata and SEO raises a question: Is it necessary to use both? Compare these two HTML snippets representing an event: Using CSS classes: <div class="event" itemscope itemtype="http://schema ...

Is it feasible to place an image on top of a box?

Hello everyone, I have been trying to figure out how to achieve a specific layout in React. I am using MUI so there is no need for any hard-coded CSS. However, my attempts have not been successful so far. The layout I am aiming for consists of an image on ...