What could be causing my opacity rule to impact neighboring elements?

I have designed button images and I want the image's opacity to change when a user hovers over it while keeping other elements like span and h2 completely visible. Despite targeting only the img element in my CSS for hover effect, all elements' opacity changes. Why is this happening? What mistake am I making and how can I correct it? Here's the code snippet:

body {
  background-color: black;
}

.featured-list li {
  display: inline-block;
  margin-right: .8%;
}

.featured-list a {
  display: block;
}

.featured-list {
  text-align: center;
  padding: 0;
  margin: 0 auto;
}

a.feature-img img {
  width: 425px;
  border-radius: 5px;
}

a.feature-img {
  text-align: center;
}


.f-cta, .f-img-content h2 {
  font-family: 'Abel',Helvetica,Arial,Lucida,sans-serif;
  font-weight: 100;
  position: absolute;
  left: 0;
  right: 0;
  margin: 0 auto;
}

.f-img-content h2 {
  color: #fff;
  top: 34%;
}

.f-cta {
  background: #fff;
  width: 50%;
  border-radius: 3px;
  padding: 7px;
  color: #000;
  top: 48%;
  font-size: 1.3em;
}

.f-img-content {
  position: relative;
  text-align: center;
  overflow: hidden;
}

a.feature-img:hover img {
  opacity: .6;
}
<ul class="featured-list">
      <li><a href="#" class="feature-img">
        <div class="f-img-content">
          <h2>Lorem Ipsum project title</h2>
          <span class="f-cta">View Project</span>
          <img src="https://i.imgur.com/EENJU66.gif">
        </div>   
      </a></li>
      <li><a href="#" class="feature-img">
      <img src="https://i.imgur.com/EENJU66.gif">
      </a></li>
      <li><a href="#" class="feature-img">
      <img src="https://i.imgur.com/EENJU66.gif">
      </a></li>
      <li><a href="#" class="feature-img">
      <img src="https://i.imgur.com/EENJU66.gif">
      </a></li>
    </ul>

Answer №1

To resolve the issue, rearrange the sequence of your elements by placing the image first, followed by h2 and span. This adjustment will correct the problem.


  <img src="https://i.imgur.com/EENJU66.gif">
  <h2>Lorem Ipsum project title</h2>
  <span class="f-cta">View Project</span>

Answer №2

Seems like there is a z-index problem. You can resolve it by simply adding z-index:1 to both the .cta and h2.

The issue arises due to the change in opacity, affecting the stacking order and causing the semi-transparent image to overlay the content. By adjusting the z-index of the content, you can fix this problem.

body {
  background-color: black;
}

.featured-list li {
  display: inline-block;
  margin-right: .8%;
}

.featured-list a {
  display: block;
}

.featured-list {
  text-align: center;
  padding: 0;
  margin: 0 auto;
}

a.feature-img img {
  width: 425px;
  border-radius: 5px;
}

a.feature-img {
  text-align: center;
}

.f-cta,
.f-img-content h2 {
  font-family: 'Abel', Helvetica, Arial, Lucida, sans-serif;
  font-weight: 100;
  position: absolute;
  left: 0;
  right: 0;
  margin: 0 auto;
  z-index: 1;
}

.f-img-content h2 {
  color: #fff;
  top: 34%;
}

.f-cta {
  background: #fff;
  width: 50%;
  border-radius: 3px;
  padding: 7px;
  color: #000;
  top: 48%;
  font-size: 1.3em;
}

.f-img-content {
  position: relative;
  text-align: center;
  overflow: hidden;
}

a.feature-img:hover img {
  opacity: .6;
}
<ul class="featured-list">
  <li>
    <a href="#" class="feature-img">
      <div class="f-img-content">
        <h2>Lorem Ipsum project title</h2>
        <span class="f-cta">View Project</span>
        <img src="https://i.imgur.com/EENJU66.gif">
      </div>
    </a>
  </li>
  <li>
    <a href="#" class="feature-img">
      <img src="https://i.imgur.com/EENJU66.gif">
    </a>
  </li>
  <li>
    <a href="#" class="feature-img">
      <img src="https://i.imgur.com/EENJU66.gif">
    </a>
  </li>
  <li>
    <a href="#" class="feature-img">
      <img src="https://i.imgur.com/EENJU66.gif">
    </a>
  </li>
</ul>

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 there a way to sort a table using a dropdown menu selection as a filter?

I am currently working on a project that involves a table with three columns, which are typically populated from a SQL database. My goal is to filter the third column based on the value selected from a dropdown menu. I came across a helpful tutorial on W3 ...

Stop :hovering on the link for iPad

My webpage contains sublinks with CSS properties as follows: #leftNav .searchBySub {...} #leftNav a.searchBySub:hover {...} #leftNav .searchBySubClicked {...} However, I have observed that on the iPad, the :hover styles are being applied. Is there a wa ...

