How to Toggle Visibility of TH and TD Elements using Bootstrap 5

In my current setup, I have the following table:

<table class="table table-sm table-borderless w-auto">
  <thead>
    <tr>
        <th>
          <input type="checkbox" value="false" class="form-check-input" />
        </th>
        <th>#</th>
        <th>Customer</th>
        <th>Date</th>
        <th>Channel</th>
        <th>Status</th>
        <th class="d-none d-sm-none d-md-block d-lg-block d-xl-block d-xxl-block">Payment</th>
        <th class="d-none d-sm-none d-md-block d-lg-block d-xl-block d-xxl-block">Summary</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>
          <input type="checkbox" value="false" class="form-check-input" />
        </td>
        <td>24334</td>
        <td>Customer X</td>
        <td><time datetime="2022-08-06T10:32">28.08.22 - 10:32</time></td>
        <td>POS</td>
        <td>Received
        </td>
        <td class="d-none d-sm-none d-md-block d-lg-block d-xl-block d-xxl-block">Cash</td>
        <td class="d-none d-sm-none d-md-block d-lg-block d-xl-block d-xxl-block">4 products = $123.45</td>
      </tr>
    </tbody>
  </table>

Upon checking the above code, I have noticed that I am trying to hide the Payment and Summary columns on smaller screens. However, the behavior is not as expected. When the page loads on a md screen size or above, both columns are visible but they stack on top of each other. This also applies to the contents of the td tags. Refer to the screenshot below for better clarification:

https://i.sstatic.net/ZgI0x.png

The same scenario occurs with span tags as well. I have three buttons with font awesome icons and text next to them. Here is the code snippet:

    <button type="button" class="btn">
      <i class="fa fa-search"></i> <span>Detailed Search</span>
    </button>

Upon execution, the output looks like this:

https://i.sstatic.net/wrBVn.png

Nevertheless, when I add the

d-none d-sm-none d-md-block d-lg-block d-xl-block d-xxl-block
to the span element, the display changes as follows:

https://i.sstatic.net/c4aTn.png

Replacing block with inline resolves the issue with the buttons. But, when applied to the th and td tags, there is a misalignment problem with the elements.

https://i.sstatic.net/cG3RZ.png

https://i.sstatic.net/DV7Rf.png

https://i.sstatic.net/E4CHx.png

I have considered using the hidden property instead of none, but it hasn't changed the behavior. Any assistance in resolving this issue would be highly appreciated.

Answer №1

One possible reason for the issue you are experiencing could be due to using display block, whereas table cells require display: table-cell. It's also important to refrain from using unnecessary classes. Remember the principle of mobile-first design (d-none), followed by adding classes only for larger screens (md => d-md-table-cell), resulting in: "d-none d-md-table-cell"

 <th class="d-none d-md-table-cell">Payment</th>
 <th class="d-none d-md-table-cell">Summary</th>

For a visual example, check out this CodePen link

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

Ways to remove the address bar from a mobile browser like Chrome or an Android browser

Is there a way to make videojs go full screen on Android devices when the URL is entered, without displaying the address bar? ...

The arrow extends just a hair below the boundaries of the div, by approximately 1 pixel

I am facing an issue with my nav bar code. In Firefox and Chrome, everything works perfectly fine - there is a small arrow displayed below the links when hovered over. However, in Internet Explorer, the arrow appears slightly below the div, causing only pa ...

Utilizing the nth-child selector to apply border-spacing to all rows except the first and last

