Displaying and hiding columns in Bootstrap4

I am looking for a solution to toggle the visibility of columns based on the screen size.

For example:

  • On screens smaller than 576px, I want 2 columns to be visible
  • On screens equal to or larger than 768px, I want 4 columns to be visible
  • On screens equal to or larger than 1200px, I want 6 columns to be visible.

Since there are multiple rows involved, I prefer not to have separate tables for each scenario, but simply show or hide columns based on the screen size.

You can view the JS Fiddle demo at: https://jsfiddle.net/tkcynbjk/

<div class="row">
    <div class="col-sm-12">
        <table class="table table-striped">
            <thead>
            <tr>
                <th class="d-none d-table-cell" scope="col">ALWAYS 1</th>
                <th class="d-none d-table-cell" scope="col">ALWAYS 2</th>
                <th class="d-none d-table-cell d-md-table-cell" scope="col">MED 1</th>
                <th class="d-none d-table-cell d-xl-table-cell" scope="col">XL ONLY 1</th>
                <th class="d-none d-table-cell d-md-table-cell" scope="col">MED 2</th>
                <th class="d-none d-table-cell d-xl-table-cell" scope="col">XL ONLY 2</th>
            </tr>
            </thead>
            <tbody>
                <tr>
                    <td>A</td>
                    <td>B</td>
                    <td>C</td>
                    <td>D</td>
                    <td>E</td>
                    <td>F</td>
                </tr>
            </tbody>
        </table>
    </div>
</div>

Answer №1

To make it work, simply delete the d-table-cell classes.

For a full demonstration of the media queries in action, run the snippet in fullscreen mode or visit the following updated fiddle: https://jsfiddle.net/jkrielaars/tkcynbjk/1/

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" rel="stylesheet"/>
    <div class="row">
        <div class="col-sm-12">
            <table class="table table-striped">
                <thead>
                <tr>
                    <th class="" scope="col">ALWAYS 1</th>
                    <th class="" scope="col">ALWAYS 2</th>
                    <th class="d-none d-md-table-cell" scope="col">MED 1</th>
                    <th class="d-none d-xl-table-cell" scope="col">XL ONLY 1</th>
                    <th class="d-none d-md-table-cell" scope="col">MED 2</th>
                    <th class="d-none d-xl-table-cell" scope="col">XL ONLY 2</th>
                </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>A</td>
                        <td>B</td>
                        <td  class="d-none d-md-table-cell">C</td>
                        <td class="d-none d-xl-table-cell">D</td>
                        <td class="d-none d-md-table-cell">E</td>
                        <td class="d-none d-xl-table-cell">F</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>

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 for PHP to process a multidimensional array being submitted via $_POST along with another field serving as a key in the same form

My web form includes two select fields: "user_id[]" and "equipment_id[]. Here are some key points: I use a Javascript function to duplicate the group of "user_id[]" and "equipment_id[] fields each time I want to add another set. The "user_id[]" field all ...

Is there a specific HTML tag or element that can achieve a similar result as shown in the image below?

https://i.sstatic.net/FjeSD.png I am looking to create a container similar to this one with a border and a caption centered within. ...

Is there a way to adjust the sorting click to cover the entire height of the table header cell?

Is it possible to make the entire blue rectangle clickable for sorting purposes? Currently, I can only click around the text if it is short, but not within the blue area. When the text is longer, like in the case of "Bla-bla-bla," the entire blue area beco ...

Utilizing ng-class with Boolean values in AngularJS

On my page, I have a pair of buttons that control the visibility of elements using ng-hide and ng-show. <a ng-click="showimage=true" ng-class="{ 'activated': showimage}" class="button">Images</a> <a ng-click="showimage=false" ng-c ...

Distinguishing between a video or image when uploading files in JavaScript

I've been researching whether JavaScript can determine if a file is a video or an image. However, most of the information I found only shows how to accept these types using the input tag. What I really need is for JS to identify the file type and the ...

