I want the hover effect to trigger specifically when I hover over the image within a column of a row using CSS

My issue lies with the hover effect on my webpage. Currently, the problem I am facing is that the hover effect activates whenever my mouse hovers over any part of the column in a row. How can I make it so that the effect only triggers when hovering over the image itself?

Below is the JSX code snippet from my project:

<div className="container App">
  <Header />
  <div className="caption-style-1 row">
    <div className="hover-img col-md-6 col-sm-6 nopadding">
      <div id="photography">
        <img
          src={require("../img/chaps_1x.jpg")}
          className="image"
        />
        <div className="overlay">
          <div className="text">Photography</div>
        </div>
      </div>
    </div>
  </div>
  <h4>CONTACT</h4>
</div>

And the corresponding CSS code block features the styling for the hover effect, where the issue persists as the entire column is affected by the hover rather than just the image:

.image {
 width: 50%;
 height: auto;
}

.overlay {
 position: absolute;
 top: 0;
 bottom: 0;
 left: 0;
 right: 0;
 margin-left:auto;
 margin-right:auto;
 height: auto;
 width: 50%;
 opacity: 0;
 transition: .5s ease;
 background-color: rgba(0,0,0,0.65);
}

.hover-img:hover .overlay {
 opacity: 1;
}

.text{
 color: white;
 font-size: 20px;
 position: absolute;
 top: 50%;
 left: 50%;
 transform: translate(-50%, -50%);
 -ms-transform: translate(-50%, -50%);
}

.nopadding{
 padding:0 !important;
} 

Answer №1

Learn about MDN's Adjacent sibling combinator

/* Styling paragraphs that follow an image */
img + p {
  font-style: bold;
}

li:first-of-type + li {
  color: red;
}
<ul>
  <li>One</li>
  <li>Two!</li>
  <li>Three</li>
</ul>

Answer №2

Upgrade this snippet

.hover-img:hover .overlay {
opacity: 1;
}

to

.hover-img img:hover+.overlay {
    opacity: 1;
}

Answer №3

To achieve this effect, you can utilize the following CSS style:

#photography:hover overlay {
   opacity: 1;
}

See the demonstration in action.

#photography:hover .overlay {
    opacity: 1;
}

.overlay {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    margin-left:auto;
    margin-right:auto;
    height: auto;
    width: 50%;
    opacity: 0;
    transition: .5s ease;
    background-color: rgba(0,0,0,0.65);
}

.text{
    color: white;
    font-size: 20px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    -ms-transform: translate(-50%, -50%);
}

.nopadding{
    padding:0 !important;
}

 
<div class="container App">
    <div class="caption-style-1 row">
      <div class="hover-img col-md-6 col-sm-6 nopadding">
        <div id="photography">
          <img src="https://test-ipv6.com/images/hires_ok.png" class="image" title="image" >
          <div class="overlay">
            <div class="text">Photography Photography Photography</div>
          </div>
        </div>
      </div>
    </div>
    <h4>CONTACT</h4>
</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

Is it possible for a scrollable div to fill the space above a BottomNavigation bar?

My current project involves creating a fixed bottom navigation bar, with a scrollable div above it displaying various card lists. In my attempts to achieve this layout, I consulted both the Material-UI documentation here and here. However, I struggled to ...

Optimize the layout with Grid CSS to allow the last two items to dynamically occupy

I've been working on a layout that looks like this: X X X X X X X X A B Currently, I have used grid-template-columns: repeat(4, 1fr); to define the columns. However, what I actually want is for the last two items to take up the full width of the rem ...

Wave your way to unique text creation with Firefox

Hey there, I've got some text inside a div that's been transformed with rotate(3deg). Strangely enough, in Firefox, the text appears wavy. When I remove the transform property from the div, the waviness disappears. Is there any way for me to keep ...

Are your divs getting truncated?

Currently faced with a perplexing issue where inserting elements into a div causes scaling problems, resulting in most of the divs being cut off. This glitch occurs when two new elements are added. Despite changing page sizes and thoroughly debugging by o ...

Using a CSS height of 100% does not always result in filling 100% of the parent's height

