Hover causing opacity problem in CSS

Check out my code snippet on JSFiddle: http://jsfiddle.net/Vbtts/2624/ Here is the HTML structure:

    <a href="#" class="col-xs-6 col-sm-4 big-container with-background">
        <div class="bottom-align">
            <label class="uppercase">preséntation</label>
            <h2>Une entreprise<br> intégrée pour<br> contrôler la qualité<br> de toutes les étapes<br> des projets></h2>
        </div>
   </a>

And here's the corresponding CSS:

.with-background{width:100%;background:url(https://cask.scotch.io/2015/04/scotch-box-sidebar.png);display:block;height:200px;}
.with-background:hover{opacity:0.7}

I'm trying to achieve a hover effect where only the background image has reduced opacity, not the text content (label and h2). Any suggestions on how I can accomplish this? Thank you!

Answer №1

It's not possible to do so. However, there are two alternative solutions you can try:

1) Adjust the opacity of the png background image and place it on a black background using Photoshop/GIMP:

 .background {
      background: url(your-semitransp.png) #000;
 }
 .background:hover {
      background-color: transparent;
 }

By setting the background color to #000, you can mimic 100% opacity and then remove it on hover to make the background semitransparent.

2) Use two separate divs:

Create a structure like this:

<div class="wrap">
   <div class="background"></div>
   <div class="text"></div>
</div>

You can position the background absolutely and place the text above it.

 .wrap { position : relative; }
 .background {
     position :absolute;
     top: 0;
     left: 0;
     z-index: 0;
 }
 .text {
     position :absolute;
     top: 0;
     left: 0;
     z-index: 1;
 }
 .wrap:hover .background{ 
     opacity: 0.7;
 }


Using the ::after or ::before pseudo-elements:

You can achieve this by visiting your fiddle link:

http://jsfiddle.net/Vbtts/2632/

However, I personally dislike using negative z-index values, so I wouldn't recommend this approach.

Answer №2

I have made some updates to your jsfiddle, try using a before or after element in your CSS