Slow mouse hover element highlighting in jQuery is causing issues

I'm currently working on creating a simple chrome extension that allows me to highlight an element when hovering over it with the mouse. I've been trying to implement a feature where the element is highlighted by adding a border shadow, but this ...

Adjusting alignment differences during the transition from Bootstrap 4 to Bootstrap 5?

While developing a blog site on Flask (Python) with Bootstrap 4, I initially used the Bootstrap 4 CDN. However, upon the release of Bootstrap 5, I made the switch to using Bootstrap 5 locally. Two issues have arisen from this switch: In Bootstrap 4, the ...

Why does the content spill out of the div when I add padding?

Can someone help me understand why when I add padding to the "header div," the content overflows the div? For instance, if I set it to 60%, instead of limiting to within the div, it takes up 60% of the entire page. I thought that % would be applied based o ...

Adjusting the dimensions of the image to fit within the header box

I need help resizing the image to fit my website header. The background-image I am using is url("image/header\header.jpg") with a background-size set to cover. Any assistance would be greatly appreciated. Please note that the picture is not my own wor ...

Is it possible to access the operating system's native emoji picker directly from a website?

While there are numerous javascript plugins and libraries available for allowing users to select emojis for text inputs, both Windows and Mac operating systems already have their own native emoji pickers accessible via ⊞ Win. or CTRL⌘Space. Is there a ...

Create an HTML div element that spans the full width of the

I'm facing an issue on my HTML page where a div containing users from a chat system database is not displaying correctly. The ul li tag within the parent div is not taking up the full width as expected. Here's how it should look: http://prntscr.c ...

Appear and disappear gradually when hovering

When hovering over #one, the class .hidden is applied to #first. I have added a script to make .hidden fade out using transition: ease-in 0.3s;, but I am having trouble getting the fade in effect to work. I attempted adding transition: ease-in 0.3s; to #fi ...

Using javascript to quickly change the background to a transparent color

I am in the process of designing a header for my website and I would like to make the background transparent automatically when the page loads using JavaScript. So far, I have attempted several methods but none seem to be working as desired. The CSS styles ...

How can you deactivate all form elements in HTML except for the Submit button?

Is there a method available to automatically deactivate all form elements except the submit button as soon as the form loads? This would entail pre-loading data from the backend onto a JSP page while restricting user access for editing. Users will only be ...

Having Trouble Changing CSS with Rails Link_to CSS ID

While following the ruby on rails guide, I came across a specific way to write link_to's with a css id: <%= link_to "About", about_path, id: "third" %> I also attempted: <%= link_to ("About", :id => "third"), about_path %> Although ...

Concealed file selection in Python Automation with Selenium

I'm experiencing an issue while attempting to upload a file to a hidden file input using Python Selenium. To provide more clarity, please refer to the image linked below. Example of Issue For the first field, I uploaded a file myself. Here's a ...

Adjust the size of the text and the textarea at the same time

I am trying to resize text simultaneously while resizing the textarea in my code. I am using jQuery for this functionality. However, I believe there is an issue with the click function being separated. Any advice on how to fix this would be greatly appreci ...

Comparison between WAMP and Live server integration with Facebook for connecting applications

I've been facing some challenges while integrating my website with Facebook Connect. I have been following the instructions provided in this guide. When attempting to run the following code from localhost, please note that for security reasons, my ap ...

Pass a variable between popup.js and background.js in a Chrome Extension

Despite finding some answers to similar questions, none of them provide a complete solution. The information in the answers lacks necessary documents that were not included in the original question. My goal is to create a scaffold for Chrome extensions wh ...

Complete and submit an HTML form generated dynamically from database entries using a for loop

I am developing an online survey feature that involves capturing input from a form on one page and displaying the polls on another page using a for loop. The forms can have radio buttons or checkboxes based on the creator's preferences. I am encounter ...