Is it necessary for children to occupy the same area while beginning from the coordinates 0, 0?

Imagine a scenario where you have a modal with tabs and tab content. Each tab has a different height - the first tab is 100px high, the second tab is 500px high, and the third tab is 50px high. The challenge here is to ensure that the modal body always matches the height of the tallest tab (500px), so when switching between tabs, the modal remains consistent in size.

There are certain limitations in achieving this:

  • You cannot set the tab content area wrapper to position: relative and each tab to position: absolute; top: 0; left: 0; because absolute-positioned elements do not occupy space in the layout.
  • Hiding all tabs except the active one will cause the tabs to stack vertically, resulting in extra space above each tab. This leads to the overall height of the modal's body being the sum of the heights of all tabs combined.

To better understand the issue, please refer to the example linked below. Toggle between "End Goal" and "Actual" to visualize the desired outcome versus the current situation.

I would prefer to avoid using JavaScript to dynamically adjust the height of each tab since it would involve constantly checking for changes due to user interactions. This approach may introduce complexities and inefficiencies. I am open to exploring alternative solutions if any exist.

As a temporary fix, I might choose a "good" height for the tab container and allow taller tabs to scroll within their designated space if necessary.

Example: https://codepen.io/joshm123/pen/NWrEVjJ (copied below)

HTML

<div class="modal">
  <div class="header">
    <h2>Welcome!</h2>
  </div>
  <div class="body">
    <div class="tabs">
      <button class="tab" onClick="selectTab(1)">Tab 1</button>
      <button class="tab" onClick="selectTab(2)">Tab 2</button>
      <button class="tab" onClick="selectTab(3)">Tab 3</button>
    </div>
    
    <div class="tabs-wrapper">
      <div id="tab1" class="tab-content active">
        blah blah blah<br/>
        blah blah blah
      </div>
      <div id="tab2" class="tab-content">
        blah blah blah<br/>
        blah blah blah<br/>
        blah blah blah<br/>
        blah blah blah<br/>
        blah blah blah<br/>
        blah blah blah<br/>
        blah blah blah<br/>
        blah blah blah<br/>
        blah blah blah<br/>
        blah blah blah
      </div>
      <div id="tab3" class="tab-content">
        blah blah blah
      </div>
    </div>
  </div>
  <div class="footer">
    <button id="showEndGoalButton" onClick="toggleView()">Show End Goal</button>
    <button id="showActualButton" onClick="toggleView()">Show Actual</button>
  </div>
</div>

CSS

.modal {
  position: absolute;
  width: 500px;
  left: calc(50% - 250px);
  top: 30px;
  border: 1px solid #999;
}

.header {
  padding: 10px;
  border-bottom: 1px solid #999;
}

.header h2 {
  margin: 0;
  padding: 0;
}

.body {
  padding: 10px;
}

.tabs > .tab {
  margin-right; 10px;
}

.tabs-wrapper {
  margin: 10px 0;
  border: 1px dotted red;
}

.tab-content {
  visibility: hidden;
  border: 1px dotted blue;
}

.tab-content.active {
  visibility: visible;
}

.footer {
  padding: 10px;
  border-top: 1px solid #999;
  text-align: right;
}

/* supporting CSS

    - height of tallest tab is 182px
    - make all tabs this hard-coded height
    - this is not a real solution, but an example
      what the end result should be (visually)
*/
.modal #showActualButton {
  display: none;
}

.modal.end-goal #showEndGoalButton {
  display: none;
}

.modal.end-goal #showActualButton {
  display: inline-block;
}

.modal.end-goal .tabs-wrapper {
  position: relative;
  height: 182px;
}

.modal.end-goal .tab-content {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
}

JavaScript

window.selectTab = function (num) {
  const tabs = document.querySelectorAll('.tab-content');
  const tab = document.querySelector(`#tab${num}`);
  
  tabs.forEach(o => o.classList.remove('active'));
  tab.classList.add('active');
}

window.toggleView = function() {
  document
    .querySelector('.modal')  
    .classList
    .toggle('end-goal');
}

Answer №1

To create a basic tab panel, you can set up a container with the CSS property overflow: hidden and include an inner-container that is wider than the outer one. In this scenario, where there are 3 tabs, the inner-container should be 300% wide.

This method allows the container to accommodate the tallest inner-div by essentially clipping the inner-container.

Subsequently, utilize JavaScript to modify the transform property on the .inner element to determine which tab is displayed. While I did not provide the specific JavaScript code, you can adjust the styling so that clicking "tab 1" sets the inner div to transform: translateX(0). Clicking "tab 3", for instance, could set it to transform: translateX(-66.666%) ... hopefully, you grasp the concept.

:root {
  --num-of-tabs: 3;
}

.container {
  width: 400px;
  overflow: hidden;
  border: 1px solid gray;
}

.inner {
  width: calc(var(--num-of-tabs) * 100%); 
  display: flex;
  transform: translateX(-33.33%); /* starting on tab #2 */
}

.box {
  width: calc(100% / var(--num-of-tabs));
}
<div class="container">
  <div class="inner">
    <div class="box">
      blah blah blah
    </div>
    <div class="box">
      blah blah blah blah
    </div>
    <div class="box">
      blah blah blah<br/>
        blah blah blah<br/>
        blah blah blah<br/>
        blah blah blah<br/>
        blah blah blah<br/>
        blah blah blah<br/>
        blah blah blah<br/>
        blah blah blah<br/>
        blah blah blah<br/>
        blah blah blah
    </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

