Creating a mobile-friendly table that remains within a specific width on smaller screens (CSS)

Ensuring consistency across all tables on my website is crucial to me, which is why I am utilizing the same class for all of them. While everything appears perfect on desktop, I'm facing challenges when it comes to mobile responsiveness. The tables fail to span the full width of the page and align columns correctly on smaller screens, and in some cases, the headers disappear for reasons beyond my understanding!

Below is the CSS code I am working with:

table {
  border-collapse: collapse;
  margin: 0;
  padding: 0;
  width: 100%;
  table-layout: fixed;
}

table caption {
  font-size: 1.5em;
  margin: .5em 0 .75em;
}

table tr {
  background-color: #f8f8f8;
  border: 1px solid #ddd;
  padding: .35em;
}

table th {
  background-color: #545556;
  color: white;
  padding: .425em;
  text-align: center;
  font-size: .75em;
  letter-spacing: .1em;
  text-transform: uppercase;    
}

table td {
  padding: .425em;
  text-align: center;
  word-wrap: break-word;
  font-size: 0.75em;
}

@media screen and (max-width: 768px) {
  img {max-width: 30%;}
  
  table {
    align-items: center;
    border: 0;
    display: inherit;
    font-size: 0.5em;
    width: 100%;
  }
  
  img {max-width: 30%;}
  
  table caption {
    font-size: 1.3em;
    overflow: hidden;
  }

  table thead {
    border: none;
    clip: rect(0 0 0 0);
    height: 1px;
    margin: -1px;
    overflow: hidden;
    padding: 0;
    position: absolute;
  }

  table tr {
    border-bottom: 1px solid #ddd;
    font-size: 0.4em;
    margin-bottom: .625em;
    display: inherit;
    width: 100%;
  }

  table td {
    border-bottom: 1px solid #ddd;
    font-size: .4em;
    text-align: center;
  }

  table td::before {
    content: attr(data-label);
    float: left;
    font-weight: bold;
    text-transform: uppercase;
  }

  table td:last-child {
    border-bottom: 0;
  }
}

HTML:

<table>
  <tbody>
    <thead>
      <th> TEST</th>
      <th> TEST</th>
      <th> TEST</th>
      <th> TEST</th>
      <th> TEST</th>
    </thead>
    <tr>
      <td> TEST</td>
      <td> TEST</td>
      <td> TEST</td>
      <td>TEST </td>
      <td>TEST </td>
    </tr>
    <tr>
      <td> TEST</td>
      <td>TEST </td>
      <td>TEST </td>
      <td>TEST </td>
      <td>TEST </td>
    </tr>
    <tr>
      <td> TEST</td>
      <td> TEST</td>
      <td> TEST</td>
      <td> TEST</td>
      <td> TEST</td>
    </tr>
    <tr>
      <td>TEST </td>
      <td>TEST </td>
      <td>TEST </td>
      <td>TEST </td>
      <td>TEST </td>
    </tr>
  </tbody>
</table>

CSS Fiddle: http://jsfiddle.net/dn1e9saL/1/

Answer №1

The reason the header is vanishing is due to the position: absolute; property being applied to it, and the incomplete width of the table is a result of setting display: inherit; on it.

To resolve this issue, simply eliminate these styles from your media query and the elements will display correctly.

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

Issue with collapsing custom-height navigation bar in Bootstrap 4

I have implemented a Bootstrap 4 navbar with a brand logo image sized at 150px x 33px. Now, I want to increase the height of the navbar to 80px. To do this, I added min-height: 80px; in my CSS. <!DOCTYPE html> <html lang="en"> <head> ...

Arranging several lists in a row without any specific order

I am trying to arrange multiple <ul> elements on the same line to create a shop-like display for products. Currently, I have set up several unordered list tags: ul { display: inline; } li { list-style: none; } <ul> <li>& ...

An unusual 1px variance appears in the background-image on Internet Explorer

Having trouble with a sprite of two images showing a 1px difference in ALL versions of Internet Explorer, yet working perfectly in Firefox. Check out the demonstration here: http://jsfiddle.net/bHUs3/6/ I'm feeling frustrated. What could be causing ...

Tips for effectively utilizing the display:none property to conceal elements and permanently eliminate them from the DOM

While working on my website, I utilized CSS media queries to hide certain elements by using display: none. Even though this effectively hides the element from view, it still lingers in the DOM. Is there a way to completely eliminate the element from the ...

