What are some solutions to correct a tab layout that is utilizing absolute positioning?

Although I understand how absolute positioning works, it is currently disrupting the layout in this particular case. Using min-height with media queries for various screen sizes seems like a viable solution, but it's not foolproof considering that the client can upload images of any size.

The issue lies in the fact that content from below is overlapping with the tab that utilizes absolute positioning. Absolute positioning is necessary for the animation to function properly. Is there a way to incorporate a jQuery code to automatically detect the image height and apply it accordingly, or is there a more effective solution to tackle this problem?

JSFiddle: https://jsfiddle.net/6yo5b198/

HTML:

<div class="modules">
    <div class="container">
        <div class="row">
            <div class="col-md-12 col-12">
        <div id="exTab1">
          <div class="row">
            <div class="col-md-3 col-12">
              <ul class="nav nav-pills">
                <li class="active">
                  <a class="nav-link active" href="#a1a" data-toggle="tab">Overview</a>
                </li>
                <li><a class="nav-link " href="#a2a" data-toggle="tab">About Us</a>
                </li>
                <li><a class="nav-link " href="#a3a" data-toggle="tab">Services</a>
                </li>
              </ul>
            </div>
            <div class="col-md-9 col-12">
              <div class="tab-content clearfix">
                <div class="tab-pane active" id="a1a">
                  <img src="https://i.postimg.cc/xjFVtP8v/1511048-810098669070876-8091817687576561426-n.jpg">
                </div>
                <div class="tab-pane" id="a2a">
                  <img src="https://i.postimg.cc/d3XM3JpH/198582c8c440a3a96817d79a6dc28731-640x469.jpg">
                </div>
                <div class="tab-pane" id="a3a">
                  <img src="https://i.postimg.cc/qMhVYC69/50445941-10213980102716788-1077889899119509504-n.jpg">
                </div>
              </div>
            </div>
          </div>
        </div>
            </div>
        </div>
    </div>
</div>
<div class="">
  <h1>Other web contents go here.</h1>
</div>

CSS:

.tab-content .tab-pane img {
  opacity: 0;
  -moz-transform: translateX(50px);
  -ms-transform: translateX(50px);
  -o-transform: translateX(50px);
  -webkit-transform: translateX(50px);
  transform: translateX(50px);
  transition: 1s all cubic-bezier(0.075, 0.82, 0.165, 1);
  max-width: 100%;
  height: auto;
}
.tab-content .active img {
  opacity: 1;
  transform: translate(0);
}
#exTab1 .tab-content {
  overflow: hidden;
}
#exTab1 .tab-content .tab-pane {
  display: block;
  position: absolute;
  overflow: hidden;
}

Answer №1

Avoid using min-height as it does not account for varying heights across responsive levels, particularly on mobile devices where the height could be less. Instead, ensure that the height of the tabs is determined by the active tab, which should be included in the document flow by setting its position back to static.

#exTab1 .tab-content .tab-pane.active {
  position: static;
}

To maintain proper positioning, add top: 0 to the inactive tabs so they align correctly with the top of the tab container.

View the updated fiddle here: https://jsfiddle.net/websiter/grkn493p/


If you wish to achieve a smooth transition in container height when changing tabs, apply a height property and transition effect each time a tab switch occurs.

Add the transition property:

#exTab1 .tab-content {
  transition: height .3s cubic-bezier(.4,0,.2,1);
}

Dynamically set the height of the container based on the active tab's height during tab changes:

$(function() {
// on document ready...

  $('.nav').on('shown.bs.tab', function(e){
  // whenever a `.nav` tab is triggered 'shown.bs.tab', perform this action:

    const $target = $($(e.target).attr('href'));
    //retrieve target from `href` tab attribute

    $target.is('div') && $target.closest('.tab-content').css({
    // if target is div, update its css properties to

      height: $target.height()
      // match the height of the current target (as defined above)

    })
  });
});

Updated fiddle showcasing improved functionality: https://jsfiddle.net/websiter/uz0fkrj2/

Note that the enhanced fiddle addresses specific issues related to updating container height upon page resize, ensuring optimal performance and functionality:

  • a) The function handling height updates needs to be target-agnostic in the resize event, easily achievable by targeting the currently active tab panel.
  • b) Excess triggering of window.resize can lead to performance bottlenecks due to frequent height adjustments. To mitigate this, application of throttle limits the execution frequency to prevent animation lag. Leveraging lodash’s _.throttle method provides a solution, while an equivalent jQuery alternative offers similar benefits.

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

Reloading PHP Variable After a Specified Time Interval

<?php $data = file_get_contents("file.txt"); ?> <html> <body> <?php echo $data; ?> </body> </html> This code belongs to me. Is there a way to automatically refresh $data every 5 seconds? ...

