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

Scrolling down a jQuery div after each new item is added.OR

I have a div acting as a chat feature, where messages are appended based on user actions. I am trying to make it so that the div automatically scrolls down after each message is added, but I'm encountering some issues with this functionality. You can ...

Emphasize multiple anchor points

I am looking to highlight two sections of a page simultaneously. Here is what I currently have: <li class="langLI" style="list-style-type:none"><a href="/non-discrimination-and-language-assistance##French">| French Creole</a></li> ...

What's the best way to refresh append() functionality within a modal? I'm currently working with NODE JS and AJAX and

Whenever I click the modal, the append() calls are adding HTML content. But if I try to use empty() or html(), the modal stops appearing altogether. What is the correct approach to creating this modal? function loadModalContent(id) { $('#myModal& ...

The columns in the table are all displaying varying widths even though they have been defined with fixed table layout

I have a nested table displayed in my HTML, and I am attempting to set each column to be 50% width, but for some reason it's not working. In the past, whenever I've needed to accomplish this, applying table-layout: fixed has usually done the tri ...

Arrangement of Div Box

Here is the HTML code that I am currently working with: <div class="item"> <div class="pageheader"> Header </div> <div class="info"> Created on... </div> <div class="image"> I ...

What is the best way to send the value from a textbox to this script?

My challenge is with this particular textbox: <input type="text" autocomplete="off" required="required" id="bar" name="bar" class="form-control" placeholder="Barcode"> Also, there's a button in the mix: <button type="button" style="float:r ...

Leverage CSS styling for database values within a PHP framework

How can I style MYSQL database values in HTML using CSS? My vegetarian database row has 'Y' for yes and 'N' for no. I want to apply different styles to these values using CSS as I display them on my PHP-based page. if ($result1->num_ ...

Run a PHP script on the current page upon choosing an option from a dropdown list with the help of Ajax or JavaScript

I'm currently working on a MySQL query that needs to be executed when a user selects options from multiple dropdown lists. What I am looking for is the ability to automatically run a query related to the selected dropdown list option using AJAX/JavaS ...

Sort various divs using a list

I have multiple divs containing different content. On the left side, there is a list of various categories. When a category is clicked, I want to display the corresponding div for that category. Initially, I want the main category to be loaded, with no opt ...

It's impossible for me to align two blocks at the same level

Check out the following code snippet. I am trying to group mid_left and mid_right within a single mid div tag, but struggling with positioning mid_right correctly - either outside of the mid tag or not at the same level as mid_left. I have experimented wit ...

Click on the entire Material-UI formControlLabel to select it, not just the text

I have recently started working with the material UI. Currently, I am dealing with a form that looks like this: <FormControl variant="outlined" className={css.formControl} margin="dense" key={"abc_" + index} > <FormControlLabel cont ...

Creating a Product Box design with CSS and incorporating visual elements

I have created a simple box design in Photoshop that I want to use to display product information. The box consists of a Header, Body, and Button, each as separate images. How can I assemble these elements using CSS/HTML? I only need the header text at th ...

What is the best way to close all other accordion tabs when selecting one?

A simple HTML code was created with the full pen accessible here. <div class="center"> <div class="menu"> <div class="item"> <button class="accordionBtn"><i class=&q ...

Tips for customizing text field appearance in Safari and Chrome

I haven't had a chance to test it on IE yet. My goal is to create a unique style for the background image and text box shape, along with borders, for the search bar on my website, [dead site]. If you check it out on Firefox or Opera and see the sear ...

Using PHP variables in CSS styling

Let me explain the situation. In my index.php file, I have a class called 'rectangles' which includes properties for color and size. I defined a variable named $rectangle($rectangle=new rectangles('red',25);). Additionally, I include ...

Initial loading issue with HTML5 Canvas in Android WebView

As I work on developing a HTML5 canvas-based game within a WebView of an existing application, I encounter a puzzling issue. Upon the initial run of the game, everything seems to be in order - logs indicate that it's ready and running, yet nothing is ...

Generate text in reStructuredText with columns

I'm attempting to style a reStructuredText segment in the following layout: type1 : this type4 : this type7 : this type2 : this type5 : this type8 : this type3 : this type6 : this type9 : this I want to fill the page with tex ...

Passing a click event to a reusable component in Angular 2 for enhanced functionality

I am currently working on abstracting out a table that is used by several components. While most of my dynamic table population needs have been met, I am facing a challenge with making the rows clickable in one instance of the table. Previously, I simply ...

Ways to ensure that a div, header, container, and other elements completely cover the viewport

Currently, I am attempting to create a header that spans the entire viewport regardless of screen size. This header will be split horizontally into 3 equal sections, each occupying one third of the header space. I experimented with setting the header hei ...

How can I change :hover to a clickable element instead?

I attempted to create a full-width accordion with the following code: .page { margin: 0; padding: 0; display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex; height: 100vh; } .content { -webkit- ...