Tips for preventing the inheritance of .css styles in React

I'm facing an issue with my react application. The App.js fragment is displayed below: import ServiceManual from './components/pages/ServiceManual' import './App.css'; const App = () => { return ( <> ...

Tips for modifying the color of the last anchor in a list item

li elements are utilized to create breadcrumb in a shopping cart. The breadcrumb displays all anchor elements using the style defined for an 'a' element from site.css. How can I make the last anchor's color black? I attempted the style belo ...

Using jQuery to manipulate the image within a specific div element

I'm facing an issue with locating the img src within a div. I've written a function to find all the divs with specific ids: function identifyDiv(){ divArray = $("div[id^='your']"); divArray = _.shuffle(divArray); } This is the ...

Ways to boost CSS transform with GPU acceleration

Is there a way to ensure smooth animations by utilizing GPU acceleration for CSS transforms in browsers? When does this acceleration occur and how can it be forced? For more information, check out this article ...

Adjust the height to match the shortest sibling child div height

In my layout, I have multiple rows with each row containing multiple columns. Within each column, there is a div and a paragraph - the div contains an image. My goal is to set the div with the class cover-bg to the lowest height of cover-bg in the same row ...

Exclusively Bootstrap-Timepicker

Is there a way to create a textbox with a time picker only? I am looking for guidance on how to do this using the following example: http://jsfiddle.net/RR4hw/11/ jQuery("#startDate").on("dp.change",function (e) { jQuery('#endDate').data("DateTi ...

Creating uniform table column widths within a flex container with dynamic sizing

I am working on designing a navigation system for my website using a table inside a flexbox. I want to ensure that the table columns have equal width and can scale dynamically. Additionally, I aim to wrap the text in the data cells without changing the cel ...

In the Swiper function of JavaScript within a Django template, the occurrence of duplicate elements (products) is being generated

Experimenting with displaying products in a Django template, I decided to utilize the Swiper js class. This allowed me to showcase the products within a div, complete with navigation buttons for scrolling horizontally. However, when testing this setup wit ...

What could be causing my Bootstrap 5 hover effect to malfunction?

Can anyone help with changing the hover effect of a bootstrap button in my code? Here's the HTML: <button type="button" class=" btn btn-dark">Sign Up</button> And here's the CSS I tried: .btn-dark:hover { color: ...

Steps for importing a JavaScript file from Landkit into a Vue project

Currently, I am working on an interesting project and utilizing Vue with Landkit Bootstrap. However, I am encountering issues with the JavaScript behavior of the Landkit component. I usually import the following links in my index.html file, but in the new ...

What is the best way to display ng-repeat elements in a horizontal layout

Looking for a way to display thumbnails horizontally using ng-repeat in AngularJS and Bootstrap. Any ideas on how to achieve this with CSS or Bootstrap? <div class="row"> <ul class="col-md-4" id="phones"> <li class="thumbnail" ...

What is the best way to apply styles based on specific screen widths while still having default styles for other screen sizes?

Here is the code snippet I am working with: <TableCell sx={{ borderBottom: { xs: 0, lg: 1 } }}> <Typography variant="body2">{account.name} ({account.currency})</Typography> </TableCell> I am facing an issue where the ...

Show or conceal input fields depending on the selection of radio buttons

Hello everyone, I am new to JavaScript and currently learning. Can someone please assist me in fixing this code? The required inputs are: radio-button-1; radio-button-2; input-fields-set-1; input-fields-set-2; input-field-submit. My objective is: Upon ...

What is the best way to create underlined text that is either left-aligned or centered?

I am looking to replicate this specific style of decoration in a full width container. My goal is to have it aligned to the left of the text and restricted to a maximum width. https://i.stack.imgur.com/H8DXT.png ...

Aligning two divs both horizontally and vertically within two adjacent columns each with a width of 50%

Struggling to align two divs both vertically and horizontally within their respective parent containers that are placed side by side with 50% width and 100% height? Check out my solution on Codepen for reference. View Codepen Example *** HTML *** <di ...

What is the best way to create square editor tabs in Eclipse without using swt-border-radius?

The design of Eclipse's rounded and/or swooshing tabs is starting to feel outdated in today's modern era. I thought that by adding the following line in my default.css file, I could get rid of the rounded corners: swt-corner-radius: 0px Howeve ...