Responsive table in bootstrap - html

My question is quite simple, how do I make this table responsive using twitter bootstrap 4? Below is a JS Fiddle Link.
<div class="some-table-section">
<div class="container">
<table class="table some-table">
<thead>
<tr>
<th scope="col"></th>
<th scope="col">Plan 1</th>
<th scope="col">Plan 2</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Option 1</th>
<td>$1</td>
<td>$2</td>
</tr>
<tr>
<th scope="row">Option 2</th>
<td>1</td>
<td>2</td>
</tr>
<tr>
<th scope="row">Option 3</th>
<td>Unlimited</td>
<td>Unlimited</td>
</tr>
<tr>
<th scope="row"></th>
<td><a href="#" class="btn btn-primary">Select this plan</td>
<td><a href="#" class="btn btn-primary">Select this plan</td>
</tr>
</tbody>
</table>
</div>
</div>
What I would like to have is a responsive version of this table such that plan 2 is below plan 1 in the responsive version. PS. You can find the current CSS in the JS Fiddle Link.

You should check Bootstrap Docs: https://getbootstrap.com/docs/4.3/content/tables/#responsive-tables
Simply wrap your table with .table-responsive
Basic example:
<div class="table-responsive">
<table class="table">
...
</table>
</div>
You can have breakpoint specific responsive table using class table-responsive{-sm|-md|-lg|-xl}

Related

React table optimization problem(HTML,CSS)

