Is the min-width attribute ineffective on table cells?

I am having trouble setting a minimum width for a table cell and enabling horizontal scrolling. I have tried using min-width but it doesn't seem to work. Can anyone provide some guidance on this issue? I came across this comment on Stack Overflow but it hasn't resolved my problem. Here is the code snippet:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
   div{
      
       width: 50%;
       overflow-x: scroll;
       border: 1px solid red;
   }
    table {
        table-layout: fixed;
        border: 1px solid black;
        width: 100%; 
    }
    th, td{
        border: 1px solid black;
        
        min-width: 1000px;
        
    }
  
</style>
<body>
    <div>
    <table>
        <tr>
            <th>nb b </th>
            <th>agahbajh gae</th>
            <th>nbb </th>
            <th>agahajh gae</th>
        </tr>
        <tr>
            <td>amame ban</td>
            <td>20</td>
            <td>amame ban</td>
            <td>20</td>
        </tr>
        <tr>
            <td>aahshu</td>
            <td>23</td>
            <td>aaajshu</td>
            <td>23</td>
        </tr>
    </table>
</div>

    

</body>
</html>

Update:- After receiving a solution from @Emaro, I am now wondering how to set the widths of 4 columns as follows: 40%, 40%, 50%, 50% of the outer div.
Thanks

Answer №1

To ensure horizontal scrolling on the table, set the width to auto instead of limiting it to 100%. This adjustment should allow for proper functionality (assuming I have interpreted your needs correctly).

div {
   width: 50%;
   overflow-x: scroll;
   border: 1px solid red;
}

table {
    table-layout: fixed;
    border: 1px solid black;
    width: auto; /* Adjusted from 100% */
}

th, td {
    border: 1px solid black;
    min-width: 1000px;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>

<div>
    <table>
        <tr>
            <th>nb b </th>
            <th>agahbajh gae</th>
            <th>nbb </th>
            <th>agahajh gae</th>
        </tr>
        <tr>
            <td>amame ban</td>
            <td>20</td>
            <td>amame ban</td>
            <td>20</td>
        </tr>
        <tr>
            <td>aahshu</td>
            <td>23</td>
            <td>aaajshu</td>
            <td>23</td>
        </tr>
    </table>
</div>

</body>
</html>

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

Express JS routing encounters 500 error following the submission of a form button

I've been developing a unique chatbot that specializes in recommending songs based on the user's current mood. However, I've hit a roadblock when it comes to routing a response to the user after they submit their preferences through an HTML ...

The onServerClick function does not seem to be functioning properly when dynamically created

I am facing an issue where my dynamically created anchor buttons with onServerClick methods are not calling the function when clicked on the webpage. Below is a sample of the anchor tag: <a href="#" runat="server" id="button0" class="btn btn-default bt ...

Styling in Next.js with conditions

I am attempting to create a scenario where a link becomes active if the pathname matches its href value. function Component() { const pathname = usePathname(); return ( <div className="links"> <Link href="/"> ...

Setting up a textarea tooltip using highlighter.js

I'm experimenting with using highlighter.js within a textarea. I've customized their sample by substituting the p with a textarea enclosed in a pre tag (for right-to-left language settings). <div class="article" style="width: 80%; height: 80% ...

The footer seems to be losing its stickiness and not staying at the bottom when I

My goal is to create a sticky footer for my webpage. Once you click the add new sports button, a drawer slides out and the footer remains fixed at the bottom. However, as I scroll down the page, the footer moves up instead of staying in place. I have tried ...

Is it possible to insert PHP and HTML within a multi-line PHP variable?

As a novice in coding, I am currently working on a project that involves creating dynamic dropdown forms. I have written some code and now I am curious if it's possible to insert PHP and HTML inside a multi-line PHP variable. The piece of code below i ...

Swap the image when hovering over it

Is it possible to modify this specific code so that a hover effect is triggered on mouseover? I attempted to look into similar questions and answers, but I found them difficult to understand. Here is the HTML snippet: <a href="RR.html"><img src ...

What is the best way to implement a shared function across two classes?

I have a jQuery code that works as intended with the .foto class. Now, I need to add the .fotoleft class to perform the same function. How can I make the function work for two different classes? $(document).ready(function() { $(".foto").click(functi ...

Instructions for incorporating animation into the CSS properties of an element as the class is updated using jQuery

The issue: I am facing a challenge with an HTML element that has a class called hidden which hides the element with the css attribute display: none;. When I use JavaScript to remove the class, the element immediately becomes visible again (the original di ...

Adjust the CSS to ensure that all elements have the height of the tallest element in the set

Can anyone help me solve a design issue I'm facing? I have a div with three floating buttons, but one of the buttons is taller than the others due to more content. I'm looking for a way to ensure that all buttons are the same height as the talle ...

"Utilizing CSS exclusively in conjunction with a PHP API for seamless integration

My CSS is working only when inline, and I'm struggling to track down the reason for this issue. As a beginner in web design, I am unsure of what steps to take to troubleshoot the problem. Since I cannot open the console to check for errors, what facto ...

Is it possible to modify the page contents using the htaccess file?

Is there a way to manipulate the content of a page using the htaccess file? Perhaps something similar to a redirect, but instead of directing the user to a new page, it would alter the existing page's content? ...

Utilize Bootstrap to allow a div's background to seamlessly spread into neighboring div elements

I am looking to create a visual effect where one splat of paint merges with another splat of paint. I currently have 4 columns, each with a different splat background. Is there a way to make these backgrounds interact with each other to create a random sp ...

iOS hover menu behavior: Close dropdown when the same element is tapped again on iPads

The navigation bar features categories such as: politics, economy, education Upon clicking on these categories, a dropdown menu appears with locations like: asia, europe, North America What I am looking for is this: If the user clicks (tabs) or hovers (o ...

What could be causing the paragraph margins to overlap and not display properly?

Two pages with identical layout and CSS stylesheets. One page displays correctly, while the other has overlapping margins for paragraphs. Unable to share entire source code due to length. Seeking insight into potential causes of this issue. https://i.sst ...

How can you adjust a background image to perfectly fit across various screen sizes?

I am just starting out with web development and I am building a website using HTML/CSS. The background of my site looks good on my macbook pro, but when I try to view it on a larger screen, the background repeats and the content stays in the top left corne ...

Create a custom input field with a dot masking feature

My challenge involves a specific input field setup: <input type="text" placeholder="........."> I want the dots in the placeholder to be replaced with letters as the user enters text. For example, if the user types "Hi", the placeholder should upda ...

When an HTML page is hosted on an external server, its formatting may

I recently put together an HTML page that incorporates some CSS and two simple highcharts from highcharts.com. After testing the page on my local machine and being satisfied with the outcome, I transferred it to a server on our internal network to make it ...

What is causing a scroll bar to appear at the bottom even though my content is not overflowing?

Here is the situation I am trying to describe: http://jsfiddle.net/CvgFS/1/ My English skills are not strong enough to explain it clearly, but I hope you can see why I am getting a scroll bar even though the content fits the page. This issue is happening o ...

Side navigation in Angular is not causing the main content to shrink

In my layout, I have a container that includes two sidenavs and multiple tables in between them. When I toggle the left sidenav, instead of the expected behavior where the content shrinks to accommodate the sidenav, the tables get pushed to the right as if ...