Display scroll bars in the table only when necessary

I am attempting to have scroll bars only appear in a table when necessary, while also showing colors upon hovering over rows and columns. The row and column highlighting on hover is functioning properly, but the scroll bar is persistently visible.

What steps can I take to ensure the scroll bar appears only when needed?

#table_grid {
  border-spacing: 0;
  border-collapse: collapse;
  /*overflow: hidden;*/
  font-family: arial, verdana, sans-serif;
  font-size: 12px;
  background: #fff;
  margin: 0px;
  text-align: left;
  max-height: 100%;
  max-width: auto;
  display: block;
  overflow: auto;
}

#table_grid td,
th {
  color: #669;
  padding: 9px 8px 4px 8px;
  font-size: 13px;
  position: relative;
}

#table_grid tr:hover {
  background-color: rgba(220, 220, 220, 0.5);
}

#table_grid td:hover::after,
th:hover::after {
  background-color: rgba(211, 211, 211, 0.2);
  content: '\00a0';
  height: 10000px;
  left: 0;
  position: absolute;
  top: -5000px;
  width: 100%;
  pointer-events: none;
}
<table id="table_grid">
  <tr>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
  </tr>
  <tr>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
  </tr>
  <tr>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
  </tr>
  <tr>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
  </tr>
  <tr>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
    <td>testing</td>
  </tr>
</table>

Answer №1

For optimal results, consider implementing the following CSS rule: overflow-x:auto; overflow-y:hidden;

This approach ensures that scrollbars are only displayed when the content exceeds the available space.

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

Is there a way to prevent an iframe from being recorded in Chrome's browsing history?

I'm in the process of developing a Chrome extension that inserts an Angular application into the user's browser window to create an interactive sidebar. I've been successful in most of my goals by inserting an iframe through a content script ...

Visual Delights on the Menu

I am having trouble adding a "Home" picture to my menu. It keeps showing up as a blank square, even though I have tried using both .png and .jpg formats. Here is the code that I am currently using: <div id='cssmenu'> <link rel= ...

Using the viewport meta tag in HTML and CSS within Laravel's Blade template while also

Greetings everyone! I have a question regarding Blade Laravel: Can we add a condition to the meta name="viewport" content="width=device-width, initial-scale=0.4"? I want the scale to start at 0.4 for smartphones and at 0.6 for tablets. ...

Selenium: A guide to specifying CSS for webpages with multiple elements sharing the same name

Looking for assistance with CSS selection: <table id="userPlaylistTable" cellspacing="0" cellpadding="0"> <div class="numCellWrapper"> ... When defining a css selector, how can I target a specific element among multiple elements of the same ...

Adjusting the transparency for every <td> element except for one child within the final <td>

<tbody id="items"> <tr><td>Item 1</td></tr> <tr><td>Item 2</td></tr> <tr><td>Item 3</td></tr> <tr><td>Item 4</td></tr> <tr><td> ...

Memory Match: Picture Edition

In this memory game, letters are used. I had considered changing it to a picture-based game, but I faced difficulty in generating images and incorporating them into the array. <script> var memory_array = ['A', 'A', 'B&ap ...

Dynamic creation of HTML/Ionic checkbox leads to ng-change not binding properly

Recently, my team and I have been actively engaged in the process of handling an XML file and dynamically constructing a settings page based on the information extracted from it. Allow me to present an illustration of how these elements are dynamically cre ...

Adjusting the width of tabs dynamically using jQuery

It's important to note that this question doesn't mirror the one found here. I am utilizing the jQuery UI Tab feature on my page and aiming for the tabs to occupy the entire width of the page. However, the number of tabs can vary between 0, 1, 2, ...

What is the best way to enable click functionality on the SVG Object and smoothly scroll to anchor using jQuery?

My SVG is embedded using an object tag and includes CSS3 hover animations. However, I am facing issues when trying to hyperlink the SVG with hover animation as it interferes with clicking actions. I chose not to use the long SVG path code for better code o ...

Make the child DIV's width equal to the width of its parent DIV, even when the parent DIV's width is not

I am experiencing an issue with a dropdown menu. I have created this code (with the assistance of w3schools)... :) The problem is that the sub-division that appears when I hover over a division does not match the width of the parent. I attempted to assig ...

Assistance needed with dynamically resizing a background image

Is there a way to automatically adjust the size of a background image for an element? For instance, I want my links in HTML to have a background image with slanted borders and rounded corners. Usually, you would set the width of the anchor element to fit t ...

Retrieving the selected value from a dropdown menu without having to click on a

I'm facing an issue with 2 dropdown menus outside of an HTML table that is generated by PHP code. There's a submit button within this table, and here's how it's set up: <!doctype html> Select Year: ...

Efficient Ways to Present and Personalize Indian Date and Time using JavaScript

Hey there, currently creating my website and need some assistance. I'm looking to display the date and time in different sections of the page. Having some trouble with getting Indian time to work properly using PHP. If you could help me out with Ja ...

There seems to be a problem with displaying two div elements side by side using Html/CSS

Recently, I've delved into the world of html/css/javascript just for fun. I've encountered a little roadblock and I could use some help. In my html code, I'm trying to display two div elements on the same line but for some reason, it's ...

Facing continuous 404 errors while working with nodejs and express

While attempting to collect data from a local host webpage's registration form that captures user information, I encounter an issue upon pressing the submit button A 404 Error is displayed stating "page not found", preventing the collection and out ...

Managing the placement of the expanded autocomplete input in Material-UI

Whenever I use the autocomplete fields on my website, I've observed an interesting behavior. When I select multiple options, the height of the input increases significantly, causing everything below it to shift downward like this. Is there a way to m ...

What is the best way to send a string value from HTML to a JavaScript function as a parameter?

When embedding HTML codes in Java, I encountered an issue where I needed to pass a string value from HTML to a JavaScript function. Initially, I tried using the following code: out.print("<script>init("+macId+")</script>"); However, this meth ...

Steps to open a URL link within a div without navigating away from the current page

I want to create an interactive icon that, when clicked, expands like a browser window. Inside this expanded div, I would like to display a URL that I provide. How can I accomplish loading a new URL within my original index.html without leaving the page or ...

The row count feature is ineffective in Materialize CSS when adjusting the text area

Can the row count of a text area in materialize css be initialized with a specific number using "rows='20'"? How can we adjust the default row count? Here is an example of code that attempts to set the row count but doesn't work: <div c ...

The AngularJS framework is failing to disable the autocomplete feature for the input field with a password type

I have attempted to disable auto-complete for the password input, but it doesn't seem to be working. Below is a sample of my code: <form name="testfrm" ng-submit="test(testfrm)" autocomplete="off"> <input type="password" id="passwor ...