Troubleshooting: Unresponsive behavior of CSS table

I have been working on implementing responsive CSS for a table. I recently came across a helpful tutorial on http://css-tricks.com/responsive-data-tables/ (How to create a responsive table using CSS). While the tutorial worked fine on some sample pages, I encountered an issue when trying to apply it to my own HTML page. The problem arises with window resizing as the table does not respond correctly. Here's a snippet of my HTML code:

    <style>
        /* 
Generic Styling, for Desktops/Laptops 
*/
        table
        {
            width: 100%;
            border-collapse: collapse;
        }
        /* Zebra striping */
        tr:nth-of-type(odd)
        {
            background: #eee;
        }
        th
        {
            background: #333;
            color: white;
            font-weight: bold;
        }
        td, th
        {
            padding: 6px;
            border: 1px solid #ccc;
            text-align: left;
        }
        /* 
Max width before this PARTICULAR table gets nasty
This query will take effect for any screen smaller than 760px
and also iPads specifically.
*/
        @media only screen and (max-width: 760px), (min-device-width: 768px) and (max-device-width: 1024px)
        {
            /* Force table to not be like tables anymore */
            table, thead, tbody, th, td, tr
            {
                display: block;
            }
            /* Hide table headers (but not display: none;, for accessibility) */
            thead tr
            {
                position: absolute;
                top: -9999px;
                left: -9999px;
            }
            tr
            {
                border: 1px solid #ccc;
            }
            td
            {
                /* Behave  like a "row" */
                border: none;
                border-bottom: 1px solid #eee;
                position: relative;
                padding-left: 50%;
            }
            td:before
            {
                /* Now like a table header */
                position: absolute; /* Top/left values mimic padding */
                top: 6px;
                left: 6px;
                width: 45%;
                padding-right: 10px;
                white-space: nowrap;
            }
            /*
    Label the data
    */
            td:nth-of-type(1):before
            {
                content: "First Name";
            }
            td:nth-of-type(2):before
            {
                content: "Last Name";
            }
            td:nth-of-type(3):before
            {
                content: "Job Title";
            }
            td:nth-of-type(4):before
            {
                content: "Favorite Color";
            }
            td:nth-of-type(5):before
            {
                content: "Wars of Trek?";
            }
            td:nth-of-type(6):before
            {
                content: "Porn Name";
            }
            td:nth-of-type(7):before
            {
                content: "Date of Birth";
            }
            td:nth-of-type(8):before
            {
                content: "Dream Vacation City";
            }
            td:nth-of-type(9):before
            {
                content: "GPA";
            }
            td:nth-of-type(10):before
            {
                content: "Arbitrary Data";
            }
        }
    </style>
    <body>
        <table>
            <thead>
                <tr>
                    <th>
                        First Name
                    </th>
                    <th>
                        Last Name
                    </th>
                    <th>
                        Job Title
                    </th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>
                        James
                    </td>
                    <td>
                        Matman
                    </td>
                    <td>
                        Chief Sandwich Eater
                    </td>
                </tr>
                <tr>
                    <td>
                        The
                    </td>
                    <td>
                        Tick
                    </td>
                    <td>
                        Crimefighter Sorta
                    </td>
                </tr>
            </tbody>
        </table>
    </body>
</html>

The output on resizing the window seems like shown in the figure below:

It functioned perfectly on js-fiddle displaying as illustrated in the figure below:

For further guidance on what might be wrong in my code, any help would be greatly appreciated. Thanks!

Answer №1

It is important to establish a min-width for your specified viewport in relation to the parent element table and the child tr if necessary.

table { 
  width: 100%; 
  min-width: 320px; // this size should suffice
  border-collapse: collapse; 
}

Additionally,

tr { 
   min-width: 200px; // define if needed or desired
}

Consider the resolution you are targeting - I see your @media query is for tablets, but it seems you are aiming for smaller screens -- typically, going below 320px shouldn't be necessary.

Have you thought about utilizing nested divs?

Answer №2

Just a minor error - I forgot to specify the document type as html5 at the top of the HTML.

After fixing that, everything works perfectly with the current code.

<!DOCTYPE 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

Arrange the element as though it were the foremost

My specific scenario is as follows: <div id="container"> <p id="first">...</p> <p id="second">...</p> </div> I am looking to rearrange the order of second and first within the HTML stru ...

Creating a Shadow Effect on CSS Triangles

I have created a unique design that resembles a large arrow pointing to the right. <div style=" font-size: 0px; line-height: 0%; width: 100px; border-bottom: 80px solid #8cb622; border-right: 62px solid #dadbdf; "></div> <div style=" font ...

