The presence of the 'absolute' attribute on an element disrupts the smooth fading

I am encountering an issue where the text I am trying to fade just suddenly jumps to its final state without any smooth transition in between, and there is also a delay. I have been using CSS properties like opacity: 0/1 and transform: opacity for the fading effect. After a thorough debugging process, I realized that this unexpected behavior was caused by an unrelated element with position: absolute underneath. You can view the example in this code snippet below:

var $root = $('.root');
$('#butt_fadetext').click(function(){
  $root.toggleClass('dermineyda');
});
$('#butt_hidefancy').click(function(){
  $root.toggleClass('nofancy');
});
.root {
  background-color: #b87988;
}

.container {
  transition: opacity 0.5s;
  opacity: 1;
  height: 100px;
}

.dermineyda .container {
  opacity: 0;
}

.inn {
  position: absolute;
  z-index: 1;
}

.unrelated {
  background-color: blanchedalmond;
  width: 100px;
  height: 30px;
  position: absolute;
  top: 0;
  left: 0;
}

.nofancy .unrelated {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="root">
  <div class="container">
    <div class="inn">text</div>
  </div>
  <div class="unrelated">fancy fx</div>
</div>
<input type="button" value="fade text" id="butt_fadetext" />
<input type="button" value="hide fancy fx" id="butt_hidefancy" />

Answer №1

Just discovered that adjusting the opacity of an element can impact its position within the stacking context:

Changing the opacity to a value other than 1 creates a new stacking context for the element.

Solution

The issue was resolved by manipulating the order of the element within the stacking context.

var $root = $('.root');
$('#butt_fadetext').click(function(){
  $root.toggleClass('dermineyda');
});
$('#butt_hidefancy').click(function(){
  $root.toggleClass('nofancy');
});
.root {
  background-color: #b87988;
}

.container {
  transition: opacity 0.5s;
  opacity: 1;
  height: 100px;
  position: relative;
  z-index: 500;
}

.dermineyda .container {
  opacity: 0;
}

.inn {
  position: absolute;
  z-index: 1;
}

.unrelated {
  background-color: blanchedalmond;
  width: 100px;
  height: 30px;
  position: absolute;
  top: 0;
  left: 0;
}

.nofancy .unrelated {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="root">
  <div class="container">
    <div class="inn">text</div>
  </div>
  <div class="unrelated">fancy fx</div>
</div>
<input type="button" value="fade text" id="butt_fadetext" />
<input type="button" value="hide fancy fx" id="butt_hidefancy" />

Answer №2

Instead of trying to recreate the wheel, it's best to utilize the tools already available to you. Since you're working with jQuery, make use of the fadeToggle(...) function.

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

Why does Facebook utilize the FB_HiddenContainer div element on their website?

This particular element has been assigned dimensions of 0px and is positioned absolutely at -10000px. I have observed similar hidden divs on various websites, usually located near the beginning of the source code with no additional elements inside. ...

Produce an additional page while remaining on the present one

I have a PHP-based invoice system that displays all customer invoices. I am interested in adding a feature that allows users to print an invoice directly from the list page without navigating away. While I am familiar with window.print() and media="print" ...

Enhance images using css or javascript

I have a quick query regarding website image editing. I was tasked with incorporating a rating system where users can give 1-2 or 3 star ratings. Each item will display only one star. The requirement is that the star updates as shown in the examples below ...

Issue with spacing in CSS sticky header on mobile devices

When implementing CSS position sticky for a floating header, I have noticed that on some mobile devices there is a small gap of around 1px between "element_b" and "element_c" while scrolling. #element_a{ width: 100%; position: -webkit-sticky !impo ...

Jumbotron text alignment in Bootstrap 3

I created a webpage using Bootstrap 3 that looks perfect on a computer screen. However, when viewed on a mobile screen, the text in the jumbotron is overflowing, causing a horizontal scroll bar to appear. <div id="home"> <div id="jumb" clas ...

Sorry, Computed styles are not available in IE 11 Edge DevTools

Currently facing an issue with a component element rendering differently on IE11, however the DevTools for both IE and Edge aren't showing any CSS in the Styles or Computed tab. What is the correct way to debug this situation? Comparison from left to ...

Ensure that the text input box is positioned at the bottom of the chatbox and enable scrolling

Is there a way to make the textbox stay fixed at the bottom of the chatbox, regardless of how many messages are present? Currently, it appears after the last message. Additionally, I would appreciate assistance in implementing a scroll feature that automat ...

Spacing between Div tags

Struggling with a tricky HTML cross-browser problem. The site was built by multiple people, making it quite messy, but needs to go live as soon as possible! Each of the 4 page layouts on the site are different and handled by separate classes. However, whe ...

Having trouble with the css style of my asp:Button, looking to replicate the precise css of my a.button class

Struggling with applying styles to an asp:button? Despite reading multiple posts, I can't seem to make it work for my situation. My css setup for href tags looks like this: a.button { padding:6px;background:#4A6491;color:#FFF; border-radius: 10px 10 ...

Displaying from the left side to the right side

Having some issues with sliding from the left to right div that has display:none. <div class="second"> <li><a id="sale_man" rel="showinfo"><?php echo $lang['sale_man'];?></a></li> <li><a id="purch_man ...

Is it possible to eliminate the default placeholder text from Safari browser?

My goal is to design a form that includes date and time input fields. The placeholder text should move up by X pixels when the user clicks on the field. While the form appears fine in Chrome, there seems to be an issue with overlapping form fields in Safa ...

Using v-model in Vue 3 will result in modifications to the table class in Bootstrap 5

Below is a snippet of the code I wrote: <table class="table table-striped"> <tr class="table-dark"> <th>#</th> <th>Column 1</th> <th colspan="3">Column 2</th> </tr> <tr ...

Personalize Angular material mattooltip and style information for display in tooltip

I need assistance displaying data in a tooltip in JSON format similar to the image provided. Although I can load the data in the tooltip, it does not appear formatted like the attached picture. <ng-container matColumnDef="Alert"> &l ...

Once a button has been clicked, it is not possible to disable another button

Two buttons labeled Accept and Deny are present, each revealing a different div with an additional button. Upon clicking the Accept button, it should disable the Deny button, slide in the Accept form, and allow the user to click on the Send Accept button. ...

Properly aligning sub-divs within a parent div using CSS

I'm facing a problem with my CSS styling that I need help with. Here is the screenshot of the issue I am trying to center align the inputs, each placed in its own div, within my form. Below is the HTML markup for the form: HTML Markup Code CSS st ...

The justify-content: space-between command functions properly for the last two products, but is not working as expected for one of the products

I have three products displayed with images and descriptions side by side. I used justify-content: space-between to create spacing between them, which seems to work for the last two products but not the first one, which is very annoying. Even after trying ...

Please ensure to dismiss the pop-up that appears when you open a new one

Hey there! I have created multiple pop-up elements. To close a pop-up, simply click on the opened popup. I would like to set it up so that when a new pop-up is opened, any previously open pop-ups are automatically closed. For instance, if I open Pop Up ...

How to cleanly print HTML pages with the use of page breaks

Having trouble with creating a table and printing the data in a specific structure. The format should be: Title Data Sum The challenge is to ensure that the title does not end up at the bottom of the page, and the sum does not start at the top of the pag ...

Executing individual if/each statements to choose specific divs and modify the position of their background images separately

I am currently faced with the challenge of relocating a background image within a div based on the content of a span. The complication arises from the fact that multiple divs share the same class and cannot be individually modified. For instance, each ce ...

The latest Bootstrap 5 design elements are incorporated into both FullCalendar version 5 and Angular 8 for a

I recently developed a calendar using FullCalendar v5 in Angular 8, and I was pleased with how it looked without any additional styling. However, I now want to integrate forms for creating events when a date is clicked, so I decided to incorporate Bootstra ...