The gap persists even when overflow is used in CSS

My HTML table is set up with an overflow property to ensure it doesn't exceed a specific height, but the following div is not being positioned directly below the scrolling table as intended.

Even though I am using overflow: hidden, I can still select the text, although it's not visible.

$('document').ready(function() {
var $table = $('#ingsNeededTable'),
    $bodyCells = $table.find('tbody tr:first').children(),
    colWidth;

$(window).resize(function() {
colWidth = $bodyCells.map(function() {
return $(this).width();
}).get();

$table.find('thead tr').children().each(function(i, v) {
$(v).width(colWidth[i] + 1);
});    
}).resize();

var loop = setInterval(function() {
$("#ingsNeededTable tbody").animate({scrollTop: $("#ingsNeededTable tbody").prop("scrollHeight")}, 100000, "linear");
$("#ingsNeededTable tbody").animate({scrollTop: 0}, 100000, "linear");
}, 1);
});
#ingsNeededTableHolder {
height: 45%;
}
#ingsNeededTitle, #ingsRecentTitle {
text-align: center;
}
#ingsNeededTable {
border-collapse: collapse;
width: 100%;
}
#ingsNeededTable th, #ingsNeededTable td {
border-bottom: 1px solid black;
}
#ingsNeededTable thead {
display: block;
width: 100%;
}
#ingsNeededTable tbody {
display: block;
height: 40%;
overflow-y: hidden;
width: 100%;
}
.ingsNeededTableIng, .ingsNeededTableProd {
width: 40%;
}
.ingsNeededTableNeeded {
width: 10%
}
#ingsNeededTable tr:nth-child(even) {
background-color: #eaf2f1
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div id='ingsNeededTableHolder'>
  <table id='ingsNeededTable'>
    <thead>
      <tr>
        <th class='ingsNeededTableIng'>Ingredient</th>
        <th class='ingsNeededTableNeeded'>Needed (Kg)</th>
        <th class='ingsNeededTableProd'>Product</th>
      </tr>
    </thead>
    <tbody>
      // additional rows of table data...
      <tr>
       <td class='ingsNeededTableIng'>foo</td>
        <td class='ingsNeededTableNeeded'>foo</td>
        <td class='ingsNeededTableProd'>foo</td>
      </tr>
    </tbody>
  </table>
</div>

<div>
  <h2>This Should Be Below The Scrolling Table</h2>
</div>

The table alignment appears correct on my webpage, but not in the snippet provided for some reason.

Answer №1

Apply a maximum height limit to #ingsNeededTableHolder

#ingsNeededTableHolder {
    height: 45%;
    max-height: 150px;
    overflow: hidden;
}

$('document').ready(function() {
var $table = $('#ingsNeededTable'),
    $bodyCells = $table.find('tbody tr:first').children(),
    colWidth;

$(window).resize(function() {
colWidth = $bodyCells.map(function() {
return $(this).width();
}).get();

$table.find('thead tr').children().each(function(i, v) {
$(v).width(colWidth[i] + 1);
});    
}).resize();

var loop = setInterval(function() {
$("#ingsNeededTable tbody").animate({scrollTop: $("#ingsNeededTable tbody").prop("scrollHeight")}, 100000, "linear");
$("#ingsNeededTable tbody").animate({scrollTop: 0}, 100000, "linear");
}, 1);
});
#ingsNeededTableHolder {
    height: 45%;
    max-height: 150px;
    overflow: hidden;
}
#ingsNeededTitle, #ingsRecentTitle {
text-align: center;
}
#ingsNeededTable {
border-collapse: collapse;
width: 100%;
}
#ingsNeededTable th, #ingsNeededTable td {
border-bottom: 1px solid black;
}
#ingsNeededTable thead {
display: block;
width: 100%;
}
#ingsNeededTable tbody {
display: block;
height: 40%;
overflow-y: hidden;
width: 100%;
}
.ingsNeededTableIng, .ingsNeededTableProd {
width: 40%;
}
.ingsNeededTableNeeded {
width: 10%
}
#ingsNeededTable tr:nth-child(even) {
background-color: #eaf2f1
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div id='ingsNeededTableHolder'>
  <table id='ingsNeededTable'>
    <thead>
      <tr>
        <th class='ingsNeededTableIng'>Ingredient</th>
        <th class='ingsNeededTableNeeded'>Needed (Kg)</th>
        <th class='ingsNeededTableProd'>Product</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td class='ingsNeededTableIng'>foo</td>
        <td class='ingsNeededTableNeeded'>foo</td>
        <td class='ingsNeededTableProd'>foo</td>
      </tr>
      <!-- additional table rows -->
    </tbody>
  </table>
</div>

<div>
  <h2>Important Content Below Scrolling Table</h2>
</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

Transform JSON data into an HTML layout

I'm looking to design a structure that showcases JSON information using HTML div elements. For example, utilizing the h4 tag for headers, p tag for text descriptions, and img tag for images. Can anyone provide guidance on the most efficient approach ...

Animate the sliding of a div from the right side to the left side with the animate

