Bootstrap tabs trigger a reconstruction of CSS pseudo elements

I've encountered an issue while trying to add a ::before pseudo element to list item elements within a Bootstrap Tab.

The pseudo element comes with a CSS animation that can be triggered by toggling the class.

Although everything seems to work, I noticed that every time I switch between tabs, the CSS animation gets triggered. Upon inspecting the DOM, I realized that the pseudo elements are being added or removed each time I change tabs.

As a result, the CSS animation plays every time the tab with the list is opened, which is not the desired behavior.

I'm puzzled as to why this is happening. Shouldn't the CSS still apply to the list items even when their parent tab is not visible? How can I prevent the CSS animation from playing when opening the tab?

.myList {
  list-style: none;
}

.myList .state-in {
  position: relative;
}

.state-in::before {
  content: '';
  position: absolute;
  left: -30px;
  top: 1px;
  width: calc(100% + 40px);
  height: 24px;
  animation: takeInFade 1s ease-in-out;
}

@keyframes takeInFade {
  0% { background-color: transparent; }
  30% { background-color: #466d2a; }
  100% { background-color: transparent; }
}

@keyframes takeOutFade {
  0% { background-color: transparent; }
  30% { background-color: #7b2d2a; }
  100% { background-color: transparent; }
}
<html>
  <head>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  
  </head>
  <body>
    
    <ul class="nav nav-tabs" id="myTab" role="tablist">
      <li class="nav-item">
        <a class="nav-link active" id="home-tab" data-toggle="tab" href="#home" role="tab" aria-controls="home" aria-selected="true">Tab One</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" id="profile-tab" data-toggle="tab" href="#profile" role="tab" aria-controls="profile" aria-selected="false">Tab Two</a>
      </li>
    </ul>
    <div class="tab-content" id="myTabContent">
      <div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">
        <ul class="myList">
          <li class="state-in">Item One</li>
          <li class="state-in">Item Two</li>
          <li class="state-in">Item Three</li>
        </ul>
      </div>
      <div class="tab-pane fade" id="profile" role="tabpanel" aria-labelledby="profile-tab">
        Tab Two Content
      </div>
    </div>
    
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
  </body>
</html>

Any advice on how to resolve this issue would be highly appreciated! Thanks in advance!

Answer №1

One creative approach is to modify the functionality of the bootstrap tab by adjusting the positioning to absolute and setting opacity to 0 instead of toggling the display property.

Here is a snippet of the modified code:

.tab-content>.tab-pane {
    display: block!important;
    position:absolute;
    opacity:0;
}
.tab-content>.active {
    display: block;
    opacity:1;
    position:relative;
}

Full code:

.myList {
  list-style: none;
}

.myList .state-in {
  position: relative;
}

.state-in::before {
  content: '';
  position: absolute;
  left: -30px;
  top: 1px;
  right:0;
  height: 24px;
  animation: takeInFade 1s ease-in-out;
}

@keyframes takeInFade {
  0% { background-color: transparent; }
  30% { background-color: #466d2a; }
  100% { background-color: transparent; }
}

@keyframes takeOutFade {
  0% { background-color: transparent; }
  30% { background-color: #7b2d2a; }
  100% { background-color: transparent; }
}


/**/
.tab-content>.tab-pane {
    display: block!important;
    position:absolute;
    opacity:0;
}
.tab-content>.active {
    display: block;
    opacity:1;
    position:relative;
}
/**/
<html>
  <head>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  
  
  </head>
  <body>
    
    <ul class="nav nav-tabs" id="myTab" role="tablist">
      <li class="nav-item">
        <a class="nav-link active" id="home-tab" data-toggle="tab" href="#home" role="tab" aria-controls="home" aria-selected="true">Tab One</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" id="profile-tab" data-toggle="tab" href="#profile" role="tab" aria-controls="profile" aria-selected="false">Tab Two</a>
      </li>
    </ul>
    <div class="tab-content" id="myTabContent">
      <div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">
        <ul class="myList">
          <li class="state-in">Item One</li>
          <li class="state-in">Item Two</li>
          <li class="state-in">Item Three</li>
        </ul>
      </div>
      <div class="tab-pane fade" id="profile" role="tabpanel" aria-labelledby="profile-tab">
        Tab Two Content
      </div>
    </div>
    
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
  </body>
</html>

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

Trouble encountered when attempting to programmatically dismiss a modal following a confirmation alert

When the user clicks on the close button, I want to display a confirmation alert to confirm if they really want to close the modal. However, I have encountered an issue with hiding the modal and preventing it from being hidden in the two solutions that I h ...

Country code dropdown causing CSS problem on mobile devices (intl-tel-input)

Hey there, I've been using this awesome library for displaying country codes and flags to our users. Check it out here: https://github.com/jackocnr/intl-tel-input Everything was working perfectly on desktop, even when resizing the screen. But when I ...

Do not incorporate a div in the overlay

I have an image inside a container. When hovering over the image/container, overlay information is displayed (which is currently working correctly). The overlay should cover the entire container (the grey part) where the image is located. The product-tit ...

What is the Best Way to Utilize CSS ID Selectors with Next JS?

After discovering that npx create-react-app is no longer supported, I decided to delve into Next JS as a new framework. My goal now is to transition one of my websites from pure HTML, SASS (CSS), and JavaScript to utilizing Next JS and SASS. The current c ...

Conceal and reveal div elements using hyperlinks

Hey everyone, I've been working on some code that allows me to show and hide divs by clicking on links. The issue I'm facing is that when I try to view a specific div (like the third one), it appears below the invisible divs instead of at the top ...

Excessive image display | HTML and CSS synergize

I am having some trouble with my image gallery. Each image is displayed as a vertical column and has zooming effects when hovered over. Clicking on an image should show it in full screen with a caption. The problem arises when dealing with images that are ...

Is there a way to dynamically alter the text color in a tag based on its value?

Utilizing ajax technology, I aim to dynamically populate a div tag with values from a table. I believe that implementing a loop is crucial in this scenario, but the challenge lies in executing it correctly. My current setup involves xampp for backend php ...

Flexbox won't align child elements of parent vertically

I am currently facing an issue with my parent container div and its child elements. Even though one child element is being vertically centered, the other two child elements remain at the top of the page. I have been trying to troubleshoot this problem but ...

Enhancing the Search Bar in Bootstrap 4 Dashboard Template: A Step-by-Step Guide

Recently got my hands on the Bootstrap 4 Dashboard template and decided to enhance it by adding a form to the top search bar. However, I encountered an issue where the search bar shrinks and loses its original design. If you want to check out the Bootstra ...

Place the file in the designated area for styling purposes

Within the pluploader, a file drop zone exists with the identifier dropFilesHere; var uploader = new plupload.Uploader({ drop_element : "dropFilesHere", /*...*/ }); If a user hovers a file over the drop zone, I want to customize it* ...

What is the best way to line up changing column values with changing row values in an HTML table?

In its current state, the data appears as follows without alignment: https://i.sstatic.net/o5OLu.jpg The data columns are housed within a single variable due to the presence of multiple excel tabs with varying columns. Within the tr tag, rows are checked ...

Refresh the content of an iframe (local HTML) by clicking on buttons

When a user clicks on a button, I am loading an HTML file into a DIV (iframe). The issue arises when the user clicks another button and wants to reload the iframe with a different HTML file. This is the current code snippet: <script type="text/javasc ...

Error: The function explore() is missing a required parameter called 'path'

Here is my code snippet: def navigate(directory): # To avoid issues with forward slashes, normalize the path directory = os.path.normpath(directory) if os.path.isdir(directory): subprocess.run([FILEBROWSER_PATH, directory]) elif os.path.isfile(directo ...

Creating a loading mask for a section of a webpage using angularjs

How can I implement a loading mask/image for a $http request that loads data for a <select> tag on my webpage? I want the loading mask/image to only cover the specific section of the page where the data is being loaded. Here's the HTML code: & ...

Is there a way to initiate a jquery function upon loading the page, while ensuring its continued user interaction?

On my webpage, there is a JavaScript function using jQuery that I want to automatically start when the page loads. At the same time, I want to ensure that users can still interact with this function. This particular function involves selecting a link tha ...

What are the best ways to format an outgoing email using inline CSS?

On my upcoming website, I have set up a single form for guests to subscribe. The issue I am facing is with styling the email that is sent upon submission. I attempted using inline CSS, but it doesn't seem to be working. Instead of transforming the cod ...

Show small information box when hovering over custom toggle button

I have developed a unique custom toggle switch feature When I hover my mouse over the switch, it should display a tooltip message as YES if the switch is in the ON position and NO if it's in the OFF position. Is there a way to implement this functio ...

What is the best way to combine two menu plugins in Wordpress?

Currently, I am in the process of creating a logo with a drop down menu for my website, which can be found at . After discovering a widget called Menu Image that successfully transformed a menu item into a photo, I decided to add another widget called Ma ...

Utilize Android accelerometer data to bring objects to life with animation

Utilizing an Android app, I am streaming accelerometer data to a Python script on my PC. The data is then saved to a text file. My goal is to utilize Javascript and jQuery to animate a 3D CSS cuboid (representing the device) to replicate the movements capt ...

CSS3 Transition effects are applied immediately to duplicated elements

My dilemma lies in applying CSS3 Transitions to elements by introducing a new class. Markup <div id="parent"> <div class="child"> </div> </div> CSS .child { background: blue; -webkit-transition: background 4s; ...