Optimal arrangement for z-index and opacity

In my design, I have placed a nested link within two absolutely positioned divs structured like this:

<div class="container">
    <div class="leftPostHolder">
        <div class="leftPost">
            <h3><a href="#">link</a></h3>
        </div>
    </div>
</div>


The first div (leftPostHolder) has a black background, while the second one (leftPost) has a red background. When the second div is hovered over, its opacity is reduced to 0.5 and the link's background turns white. My goal is to position the link above these two divs so that the darkening effect only applies to the second div with the red background. The link itself should not be affected by the opacity change. I attempted to use z-index and positioning for the link without success. For the full code sample, visit: http://jsfiddle.net/v21t290a/

Answer №1

This issue is not related to the z-index property. Since your a tag is a child of leftPost, it will inherit the opacity set on its parent. Changing the z-index value will not resolve this problem.

To fix this issue, you have two options:

Option 1:

Move the a element so that it is no longer a direct child of leftPost.

or

Apply the opacity to an element that is not the parent of your link.

EXAMPLE:

$(".leftPost").hover(function() {
  $(".leftPost a").toggleClass("aHover");
});
.container {
  -webkit-transform: rotate(2deg);
  -moz-transform: rotate(2deg);
  -o-transform: rotate(2deg);
  -ms-transform: rotate(2deg);
  transform: rotate(2deg);
  -webkit-transform-origin: left center;
  -moz-transform-origin: left center;
  -o-transform-origin: left center;
  -ms-transform-origin: left center;
  transform-origin: left center;
}
.leftPostHolder {
  background-color: #000;
  position: absolute;
  width: 30%;
  height: 100px;
  overflow: hidden;
  -webkit-backface-visibility: hidden;
}
.leftPost {
  width: 100%;
  position: absolute;
  background-position: center;
  background-size: cover;
  height: 100%;
  -webkit-transition-duration: .2s;
  transition-duration: .2s;
  display: table;
  z-index: 2;
}

.leftPost:hover{
    
  transform: scale(1.05);
  -ms-transform: scale(1.05);
  -mos-transform: scale(1.05);
  -webkit-transform: scale(1.05);
  transform-origin: top center;
  -ms-transform-origin: center;
  -mos-transform-origin: center;
  -webkit-transform-origin: center;
}