I've been attempting to add border-spacing to all rows except the first and last ones using nth-child, but for some reason, it's not having the desired effect. Can anyone point out what mistake I might be making? table { border: 1px solid blac ...

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 ...

When the "change" event listener is added to an input element, the innerHTML is updated as the value increases but not when it decreases

My div block contains a value that I need to adjust as the input value changes. While the adjustment works when the input value increases, it does not change when the input value is decreased. It seems like there may be an oversight in my logic that I am m ...

The issue with jQuery click function seems to be limited to Firefox

After a long hiatus from posting here, I hope this isn't breaking any rules. For those who prefer visual examples, you can visit the live page at: The drop-down menu functions perfectly in IE, Chrome, and Safari, but seems to be causing issues in Fir ...

Implementing Content-Security-Policy for a web application embedded in an iframe

Hey there! I've got this cool webapp called myApp, developed using Spring Boot and Vaadin. It's going to be deployed on a Tomcat server at http://tomcatserver:8080/myApp Now, what I want to do is display the webapp in an iframe like this: <if ...

When hovering on the list items, the text-decoration with a border-bottom and vertically centered unordered list "jumps"

I'm specifically looking for a solution using CSS only. I have a navigation bar with an unordered list containing list items. My question is how to underline these list items with a border-bottom. The additional requirement is that the navigation bar ...

Achieving Dynamic Center Alignment for One or Two DIVs

My goal is to create a page layout with three DIVS arranged as Left|Center|Right, while still being able to display different combinations such as Center, Left|Center, or Center|Right in the center of a wrapper div. I have been using jQuery toggle to swit ...

A comprehensive guide on creating a package that combines an href link with an <li> element

I am currently using the <li> tag to display href links. The issue I am facing is that when I click on the 'box,' it does not directly link to another page. Instead, I have to click on the href link for it to redirect to another page. Is th ...

Obtaining a binary value in the switch component of materialize framework with Typescript

Is there a way in Typescript to assign a value of 1 when a checkbox is checked and 0 otherwise? I am working on a project that uses the materialize framework. Below is the code snippet in question: <div class='switch'> <label&g ...

Using an HTML element to pass a variable into a replace function

I am looking to highlight a 'SearchString' by replacing it with <span style="background-color: yellow">SearchString</span> within a targetString. The SearchString varies, so I am wondering how I can make this happen. This is what I ...

HTML - Selecting Different Values in One Drop Down Based on Another Drop Down

When selecting "First Year" in the initial drop-down menu, the options "Sem1" and "Sem2" should be displayed in the second drop-down menu. Similarly, when choosing "Second Year" in the first drop-down menu, the choices "Sem3" and "Sem4" should appear in th ...

Issue with Firefox-Android causing dropdown toggle to malfunction

When I manually trigger a dropdown, it closes when any click is performed outside of it (while open). This code works in all browsers except for Firefox on Android. Why does this happen? It seems like the event parameter doesn't reach the function ...

What could be causing certain items in my Bootstrap navbar to be hidden on mobile screens?

When I view my website on mobile devices, only the logo appears in the navbar, not the other elements. Can you help me troubleshoot this issue? NavBar HTML Code: .navbar-logo { width: 93.75px; height: 23.5px; margin-left: 10rem; } .navba ...

I encountered an issue where the font awesome icons failed to load on my website

My website is experiencing an issue where all the Font Awesome icons are not displaying despite having added Font Awesome. Here is the link to my site: https://i.stack.imgur.com/RcNtv.png .login-top .lg-in::before { font-family: "Fontawesome"; ...

jQuery is an excellent tool for implementing drag and drop folder upload functionality, all without

I am creating a drag and drop file uploader with basic functionality. Here is the code: HTML: <div class="drop-box drop-area"> <form enctype="multipart/form-data" id="yourregularuploadformId"> <input type="file" name="files[]" ...

Import css background-images from a separate local directory that is located outside the root directory of the Next JS

I am facing a challenge with hosting a next.js app within the file structure of a parent website. I need the CSS in the app to use images that are located outside the app's file structure but within the parent website's file structure. Here is a ...

What could be causing the server to return an empty response to an ajax HTTP POST request?

Attempting to make a POST request using ajax in the following manner: $.ajax({ type: "POST", url: 'http://192.168.1.140/', data: "{}", dataType: "json", ...

Trouble with Click event not working in Electron and mouse cursor not changing when hovering over an HTML button?

I'm in the midst of a project that involves using the electron framework. Within my project, I have an HTML file, a CSS file, and a Javascript file. The issue at hand is that when I hover over a button, the expected hand pointer icon fails to appear d ...