The alignment of Bootstrap table cells is inconsistent

Trying to implement a fixed header table, but encountering issues with alignment

table-fixed tbody {
    height: 300px;
    overflow-y: auto;
    width: 100%;
  }

  .table-fixed thead,
  .table-fixed tbody,
  .table-fixed tr,
  .table-fixed td,
  .table-fixed th {
    display: block;
  }

  .table-fixed tbody td,
  .table-fixed tbody th,
  .table-fixed thead>tr>th {
    float: left;
    position: ative;

    &::after {
      content: '';
      clear: both;
      display: block;
    }
  }
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="col-xs-5 col-sm-6 col-lg-8 rounded shadow">
        <div class="table-responsive">
          <table class="table table-fixed">
            <thead>
              <tr>
                <th scope="col" class="col-3">#</th>
                <th scope="col" class="col-3">First</th>
                <th scope="col" class="col-3">Last</th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <th scope="row" class="col-3">1</th>
                <td class="col-3">Mark</td>
                <td class="col-3">Otto</td>
              </tr>
              <tr>
                <th scope="row" class="col-3">2</th>
                <td class="col-3">Jacob</td>
                <td class="col-3">Thornton</td>
              </tr>
            </tbody>
          </table>
        </div><!-- End -->
      </div>

Observing the result:

view image for reference

The table rows are not aligning correctly due to having only 3 cells in each row. Adding one more cell (total of 4) fixes the issue. The code was sourced from here, and I'm unsure where the problem lies.

Answer №1

modify your content to replace th with td in the body:

<div class="col-xs-5 col-sm-6 col-lg-8 rounded shadow">
    <div class="table-responsive">
      <table class="table table-fixed">
        <thead>
          <tr>
            <td scope="col" class="col-3">#</td>
            <td scope="col" class="col-3">First</td>
            <td scope="col" class="col-3">Last</td>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td scope="row" class="col-3">1</td>
            <td class="col-3">Mark</td>
            <td class="col-3">Otto</td>
          </tr>
          <tr>
            <td scope="row" class="col-3">2</td>
            <td class="col-3">Jacob</td>
            <td class="col-3">Thornton</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

Box Pattern with Pop-Up Modal

Check out the grid of squares I've coded below: <div class='square-container'> <div class='square'></div> <div class='square'></div> <div class='square'></div& ...

Tips for adjusting the height of a div element to completely occupy the unused portion of the viewport

How can I adjust the height of the sliding-in menu to fill the viewport below the mobile-navbar div without extending beyond it and causing scroll bars? The desired height should be: [100vh-(height of mobile-navbar)] I want to avoid setting a fixed value t ...

if the condition is not met, proceed to the alternate template eventually

Hey there, I'm facing an issue with my code that resembles something like this: <ng-container *ngIf="firstCondition; else Farewell"> <ng-container *ngIf="innerContainer"> <div class="Hello Message"> {{HelloMessa ...

Automatically align a Div to the right within an ngFor directive in an Angular 6 application

<div class="row"> <div class="col-lg-4 col-xs-6" *ngFor="let client of clients;index as i"> <!-- small box --> <div class="small-box bg-dashboard-box" > <div class="inner"> <div class="text-black"> ...

Place the image on the canvas

I currently have a canvas where I am able to add text layers and images from flickr. My goal is to enable users to upload an image to the canvas using the html input. For uploading images from flickr, I am using this code: $(".search form.image-search"). ...

Detecting the click area within a window using JavaScript to automatically close an element

Hello everyone, I am currently working on implementing a JavaScript code that is commonly used to detect click areas for closing an element such as a side navigation or a floating division when the user clicks outside of the element. The functionality wo ...

What is the best way to reach the top of an element that has padding?

An interactive jump menu allows users to navigate to different sections on the page by clicking on menu items. However, there seems to be an issue where the page scrolls to the top of the <h3> element instead of the entire element including any paddi ...

Ensure that Bootstrap Carousel images stay centered as the browser window is resized

I have encountered several questions similar to mine, but none of the solutions provided worked for me. My objective is to create a Bootstrap carousel that will keep the image centered when the window size is adjusted, while also maintaining specific imag ...

Having trouble with Bootstrap's pull-right feature not behaving as it should?

Hi there! I've encountered a little issue with using Bootstrap3. I was attempting to create a small filter that reloads the content below it. My goal was to align this filter (consisting of two inputs and a button) perfectly with the content underneat ...

Revising the hierarchy between !important in CSS and jQuery

(Attempting to modify CSS properties within a Chrome extension content script) Is it feasible to override CSS properties that have been marked as !important on the webpage? For example, if I aim to eliminate an important border: $(".someclass").css(&apo ...

The :before element is not displaying when using jQuery's slideToggle() method

When using only CSS, the .nav-main:before{} element is displayed, but with jQuery, it does not toggle that element and always keeps it hidden. Disclaimer: This issue has been observed on mobile devices (tested in Android Chrome). jQuery $('.menu-to ...

Preventing data loss upon submitting a form (PHP)

Currently, I have an HTML file that allows users to select appointment times for specific days of the week. Once they click submit, a PHP file generates and displays a calendar with the chosen times as checkboxes. My goal is to enable users to input their ...

Does clip-path jump at the top because of JavaScript offset?

When hovering on the box, a circle appears and moves based on JavaScript offset values. However, when the circle reaches the second paragraph element, it jumps to the top. I am looking to understand why this is happening and how to ensure smooth movement o ...

Tips for obtaining the necessary value input upon clicking?

Let's consider a scenario where you have the following JavaScript code: function openAlbum() { $("#tracks").html('Some text...'); var uid = $("#uid").val(); $.ajax({ type: "POST", url: "s.php", ...

Converting a JSON array into a single concatenated string

Currently, I am parsing a JSON file that contains data about multiple users. { "SRM": [{ "title": "Firstname Surname", "image": "firstname", "subtitle":"the subtitle" }, { "title": "Firstname Surname", "image": "firstname", "s ...

Adjust item size and move them into the panel

I am looking to create a panel that arranges items in a specific layout shown here: https://i.stack.imgur.com/qw3lS.png Below is the code I have attempted so far: import React, { useCallback, useEffect } from "react"; import { Box, Grid, Tab, T ...

Using CSS sprites for background images

I have been focusing on improving the performance of my web application, and one approach I took was to utilize CSS sprites. All of my images are now consolidated in a single .png file named images.png. While CSS sprites have worked effectively for displa ...

Leveraging webpack alongside url-loader for embedding images within React applications

When styling with an inline style and passing in the URL of an image file, I typically do it as shown below: let url = `url(${this.state.logo})` var cardStyle = { backgroundImage: url, backgroundSize: '200px 50px' ...

Tips for utilizing the font-family style to span multiple columns

I recently discovered that you can set the background color across table columns using the <colgroup> tag. table { border: 2px solid; border-collapse: collapse; } td { border: 2px solid; } <ta ...

Is it within the realm of possibility for a user to access and peruse the contents of this

I have taken measures to protect the contents of "x.txt" on my server from being viewed by any users. Here is what I have done so far: Blocked direct access to the file using .htaccess Implemented an ajax request on one of my pages to retrieve and s ...