.opacityDiv {
  width: 100%;
  position: absolute;
  background-position: center;
  background-size: cover;
  height: 100%;
  -webkit-transition-duration: .2s;
  transition-duration: .2s;
  display: table;
  z-index: 1;
}
.leftPost:hover + .opacityDiv {
    opacity: 0.5;
}
.leftPost h3 {
  height: 100%;
  display: table-cell;
  vertical-align: middle;
}
.leftPost a{
  color: transparent;
  text-align: center;
  padding:10px;
  display:block;
}
.aHover {
  color: #454545!important;
  transform: rotate(-2deg);
  background-color: #fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div class="container">
  <div class="leftPostHolder">
    <div class="leftPost">
      <h3><a href="#">link</a></h3>
    </div>
    <div class="opacityDiv"></div>
  </div>
</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

Tips for locating a specific HTML element within a string

Trying to convert a website into a phonegap app is proving to be challenging for me. When I send a request to the server, it responds with the HTML content as text. My goal is to extract certain HTML elements from this text and then append them to a div in ...

Choosing a dynamic dropdown option using jQuery and AJAX

I am facing an issue with a select box that is dynamically populated and should display information about a single option. The problem I am encountering is that the browser does not trigger a ':selected' event when I click on any of the options. ...

fixed divs that remain in place while scrolling

In the past, I have implemented a method similar to this: However, I am not a fan of the "flicker" effect that occurs when the page is scrolled and the div needs to move along with it. Recently, I have come across websites where certain elements (such as ...

Want to learn how to center a webpage only for a specific device width? For example, have your mobile device display the content in the center, while

Hello! I'm new to the world of @media queries and am currently trying to make my webpage responsive by centering it on smaller devices while keeping the display normal on larger screens. My website, mastercontrolunit.com, looks great in deskto ...

How can I create a reverse animation using CSS?

Is there a way to reverse the animation of the circular notification in the code provided? I want the circle to move forward, covering up the info and fading out. I've tried switching the animation around but haven't had success. Any suggestions? ...

Incorporating JavaScript unit tests into an established website

I am facing a challenge with testing my JavaScript functions in the context of a specific webpage. These functions are tightly integrated with the UI elements on the page, so I need to be able to unit-test the entire webpage and not just the functions them ...

Measure with an "em" unit indicated by a dot

Could you clarify if there is a distinction between writing padding:.6em and padding:0.6em; and if they have the same value, why not just use padding:0.6em; ? Thank you ...

Having trouble understanding why adding raw HTML is yielding different results compared to generating HTML using jQuery

I need assistance with two jsFiddles: http://jsfiddle.net/kRyhw/3/ http://jsfiddle.net/kBMSa/1/ In the first jsFiddle, there is code that adds HTML to my ul element, including an 'X' icon in SVG format. Attempting to recreate this functionali ...

The on-screen keyboard using JQuery and CSS is not working properly on an HTML redesign

UPDATE - Check out the modified version here: https://codepen.io/anon/pen/wZQjQy Here is the original for reference: https://codepen.io/anon/pen/YMRLBN (had to utilize CodePen, as simply adding a snippet here doesn't link the CSS) I essentially copie ...

Guide on extracting the ID within a for loop and incorporating it into a Vue.js function

In order to make an API request, I need to retrieve the id. However, whenever I try to include <a v-on:click="showRecipe({{inf.Id}})">Recipe</a> in my code, the entire page crashes. Removing this line resolves the issue. How can I pass the id ...

Looking for assistance in streamlining my jQuery code for bootstrap tab pane. Any takers?

Having trouble setting a specific tab as active when navigating from different links. I have found a solution, but it involves writing code for each individual tab. Can someone offer assistance in simplifying the code? <table class="nav nav-tabs"> ...

Formatting text to automatically continue onto the next line without requiring scrolling through long blocks of

I have a unique Angular project with a terminal interface that functions properly, maintaining a vertical scroll and automatically scrolling when new commands are entered. However, I am struggling to get the text within the horizontal divs to wrap to the ...

Arranging multi-level array with distinct keys and values

Users have the ability to add custom fields on a form. Note: I am looking for ways to streamline and organize the code more efficiently, with further explanations to follow. The HTML structure has been simplified for clarity: <form action="" method=" ...

Can I programmatically retrieve a comprehensive list of all global HTML attributes available?

Exploring the extensive list of global HTML attributes can be overwhelming, especially when considering how it continues to evolve with browser updates. Can JavaScript be leveraged to dynamically generate a complete list of these attributes without the ne ...

Tips for securely storing visitor-entered form data on our computer from our webpage

Is there a way to store the information filled out by visitors on our website? <ul class="form"> <li class="short"> <label>First Name<span class="required"></span></ ...

Clearly define where each navigation bar item should be displayed within your Django application using Bootstrap

I've been attempting to specify the exact page where this navbar dropdown should be displayed. I attempted adding this line: {% if request.resolver_match.url_name == 'club_selection' %} class = "active" {% endif %} but it doesn&ap ...

Maintaining my navigation menu as you scroll through the page

I've been working on creating a website for my business but I'm facing a challenge. My goal is to have a fixed navigation bar that stays in place as people scroll down the page, similar to what you can see on this website: (where the navigat ...

Discovering the chosen value from an asp.net radio button control based on its class can be accomplished using Jquery

Currently, I am working with a radio button control that is defined like this: <asp:RadioButtonList ID="rbQ2" CellSpacing="10" runat="server" class="rbQ2" CssClass="radioButtonList" RepeatDirection="Horizontal" RepeatLayout="Table" onclick="javascript: ...

What you see is what you get when it comes to effortlessly copying and pasting

As a web developer, I am aware that images cannot simply be copied from a local machine and pasted into a website - they must be uploaded. I have experience with wysiwyg tools like TinyMCE and FCKEditor, but my client is frustrated about not being able to ...

The window.frames function is coming back with a blank, empty

In an attempt to set up a simple HTML file on my local machine, I have the following code: <html> <head> <title>Testing</title> </head> <frameset framespacing="0" frameborder="0" rows="20px,100%" border="0"> ...