What exactly is the value when using jQuery's `.each()` method with the function accepting parameters for

Currently, I am diving into the world of jQuery with guidance from a fantastic book known as Head First jQuery. This book has made the learning process incredibly straightforward. The topic at hand is the .each() function, which I encountered in an image ...

Trouble with Angular Charts: Data Reading Issue

Trying out chart.js for the first time and encountered an issue where the chart is not able to read data from the model. Previously, I had an error message of Error: $injector:modulerr Module Failed to instantiate module chart.js. This was fixed by switch ...

The issue arises when the cache code in jQuery Ajax interferes with the callback function, causing it to

I encountered an issue with my code related to cache. The problem arises when there is an ajax call with a callback in the success function. var localCache = { /** * timeout for cache in millis * @type {number} */ timeout: 30000, ...

What is the best method to update the content of one div with content from another page using AJAX?

Having trouble achieving smoother page loads? My goal is to utilize AJAX to specifically fetch a certain div from another page and then swap out the content of a div on this current page with the response. Below is my JavaScript script that uses AJAX to r ...

Is it possible to create Excel documents containing statistical graphs and pie charts by utilizing PHP and SQL?

I have a database filled with statistical data that I want to export into an excel file. Can anyone recommend any popular libraries or scripts for generating excel files? Additionally, I am interested in displaying some of the dry numerical data in p ...

Tips for incorporating color into the bootstrap card's left side

I need assistance in adding color to the left side of a bootstrap card like shown below: enter image description here I am unsure how to do this, can anyone provide guidance? ...

Changing variables within anonymous functions in javascript can be done by using the parameters passed into

Hello everyone, I'm currently working on a new JavaScript project and I've encountered an issue. I need to figure out how to change variables in anonymous functions. Can anyone help me with this? updateName : function(){ var firstNa ...

Conceal Child Component Element Upon onClick Event with ReactJS

As someone new to React, I am learning by coding. Currently, I have component A which includes a select element with menu items (all material UI). Is it possible for the whole component to disappear once a user chooses an option from the dropdown? Essentia ...

The TinyMCE editor is experiencing difficulty with editing the textarea

Having an issue with my PHP submission page. I have a form with various fields, including a textarea that uses TinyMCE. I also have an option to duplicate the existing form. The problem is, I can't edit the duplicated second editor - it shows up in th ...

JavaScript: specify parameters for function inputs

I'm curious about optimizing Javascript/Typescript functions arguments for clean code. To demonstrate, let's consider a basic example. Imagine I have a React component with a view property as props: <Grid view="Horizontal" /> ty ...

Which tool would be better for starting a new Angular project: angular-seed or yeoman?

I'm having trouble deciding on the best approach to create a new AngularJS application. There seem to be various methods available, such as using angular-seed from https://github.com/angular/angular-seed or yeoman - http://www.sitepoint.com/kickstar ...

Container that can be scrolled under varying heights

Trying to structure some CSS in order to create a website that resembles the layout of a typical desktop application: Almost there, but struggling to make the scrollable panel stick to the bottom of the viewport and show a scrollbar instead of overflowing ...

jQuery page hanging during DOM update with large data set

I am currently using a jQuery post method with the following structure: $.post("/SearchCompetitor/Index", { username: _username }, StartLoading()) .done(function (data) { if (data !== "UsernameErro ...

The offspring elements are positioned outside of the main footer element

There seems to be an issue with my <footer>. All child elements of the <footer> are appearing outside of it. Does anyone know how I can properly place child elements inside the <footer> and align them from left to right? footer{ bac ...

What is causing ES6 Class properties to be concealed by Higher Order Functions?

UPDATE: Added new screenshots to provide clarity at the end. My current challenge involves utilizing high order functions to combine subclasses/mixins. I've noticed that I can only access properties from the first class I extend, and only properties ...

Is there a way to extract individual values from a for-each loop in JavaScript?

Would appreciate any guidance on my use of Bootstrap vue table with contentful's API. I'm currently working on implementing a for loop to iterate through an array and retrieve the property values. Although the console.info(episodes); call success ...

Add hyphens to separate the words in AngularJS if there is a break in the string

Within a div of set width, a string is being bound to it. This string could be short or long. I would like for the string to break with a hyphen inserted on each line except for the last one. For example: If the string is "misconception" and it breaks at ...

Transform your HTML audio player into a Vue component

I am in the process of converting an HTML player into a Vue component. Half of the component has been successfully converted, but the time control slider is still missing. Below is the original HTML code for the player: // JavaScript code for the audi ...

Generating PDFs in Grails using bootstrap.cssWould you like to know how to create

I am currently developing a project using Grails-5. My goal is to export data in PDF format, and I have been utilizing the rendering plugin for this purpose. Everything was going smoothly until I encountered an issue when trying to include boostrap.css in ...