Gulp and Babel: Module Not Found Error

I am facing an issue with my project setup using gulp and babel. Everything seems to be functioning correctly, except for when I create a module and import it after conversion from ES6 to ES5. At that point, it fails to work and displays the following erro ...

How can I call a function from one Vue component in another component?

I have developed a component with the function "logout" as seen in the code snippet below: // @/component/Painel.vue <template></template> <script> export default { name: 'panel', methods: { logout: function () { ...

Encountered an Angular SSR error stating "ReferenceError: Swiper is not defined"

When attempting to implement SSR (Server-Side Rendering) in a new project, everything runs smoothly and without issue. However, encountering an error arises when trying to integrate SSR into an existing project. https://i.sstatic.net/QOI6A.png ...

Mastering JavaScript Testing with Selenium 2

I am currently experimenting with JavaScript in Selenium 2 WebDriver using Visual Studio 2010. Are there any effective methods to call a function that is part of the current JavaScript object on the page? ...

Error message displayed: MUI Textfield does not support input of decimal values

I have a textfield where users can enter the unit price const [unitPrice, setUnitPrice] = useState(0); <TextField label="Unit Price" variant="outlined" margin="normal" value={unitPrice.toString ...

Using React components to create an anchor element for a popover display

Hey, I'm just starting out with React and trying to wrap my head around Hooks like useState. It's a bit challenging for me, and I want to keep things simple without making them too complex. I've encountered an issue when transitioning a Rea ...

Error: Express is undefined and does not have a property called 'use'

I'm encountering a problem with my express server specifically when I utilize the 'app.use' command. Within my task-routes.js file, the following code is present: import express from 'express'; const router = express.Router(); ...

How can I match all routes in Express except for '/'?

I've been working on implementing an authentication system for my app that involves checking cookies. My approach was to use router.all('*') to handle every request, verify the cookie, and then proceed to the actual handler. However, I encou ...

Modify the default color selection tool

I am utilizing Angular6 with material-designs. My aim is to obtain color input from mat-form-field. I achieved this by using matinput type="color" within my input tag. Below is a snippet of my HTML file, <div> <mat-form-field class="fo ...

What is the best way to navigate through the underlying MatDialog while the MatSelect is active?

When attempting to customize the scroll behavior of a MatSelect in a regular page, I discovered that using the MAT_SELECT_SCROLL_STRATEGY injection token with the NoopScrollStrategy allows for scrolling the underlying page while keeping the MatSelect stati ...

What is the process for decrypting the data that was encrypted using node-jose?

I am currently in the process of incorporating basic JOSE encryption and decryption functionalities by utilizing node-jose. This is my code implementation (using Node 8.2.1) const { JWE } = require('node-jose'); const jose = (publicKey, privat ...

What is the importance of using ChangeDetectorRef.detectChanges() in Angular when integrating with Stripe?

Currently learning about integrating stripe elements with Angular and I'm intrigued by the use of the onChange method that calls detectChanges() at the end. The onChange function acts as an event listener for the stripe card, checking for errors upon ...

When you hover over a link, the target will appear in the status bar with location.href

Upon clicking the code snippet provided, it redirects me to a dynamically generated details page: <tr class="itemList" onclick="location.href='Details/' + @item.ID"> However, my concern is that the target URL (website/Details/ID) does not ...

Switch Bootstrap Tab

I have successfully implemented a bootstrap tab on my webpage and it is functioning as intended. Now, I am interested in adding an additional feature to the tabs. My question is, is it possible to toggle the content area if the same tab is clicked again? ...

Searching for documents in MongoDB using multiple equality conditions with the find command

While I've managed to filter results by the month using this query, I'm struggling to also add a year filter. db.collection.find({ "$expr": { "$eq": [{ "$month": "$timestamp" }, 12] } }); I attempted this approach, but with no success. ...

The camera will only activate once the page is manually clicked using Puppeteer

My recent project involved developing a NodeJS application that performs the following tasks: Setting up a server to serve static files using http.createServer Launching a puppeteer process to open Chrome browser in non-headless mode. The browser is direc ...

Differences in <img> sizing behavior between Chrome and Firefox

Objective: Develop a scrollable image-gallery web component with layouting that functions without script intervention. Specifications: The size of the web component must be fully responsive and/or adjustable. The top main area of the widget is the galle ...

Why is the jQuery not functioning within the AngularJS function?

I am encountering an issue with writing jQuery inside an AngularJS function. I can't seem to figure out why it's not working properly. HTML <div> <span ng-repeat="image in post.postImages" ng-if="$index <= 3" ng-init="image.showD ...

Is the user currently accessing the website in multiple tabs?

I am currently working on detecting the online status of users and need to update it when the tab is closed. The challenge I am facing is determining if the user has multiple tabs open so that the status remains the same, only updating when there are no ...

Using HTML and CSS to selectively apply CSS styling based on certain conditions

Hey there, I'm currently facing an issue where I've tried multiple solutions but none seem to be working.. I have a scenario with two HTML divs <div class="lmn-tab-item"></div> <div class="lmn-tab-item lmn-tab-item- ...