"Even when hidden, an HTML Div element still occupies space on the

I encountered an issue with my tabbedPage setup. Even though I have hidden content, it still occupies space on the page. Here is the code snippet that I am using:

let tabHeader = document.getElementsByClassName("tabbedpage-header")[0];
let tabIndicator = document.getElementsByClassName("tabbedpage-indicator")[0];
let tabContent = document.getElementsByClassName("tabbedpage-content")[0];

let tabsPane = tabHeader.getElementsByTagName("div");

for (let i = 0; i < tabsPane.length; i++) {
  tabsPane[i].addEventListener("click", function() {
    tabHeader.getElementsByClassName("active")[0].classList.remove("active");
    tabsPane[i].classList.add("active");
    tabContent.getElementsByClassName("active")[0].classList.remove("active");
    tabContent.getElementsByTagName("div")[i].classList.add("active");

    tabIndicator.style.left = `calc(calc(100% / 2) * ${i})`;
  });
}

The code implementation seems to be working fine, but there is a problem with the hidden tab content taking up space. Applying position: absolute; fixes this issue; however, it causes the content to overlap with the footer. Is there a way to prevent the hidden tab content from occupying space without interfering with the layout of the footer?

Answer №1

It is advisable to utilize the display property instead of opacity.

Furthermore, ensure that you set div to display: block; within div.active as shown below:

.tabbedpage .tabbedpage-content>div.active, .tabbedpage .tabbedpage-content>div.active div {
  display: block; /* show it */
}

Note that if you want to assign a different display property to div within div.active, you need to include !important to ensure proper application.

Additionally, make changes to your JavaScript for loop as follows:

/** IF to point to the right div **/
    if(tabsPane[i].className.match(/\bstat\b/)){
      document.getElementById("stat").classList.add('active');
      document.getElementById("userManagement").classList.remove('active');
    }else if(tabsPane[i].className.match(/\buserManagement\b/)){
       document.getElementById("userManagement").classList.add('active');
      document.getElementById("stat").classList.remove('active');
    }

To ensure functionality, additional classes and IDs such as stat and userManagement were added due to identification issues with the div element previously.

DEMO (Simple example):

DEMO (Large example):

Answer №2

To hide the inactive element, you can utilize display: none and display: block instead of using opacity: 0 and opacity: 1:

let tabHeader = document.getElementsByClassName("tabbedpage-header")[0];
let tabIndicator = document.getElementsByClassName("tabbedpage-indicator")[0];
let tabContent = document.getElementsByClassName("tabbedpage-content")[0];

let tabsPane = tabHeader.getElementsByTagName("div");

for (let i = 0; i < tabsPane.length; i++) {
  tabsPane[i].addEventListener("click", function() {
    tabHeader.getElementsByClassName("active")[0].classList.remove("active");
    tabsPane[i].classList.add("active");
    tabContent.getElementsByClassName("active")[0].classList.remove("active");
    tabContent.getElementsByTagName("div")[i].classList.add("active");

    tabIndicator.style.left = `calc(calc(100% / 2) * ${i})`;
  });
}
/*--------------Styles for Tabbed Page----------------*/

body {
  font-family: Arial;
}

.container {
  width: 100%;
  max-width: 1300px;
  margin: 0 auto;
}

.tabbedpage {
  padding: 20px 0px;
}

.tabbedpage .tabbedpage-header {
  height: 60px;
  display: flex;
  align-items: center;
}

.tabbedpage .tabbedpage-header>div {
  width: calc(100% / 2);
  text-align: center;
  color: #888;
  font-weight: 600;
  cursor: pointer;
  font-size: 20px;
  text-transform: uppercase;
  outline: none;
}

.tabbedpage .tabbedpage-header>div>i {
  display: block;
  margin-bottom: 5px;
}

.tabbedpage .tabbedpage-header>div.active {
  color: #d81e05;
  display: block;
}

.tabbedpage .tabbedpage-indicator {
  position: relative;
  width: calc(100% / 2);
  height: 5px;
  background: #d81e05;
  left: 0px;
  border-radius: 5px;
  transition: all 500ms ease-in-out;
}

.tabbedpage .tabbedpage-content {
  position: relative;
  height: calc(100% - 60px);
  padding: 10px 5px;
}

.tabbedpage .tabbedpage-content>div {
  width: 100%;
  top: 0;
  left: 0;
  display: none; /* hide it */
  transition: opacity 500ms ease-in-out 0ms, transform 500ms ease-in-out 0ms;
}

.tabbedpage .tabbedpage-content>div.active {
  display: block; /* show it */
}

/*------------------------------------------*/
<div class="container">
  <div class="tabbedpage">
    <div class="tabbedpage-header">
      <div class="active">
        statistics
      </div>
      <div>
        user management
      </div>
    </div>
    <div class="tabbedpage-indicator"></div>
    <div class="tabbedpage-content">
      <div class="active">
        <h2>This is statistics section</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Error neque saepe commodi blanditiis fugiat nisi aliquam ratione porro quibusdam in, eveniet accusantium cumque. Dolore officia reprehenderit perferendis quod libero omnis.</p>
      </div>
      <div>
        <h2>This is the user management section</h2>
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Modi minus exercitationem vero, id autem fugit assumenda a molestiae numquam at, quisquam cumque. Labore eligendi perspiciatis quia incidunt quaerat ut ducimus?</p>
      </div>
    </div>
  </div>
</div>

<footer>
  <div class="red-bar">
    <div class="container content">
      <p> Bel nu ons contact center <br> <b>023 751 06 06</b> </p>
    </div>
  </div>
</footer>

Answer №3

If you need to manipulate the document by adding or removing a div as needed, you can utilize document.write(). Here's an example:

function updateDocument(){
  document.write("<div> hello </div>")
}
#button{
background-color: blue;
padding: 10px 20px;
}
#button:hover{
background-color: lightblue;
}
<button onclick="updateDocument()" id="button">Click here to add a div saying hello</button>

Answer №4

In my opinion, it would be more effective to utilize the CSS property display rather than opacity, as suggested by @MaxiGui. For added flair, you could also consider creating a custom tag for this purpose:

.invisible {
  display:none;
}

Answer №5

To address this issue temporarily, you have the option to adjust the footer design like this.

footer {
    width: 100%;
    position: fixed;
    left: 0;
    bottom: 0;
}

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

Changing the order of element names depending on their location within the parent element using jQuery

<div class="content"> <div> <input type="text" name="newname[name]0"/> <div> <input type="text" name="newname[utility]0"/> <div> <textarea name="newname[text]0 ...

Utilize Javascript to create a function that organizes numbers in ascending order

Is there a way to modify this code so that the flip clock digits appear in ascending order rather than randomly? $( '.count' ).flip( Math.floor( Math.random() * 10 ) ); setInterval(function(){ $( '.count' ).flip( Math.floor( Math.rand ...

Validating Credit Card Expiration Dates in AngularJS Form

I recently started learning AngularJS and decided to create a credit card validator. I successfully implemented the Luhn Algorithm in a custom filter, but now I'm facing issues with validating the expiration date as well. The conditions for a valid ex ...

Is it possible to update only the inner text of all elements throughout a webpage?

Scenario After coming across a thought-provoking XKCD comic, I decided to craft a script that would bring the comic's concept to life: javascript:var a=document.getElementsByTagName('body')[0].innerHTML;a=a.replace(/Program(\w\w+ ...

Toggle the sliding menu drawer option (upward/downward motion)

When it comes to my simple menu, I'm using jQuery to toggle the visibility of a few DIVs. The code is straightforward as shown below, and I could really use some assistance with adding extra functionalities. <div id="one" class="navLinks"> cont ...

Conceal an element along with its space, then signal the application to show alternative content using React

Greetings everyone! I seek assistance with a React Application that I am currently developing. As a newcomer to the Javascript world, I apologize if my inquiry seems trivial. The application comprises of two main elements: a loader, implemented as a React ...

Display the percentage text behind a filling Bootstrap progress bar

I'm working on a bootstrap progress bar that displays a percentage along with the numerical value. Currently, the text fits next to the progress bar up until around 85%, after which it blocks the bar from growing further. You can see the issue in the ...

Fundamentals of object property in jQuery and Javascript

Currently working on creating a new property named startPosition and setting the top property to equal the button's current top value in CSS. Below is the jQuery code snippet: var morphObject = { button: $('button.morphButton'), c ...

Tips for Concealing PHP Undefined Error Messages Using Echo

I'm new to PHP and I'm attempting to display user information in a div after submission using the echo function. Here is my current code snippet: <label class="input-fields" name="cust-name"> Name <input type="text" id="cust[name]" ...

Combining multiple HTML elements onto a single page with just one function

I am trying to dynamically import specific HTML content into a new page, rather than the entire page itself. The issue I am encountering is that I have to create a document load function for each individual item I want to import. Is there a more efficient ...

What is the best way to explain the concept of type indexing in TypeScript using its own keys?

I'm still learning TypeScript, so please bear with me if my question sounds basic. Is there a way to specify the index for this type so that it utilizes its own keys rather than just being an object? export type TypeAbCreationModal = { [index: stri ...

Why is it that when implementing React JS, the function setState is not updating the values on the chart when a Button is

Currently, my webpage is built using React JS and includes a JavaScript chart. I am trying to make the data in the chart dynamically change based on a value entered into a text box. When a button is clicked, the graph should update with the new results. Ho ...

Using Angular 7 shared service to allow sibling components to exchange data between each other

In my Angular 7 application, I have two sibling components - a configurator component and a custom stepper component. The configurator component is responsible for fetching data from the API and performing calculations on it. I would like to display the ca ...

Leverage React Context beyond the confines of a React component

The React Context API has impressed me, but I often find myself struggling to access it outside of a React component. Everything works smoothly within a React function or class component, but when it comes to fetching a value from the context for tasks lik ...

execute function once eventlistener completes running

I've implemented a code snippet to detect the availability of a gyroscope for user interaction. Here's how it works: function check_user_hardware(){ window.addEventListener("devicemotion", function(event){ if(event.rotationRate.alpha ...

Ensure that both tables contain columns of equal size

I am facing a challenge with 2 HTML tables that are positioned directly on top of each other. Each table has the same number of columns, however, the text within them may vary. Additionally, both tables can consist of multiple rows. My goal is to ensure th ...

Tips for arranging letters on separate lines using CSS

I have a set of four divs that display numbers, and I want to show the corresponding words below each number. Despite trying to use the p tag with white-space: pre; in CSS, I have not been successful: #c { padding-top: 30px; padding-bottom: 30px; } ...

What is the best way to make the child Div float to the bottom of the parent Div?

Screenshot I attempted various CSS techniques in an effort to make the .booknetic_appointment_container_footer float to the bottom of #booknetic_theme_5, but unfortunately, I was unsuccessful. The closest solution I found involved hardcoding padding, but ...

Dealing with an Undefined Property: Troubleshooting Guide

Encountering an error message Uncaught TypeError: Cannot read property 'top' of undefined at VM32939 own.js:819 resulting from var hands_corrected = (hands_original.top + 680) this issue arises because "hands_original" is not used in any par ...

The quickest regular expression match possible if it is already a subsection of another match

Is there a simple way to find the shortest match in a long text where strings are repeated? I'm having trouble because matches within already matched text aren't being found. Here's an example of the issue: Using code: "ababc".match(/a.+c ...