I am making a simple web page using React and I need a table on this page. The contents of the table are OK, but the table is not properly aligned. I do not see any problem in the code. How Can I Solve This?
import React from 'react'
const StuffList = () => {
return (
<div className='list-group'>
<table className="table table-hover table-danger"></table>
<thead>
<tr className="bg-primary">
<th scope="col">T.C</th>
<th scope="col">Name</th>
<th scope="col">Surname</th>
<th scope="col">City</th>
<th scope="col">Jobe</th>
<th scope="col">Salary</th>
<th scope="col">Hobbies</th>
<th scope="col">U.Graduate</th>
<th scope="col">Graduate</th>
<th scope="col">P.Graduate</th>
<th scope="col">Blood Group</th>
<th scope="col">Edit</th>
<th scope="col">Delete</th>
<th scope="col">Details</th>
</tr>
</thead>
<tbody>
<tr>
<td>2826572</td>
<td>Mike</td>
<td>White</td>
<td>Kansas</td>
<td>Student</td>
<td>41</td>
<td>Parachute</td>
<td>no</td>
<td>no</td>
<td>no</td>
<td>ARH-</td>
<td><button className='btn btn-warning'>Update</button></td>
<td><button className='btn btn-danger'>Delete</button></td>
<td><button className='btn btn-info'>Details</button></td>
</tr>
</tbody>
</div>
)
}
export default StuffList
you can give the <tr style={{width:"100%",padding:"8px"}}> and for tds you can add
<td style={{padding:"8px}}>
if it didn't worked give the table and its parent div 100% width

Bootstrap 4 text "go out" from the table on phone resolution

I have a problem with bootstrap table, on phone resolution, my table in phone resolution will appear like this:
Here is my code:
<table class="table table-hover table-dark mt-3 table-bordered">
<thead>
<tr>
<th scope="col" >#</th>
<th scope="col" >Value</th>
<th scope="col" >Action</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Username</th>
<td><?=$Username?></td>
<td></td>
</tr>
<tr>
<th scope="row">Email</th>
<td><?=$Email?></td>
<td></td>
</tr>
</tbody>
</table>
If you look the text of the photo you will see that the text "go out" from the screen, how can I fix it?
Add table-responsive class to the table. You can also make is specific to view breakpoints.
More details here

Setting Table height per page when printing

I have a page with a footer that repeats on every page. The main content of the page is a long table of multiple pages.
I need the number of rows per page to be fixed so that it does not overlap my page footer when printing the page.
I have tried page-break-inside: avoid; on the footer but it does not work.
I am using bootstrap of if that helps.
<html>
<link rel="stylesheet" href="/css/bootstrap.min.css">
<link rel="stylesheet" href="/assets/css/style.css">
<body style="padding-top:20px">
<div class="container">
<div class="row">
Header Section
</div>
<br>
<table class="table table-striped table-bordered">
<thead>
<tr style="background-color:yellow">
<th scope="col">S.No</th>
<th scope="col">Item Description</th>
<th scope="col">Qty</th>
<th scope="col">Unit Price</th>
<th scope="col">Total Price</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>CAT 6 Outlet - Optronics CAT6 Single Outlet( Connector + Module + Faceplate)</td>
<td>40</td>
<td>30</td>
<td>5000</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Patch Panel - Optronics 24-Port Patch Panel Loaded</td>
<td>10</td>
<td>20</td>
<td>2000</td>
</tr>
<tr>
<th scope="row">3</th>
<td>Brush Panel - Optronics Cable Manager Brush</td>
<td>500</td>
<td>10</td>
<td>20000</td>
</tr>
<tr>
<td></td>
<td>Grand Total (AED)</td>
<td></td>
<td></td>
<td>501000.00</td>
</tr>
</tfoot>
</table>
{{!-- Page Footer --}}
<div class="fixed-bottom">
<hr>
**** FOOTER SECTION ****
</div>
</div>
</body>
<script src="/js/jquery.min.js"></script>
<script src="/js/bootstrap.min.js"></script>
</html>

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>

Writing accurate <th> for accessible <table>

I am following the UN's accessibility guidelines to write accessible table.
I have this table. I made what -I think- should be a <th> bold.
This is the HTML:
<table width="100%">
<thead>
<tr>
<th>Year</th>
<th>1991</th>
<th>1995</th>
<th>2000</th>
<th>2002</th>
<th>2007</th>
</tr>
</htead>
<tbody>
<tr>
<th>Indicator 1</th>
<td>3.0</td>
<td>11.0</td>
<td>7.8</td>
<td>4.0</td>
<td>4.7</td>
</tr>
<tr>
<th>Indicator 2</th>
<td>9.0</td>
<td>23.4</td>
<td>19.5</td>
<td>9.4</td>
<td>9.1</td>
</tr>
<tr>
<th>Indicator 3</th>
<td>18.7</td>
<td>32.0</td>
<td>30.0</td>
<td>20.1</td>
<td>21.8</td>
</tr>
</tbody>
</table>
But I am not sure it is accurate. I think this makes more sense:
<table>
<tbody>
<tr>
<th id="year">Year</th>
<td headers="year" id="year-1991">1991</td>
<td headers="year" id="year-1995">1995</td>
<td headers="year" id="year-2000">2000</td>
<td headers="year" id="year-2002">2002</td>
<td headers="year" id="year-2007">2007</td>
</tr>
<tr>
<th id="indicator-1">Indicator 1</th>
<td headers="indicator-1 year-1991">3.0</td>
<td headers="indicator-1 year-1995">11.0</td>
<td headers="indicator-1 year-2000">7.8</td>
<td headers="indicator-1 year-2002">4.0</td>
<td headers="indicator-1 year-2007">4.7</td>
</tr>
<tr>
<th id="indicator-2">Indicator 2</th>
<td headers="indicator-2 year-1991">9.0</td>
<td headers="indicator-2 year-1995">23.4</td>
<td headers="indicator-2 year-2000">19.5</td>
<td headers="indicator-2 year-2002">9.4</td>
<td headers="indicator-2 year-2007">9.1</td>
</tr>
<tr>
<th id="indicator-3">Indicator 3</th>
<td headers="indicator-3 year-1991">18.7</td>
<td headers="indicator-3 year-1995">32.0</td>
<td headers="indicator-3 year-2000">30.0</td>
<td headers="indicator-3 year-2002">20.1</td>
<td headers="indicator-3 year-2007">21.8</td>
</tr>
</tbody>
</table>
What do you think? Does anyone have experience with tables and accessibility? Please provide references if possible. Thanks.
Note: I am aware of the summary attribute but I removed it here for simplicity.
For a table with a simple structure like this, your markup (the first, simple version) is sufficient for accessibility. A table with similar structure appears in HTML and XHTML Techniques for WCAG 2.0, item Using table markup to present tabular information, with no extra markup, just th for header cells.
Special techniques may be need for structurally more complicated tables.
However, a data table should normally have a caption element for accessibility and usability. It helps the user get key information about the table as a whole. Instead of a caption, a heading or just text before the table could be used for the purpose, but they do not associate with the table (at the markup level) the same way.
I found that using the scope attribute makes more sense:
<table width="100%">
<thead>
<tr>
<th scope="col">Year</th>
<th scope="col">1991</th>
<th scope="col">1995</th>
<th scope="col">2000</th>
<th scope="col">2002</th>
<th scope="col">2007</th>
</tr>
</htead>
<tbody>
<tr>
<th scope="row">Indicator 1</th>
<td>3.0</td>
<td>11.0</td>
<td>7.8</td>
<td>4.0</td>
<td>4.7</td>
</tr>
<tr>
<th scope="row">Indicator 2</th>
<td>9.0</td>
<td>23.4</td>
<td>19.5</td>
<td>9.4</td>
<td>9.1</td>
</tr>
<tr>
<th scope="row">Indicator 3</th>
<td>18.7</td>
<td>32.0</td>
<td>30.0</td>
<td>20.1</td>
<td>21.8</td>
</tr>
</tbody>
Reference: Use the th element to specify row and column headers in data tables | 456 Berea St