I have a table containing 3 columns of text and a fourth column with a button in it.
Normally the text in the text cells do not align vertically with the text in the button because the button gets a vertical-align value of 'middle'. So far they only way I've gotten the text in the text cells to align with the button text is to wrap them in a span and add an align-middle class. This seems like a fair amount of html bloat. Is there another way?
Here's an example of a table where some cells align and some don't, through the use of the extra span element.
I'm using bootstrap 5 but I think this applies to previous versions as well.
Edit
I've tried comparing different approaches
table with no vertical alignment classes
vertical alignment specified on the td
vertical alignment specified on the table
vertical alignment specified on a span in the td
#2 and #3 are functionally equivalent, but when some cells wrap and cause the row height to grow, text in cells that do not wrap center vertically within the entire cell.
#4, when some cells wrap and cause the row height to grow, text in cells that do not wrap still appear near the top, and are aligned closer to the alignment of the button text, but not perfectly, and the alignment is best with table-sm and btn-sm are used, but if the btn is different size alignment is thrown off.
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<span>No classes</span>
<table class="table table-sm table-striped table-hover table-borderless table-stacked-md label-20 mt-2" id="drugSearchList">
<thead>
<tr class="my-2">
<th scope="col">Drug Name</th>
<th scope="col">Number</th>
<th scope="col" />
</tr>
</thead>
<tbody>
<tr>
<td>wrapping text here this is a test wrapping text here this is a test</td>
<td>1</td>
<td class="text-end"><a class="btn btn-sm btn-outline-primary" href="#">Edit</a></td>
</tr>
<tr class="border-bottom">
<td>wrapping text here this is a test wrapping text here this is a test</td>
<td>1</td>
<td class="text-end"><a class="btn btn-sm btn-outline-dark disabled">Edit</a></td>
</tr>
</tbody>
</table>
<span>Align-middle on td</span>
<table class="table table-sm table-striped table-hover table-borderless table-stacked-md label-20 mt-2" id="drugSearchList">
<thead>
<tr class="my-2">
<th scope="col">Drug Name</th>
<th scope="col">Number</th>
<th scope="col" />
</tr>
</thead>
<tbody>
<tr>
<td class="align-middle">wrapping text here this is a test wrapping text here this is a test</td>
<td class="align-middle">1</td>
<td class="text-end"><a class="btn btn-sm btn-outline-primary" href="#">Edit</a></td>
</tr>
<tr class="border-bottom">
<td class="align-middle">wrapping text here this is a test wrapping text here this is a test</td>
<td class="align-middle">1</td>
<td class="text-end"><a class="btn btn-sm btn-outline-dark disabled">Edit</a></td>
</tr>
</tbody>
</table>
<span>Align-middle on table</span>
<table class="table table-sm table-striped table-hover table-borderless table-stacked-md label-20 mt-2 align-middle" id="drugSearchList">
<thead>
<tr class="my-2">
<th scope="col">Drug Name</th>
<th scope="col">Number</th>
<th scope="col" />
</tr>
</thead>
<tbody>
<tr>
<td>wrapping text here this is a test wrapping text here this is a test</td>
<td>1</td>
<td class="text-end"><a class="btn btn-sm btn-outline-primary" href="#">Edit</a></td>
</tr>
<tr class="border-bottom">
<td>wrapping text here this is a test wrapping text here this is a test</td>
<td>1</td>
<td class="text-end"><a class="btn btn-sm btn-outline-dark disabled">Edit</a></td>
</tr>
</tbody>
</table>
<span>Align-middle on td span</span>
<table class="table table-sm table-striped table-hover table-borderless table-stacked-md label-20 mt-2" id="drugSearchList">
<thead>
<tr class="my-2">
<th scope="col">Drug Name</th>
<th scope="col">Number</th>
<th scope="col" />
</tr>
</thead>
<tbody>
<tr>
<td><span class="align-middle">wrapping text here this is a test wrapping text here this is a test</span></td>
<td><span class="align-middle">1</span></td>
<td class="text-end"><a class="btn btn-sm btn-outline-primary" href="#">Edit</a></td>
</tr>
<tr class="border-bottom">
<td><span class="align-middle">wrapping text here this is a test wrapping text here this is a test</td>
<td><span class="align-middle">1</span></td>
<td class="text-end" class="align-middle"><a class="btn btn-sm btn-outline-dark disabled">Edit</a></td>
</tr>
</tbody>
</table>
Instead of using a span with .align-middle. Just add the align-middle class directly on the td.
I added a bottom border to the last tr to visualize alignment.
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<table class="table table-sm table-striped table-hover table-borderless table-stacked-md label-20 mt-2" id="drugSearchList">
<thead>
<tr class="my-2">
<th scope="col">Date</th>
<th scope="col">Drug Name</th>
<th scope="col">Number</th>
<th scope="col" />
</tr>
</thead>
<tbody>
<tr>
<td data-col="Date" class="align-middle">12/14/2022</td>
<td data-col="Drug" class="align-middle">hydroxyurea</td>
<td data-col="Number" class="align-middle">1</td>
<td class="text-end" class="align-middle">
<a class="btn btn-sm btn-outline-primary" href="#">Edit</a>
</td>
</tr>
<tr class="border-bottom">
<td data-col="Date" class="align-middle">12/14/2022</td>
<td data-col="Drug" class="align-middle">doxorubicin</td>
<td data-col="Number" class="align-middle">1</td>
<td class="text-end" class="align-middle">
<span class="btn btn-sm btn-outline-dark disabled">Edit</span>
</td>
</tr>
</tbody>
</table>
Related
I have one question how can I align my text inside thead to center horizontally and vertically
<table id="stanari" class="table table-striped table-bordered w-100 animate__animated animate__fadeIn">
<thead>
<tr>
<th>First name and last name of tenants</th>
<th>Cell phone of tenants</th>
<th>Apartment address</th>
<th>Number at the door of the apartment</th>
<th>Paid deposit in euros</th>
<th>Date of tenants registration</th>
<th>Monthly rent in euros</th>
<th>Is the monthly rent paid</th>
<th>Payment date</th>
<th>For month</th>
<th></th>
</tr>
</thead>
<tbody>
</tbody>
</table>
Add the bootstrap class align-middle and text-center to the th elements.
Bootstrap Vertical alignment also Bootstrap Text alignment
th:nth-of-type(1),
th:nth-of-type(4),
th:nth-of-type(5),
th:nth-of-type(8){
min-width: 150px;
}
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/js/bootstrap.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"/>
<table id="stanari" class="table table-striped table-bordered w-100 animate__animated animate__fadeIn">
<thead>
<tr>
<th class="align-middle text-center">First name and last name of tenants</th>
<th class="align-middle text-center">Cell phone of tenants</th>
<th class="align-middle text-center">Apartment address</th>
<th class="align-middle text-center">Number at the door of the apartment</th>
<th class="align-middle text-center">Paid deposit in euros</th>
<th class="align-middle text-center">Date of tenants registration</th>
<th class="align-middle text-center">Monthly rent in euros</th>
<th class="align-middle text-center">Is the monthly rent paid</th>
<th class="align-middle text-center">Payment date</th>
<th class="align-middle text-center">For month</th>
<th class="align-middle text-center"></th>
</tr>
<tr>
<td class="align-middle text-center col-3">John and Jane Doe</td>
<td class="align-middle text-center">555-555-5555</td>
<td class="align-middle text-center">5555 Someroad streer</td>
<td class="align-middle text-center">555</td>
<td class="align-middle text-center">1555 Euro</td>
<td class="align-middle text-center">5-5-21</td>
<td class="align-middle text-center">2555 Euro</td>
<td class="align-middle text-center">Paid in full</td>
<td class="align-middle text-center">5-1-21</td>
<td class="align-middle text-center">May</td>
<td class="align-middle text-center">N/A</td>
</tr>
</thead>
<tbody>
</tbody>
</table>
by default th tags should be aligned center vertically, so you've something overides the default behavior which i recommend you to fix.
however, you can still fix this issue by css like the following:
th {
display:flex,
align-items:center,
}
Why bootstrap responsive table is not working with dir-paginate.
I have created a responsive grid HTML table by using bootstrap which is working with hard-coded data, but when my angular developer colleague used some 'dir-paginate' for retrieving data from the database then my responsive design code is not working when minimizing the browser shorter.
<div class="portlet-body flip-scroll">
<div style="overflow-x:auto;">
<table id="Tbl_SiteVisitViewAll" class="table table-bordered table-striped table-condensed flip-content">
<thead class="flip-content">
<tr>
<th>S. No.</th>
<th ng-click="Sort('Active')">Attended By</th>
<th>Edit</th>
</tr>
</thead>
<tbody dir-paginate="S in SiteVisits |filter : FollowUp |orderBy:'key':AscOrDesc | itemsPerPage:20">
<tr>
<td>{{S.index}}</td>
<td>{{S.UpdatedBy}}</td>
<td>
<a href="#" ">
Edit
</a>
</td>
</tr>
</tbody>
</table>
</div>
</div>
I think you are using the directive at the wrong Position. You want to repeat the <tr>-element instead of the <tbody>-element.
For more Information look at this answer: https://stackoverflow.com/a/34392061/7225524
<div class="portlet-body flip-scroll">
<div style="overflow-x:auto;">
<table id="Tbl_SiteVisitViewAll" class="table table-bordered table-striped table-condensed flip-content">
<thead class="flip-content">
<tr>
<th>S. No.</th>
<th ng-click="Sort('Active')">Attended By</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
<tr dir-paginate="S in SiteVisits |filter : FollowUp |orderBy:'key':AscOrDesc | itemsPerPage:20">
<td>{{S.index}}</td>
<td>{{S.UpdatedBy}}</td>
<td>
<a href="#" ">
Edit
</a>
</td>
</tr>
</tbody>
</table>
</div>
</div>
I have one HTML table which I have kept in bootstrap's accordion, I am taking the input from user, whenever input is too long table is getting outside accordion. How can I set width the fixed, and let it expand vertically.
<table id="recordTable" class="table table-bordered table-striped">
<thead>
<tr>
<th class="col-md-1">Question Number</th>
<th class="col-md-6">Question Text</th>
<th class="col-md-3">upload Document(if any)</th>
<th class="col-md-1">Document uploaded?</th>
<th class="col-md-1">Delete</th>
</tr>
</thead>
<tbody>
<tr>
<td class="ques_id col-md-1">12 </td>
<td class="ques_text col-md-6">asd</td>
<td class="ques_file col-md-3"><input type="file" name="supportingDocument" accept="application/pdf" id="uploadSupportingDoc"></td>
<td class="fileuploadSuccess col-md-1"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></td>
<td class="col-md-1">delete</td>
</tr>
<tr>
<td class="ques_id col-md-1">14 </td>
<td class="ques_text col-md-6">asd</td>
<td class="ques_file col-md-3"><input type="file" name="supportingDocument" accept="application/pdf" id="uploadSupportingDoc"></td>
<td class="fileuploadSuccess col-md-1"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></td>
<td class="col-md-1">delete</td>
</tr><tr><td class="ques_id">15</td><td class="ques_text">sdfdfgdfgaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</td><td class="ques_file"><input type="file" name="supportingDocument" accept="application/pdf" id="uploadSupportingDoc"></td><td class="fileuploadSuccess"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></td><td>delete</td></tr>
</tbody>
</table>
Here is screenshot of the image.
jsfiddle link: https://jsfiddle.net/govi20/obnpjt7h/
add this property word-break: break-all; to td tag
another way add class to table outer element table-responsive
<div class="table-responsive">
<table class="table table-bordered table-striped">
...
</table>
</div>
https://jsfiddle.net/obnpjt7h/1/
I am trying to prepare a simple mark sheet [as an example to demonstrate my requirement]. Am planning to have a collapsible div which will render all the mark details once it is expanded.
<div class="well">
<table class="table table-bordered"
style="background:#fff; margin-bottom:10px;">
<tbody>
<tr class="inverse">
<th> ID</th>
<th colspan="2"> Name</th>
</tr>
<tr>
<td>1</td>
<td>ABC</td>
<td>
<a class="btn btn-primary" data-toggle="collapse" href="#ID_1"
aria-expanded="false" >
Hide/Unhide
</a>
</td>
</tr>
<tr>
<td colspan="3">
<div class="collapse" id="ID_1">
<div class="well">
<table class="table table-hover table-bordered" style="background:#fff; margin-bottom:10px;">
<thead>
<tr class="inverse">
<th>Subject</th>
<th>Mark</th>
</tr>
</thead>
<tbody>
<tr>
<td>Physics </td>
<td>100</td>
</tr>
</tbody>
</table>
</div>
</div>
</td>
</tr>
<tr class="inverse">
<th> ID</th>
<th colspan="2"> Name</th>
</tr>
<tr>
<td>2</td>
<td>PQR</td>
<td>
<a class="btn btn-primary" data-toggle="collapse" href="#ID_2"
aria-expanded="false" >
Hide/Unhide
</a>
</td>
</tr>
<tr>
<td colspan="3">
<div class="collapse" id="ID_2">
<div class="well">
<table class="table table-hover table-bordered" style="background:#fff; margin-bottom:10px;">
<thead>
<tr class="inverse">
<th>Subject</th>
<th>Mark</th>
</tr>
</thead>
<tbody>
<tr>
<td>Chemistry</td>
<td>100</td>
</tr>
</tbody>
</table>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
fiddle
But in this approach, when a section is not expanded, the row which encloses the collapsible div appears like a blank row in the table - which is quite misleading.
I am sure this is not the best way to render this sort of details.Would like to know what are the alternate UI options to render these details.
Use this CSS:
td[colspan="3"] {
padding: 0;
}
Fiddle
I'm trying to understand how I can have my th tags that have a class assigned to them called "nosortable" to have its content of that cell be perfectly centered whether its a checkbox or text. I tried applying a padding-right of 0 but that took it all off. Are their any ideas?
<table id="basicTable" class="table table-striped table-bordered responsive">
<thead class="">
<tr>
<th class="nosortable center"><input type="checkbox" id="selectall" /></th>
<th class="center">ID</th>
<th>Name</th>
<th>Email Address</th>
<th>Username</th>
<th>Role</th>
<th>Status</th>
<th class="nosortable center">Actions</th>
</tr>
</thead>
<tbody>
<tr>
<td class="center"><input type="checkbox" name="check[]" class="tablecheck" data-id="1"/></td>
<td class="center">1</td>
<td>My name</td>
<td>myemail#email.com</td>
<td>testuser</td>
<td>Owner</td>
<td>Active</td>
<td class="center">
<a data-original-title="Edit" href="" data-toggle="tooltip" title="" class="tooltips"><i class="fa fa-pencil"></i></a>
<a data-original-title="Delete" href="" data-toggle="tooltip" title="" class="delete-row tooltips"><i class="fa fa-trash-o"></i></a>
</td>
</tr>
</tbody>
</table>
http://jsfiddle.net/xtremer360/cLzj4kL1/
In your stylesheet add the following
table#basicTable th {
text-align: center;
}