"The sliding function of the React Bootstrap carousel is malfunctioning as it goes blank just before transitioning

Here is the code snippet I am working with: Whenever the carousel transitions to the next image, the current image disappears before displaying the next one. I am using react-bootstrap version 5.1.0, but it seems like there may be an issue with the transi ...

The reason why the div appears below the image

Is there a way to position the div above the image rather than below it, even when setting the z-index attribute in the code? The current code structure is as follows: <main id="content" role="main"> <div style="text-align: center"> & ...

To add a span within a contenteditable field, restricting the user from inputting beyond the inserted span

When the user presses the space key, I want an auto input span to appear. However, when I trace them in the devtools, range.setEnd(lasttextnode, 0); and range.collapse() seem to work correctly. The issue arises when the keyboard input is not right. I try t ...

Python 2.7 raises a HTTP Error 403 indicating that the requested URL is forbidden when using the

I've encountered an issue while using urllib2 on a particular website that was previously working fine. Despite trying various solutions found in forums, I haven't been able to resolve it. Below is an example of one such solution that didn't ...

The mouse scurries away once the div height has been adjusted

How can I make the height of #header change when hovering over #hoverme, and then revert back to its original height when the mouse leaves #hoverme? If anyone knows a solution, please check out my jsfiddle as it's not working as I intended. Here is ...

Creating a Sticky Header and Footer with Responsive Design: Ensuring Content Div Expansion between Them

Greetings. I am working on creating a simple responsive HTML layout as described below: HTML: <div id="header"></div> <div id="content"></div> <div id="footer"></div> CSS: #header{ wi ...

Python web scraping for IMDb data

I am currently working on analyzing data from a previous Harvard CS 109 course and I'm having trouble extracting the ratings from the top 250 most voted movies in the dataset. It seems like there are two instances of td.ratingColumn, one containing th ...

Tips for arranging images of varying heights on separate lines so they appear cohesive as a group

I've been working with masonry.js but I'm still not achieving the effect I want. Javascript:` var container = document.querySelector('#container'); var msnry = new Masonry( container, {columnWidth: 200, itemSelector: '.item' ...

Display the contents in a textarea with modal, not as a string

I'm currently working on implementing a modal overlay window with simplemodal that will display the contents of a text area. My goal is to show the rendered output rather than just the string value. For example, if a user enters HTML or includes an im ...

Encountering an issue with angular ag-grid: "Module build failed (from ./node_modules/sass-loader/dist/cjs.js): SassError: Invalid CSS after "adecimal"

I've encountered an error while using node-sass, sass, or both. Even after removing node modules and clearing the cache, the error persists. I've attempted different versions of node-sass and sass, but the issue continues to arise. This problem a ...

Using ngModel to bind data within an Angular dialog box

I'm facing an issue with my project where changes made in the edit dialog are immediately reflected in the UI, even before saving. This causes a problem as any changes made and then canceled are still saved. I want the changes to take effect only afte ...

What is the best way to surround the initial x words within an element with a span tag?

Is there a way to style the first 10 words of a paragraph in a different color when the content is constantly changing? I can't manually add a span in the HTML. My approach involves using JavaScript to iterate through all instances of .overview__text. ...

Alignment problem

In my design, I have a toolbar with flex display and I am trying to center my span element within it. However, I seem to be missing something in the CSS. CSS .toolbar { position: absolute; top: 0; left: 0; right: 0; height: 60px; d ...

What is the default DTD used if it is not specified in the doctype declaration?

On my webpage, the doctype is specified as: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> No DTD is explicitly set. I'm curious which DTD will be defaulted in IE? It seems that it doesn't function the same way as "http ...

instructions on how to showcase a text message within the fontawesome square icon

Can someone help me with styling my code? I want to display a text message inside a square box with dimensions of 1x. <span class="fa-stack fa-5x" style="margin-left:5%"> <i class="fa fa-square-o fa-stack-2x"></i> </span> ...

The formatting of the datepicker-popup is malfunctioning when the initial value is set within the scope

I have implemented the Angular UI bootstrap date picker popup using a custom directive on Plunker (http://plnkr.co/edit/053VJYm1MpZUiKwFTfrT?p=preview): //Module var userModule = angular.module("userModule",['ui.bootstrap']); //Controller userM ...

The code encountered an error because it was unable to access the property 'style' of an undefined element on line 13 of the script

Why is it not recognizing styles and showing an error? All paths seem correct, styles and scripts are connected, but it's either not reading them at all (styles) or displaying an error. Here is the html, javascript, css code. How can this error be fix ...

The variable $ has not been defined in Jquery framework

<html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> <script type="text/javascript" src="deployJava.js"></script> <script type="text/javascr ...