Tips for creating a responsive table using class naming conventions

Currently, I am working with media queries that target specific devices to adjust the display of tables. By using the code provided below, I can modify the table structure based on screen size and device specifications:

@media
only screen and (max-width: 760px),
(min-device-width: 768px) and (max-device-width: 1024px)  {

    /* Changing the table layout */
    table, thead, tbody, th, td, tr {
        display: block;
    }

    /* Hiding the table headers for accessibility */
    thead tr {
        position: absolute;
        top: -9999px;
        left: -9999px;
    }

    tr { border: 1px solid #ccc; }

    td {
        border: none;
        border-bottom: 1px solid #eee;
        position: relative;
        padding-left: 50%;
    }

    td:before {
        position: absolute;
        top: 6px;
        left: 6px;
        width: 45%;
        padding-right: 10px;
        white-space: nowrap;
    }

}

The challenge now is applying this media query selectively to a specific table rather than all table elements. Any suggestions on how I can achieve this?

Answer №1

To customize the appearance of your table, you can assign a class or id to it - for example, I will add class="test" to your table. Then you can style it using CSS like this:

@media
only screen and (max-width: 760px),
(min-device-width: 768px) and (max-device-width: 1024px)  {

    /* Modify table layout */
    .test, .test thead, .test tbody, .test th, .test td, .test tr {
        display: block;
    }

    /* Keep table headers accessible but hidden */
    .test thead tr {
        position: absolute;
        top: -9999px;
        left: -9999px;
    }

    .test tr { border: 1px solid #ccc; }

    .test td {
        /* Style as "row" elements */
        border: none;
        border-bottom: 1px solid #eee;
        position: relative;
        padding-left: 50%;
    }

    .test td:before {
        /* Treat as table header */
        position: absolute;
        /* Top/left values simulate padding */
        top: 6px;
        left: 6px;
        width: 45%;
        padding-right: 10px;
        white-space: nowrap;
    }

}

Answer №2

Using CSS3 media rules is similar to implementing an if condition in your CSS code.

For instance, imagine you have a table with the class 'myTable'.

For example: If you want to set the width of the myTable class to 500px for any devices with a screen width resolution between 640px to 1024px, here's how you can achieve that:

@media all and (max-width: 1024px) and (min-width: 640px)  
{
   /* This comment explains that you are setting the width of tables with the myTable class. */
   table.myTable
   {
      width: 500px;
   }
}

By using the above CSS statement, you're essentially stating

If the client is using any kind of device with a width between 640px to 1024px, apply the specified rule.
.

Alternatively, if you only want to set the width of myTable to 500px for users who are using an iPhone in landscape mode, you can do this:

@media only screen 
  and (min-device-width: 320px) 
  and (max-device-width: 480px)
  and (-webkit-min-device-pixel-ratio: 2)
  and (orientation: landscape)
{
   table.myTable
   {
      width: 500px;
   }
}

Explore more about CSS Media Query Tricks at CSS-Tricks

Now, let's take your media rule as an example:

@media only screen and (max-width: 760px),
(min-device-width: 768px) and (max-device-width: 1024px)

This particular rule indicates that the wrapped CSS rules should be applied to

Screens, tablets, or smartphones with a screen width less than 760px OR any device with a screen width ranging from 768px to 1024px.

Check out the CSS Documentation for further understanding.

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 it possible that the HTTP module's deflate compression is only effective on specific CSS files and not all

I've been working on setting up mod deflate and gzip on my server, and while it's mostly functioning smoothly, I've noticed that not all files are being caught. It's not a matter of missing ALL javascript or CSS, but rather that some ar ...

How to center a video horizontally using Bootstrap styling

In my search for a solution, I have not been able to find a way to horizontally center a video within a Bootstrap div or header section while also keeping the video vertically aligned with the top. The div or header section has a 100% width with fixed heig ...

Looking for CSS templates for improving your business web apps?

Many CSS templates out there are fixed-width, typically around 900 pixels wide... Right now, I'm in the process of creating an intranet Rails application and I'm seeking a good resource for CSS templates designed specifically for business applic ...

Retrieve the element by its Id for precise targeting on a particular webpage

I'm new here and looking for some assistance, so please help if you can :) I'm working on a web page but encountered an issue that I hope you can help me solve. Here are the 3 files involved: home.html --> Main page login.html --> Page ...

Add a captivating backdrop to a div element

So I have this unique HTML element that showcases a large headline with two decorative lines on either side, extending to the full width of the element. However, I now have a requirement to display some text as a background behind this element. The issue ...

Adjusting the position of a stationary element when the page is unresponsive and scrolling

Managing a large web page with extensive JavaScript functionality can be challenging, especially when dealing with fixed position elements that update based on user scroll behavior. A common issue that arises is the noticeable jumping of these elements whe ...

Effortless side-to-side and up-and-down scrolling

I recently launched a new website and I am looking to implement smooth horizontal and vertical scrolling. I want to have a menu on the right side that allows users to easily navigate to different sections. While I have found helpful codes and guides for ...

Need to conceal certain images in Isotope, but ensure they can easily be revealed when necessary?

I have a collection of various images on my website coded with isotope. My question is, how can I hide specific images that I don't want to display initially, but have them appear when needed? I have managed to create a "show all" list using the code ...

CSS Duo-Toned Background

I've been searching everywhere for a solution to this issue. I have a design that I'm attempting to code using divs and CSS. The top half of the image features a gradient that transitions from left to right with different colors. My struggle lies ...

Expanding the table area in bootstrap

I am currently working on a video slider using bootstrap. I have added a table within the slider (carousel) and it is functioning perfectly. However, I am now looking to update the layout to resemble the image below. I have successfully applied the backgro ...

Switching background images on click with a smooth transition

I'm in the process of creating a website and I want to be able to change background images with ease when clicked, like using fade in or fade out effects. The onclick functionality is already implemented with jQuery, but I'm having trouble incor ...

Paragraph featuring keywords highlighted by categoryIn this text, we will be analyzing different categories

I am looking to implement a visual feature similar to the image below using react js. Can anyone provide guidance on how to accomplish this? I have a collection of words categorized as Organization, Person, and Location. My goal is to assign different colo ...

Angular, a Self-sufficient Module of Cascading Style Sheets

I have developed a factory that generates HTML content. I want to showcase this generated HTML within a specific section of my application, without its CSS affecting the rest of the site, and vice versa. While using an iframe is one option, I believe there ...

Tips on toggling the visibility of a div using jQuery and CSS via a toggle switch

Hey there, I need some help with toggling the opening and closing of a div when clicking on a toggle switch. $('.tog').on('click', function() { $('.cntr').show(); }); .switch { position: relative; display: inline-bloc ...

What disparities exist between utilizing Arial Black font and regular Arial font with the <Strong> tag?

Despite setting my text to Arial Black, it appears as regular text on Firefox. To workaround this issue, I tried using the Arial font with the Strong tag but couldn't notice any visual difference. Should I be concerned about this discrepancy? Thank y ...

Is it possible to adjust the width of Material-UI TextField to match the width of the input text?

Is there a way for Material-UI to adjust the width of the TextField element automatically based on the input text? When creating a form view/edit page and rendering data into fields, I also have parameters set by the server. It would be convenient to have ...

Linking all styles with Angular 2

Is it possible to apply a style when the exact nature of that style is unknown? Consider a scenario where I have a model containing string variables defining styles, as shown below: myStyle1:string="margin-left:10px"; myStyle2:string="margin ...

Add hyphens to separate the words in AngularJS if there is a break in the string

Within a div of set width, a string is being bound to it. This string could be short or long. I would like for the string to break with a hyphen inserted on each line except for the last one. For example: If the string is "misconception" and it breaks at ...

Challenges arise when trying to maintain a div aligned to the far right of the browser window as the dimensions of the browser change, especially after applying an

I have a main container div that holds two smaller divs side by side. When one of these smaller divs is clicked, it moves to the far left or right side of the browser window depending on which one was clicked. This functionality works perfectly as intended ...

Adjustable slider height in WordPress

Struggling with the height of the slider on my website. I've tried various CSS adjustments and even tweaked the Jquery, but it still doesn't display properly on mobile devices. For instance, setting a height of 300px looks perfect on desktop brow ...