Create a nested table with a table-layout set to auto and a width of 100%, allowing for overflow when the window

How can I achieve a table with table-layout: auto and a 100% width that overflows horizontally when the window is too narrow without showing a scrollbar?

Consider these examples:

WORKING EXAMPLE 1

<div style="width: 100%; overflow-x: auto">
  <table style="table-layout: auto; width: 100%; overflow-x:auto; border: 1px solid black">
    <tr>
      <td>dcsdcsdcasd1</td>
      <td>dcsdcsdd2</td>
      <td>dcsdcsdasxascasd3</td>
      <td>dcsdcsdasd4</td>
      <td>dcsdcsdxasacasd5</td>
      <td>dcsdcsdcxasd6</td>
      <td>dcsdcsxaxasdcasd7</td>
      <td>dcsdasd8</td>
      <td>dcsdd9</td>
      <td>dcsdxaxasxascsdcasd10</td>
    </tr>
  </table>
</div>

NOT WORKING EXAMPLE 2

    <table>
      <tr>
        <td>
          <div style="width: 100%; overflow-x: auto">
            <table style="table-layout: auto; width: 100%; overflow-x:auto; border: 1px solid black">
              <tr>
                <td>dcsdcsdcasd1</td>
                <td>dcsdcsdd2</td>
                <td>dcsdcsdasxascasd3</td>
                <td>dcsdcsdasd4</td>
                <td>dcsdcsdxasacasd5</td>
                <td>dcsdcsdcxasd6</td>
                <td>dcsdcsxaxasdcasd7</td>
                <td>dcsdasd8</td>
                <td>dcsdd9</td>
                <td>dcsdxaxasxascsdcasd10</td>
              </tr>
            </table>
          </div>
        </td>
      </tr>
    </table>

In example 2, when the window is resized below the content width, a horizontal scrollbar appears for the entire window due to the wrapping table. This is not the desired behavior. However, in example 1, this issue is resolved successfully. Any suggestions on how to fix this while keeping the outer wrapping table present?

Answer №1

In the second scenario, your outer table continues to expand based on its content, resulting in a scrollbar for the entire document.

To address this issue, you should initially constrain the width of the table with

width: 100%; table-layout: fixed;
- allowing it to overflow if the inner table within it becomes wider.

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

Include the className attribute on the contents received from dangerouslySetInnerHTML

What is the best way to define the className attribute for a div that contains 'hello world'? <div dangerouslySetInnerHTML={{__html: '<div>hello world</div>'}} /> One option is to set it on the outer div like this: &l ...

creating a live updating dropdown menu using Node.js and MySQL

Upon a user clicking and selecting a client, I would like to dynamically update the region dropdown menu with options that are related to that specific client from the databaseenter code here This is my client table: Here is the Region table, where clien ...

Vue's v-model functionality encounters issues when navigating back and forth in the browsing history

Below is a sample of code from the local index.html file: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">< ...

Unable to alter the content within an iframe when using the Google Chrome browser

Currently, I am tackling a project that involves incorporating animations into multiple generated pages. These pages are nested within iframes on the main index page. As part of this same project, I need to dynamically add elements to these iframe-embedd ...

Using `require` in place of `script` tags in a three.js file when working with Node.js environment

Instead of using the html script tag, I have implemented javascript require in my three.js file for node.js. Here is a snippet from an html file: <script src="../build/three.js"></script> <script src="js/controls/DragControls.js"></s ...

How can I include multiple search forms on a single page in TYPO3 Ke_Search without any conflicts between them?

In the header of my website, I have a search form that is generated as a cObject from a content element containing a ke_search searchbox. When this form is submitted, it redirects to a /search page. In addition to this, I have subpages that are meant to se ...

Is there a way to remove identical individual rows from the dataset?

Can someone help me? I am trying to display only the unique values of Barangay, specifically 'Talojongon', without any duplicates. Currently, my output looks like this: | Barangay | Disease | __________________________________ | Ta ...

Ways to remedy the white line produced by CSS transform when hovered over?

Has anyone discovered a fix for the white line or border that appears when an element is transformed with CSS scale on hover? I've tried various solutions such as: transform: translateZ(0), -webkit-backface-visibility: hidden, transform-style: preser ...

Showing a div based on the selected value from a dropdown menu

I am currently working on a project where I have 3 separate div elements, each with a unique ID. Depending on the selected value from a dropdown menu, I want to display one of these divs. I have created a function to handle this logic, but for some reason, ...

What is the best way to create a hover effect for Material Icons?

Having issues with Material Icon not displaying the specified changes when using CSS modules in my project import SettingsIcon from "@mui/icons-material/Settings"; import css from "./LandingPage.module.css"; <SettingsIcon className= ...

How to retrieve an array from an object using React

In the context of my React application, I have a function called Query2 within a file named service.ts. The structure of this function is as follows: interface Result { products: number[]; } export async function Query2(molsSdf: string): Promise<Res ...

Adjusting the header background color and inserting a logo once a specific threshold is reached

Currently, I am designing a website where I need the background color to transition from transparent to black and display a logo once the user scrolls down to a certain point. I am a beginner in javascript and I am facing some difficulties with this task. ...

Stuck on changing the background color of a DIV in WooCommerce product descriptions with CSS

I seem to be stuck on a simple fix and despite searching everywhere, I can't seem to make it work. The problem lies within WooCommerce on my WordPress site, where I suspect that my CSS is conflicting with WC's and causing some color problems. Yo ...

Customizing the Class of a jQuery UI ui-autocomplete Combobox Container

Is there a way to customize the ui-autocomplete div by adding a custom class? I have several autocomplete widgets on my webpage, and I need to style their drop-downs differently. Since editing the ui-autocomplete class directly is not an option, I am wor ...

Does Div have the capability for text-shadow?

Is there a way to create a shadow effect for DIVs similar to the text-shadow property? While text-shadow is great for adding shadows to individual pieces of text, I'm interested in applying a shadow effect to an entire DIV element. Does anyone have a ...

Is there a Safari glitch related to floating elements?

I stumbled upon a strange issue with the layout I've been working on, and it led me to this concise code snippet that seems to be causing misrendering in desktop Safari. It appears that the div#shim element is what's triggering the text misrender ...

Tips for creating a seamless merge from background color to a pristine white hue

Seeking a seamless transition from the background color to white at the top and bottom of the box, similar to the example screenshot. Current look: The top and bottom of the box are filled with the background color until the edge https://i.stack.imgur.com ...

Attempting to zoom in when hovering over the circular image causes it to spill out of the container

Can someone help me figure out why my circular image isn't zooming when hovered? I've tried adding CSS properties, but the image keeps overflowing the container. What am I missing here? See the code snippet below. .col-lg-4.col-md-6.p-4{ ove ...

Using Jquery to dynamically adjust the size of a div based on the width and height of the browser window

How can I dynamically change the height of a .test div based on the browser width and height? I want the value to update whenever the browser is resized. $(document).ready( function() { window.onresize = function(event) { resizeDiv(); ...

Customize Material-UI Icons styles in React app

I'm working on a React.js app with Typescript and I need to remove the default visited Material Icons coloring from an anchor tag. Here's the stylesheet I tried: const useStyles = makeStyles((theme: Theme) => createStyles( myAnchor: ...