Adjusting the header row to match the width of the data row in an HTML table

I am facing an issue with the layout of my HTML table. I have set space between each cell using margin, but this is causing a problem with column width. Instead of having col-12 for a row, it has become col-11 due to the spacing. This has resulted in the header row spanning the full width while the data rows are not aligned properly. How can I adjust the header row to match the width of the data rows?

https://i.sstatic.net/fJbKk.jpg

.event-col {
  border-radius: 10px;
  box-shadow: 3px 3px 3px rgba(0, 0, 0, 0.3);
  background-color: #fff;
  padding: 10px;
  margin: 2px;
  display: inline-block;
}

.event-col-header {
  padding: 10px;
  margin: 2px;
  display: inline-block;
  border: 0px !important;
  font-weight: bold;
  font-family: IRANSans;
}

.event-row {
  border-radius: 10px;
  box-shadow: 3px 3px 3px rgba(0, 0, 0, 0.3);
  background-color: #c1d2fe;
  margin: 10px;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="03616c6c77707771627343372d352d32">[email protected]</a>/dist/css/bootstrap.min.css" integrity="sha384-zCbKRCUGaJDkqS1kPbPd7TveP5iyJE0EjAuZQTgFLD2ylzuqKfdKlfG/eSrtxUkn" crossorigin="anonymous">

<div class="row">
  <div class="col-12">
    <table id="example" class="table dt-responsive nowrap showborder ">
      <tr class="event-row mb-1">
        <th class="col-1 event-col-header">No</th>
        <th class="col-2 event-col-header">Name </th>
        <th class="col-1 event-col-header">Date</th>
        <th class="col-2 event-col-header">Hour</th>
        <th class="col-5 event-col-header">Address</th>
      </tr>

      <tr>
        <td class="col-1 event-col">@counter</td>
        <td class="col-2 event-col">@item.Name</td>
        <td class="col-1 event-col">@item.EventDate</td>
        <td class="col-2 event-col"> @item.StartTime </td>
        <td class="col-5 event-col">@item.Address</td>
      </tr>
    </table>
  </div>
</div>

Answer №1

Here are the changes I made to your code:

.event-col {
    border-radius: 10px;
    box-shadow: 3px 3px 3px rgba(0,0,0,0.3);
    background-color: #fff;
    /*padding: 10px;*/
    /*margin: 2px;*/
    display: inline-block;
    width: 100%;
}
.event-col-header {
    /*padding: 10px;*/
    /*margin: 2px;*/
    display: inline-block;
    /*border: 0 !important;*/
    font-weight: bold;
    font-family: IRANSans;
}
.event-row {
    border-radius: 10px;
    box-shadow: 3px 3px 3px rgba(0,0,0,0.3);
    background-color: #c1d2fe;
    /*margin: 10px;*/
}
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8>
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <!-- bootstrap 4.6 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap/dist/css/bootstrap.min.css" integrity="sha384-zCbKRCUGaJDkqS1kPbPd7TveP5iyJE0EjAuZQTgFLD2ylzuqKfdKlfG/eSrtxUkn" crossorigin="anonymous">

    <!-- my style-sheet -->
    <link rel="stylesheet" href="style.css">
    <title>Document</title>
</head>
<body dir="rtl">

<div class="container-fluid">
<div class="row text-right">
    <div class="col-11 mx-auto">
        <table id="example" class="table table-borderless table-responsive" >
            <tr class="event-row mb-1">
                <th class="col-1">
                    <span class="event-col-header">No</span>
                </th>
                <th class="col-2">
                    <span class="event-col-header">Name</span>
                </th>
                <th class="col-1">
                    <span class="event-col-header">Date</span>
                </th>
                <th class="col-2">
                    <span class="event-col-header">Hour</span>
                </th>
                <th class="col-5">
                    <span class="event-col-header">Address</span>
                </th>
            </tr>
             ... // additional HTML rows
        </table>
    </div>
</div>
</div>


<script src="https://cdn.jsdelivr.net/npm/jquery/dist/jquery.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap/dist/js/bootstrap.bundle.min.js" integrity="sha384-fQybjgWLrvvRgtW6bFlB7jaZrFsaBXjsOMm/tB9LTS58ONXgqbR9W8oWht/amnpF" crossorigin="anonymous"></script>

</body>
</html>

These are some important tips based on my experience with this code:

1. Avoid placing text directly within col-1, col-2, etc. Consider them as containers and place contents inside other elements like <span>.

2. Instead of manually setting margins and padding, utilize Bootstrap's margin and padding utilities.

3. When using row and col-* classes, container-fluid or container classes often provide better results. Therefore, I added a .container-fluid encompassing the entire table.

4. To ensure responsiveness in tables, consider utilizing Bootstrap's .table-responsive class for optimal performance.

Answer №2

Upon reviewing the bootstrap table documentation, it was noted that there were no examples utilizing the col-* classes within tables. As a result, an alternative code snippet was crafted that uses the HTML <colgroup> tag instead of the mentioned classes to achieve a similar outcome:

.event-col {
    border-radius: 10px;
    box-shadow: 3px 3px 3px rgba(0,0,0,0.3);
    background-color: #fff;
    display: inline-block;
    width: 100%;
}
.event-col-header {
    display: inline-block;
    font-weight: bold;
    font-family: IRANSans;
}
.event-row {
    border-radius: 10px;
    box-shadow: 3px 3px 3px rgba(0,0,0,0.3);
    background-color: #c1d2fe;
}
<!doctype html>
<html lang="en>
<head>
  <meta charset="UTF-8>
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge>
  <!-- bootstrap 4.6 -->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-zCbKRCUGaJDkqS1kPbPd7TveP5iyJE0EjAuZQTgFLD2ylzuqKfdKlfG/eSrtxUkn" crossorigin="anonymous>

  <!-- my style-sheet -->
  <link rel="stylesheet" href="style.css>
  <title>Document>
</head>
<body dir="rtl>

<div class="container-fluid>
<div class="row text-right>
  <div class="col-11 mx-auto>
    <table id="example" class="table table-borderless table-responsive>
      <colgroup>
        <col style="width: 9%">
        <col style="width: 18%">
        <col style="width: 9%">
        <col style="width: 18%">
        <col style="width: 46%">
      </colgroup>
      <tr class="event-row mb-1>
        <th>
          <span class="event-col-header>No</span>
        </th>
        <th>
          <span class="event-col-header>Name</span>
        </th>
        <th>
          <span class="event-col-header>Date</span>
        </th>
        <th>
          <span class="event-col-header>Hour</span>
        </th>
        <th>
          <span class="event-col-header>Address</span>
        </th>
      </tr>
      <tr>
        <td>
          <span class="event-col p-3>@counter</span>
        </td>
        <td>
          <span class="event-col p-3>@item.Name</span>
        </td>
        <td>
          <span class="event-col p-3>@item.EventDate</span>
        </td>
        <td>
          <span class="event-col p-3>@item.StartTime</span>
        </td>
        <td>
          <span class="event-col p-3>@item.Address</span>
        </td>
      </tr>
      <tr>
        <td>
          <span class="event-col p-3>@counter</span>
        </td>
        <td>
          <span class="event-col p-3>@item.Name</span>
        </td>
        <td>
          <span class="event-col p-3>@item.EventDate</span>
        </td>
        <td>
          <span class="event-col p-3>@item.StartTime</span>
        </td>
        <td>
          <span class="event-col p-3>@item.Address</span>
        </td>
      </tr>
      <tr>
        <td>
          <span class="event-col p-3>@counter</span>
        </td>
        <td>
          <span class="event-col p-3>@item.Name</span>
        </td>
        <td>
          <span class="event-col p-3>@item.EventDate</span>
        </td>
        <td>
          <span class="event-col p-3>@item.StartTime</span>
        </td>
        <td>
          <span class="event-col p-3>@item.Address</span>
        </td>
      </tr>

    </table>
  </div>
</div>
</div>

<!-- bootstrap scripts -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-fQybjgWLrvvRgtW6bFlB7jaZrFsaBXjsOMm/tB9LTS58ONXgqbR9W8oWht/amnpF" crossorigin="anonymous></script>

</body>
</html>

Answer №3

Avoid mixing Bootstrap's grid layout with tables for a cleaner design. If you simply need the bubble effect, insert divs inside your table cells and style them accordingly.

If you wish to customize column sizes, go ahead and do so.

.event-row {
  border-radius: 10px;
  box-shadow: 3px 3px 3px rgba(0, 0, 0, 0.3);
  background-color: #c1d2fe;
}

.event-col {
  border-radius: 10px;
  box-shadow: 3px 3px 3px rgba(0, 0, 0, 0.3);
  background-color: #fff;
  padding: 10px;
  display: inline-block;
}

.event-col-header {
  padding: 10px;
  display: inline-block;
  border: 0px !important;
  font-weight: bold;
  font-family: IRANSans;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5b3934342f282f293a2b1b6f756d756a">[email protected]</a>/dist/css/bootstrap.min.css" integrity="sha384-zCbKRCUGaJDkqS1kPbPd7TveP5iyJE0EjAuZQTgFLD2ylzuqKfdKlfG/eSrtxUkn" crossorigin="anonymous">

<div class="row">
  <div class="col-12">
    <table id="example" class="table dt-responsive nowrap showborder">
      <tr class="event-row mb-1">
        <th>
          <div class="event-col-header">No</div>
        </th>
        <th>
          <div class="event-col-header">Name</div>
        </th>
        <th>
          <div class="event-col-header">Date</div>
        </th>
        <th>
          <div class="event-col-header">Hour</div>
        </th>
        <th>
          <div class="event-col-header">Address</div>
        </th>
      </tr<

      <tr>
        <td>
          <div class="event-col>@counter</div>
        </td>
        <td>
          <div class="event-col">@item.Name</div>
        </td>
        <td>
          <div class="event-col">@item.EventDate</div>
        </td>
        <td>
          <div class="event-col"></div> @item.StartTime"> @item.StartTime</div ></td>
        <td>
          <div class="event-col">@item.Address</div>
        </td>
      </tr>
    </table>
  </div>
</div>

Answer №4

Even though they are the same width, using inline-block does not make them utilize the full space of the parent element. It seems like you may not be correctly utilizing Bootstrap's columns in this case. Remember, tables do not require the use of columns and rows.

The Bootstrap grid system may not be the most suitable choice for tables. I suggest trying to design your table layout without relying on the Bootstrap grid.

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

The navigation bar text overflows when the sidebar is collapsed into icons

My sidebar contains icons and a menu name. When the screen size is smaller than a certain amount, the sidebar collapses to show only the icon and hides the menu name. Here is the structure of the navbar: <nav id="sidebar" [ngClass]="{active: barActive} ...

What is causing these dynamic carousels to malfunction in Chrome?

After using Agile Carousel successfully for a while, I am now experiencing issues with it not working properly in Safari and Chrome, although it functions fine on Firefox and Safari for iPad. On this specific page, the carousel stops at the second image, ...

Uniformly allocate space to all table cells

I am facing an issue with the CSS property display: table-cell; and the space between the cells. I want all cells to have dynamically the same width. You can view the code on JSFiddle. As you can see, the cell titled "Ausstattung & Skizze" is wider t ...

What is the best way to modify a variable in a nested element using ng-click in the parent element?

Review this code snippet: <section class="page-section about-us" scroll-bookmark="about-us" ng-click="activeSection=true" ng-init="activeSection=false"> <div class="page-content sub-content active-section">{{activeSection}} < ...

What is the procedure for retrieving values from a table?

<tbody> <tr> <td><span style="">Price</span></td> <td><span style="">:</span></td> <td><span style="">660</span></td> </tr> <tr> <td><s ...

What is the preferred convention for defining AngularJS directives as "attributes" versus "elements"?

When creating Angular directives, I've noticed that some directives could also be defined as either an HTML element or an attribute. I'm curious to know what the community convention is for choosing between the two, and the reasons behind it. Fo ...

Looking for an image that spans the entire height of the container

I need help with positioning an img inside a div container that has a background color. I want the image to overlay on top of the div, extending beyond its height without causing any scroll bars. Here is a fiddle related to this issue: http://jsfiddle.net ...

Utilizing JQuery for adjusting the CSS top property

I needed to overlay text on an image, so I used CSS for this purpose. However, as I incorporated Bootstrap and hesitated to include absolute positioning in the CSS file, I faced issues with centering the text when resizing the image. To tackle this problem ...

What is the best way to implement auto-width columns with flexbox in Bootstrap 4?

My goal was to transition from using bootstrap 3 to bootstrap 4 so that I could achieve uniform-sized divs in a row by utilizing flexbox. I acquired the precompiled files from https://getbootstrap.com/docs/4.6/getting-started/download/. After downloading ...

What is the best way to eliminate the gap between these two div elements?

There's a gap between two divs in my HTML that I can't seem to remove. Here are the relevant CSS styles: #tab-2-content { display: grid; grid-template-rows: 1fr 2fr; width: 70%; margin: 0 auto; grid-gap: 0; } ... (CSS code continues) ...

Ensure that the input group is entirely visible and does not wrap within a table cell

I am facing an issue with an input group placed in a table cell where the button wraps below a certain size. I want to prevent this behavior specifically for this table cell and the rest of the column. The relevant html generated with added javascript elem ...

Create a stylish frame for designated rows within a table

Col1 Col2 Col3 Col4 Col5 Col6 Row11 Row12 Row13 Row14 Row15 Row16 Row21 Row22 Row23 Row24 Row25 Row26 Row31 Row32 Row33 Row34 Row35 Row36 Looking for a way to add a border around rows with matching values in the first column, or around a specif ...

Tips on creating a React Vite Typescript website that utilizes the entire height and ensures a minimum width

I'm currently working on building a portfolio webpage with React, Vite, and Typescript, but I'm facing an issue where the content is not taking up the full height of the page. I have multiple pages with different lengths of content that I want to ...

Tips for entering multiple values in an input field

I am attempting to implement a feature where multiple names can be added from an autocomplete drop-down menu to an input field, similar to the example shown below: https://i.sstatic.net/D4hGA.jpg Here is what I aim to create: Upon selecting an item from ...

Leveraging BeautifulSoup for retrieving targeted dl and dd item listings

Today marks my first time posting. I am currently utilizing BeautifulSoup 4 and Python 2.7 (PyCharm) to work on a webpage that contains various elements. Specifically, I am aiming to extract certain elements where the tags are either 'Salary:' or ...

When should I use event listeners with Bootstrap 4 and jQuery in my nav-link?

In my Bootstrap 4 project, I have successfully implemented a navigation bar that is fully functional. However, when trying to replicate the same bar in another section of my code, only one of the two items seem to be working properly! After thorough invest ...

css allow inline-block elements to fill the entire width of their container

Well, here's the thing - it's a bit complicated. I'm dealing with a navigation list where the list items are set to inline-block. The number of items in the list can change, so it's not fixed. My goal is to make the list items stretch ...

Tips for implementing multiple CSS image filters with jQuery

Can you show me how to use jQuery to apply the CSS code below? #centeredImage { -webkit-filter: grayscale(20%) invert(0) blur(1px); } I attempted this method already, but it didn't have any effect. $('#centeredImage').css('-webki ...

Save data to local storage using the 'onclick' event in jQuery

I'm facing an issue with local storage. My portfolio includes my work, contacts, etc., and I lack buttons that can toggle between Swedish and English languages when clicked. How can I use local storage to remember the language choice even after refres ...

Firefox's keyup event

Is it possible to detect a keypress using the jQuery keyup function, as I am facing an issue where it works in Chrome, Edge, IE, and Opera but not in Firefox. $(".textfield").contents().keyup(function(evnt) { document.getElementById("btn_save").style. ...