How to make a responsive table header? - html

I have this table, but elements at last column don't fit the entire table when display is in a big size. What am I doing wrong?
Large
Medium
Small
I have
<div class="panel-body">
<table class="table mb30" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th width="5%">#</th>
<th width="30%">Site Name</th>
<th width="40%">Site URL</th>
<th width="25%">Action</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Name</td>
<td>URL</td>
<td>Action</td>
</tr>
</tbody>
</table>
</div>
I've put together a fiddle

You need <meta name="viewport" content="width=device-width"> in the header.
then use:
#media screen and (max-width : 725px ){
#id {}
.class {}
}
change max-width to what you want.

Don't give the action column a percentage width. If you give table headers a percentage that adds up to the total and leave out a column, all the % based columns will take up as much room as they have available.
https://jsfiddle.net/pkjbpsr7/1/
For example:
<tr>
<th width="5%">#</th> <!-- This column and the next two will fill the space available. -->
<th width="30%">Site Name</th>
<th width="65%">Site URL</th>
<th>Action</th> <!-- This one will just chill off to the side and only take up as much room as it has width -->
</tr>
If you need it to take up a certain amount of space, you can always give it a min-width:
https://jsfiddle.net/pkjbpsr7/4/
<th width="5%">#</th>
<th width="30%">Site Name</th>
<th width="65%">Site URL</th>
<th id="min-width">Action</th> <!-- This will take up 100px per it's min-width -->

Related

Bootstrap 5 table equal column width

Is there any way to force all the columns inside a bootstrap table to have the same width ? By default it seems to give a larger size to elements that have more content but I'd like all of them to share the same width. Here's my code:
<table class="table table-hover">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">All</th>
<th scope="col">Selected</th>
<th scope="col">1</th>
<th scope="col">12.5</th>
<th scope="col">301</th>
<th scope="col">405.88</th>
<th scope="col">77</th>
</tr>
</thead>
<tbody id="table-body">
<tr>
<th scope="row">123456</th>
<td class="table-danger"></td>
<td class="table-success"></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
The "selected" column ends up being much larger than the rest since it's more text, even though there's enough space to keep all columns using the same width.
I would like to avoid using the "col-x" classes if possible since my columns are generated dynamically and i can easily end up with more than 12 columns.
Add a class to your "td" and specify their size in css.
Define your table size may also be necessary.
table {
table-layout: fixed;
width: your_size px;
}
No extra CSS is required. You could put all of the Columns inside a single Row and then use the row-cols class. What this does is, instead of dictating a class on each Column for its width, you dictate the width of all Columns using a class in the parent Row.
The code below says whatever happens only put 2 Columns on each Row but you can change 2 to anything between 1 and 12.
<div class="row row-cols-2">
<div class="col">
<!-- Content 1 -->
</div>
<div class="col">
<!-- Content 2 -->
</div>
<div class="col">
<!-- Content 3 -->
</div>
<div class="col">
<!-- Content 4 -->
</div>
<!-- Add as many columns as you want -->
</div>
Also, somebody may correct me here but I am fairly certain that adding the row or col class to your table elements is going to make a real struggle when it comes to layout.
Setting table-layout to fixed should automatically give you equal width columns in Bootstrap 5.
your.html
<table class="table table-hover custom-class"> ... </table>
your-custom.css
.custom-class {
table-layout: fixed;
}
You can do it in your table header without any extra css or anything using the col-<n> class. Just adjust accordingly to your needs.
<tr>
<th scope="col" class="col-1">#</th>
<th scope="col" class="col-3">All</th>
<th scope="col" class="col-3">Selected</th>
<th scope="col" class="col-1">1</th>
<th scope="col" class="col-1">12.5</th>
<th scope="col" class="col-1">301</th>
<th scope="col" class="col-1">405.88</th>
<th scope="col" class="col-1">77</th>
</tr>

How Do You Make Bootstrap Responsive Tables Wider

I'm using bootstrap 5 to create responsive tables which works fine but when one column has a lot of text all other columns get squished making it look weird. I would like to set it so that the columns with smaller amounts of texts and the table headers to not get cut off. I also have the table set to responsive which in bootstrap's css sets overflow-x: auto.
Picture of what the table looks like on mobile
Second Picture of mobile
Picture of what it looks like on pc
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
</head>
<div class="container">
<div class="table-responsive">
<table class="table table-sm table-bordered table-hover">
<thead class="table-warning">
<tr>
<th scope="col">Ticket ID</th>
<th scope="col">Asset Type Or User</th>
<th scope="col">Asset Tag</th>
<th scope="col">User ID</th>
<th scope="col">Name</th>
<th scope="col">Location</th>
<th scope="col">Description</th>
<th scope="col">Date Added</th>
<th scope="col">Priority</th>
<th scope="col">Mark As Done</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">4</th>
<td>computer</td>
<td>C-Place-Holder</td>
<td>NA</td>
<td>Tom Blow</td>
<td>In Health Building, Bottom Floor</td>
<td>User would like to print color, has missing files, and monitor is flickering.</td>
<td>2021-02-24</td>
<td>3</td>
<td>
<button class="btn btn-primary" id="4" onclick="markDone(4)">Done</button>
</td>
</tr>
</tbody>
</table>
</div>
</div>
To make it 100% you have to add this command to your CSS
.table-responsive { display: table; } but this won't work for smaller screens. You have to use media queries for that.
Hope my answer helps you.