Is it possible to utilize :not in this scenario?

<ul class="promocode"> <li>one</li> <li>two</li> <li class="three">three</li> </ul> Is there a way to use :not in order to apply a hover effect to the entire list, except when hovering ov ...

Updating part of a page while also changing the navigation

Apologies in advance, as this is my first attempt at coding a website. I have a specific need where I want to update only one div on my web page when a link in the navigation bar is clicked. <body> <div id="wrapper"> <header id= ...

What is the best way to achieve a transparent background for an element?

Having trouble making elements transparent in the background for mobile view with React. I've attempted adding background-color and background with a value of transparent to various classes, but it's not working as intended. Any suggestions on h ...

Displaying an interactive 2D floorplan in a web browser with the use of html5 and javascript

In the process of updating my old Flash viewer, I am looking to display interactive 2D floorplans exported from AutoCAD. Currently, I convert the AutoCAD files into XML files containing the X and Y coordinates of the various elements on the floorplan such ...

How can I create editable text using typed.js?

How can I achieve the same text animation effect on my website as seen on the homepage of ? I want to utilize the library available at . Specifically, how do I stop the animation when the text is clicked, allowing users to input their own text? Below is ...

Dark Mode Caused by CSS Overriding Accordion

I'm currently using an accordion feature from Flowbite, but I am facing issues with it defaulting to dark mode upon page load. Upon inspecting the element, I discovered that there is some dark mode CSS included in the Flowbite package. In an attempt t ...

CSS - When using both display: flex and position: absolute, the anchor point is shifted

I have a flexbox element with three children. I want the second child to overlap the first child using absolute positioning, like this: Desired Outcome: https://i.stack.imgur.com/i479w.png It's important that the second child appears on top of the ...

Align text divs vertically and ensure font size is responsive within a container div

Within this designated div, I have included: https://i.stack.imgur.com/j099H.png In an attempt to ensure that the font size of the text adjusts responsively, I implemented the following code: font-size:calc(xx + 1.5vw); However, upon resizing the scree ...

Ajax - Retrieving data from a different webpage

My index.php contains the following HTML: <div id="showDetails"> </div> <div id="showList"> </div> And this Ajax function is also in index.php: function ...

Personalizing the share button by changing the icon font to an image

I've been working on incorporating this share button into my website.... Although it functions properly as is, I want to switch out the icon font for an image. I attempted to modify certain CSS properties, but encountered some issues. http://jsfidd ...

The standard jQuery Mobile CSS styling does not seem to be working across various browsers, extensive testing has been conducted

I'm currently experimenting with jQuery Mobile to enhance my skills, but I'm facing issues with applying the basic CSS styling. I have included the css link from the jQuery Mobile website and ensured that I am connected to the internet. However, ...

Looking to align a radio button in the middle of an inline-block?

Is it possible to center a radio button within an inline-block? I have provided a demo on JSFiddle that displays the current layout and how I envision it should appear. Check out the demo here Here is the code snippet in question: <span style="widt ...

The reverse is concealed following a CSS flip transition

After applying a CSS flip animation to a card, the backside isn't visible once flipped. Here is the HTML code for the card component named "ptree-card": <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <!-- Rest of th ...

Enhancing the performance of jquery selection speed

I am currently working on a code that manipulates the HTML code of an asp.net treeview. Since this code runs frequently, I need to ensure that it executes with maximum speed. I am interested in expanding my knowledge on jQuery selectors and optimizing th ...

Run JavaScript code before finalizing the "submit" action within a Ruby on Rails application

Having trouble with utilizing old JS scripts found on Stack Overflow. <div class="form-actions"> <%= f.button :submit, class:"btn btn-success create-campaign" %> </div> The submit button is causing an issue for me. <div clas ...

Navigate through information within designated boundaries

In order to achieve vertical scrolling of a sidebar div and its content (3 links) between two points, I have utilized the CSS property position:fixed; top: 100px. While this setup works well initially by positioning the sidebar 100px down from the top and ...

What is the best way to protect old documents when selecting new files in a multi-file uploader?

I created a file upload feature with file previews using HTML5 and the file reader, which is functioning well. However, I'm facing an issue where old files selected by the user get removed from the input file field if a new file is selected in a singl ...

What is the best way to code an HTML block with jQuery?

Could someone guide me on creating the following using jQuery: <div class="card"> <img src="images/cat6.jpg" alt="Avatar" style="width:100%"> <div class="container"> <h4><b>John Watson</b></h4> ...