My parent div has a height of 250px. I then have multiple children with their min-height set to 100%. While the debugger confirms that the selector is correct and targeting the right elements, the height does not fill the parent's height at all; inste ...

Styling the text of segmented bars in ControlsFX using CSS

Is there a way to customize the size and color of the text for the ControlsFX segmented bar? I've attempted the typical method: -fx-font-size: 15px Unfortunately, it doesn't seem to be effective. Whether it's through CSS or code, I'm ...

How can I utilize a PHP variable as the height and width parameters in a CSS style?

After spending a considerable amount of time on this project, I am still struggling to make it work. Can someone please guide me on how to correctly assign the width and height values to the img element using variables? for ($x = 1; $x <= $aantal; $x+ ...

Text centered on hover for figure caption

Check out this fiddle http://jsfiddle.net/sLdhsbou/ where I am trying to center the "x" that appears on hover perfectly no matter what size the image is. Also, why does the transition not occur when moving the mouse outside of the figure, unlike when movi ...

Choose just one column within an HTML table

Is there a way to easily select the text of a single column from an HTML table using just the mouse pointer? Imagine you have a table that looks something like this: https://i.sstatic.net/n3lZR.png When trying to select only the text in the Lastname col ...

What is the best way to convert a string from a csv file into an integer using Python?

I'm currently working on a code that involves processing data obtained from this source () and organizing it into a CSV file. My goal is to create a program that calculates the BMI of each player, then identifies them as obese if their BMI exceeds 30 ...

Issue with the flexbox div not fully occupying the vertical space

I can't seem to get flexbox to fill the spaces as I want it to. I've been trying to figure out what I'm missing, but I just can't wrap my head around it. Here's a recreation of the issue with a link to a codepen. I want the div in ...

How can I position a div column-10 to the right and make it stay fixed in place

I'm having an issue trying to position a div on the right side without causing content to overlap with the navbar. I've attempted using `position:absolute top:0 right:0`, but it's not working as intended. <link href="https:// ...

Repeating background images in CSS

Hey there! I'm having a little trouble with my background image in HTML. It's showing up multiple times in a row and despite searching online, I can't seem to find the right solution. Here is the code snippet from my file: <!DOCTYPE html& ...

What is the best way to place a popover above a selected text section?

Imagine a situation where a body of text is present. When a word is highlighted within this text, a popover should appear with a tooltip positioned directly at the highlighted word. Think of it as similar to how a Mac displays definitions of words within ...

Struggling to make the right-side margin function correctly on a fixed navbar using a grid layout

Currently, I have successfully implemented a sticky top navbar. The issue arises when I try to add a 15vw margin to the right side of the logo image, similar to what I have done on the left side. Despite my attempts, it doesn't seem to work and I&apos ...

Manipulate CSS using jQuery's ID selector

I am attempting to modify a particular style of a div by its unique id, but it appears that my code is not functioning properly. The relevant codes are provided below: Jquery: $(document).ready(function () { $(".a_10.08.201223:56:49").hover(function(){ ...

Is there a way to customize the design of an HTML scrollbar to resemble the one often seen on Google search engine result pages

While casually browsing online, I stumbled upon something truly unique. The image below shows a Google search result in Firefox that I have never encountered before or since. This screenshot captured a search result that was scrollable within itself. Isn& ...

Is there a way for me to adjust my navbar menu items so they are aligned to the right

Currently, I'm designing a navbar and I'm facing an issue with aligning a specific part to the right side: Here is the current setup I have: Moreover, the collapsible navbar feature seems to be malfunctioning. When I try to shrink the screen, t ...

Hover over two different divs with JQuery

I have a situation where I have two HTML table rows. When I hover over the first row, I want to display the second row. However, once the mouse leaves both rows, the second row should be hidden. Is there a way to achieve this using JQuery? <tr class=" ...

Tips for creating a button that maintains consistency across different screen sizes

One of the images linked below shows a button displayed on two different sized screens, one 24 inches and the other 13 inches. The button on the 24-inch display appears normal, while the "Add to Cart" button on the 13-inch display is broken. Check out th ...