table in html bootstrap not fitting in to screen

I have a table in html bootstrap, the table looks fit when the page loads, but after the page loads fully the table becomes scrollable
My code is:
<div class="table-responsive">
<table class="table table-striped table-borderd" id="orderTable">
<thead>
<tr>
<th>S.No</th>
<th>Order ID</th>
<th>Ordered By</th>
<th>Branch name</th>
<th>Order Date </th>
<th>Order Time </th>
<th>Status</th>
<th>Invoice Status</th>
<th>Actions</th>
</tr>
</thead>
<tfoot>
<tr class="sn">
<th>S.No</th>
<th>Order ID</th>
<th>Ordered By</th>
<th>Branch name</th>
<th>Order Date </th>
<th>Order Time </th>
<th>Status</th>
<th>Invoice Status</th>
<th>Actions</th>
</tr>
</tfoot>
<tbody>
</tbody>
</table>
</div>
Can anyone please tell me how to make it fit to the screen and avoid the scrolling. Thanks in advance
As it turns out, Main issue in this case is the width of the <input/> elements which was making all our columns follow the same width.
I have solved it with the following CSS for Desktop Screen. For smaller screens, it is best to have the scroll for us to not loose any of our data.
#media (min-width:1320px{
tfoot th input {
width: 90px !important;
}
}
Later you can adjust this for even larger screen break point as per your requirement.

Bootstrap table-responsive on tablet

I am using bootstrap's table-responsive to create a simple responsive table for my layout. However this class only works on mobile. Is there a way I can make it work on tablet as well?
<div class="histlog col-md-12">
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th class="hist1">One</th>
<th class="hist2">Two</th>
<th class="hist3">Three</th>
<th class="hist4">Four</th>
<th class="hist5">Five</th>
<th class="hist6">Siz</th>
<th class="hist7">Seven</th>
<th> </th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
</tr>
</tbody>
</table>
</div>
Here is the code. it is just a simple table layout.
And this is how it looks on mobile, my cells got smaller. Even when i adjusted the widths on my css, it wont respond to it.
You may want to consider using the !important rule in your CSS for your widths if you want to force the table to maintain the columns displayed as you anticipate.
Or you can use white-space: nowrap; like the example below to force the columns to not insert break lines that otherwise would happen first before your table starts behaving in a responsive manner. In other words, you are forcing the table to become responsive before trying to move text to a second line.
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<style>
td {
white-space: nowrap;
}
</style>
<div class="histlog col-md-12">
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th class="hist1">One</th>
<th class="hist2">Two</th>
<th class="hist3">Three</th>
<th class="hist4">Four</th>
<th class="hist5">Five</th>
<th class="hist6">Siz</th>
<th class="hist7">Seven</th>
<th> </th>
</tr>
</thead>
<tbody>
<tr>
<td>1asdfasdf</td>
<td>2asdfasdfasdf</td>
<td>3asdfasdfasdf</td>
<td>4asdfasd</td>
<td>5asdfasddf asdf as</td>
<td>6wer wer we </td>
<td>7asdf asdfwe </td>
<td>8sdfds f sd</td>
</tr>
</tbody>
</table>
</div>

Adding a <th> element within a <th> element

I have an html table with one of the headers spanning over 2 columns. How can I add sub-headers to each of the 2 columns ?
For e.g. in the attached image, I want the 'Contact' column to have sub-headers 'Phone' and 'Address' for the respective columns.
The same way you would if you were drawing out the table on paper:
<table>
<thead>
<tr>
<th rowspan="2">Name</th>
<th rowspan="2">Email</th>
<th colspan="2">Contact</th>
</tr>
<tr>
<th>Phone</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<!-- your data goes here -->
</tbody>
</table>
You need to have two separate header rows:
<tr>
<th rowspan="2">Name</th>
<th rowspan="2">Email</th>
<th colspan="2">Contact</th>
</tr>
<tr>
<th>Number</th>
<th>Address</th>
</tr>
Add another row and put sub headers in <td /> tags. Maybe give the row a class and style the td text? That way they won't look identical to the real headers, that might cause confusion.
<table>
<tr>
<th>Title 1</th><th>Title 2</th><th colspan="2">Title 3</th>
</tr>
<tr>
<td>content</td><td>content</td><th>subtitle 1</th><th>subtitle 2</th>
</tr>
<tr>
<td>content</td><td>content</td><td>content</td><td>content</td>
</tr>
</table>