.with-background{
  width:100%;
  display:block;
  height:200px;
  position: relative;
}
.with-background:before{
  width:100%;
  background:url(https://example.com/image.jpg);
  display:block;
  height:100%;
  content: " "; 
  position: absolute;
  top:0;
  left:0;
  z-index: -1;
}
.with-background:hover:before{opacity:0.2}

http://jsfiddle.net/Abcde/1234/

If you prefer to avoid negative z-index, you can also try this approach: http://jsfiddle.net/Fghij/5678/

Answer №3

While there isn't a CSS property called background-opacity, you can mimic it by adding a pseudo element with regular opacity that covers the entire element. This way, you can achieve the appearance of a transparent background without altering the HTML structure.

.with-background::after {
  content: "";
  background: url(https://example.com/background-image.jpg);
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  position: absolute;
  z-index: -1;   
}

.with-background:hover::after {
  opacity: 0.7;
}

You can view the updated jsfiddle here: https://jsfiddle.net/example/123/

For more information, check out this resource: https://example.com/tutorial

Answer №4

To achieve the desired effect, absolute positioning should be utilized. This will allow you to position the image behind the text without it being wrapped.

I have made changes to your fiddle as shown below:

<a href="#" class="col-xs-6 col-sm-4 big-container">
  <div class="with-background"></div>
    <div class="bottom-align">
                            <label class="uppercase">presentation</label>
                            <h2>A unified company<br> committed to<br> ensuring quality control<br> at every stage<br> of projects></h2>
                        </div>

                    </a>

CSS:

.with-background{width:100%;background:url(https://cask.scotch.io/2015/04/scotch-box-sidebar.png);display:block;height:200px; position: absolute; }
.with-background:hover{opacity:0.7}
.bottom-align {position: absolute; }

I trust this demonstrates how to achieve the desired outcome :)

Answer №5

Opacity can have an impact on how children perceive things, making it challenging to achieve your desired effect with opacity alone.

One possible solution is to incorporate a gradient with rgba into the background image. This creates an invisible overlay that becomes visible upon hover.

Invisible layer:

.with-gradient {
    background: linear-gradient(
        rgba(255, 255, 255, 0),
        rgba(255, 255, 255, 0)
    ),
    url('https://example.com/image.png');
}

Opacity applied on hover:

.with-gradient:hover {
    background: linear-gradient(
        rgba(255, 255, 255, 0.4),
        rgba(255, 255, 255, 0.4)
    ),
    url('https://example.com/image.png');
}

Check out the live example here - http://jsfiddle.net/5gnk28ah/1/

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

steps to receive an event within an AngularJS function

I'm encountering an issue with the event display in this code. <div class="div1" ng-click="displayinfo(event)"> sadfasf </div> $scope.displayinfo = function(event) { alert(event); } Does anyone have a solution for this problem? ...

sending additional parameters within a URL query string

Is it even possible to include a URL with get parameters within another get parameter? For example, I'd like to pass http://example.com/return/?somevars=anothervar as the value for the "return" parameter as shown below: http://example.com/?param1=4&a ...

Determining WebElement Clickability in Java and Selenium: Beyond the Basics of isDisplayed, isEnabled, and findElement

There is a common method to test if a WebElement is clickable: It typically involves something like this: public static boolean isElementClickable(WebDriver driver, String accessor){ return driver.findElements(By.xpath(accessor)).size() > 0 & ...

The footer seems to be losing its stickiness and not staying at the bottom when I

My goal is to create a sticky footer for my webpage. Once you click the add new sports button, a drawer slides out and the footer remains fixed at the bottom. However, as I scroll down the page, the footer moves up instead of staying in place. I have tried ...

What are the best ways to prevent JavaScript and CSS from blocking the render?

I ran my webpage through Google PageSpeed Insights and it keeps flagging an issue with render-blocking JavaScript and CSS in the above-the-fold content. The report indicates that there are 17 blocking scripts and 11 blocking CSS resources on the page. De ...

Strategies for optimizing PPI and DPI in responsive design

Imagine having two monitors: one 15.5 inches with a resolution of 1920 x 1080 and the other 24 inches with the same resolution. The first monitor has a pixel density of around 72 PPI, while the second has around 90 PPI. If I apply a media query in CSS for ...

What is the best way to show only the weekdays on the x-axis?

My goal is to create a scatter-linked graph using D3.js, showcasing the people count for various shifts on different dates in January 2020. Here is the code snippet I am working with: <!DOCTYPE html> <html lang="en" > <head> & ...

Having difficulty generating a footer for a page that includes a Material-UI Drawer component

Looking to create a standard full-width footer at the bottom of my page, I need help with the implementation. Utilizing the "Permanent drawer" from Material-UI Drawer for reference here. If you're interested in experimenting with it, CodeSandbox link ...

Why does my array seem to update only once in the view?

I am currently working on a project that aims to visually represent sorting algorithms, but I have encountered an issue. In order to effectively visualize the sorting process of an algorithm, it is crucial to display every change in the array as the proc ...

What is the method for identifying which individual element is responsible for activating an event listener?

While working on a color picker project in JavaScript for practice, I encountered an issue. I needed the #screen element to display the selected color when clicked. How can I determine which color has been clicked and pass its value to the function so that ...

Vue-powered custom mute/unmute button malfunctions inexplicably when deployed, despite functioning flawlessly during development phase

I'm currently struggling to understand why the mute/unmute button is not functioning as expected. The issue seems to arise when the code is deployed on Netlify, as it works fine on localhost. Essentially, the button mutes and unmutes the video, but th ...

Guide on accessing a method from a div element in VueJS

Is there a way to only render a div based on a specific condition and call a method when the condition is true? Here's an example of what I'm trying to achieve: <div v-if="timetable === null" @click="mymethodhere"> ...

Wrap the text within the contenteditable div with the <p> tag

By utilizing the code snippet below, I have managed to input text into a contenteditable div and instead of generating a new div on pressing the enter key, it generates <br> tags. Is there a way to enclose the text inside the contentedtiable div wit ...

A sleek fixed navbar in Bootstrap showcasing a shrunk logo positioned above the navbar

Is there a way to implement a fixed navbar with a parent logo using bootstrap? I would like the logo to shrink as you scroll, while still remaining visible within the fixed navbar. For a demo of the shrinking logo and navbar, you can visit: ...

easy method for creating a hyperlink that triggers a "download" pop-up box

Is there a simple and efficient way to have some of my links trigger the 'save file as' prompt (similar to right-clicking) immediately after they are clicked for the first time? ...

Align two tables horizontally in the center using HTML

Is there a way to center two tables side by side using CSS? I have been able to center a single table, but I am struggling to center two tables together. Can anyone provide a simple solution using CSS? Below are the codes I have used: @import url(ht ...

"Embedding Bootstrap, jQuery, and Twitter Bootstrap directly onto

I'm currently working on customizing some source code to fit my needs. To do this, I need to download and use Bootstrap, jQuery, and Bootstrap locally. I have already downloaded all of them to a directory named "libs". However, when I try to run the P ...

Issue with sending an ajax post request using Jquery

The jquery ajax request below is not working as expected: $(document).ready(function(){ var friendrequest = $(".friendrequest").val(); $(".afriendreq").click(function(){ $(".afriendreq").hide(); ...

To create a complete layout without relying on fixed positioning, ensure that the header, container, and footer collectively

Is there a method to ensure the header, container, and footer encompass the entire layout without using fixed positioning in CSS? Check out this HTML fiddle for reference. <div class="wrapper"> <header> <img src="https://www.ecobin.co ...

Displaying a picture solely in a designated region

I created a basic menu using an unordered list: <ul class="courses"> <a href="#"> <li class="spinny"> <h3>Course Title</h3> <img class="rotateme" src="images/logo_transparent.png"/> </li& ...