The css for tr causes the elements inside to not be centered anymore (you can see in the example they're too high up). How should I fix this?
tr {
height: 50px}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<table class="table">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>Doe</td>
<td>john#example.com</td>
</tr>
<tr>
<td>Mary</td>
<td>Moe</td>
<td>mary#example.com</td>
</tr>
<tr>
<td>July</td>
<td>Dooley</td>
<td>july#example.com</td>
</tr>
</tbody>
</table>
</div>
I believe your issue is because your vertical alignment is being set from bootstrap.
If you were to add this, for example, to your CSS:
td, th{
vertical-align:middle !important;
}
You'll find that your text has been vertically centered within the 50px rows. The issue only becomes apparent when your rows have a large enough space within them to allow text the room to be biased towards one side or the other.
Note: the !important declaration is only to fix specificity issues. You can always add classes and/or IDs to allow more specific rules to be set, effectively overriding the default styling coming from bootstrap.
Setting
tr {
height: 50px
vertical-align:middle}
Won't work, simply because of specificity and the fact that the TD/TH elements require the property in this context.
Here's a demo:
http://codepen.io/anon/pen/QEQgdo
Here's a stackoverflow snippet:
tr {
height: 50px}
td, th{
vertical-align:middle !important;}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<table class="table">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>Doe</td>
<td>john#example.com</td>
</tr>
<tr>
<td>Mary</td>
<td>Moe</td>
<td>mary#example.com</td>
</tr>
<tr>
<td>July</td>
<td>Dooley</td>
<td>july#example.com</td>
</tr>
</tbody>
</table>
</div>
Just do:
td {
height: 100px;
vertical-align:middle !important;
}
As you can see:
td {
height: 100px;
vertical-align:middle !important;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<table class="table">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>Doe</td>
<td>john#example.com</td>
</tr>
<tr>
<td>Mary</td>
<td>Moe</td>
<td>mary#example.com</td>
</tr>
<tr>
<td>July</td>
<td>Dooley</td>
<td>july#example.com</td>
</tr>
</tbody>
</table>
</div>
Try using line-height it should center your text without the need of !importent.
tr > td {
height: 50px;
line-height: 50px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<table class="table">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>Doe</td>
<td>john#example.com</td>
</tr>
<tr>
<td>Mary</td>
<td>Moe</td>
<td>mary#example.com</td>
</tr>
<tr>
<td>July</td>
<td>Dooley</td>
<td>july#example.com</td>
</tr>
</tbody>
</table>
</div>
Related
I'm trying to style all tbody tags except the first one but with poor results.
As you can see in the snippet, the style is applied to all elements, including the first one, where am I wrong?
div.cont_table_toggle table#general_list tbody.divider:not(:first-child) {
border-top: 8px solid red;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="cont_table_toggle">
<table id="general_list" class="table table-bordered">
<thead>
<tr>
<th>AAAA</th>
<th>BBBB</th>
</tr>
</thead>
<tbody id="block-1" class="divider">
<tr>
<td>1111</td><td>2222</td>
</tr>
</tbody>
<tbody id="block-2" class="divider">
<tr>
<td>3333</td><td>4444</td>
</tr>
</tbody>
<tbody id="block-3" class="divider">
<tr>
<td>5555</td><td>6666</td>
</tr>
</tbody>
</table>
</div>
Try using tbody.divider:not(:first-of-type).
The :first-of-type selector matches every element that is the first child, of a particular type, of its parent.
Reference : https://www.w3schools.com/cssref/sel_first-of-type.asp
https://www.w3schools.com/cssref/css_selectors.asp
Try it below.
div.cont_table_toggle table#general_list tbody.divider:not(:first-of-type) {
border-top: 8px solid red;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="cont_table_toggle">
<table id="general_list" class="table table-bordered">
<thead>
<tr>
<th>AAAA</th>
<th>BBBB</th>
</tr>
</thead>
<tbody id="block-1" class="divider">
<tr>
<td>1111</td><td>2222</td>
</tr>
</tbody>
<tbody id="block-2" class="divider">
<tr>
<td>3333</td><td>4444</td>
</tr>
</tbody>
<tbody id="block-3" class="divider">
<tr>
<td>5555</td><td>6666</td>
</tr>
</tbody>
</table>
</div>
tbody is not the first child of the table. So the :first-child selector does not work. If you remove the thead, it works.
div.cont_table_toggle table#general_list tbody.divider:not(:first-child) {
border-top: 8px solid red;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="cont_table_toggle">
<table id="general_list" class="table table-bordered">
<tbody id="block-1" class="divider">
<tr>
<td>1111</td><td>2222</td>
</tr>
</tbody>
<tbody id="block-2" class="divider">
<tr>
<td>3333</td><td>4444</td>
</tr>
</tbody>
<tbody id="block-3" class="divider">
<tr>
<td>5555</td><td>6666</td>
</tr>
</tbody>
</table>
</div>
You could use :not(#block-1), :nth-child(2), or as the other answer has suggested, :not(:first-of-type).
Use not(#block-1)
The first tbody has id that you can use. As you many know, id must unique.
tbody:not(#block-1) selects all tbodys except the first one.
#general_list tbody:not(#block-1) {
border-top: 8px solid red;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="cont_table_toggle">
<table id="general_list" class="table table-bordered">
<thead>
<tr>
<th>AAAA</th>
<th>BBBB</th>
</tr>
</thead>
<tbody id="block-1" class="divider">
<tr>
<td>1111</td><td>2222</td>
</tr>
</tbody>
<tbody id="block-2" class="divider">
<tr>
<td>3333</td><td>4444</td>
</tr>
</tbody>
<tbody id="block-3" class="divider">
<tr>
<td>5555</td><td>6666</td>
</tr>
</tbody>
</table>
</div>
Use :not(:nth-child(2))
#general_list tbody:not(:nth-child(2)) {
border-top: 8px solid red;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="cont_table_toggle">
<table id="general_list" class="table table-bordered">
<thead>
<tr>
<th>AAAA</th>
<th>BBBB</th>
</tr>
</thead>
<tbody id="block-1" class="divider">
<tr>
<td>1111</td><td>2222</td>
</tr>
</tbody>
<tbody id="block-2" class="divider">
<tr>
<td>3333</td><td>4444</td>
</tr>
</tbody>
<tbody id="block-3" class="divider">
<tr>
<td>5555</td><td>6666</td>
</tr>
</table>
</div>
Would it be possible to use table-responsive that only use the minimal column width based on the length of the data in the column, and also having the thead using all the canvas width.
Example: (the last column that has no content would use the remaining space on the right)
If you add a table-responsive class to your table, it will occupy 100% of it's parent's width and will space the columns equally... this is shown in the top table in the code snippet below
But we can do what you're looking for:
want all the columns to use minimal width
last empty column to occupy the remaining width.
check the bottom table in the code snippet below
th {
background: lightgray
}
.myTableOne td {
white-space: nowrap
}
.myTableOne th:last-of-type {
width: 100%;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<div class="container-fluid">
<h4>Table with table-responsive class</h4> <br/>
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr>
<th>#</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>City</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Anna</td>
<td>Pitt</td>
<td>35</td>
<td>New York</td>
<td></td>
</tr>
</tbody>
</table>
</div>
</div>
<hr/>
<h4>Table for your requirements </h4>
<br/>
<div class="myTableOne">
<table class=" table-bordered">
<thead>
<tr>
<th>#</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>City</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Anna</td>
<td>Pitt</td>
<td>35</td>
<td>New York</td>
<td></td>
</tr>
</tbody>
</table>
</div>
If you put the below code in fullscreen, the elements are only centering on the page once I'm zoomed in enough. How can I make it so it's centered regardless?
.col-centered {
float: none;
text-align: center;
}
.col-centered table {
margin: 0 auto;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<div class='container-fluid'>
<div class='quiz-list'>
<div class='row'>
<div class='col-md-4 col-centered'>
This text should be centered and the table underneath should be centered as well
<table class="table">
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>Last Name</th>
<th>Username</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>#mdo</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jacob</td>
<td>Thornton</td>
<td>#fat</td>
</tr>
<tr>
<th scope="row">3</th>
<td>Larry</td>
<td>the Bird</td>
<td>#twitter</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
Because Bootstrap.
The default value for width on a .col-md-4 is 33.3333%. So, since the div doesn't have float or margins or anything, it is simply aligned to the left side of the screen, and the content inside it is centred all right, but that isn't noticeable.
One solution is to remove the col-md-4 class.
.col-centered {
float: none;
text-align: center;
}
.col-centered table {
margin: 0 auto;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<div class='container-fluid'>
<div class='quiz-list'>
<div class='row'>
<div class='col-centered'> <!-- this -->
This text should be centered and the table underneath should be centered as well
<table class="table">
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>Last Name</th>
<th>Username</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>#mdo</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jacob</td>
<td>Thornton</td>
<td>#fat</td>
</tr>
<tr>
<th scope="row">3</th>
<td>Larry</td>
<td>the Bird</td>
<td>#twitter</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
I tried other solutions as well, such as setting the width or the margins explicitly, but that doesn't seem to work.
Edit:
Oh, I see why the other solutions don't work: because in this snippet the Bootstrap css is loaded after the style block in the page.
Under normal circumstances, putting things in a stylesheet after loading the Bootstrap stylesheet will work fine to override it. So you can use width:auto or margin:0 auto, whatever you like.
In css file, Update
.col-centered {
float: none;
text-align: center;
}
as
.col-centered {
float: none !important;
text-align: center;
margin: 0 auto;
}
I have a bootstrap table, but I can't seem to get the text to align in the middle because the image expands the row.
I have tried adding vertical-align: middle to .table in my CSS, but it doesn't seem to do anything.
Example
https://jsfiddle.net/DTcHh/#&togetherjs=hy7S1lFDR3
<div class="container">
<h2>Hover Rows</h2>
<p>The .table-hover class enables a hover state on table rows:</p>
<table class="table table-hover">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
<th>Image</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>Doe</td>
<td>john#example.com</td>
<td><img src="http://fullsoftwarepro.com/wp-content/uploads/2015/07/Radmin- server-and-viewer-3.5-Crack-License-key-Download.jpg" width=50></td>
</tr>
<tr>
<td>Mary</td>
<td>Moe</td>
<td>mary#example.com</td>
<td><img src="http://fullsoftwarepro.com/wp-content/uploads/2015/07/Radmin- server-and-viewer-3.5-Crack-License-key-Download.jpg" width=50></td>
</tr>
<tr>
<td>July</td>
<td>Dooley</td>
<td>july#example.com</td>
<td><img src="http://fullsoftwarepro.com/wp-content/uploads/2015/07/Radmin-server-and-viewer-3.5-Crack-License-key-Download.jpg" width=50></td>
</tr>
</tbody>
</table>
</div>
Answer
Twitter Bootstrap have a CSS class called text-center, you can simply use this one, as shown below.
Try this:
CSS
.mid-align:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
margin-right: -0.25em;
}
HTML
<div class="container">
<h2>Hover Rows</h2>
<p>The .table-hover class enables a hover state on table rows:</p>
<table class="table table-hover text-center">
<thead>
<tr>
<th class="text-center">Firstname</th>
<th class="text-center">Lastname</th>
<th class="text-center">Email</th>
<th class="text-center">Image</th>
</tr>
</thead>
<tbody>
<tr>
<td class="mid-align">John</td>
<td class="mid-align">Doe</td>
<td class="mid-align">john#example.com</td>
<td><img src="http://fullsoftwarepro.com/wp-content/uploads/2015/07/Radmin-server-and-viewer-3.5-Crack-License-key-Download.jpg" width=50></td>
</tr>
<tr>
<td class="mid-align">Mary</td>
<td class="mid-align">Moe</td>
<td class="mid-align">mary#example.com</td>
<td><img src="http://fullsoftwarepro.com/wp-content/uploads/2015/07/Radmin-server-and-viewer-3.5-Crack-License-key-Download.jpg" width=50></td>
</tr>
<tr>
<td class="mid-align">July</td>
<td class="mid-align">Dooley</td>
<td class="mid-align">july#example.com</td>
<td><img src="http://fullsoftwarepro.com/wp-content/uploads/2015/07/Radmin-server-and-viewer-3.5-Crack-License-key-Download.jpg" width=50></td>
</tr>
</tbody>
</table>
</div>
As for specificity, declaring a style attribute inside a tag overwrites the style written in your *yourStyle*.css file, so in most cases it is not the best practice to declare the style inside the HTML element, but it is surely acceptable (just added a small note).
I have a Bootstrap 3 table inside a div. It is defined like this:
<div id="todays-table">
<table class="table">
<thead>
<tr>
<th>Train Number</th>
<th>Status</th>
<th>Last Attempt</th>
</tr>
</thead>
<tbody>
<tr class="danger">
<td>2001</td>
<td>0</td>
<td>2015-05-12 11:25:16</td>
</tr>
<tr class="danger">
<td>2005</td>
<td>0</td>
<td>2015-05-12 11:25:16</td>
</tr>
<tr class="success-row">
<td>2006</td>
<td>1</td>
<td>2015-05-12 11:25:16</td>
</tr>
<tr class="danger">
<td>2007</td>
<td>0</td>
<td>2015-05-12 11:25:16</td>
</tr>
<tr class="danger">
<td>2008</td>
<td>0</td>
<td>2015-05-12 11:25:16</td>
</tr>
<tr class="danger">
<td>2009</td>
<td>0</td>
<td>2015-05-12 11:25:16</td>
</tr>
<tr class="danger">
<td>2010</td>
<td>0</td>
<td>2015-05-12 11:25:16</td>
</tr>
<tr class="danger">
<td>2011</td>
<td>0</td>
<td>2015-05-12 11:25:16</td>
</tr>
<tr class="danger">
<td>2012</td>
<td>0</td>
<td>2015-05-12 11:25:16</td>
</tr>
<tr class="danger">
<td>2013</td>
<td>0</td>
<td>2015-05-12 11:25:16</td>
</tr>
</tbody>
</table>
</div>
The problem is there is no padding on the table and it goes to the very edges of the web browser like this:
How do I add padding to this bootstrap table? I tried <div id="todays-table" style="padding: 20px;"> and table { padding: 20px; }, neither of which worked. Can someone point in the right direction on how to do this please?
You want margin, not padding:
#todays-table {
margin: 20px;
}
Demo
Better, set styles on a reusable class:
.padded {
margin: 20px;
}
<div id="todays-table" class="padded">
That said, Bootstrap's grid system does a nice job of that type of thing without additional CSS:
<div class="container-fluid">
<div class="row">
<div class="col-xs-12" id="todays-table">
Demo 2
http://getbootstrap.com/css/#grid
Try adding some styles to the tds themselves
div#todays-table > table.table td {
padding: 20px;
}
EDIT
Just re-read the question, this should be more what you are after:
div#todays-table > table.table {
margin: 20px;
}