I am interested in implementing an animation effect where a div with the class '.whole' slides from right to left. This can be achieved using jQuery: $('#menu').click(function() { $('.whole').toggleClass('r2' ...

Incorporate a Background Image, which is linked to a website URL, by utilizing an external CSS file within

I am struggling to incorporate an external CSS file (specifically a background image from a URL) into my HTML document. I have successfully applied internal CSS, so the issue is not with the website link. Despite reviewing similar posts addressing comparab ...

Error message: Unable to access the state property of an undefined object

I've been attempting to integrate a react sticky header into my stepper component. However, I've encountered an issue where it doesn't render when added inside my App.js file. As a result, I've started debugging the code within App.js ...

What could be the reason for the malfunctioning transition effect in my slider animation?

Having trouble getting my slider animation to work. I've tried different CSS styles but the slide transition is not functioning as expected. When one of the buttons is clicked, I want the slide to change from right to left or left to right. Can anyone ...

Using Jquery Simplyscroll to incorporate images on both the left and right side within a container or clip

I am currently utilizing Jquery Simplyscroll (http://logicbox.net/jquery/simplyscroll/) and I am seeking a way to incorporate a faded png image on both the left and right sides to prevent my images from appearing "cut off" while sliding through. Below is ...

What is the best way to showcase the reading from a stopwatch on my screen?

Working on these codes has been quite a challenge for me. I have recently embarked on a JavaScript journey as a beginner and attempted to create a stopwatch project. However, it seems like I may have missed the mark with the logic or implementation somewhe ...

Obtaining user Object data from an HTML form in Spring Boot Controller

Having trouble retrieving data from an HTML form in my controller method while trying to create a straightforward Web application for user registration. I've attempted various solutions without success. Main class: /* Main class code here */ Con ...

A guide on extracting keys and values from a JSON object and saving them in an array with the useState hook in a React application

function Graph() { const [tcases, setCases] = useState([]); const [recovered, setRecovered] = useState([]); const [deaths, setDeaths] = useState([]); useEffect(() => { axios .get("https://disease.sh/v3/covid-19/historical/all?last ...

Eliminate the content located between two specific words in the img src URL and then render the file from the new source by utilizing ht

My img tags have parameters in the url for ajax image crop functionality. Here's an example: <img src="/resize/45-800/files/myImage.jpeg" alt="Chania"> Add /resize/45-800/ before the url to instruct the ajax script to fetch the file from the f ...

Preventing a JavaScript timer function from executing multiple times when triggered by an 'in viewport' function

I am trying to create a website feature where a timer starts counting up once a specific div is scrolled into view. However, I am encountering an issue where scrolling away restarts the timer, and I would like the final value that the timer reaches to rema ...

Utilizing HTML5 history instead of hash URLs to preserve the user's browsing history

Our application is a single page that dynamically loads results based on query strings. The query string format we use is as follows: ?city=Delhi&pn=1 Within the SPA, there are different sections displayed on the same page. As users navigate through ...

Minimize the count of switch cases that are not empty

How can I optimize the number of cases in my switch statement to align with SonarQube recommendations? Currently, I have 37 cases in a switch statement, but SonarQube recommends only 30. I believe that my code is functioning correctly, and the issue lies ...

Unable to interact with web element

When using selenium, I have a web element that I want to select. The WebElement.IsDisplayed() method returns true, but I am unable to perform the webelement.click() operation. port_dimension = canvas.find( By.xpath( "//*[local-name() = 'rect'][ ...

Proper method for adding elements in d3.js

I have a block of code that selects an #id and appends a svg-element into it: var graph = d3.select(elemId).append("svg") .attr('width', '100%') .attr('height', '100%') .append('g') Within th ...

Learn how to prompt an event in Angular when the browser is closed instead of being refreshed

Is there a method to remove a token from local storage only when the browser is closed, rather than on refresh? I have attempted the approach below but it is also being removed on refresh. If possible, please suggest a more effective solution as my previ ...

The JavaScript array on its second usage had a length of '0', yet it still contained objects within it

About the Task: This task involves working with two arrays: arrNumber, which is a number array, and arrString, which is a string array. The goal is to create a list of objects using the values from arrNumber, where the object keys are numbers or characte ...

When the sidebar is set to position: fixed, the icons magically vanish

This CSS is specifically for the side navigation bar. .side-nav { // position: sticky; top: 0; left: 0; height: 100vh; z-index: 100; width: calc(3.5vw + 3.5vh); // position: fixed; &.content { height: 100%; display: flex; fl ...

Spreading out the 'flip card' divs across various sections of the website while maintaining a seamless animation experience

I found a helpful answer on Stack Overflow that I'm currently following, and it's working perfectly for me. However, I am facing an issue where I need to position the boxes in different parts of the document, causing the animation to break. You ...

Disable the default behavior for every hyperlink within a container using jQuery

There is a div structure as follows: <div id="blah"> <a href="#" >hello</a> <a href="#"> world</a></div> I am seeking to disable all the links within this div using e.preventDefault(); Is there a way to target and s ...