Why is the following HTML code not producing the table I would expect?
Some of my cells are being skipped over. I want the following table (https://jsfiddle.net/t4q801s7/1/). The only difference being I want to use rowspan (e.g. Johnny and San Diego won't be repeated).
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet"/>
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>City</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="3">Johnny</td>
<td>Bay Area</td>
</tr>
<tr>
<td>New York</td>
</tr>
<tr>
<td rowspan="2">San Diego</td>
</tr>
<tr>
<td>Zack</td>
</tr>
</tbody>
</table>
Table cells have vertical-align:middle by default, so if you don't want that, you will have to change that in the stylesheet.
(For some unfathomable reason, we seem to need !important here. Sorry.)
table, td, th {
vertical-align: baseline !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet"/>
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>City</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="3">Johnny</td>
<td>Bay Area</td>
</tr>
<tr>
<td>New York</td>
</tr>
<tr>
<td rowspan="2">San Diego</td>
</tr>
<tr>
<td>Zack</td>
</tr>
</tbody>
</table>
The problem, however, as you can see, is that with a table cell with a height of three rows, it doesn't have the contiguous lines between the rows. Table cells don't have borders inside of them, and table rows can't have borders of their own! So you will have to think about a workaround for that. Again, sorry.
Related
table.parent td:nth-of-type(1):not(table.nested td){
color: red;
}
<table class="table parent">
<tbody>
<tr>
<td>TEXTA</td>
<td>TEXTB</td>
</tr>
<tr>
<td>Has nested table below
<table class="table nested">
<tbody>
<thead>
<th>S.No.</th>
<th>Name</th>
<th>Contact</th>
</thead>
<tr>
<td>1</td>
<td>ABC</td>
<td>PQR</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>TEXTC</td>
<td>TEXTD</td>
</tr>
</tbody>
</table>
I have a nested table as follows -
<table class="table parent">
<tbody>
<tr>
<td>TEXTA</td>
<td>TEXTB</td>
</tr>
<tr>
<td>Has nested table below
<table class="table nested">
<tbody>
<thead>
<th>S.No.</th>
<th>Name</th>
<th>Contact</th>
</thead>
<tr>
<td>1</td>
<td>ABC</td>
<td>PQR</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>TEXTC</td>
<td>TEXTD</td>
</tr>
</tbody>
</table>
Requirement - Only TEXTA and TEXTB should be colored in red. In real scenario there are many rows. I want only the first td of each row in the parent table to be colored. I am doing something like -
table.parent td:nth-of-type(1):not(table.nested td){
color: red;
}
This is not giving me any result. What is the correct way of achieving this?
Spent a while playing around with this. The best I can do is to suggest using 2 lines of CSS rather than 1. One selector to do all of the first row of td and one to set the nested ones back to how they belong.
table.parent tr:first-child td {
color: red;
}
table.nested tr:first-child td {
color: black;
}
<table class="table parent">
<tbody>
<tr>
<td>TEXTA</td>
<td>TEXTB</td>
</tr>
<tr>
<td>Has nested table below
<table class="table nested">
<tbody>
<thead>
<th>S.No.</th>
<th>Name</th>
<th>Contact</th>
</thead>
<tr>
<td>1</td>
<td>ABC</td>
<td>PQR</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>TEXTC</td>
<td>TEXTD</td>
</tr>
</tbody>
</table>
You said that..
I want only the first td of each row in the parent table to be colored
So, I am assuming you want TEXTA and TEXTC to be colored (and not TEXTB as you stated).
If thats the case, then your idea was to select elements (first td of each row) if they dont contain a specific child element (table.nested).
This is not possible with CSS2 or CSS3.
The CSS2 and CSS3 selector specifications do not allow for any sort of parent selection.
See CSS selector - element with a given child
Edit
You can use jquery/javascript to do so.
For example, to add opacity and color css properties:
$(document).ready(function(){
$('table.parent > tbody > tr > td:first-child').each(function(){
if ($(this).has('table.nested').length == 0){
$(this).css('opacity', '0.5');
$(this).css('color', 'red');
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table parent">
<tbody>
<tr>
<td>TEXTA</td>
<td>TEXTB</td>
</tr>
<tr>
<td>Has nested table below
<table class="table nested">
<thead>
<th>S.No.</th>
<th>Name</th>
<th>Contact</th>
</thead>
<tbody>
<tr>
<td>1</td>
<td>ABC</td>
<td>PQR</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>TEXTC</td>
<td>TEXTD</td>
</tr>
</tbody>
</table>
Just give some class to TEXTA & TEXTB
for example:
(html)
<td class="red-color-text">TEXTA</td>
<td class="red-color-text">TEXTB</td>
(css)
.red-color-text{color: red;}
I am getting nTd is undefined when using TableData of bootstrap. I have already read in many forums that it's because of not having the same number of td's and th's in the header and body elements.
But I think it should work. I have counted the number of tds and ths and they are the same.
Here is my code:
<table id="searchResultTable">
<thead>
<tr>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<th:block th:each="element, iterStat : ${searchResult}">
<tr>
<td class="underline font-weight-bold" colspan="3"
data-toggle="collapse" th:data-target="|.demo${iterStat.count}|"
th:text="${element.key}" />
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr th:id="'demo'+ ${iterStat.index}"
th:class="|accordian-body collapse demo${iterStat.count}|"
th:each="anews : ${element.value}">
<td class="boldanditalic" th:text="${anews.getDate()}">Date</td>
<td class="boldanditalic" th:text="${anews.user.getFullname()}">
writer</td>
<td th:if="${!#strings.equals(anews.fp,'')}"
th:text="'$'+${anews.fp}">fp</td>
<td th:if="${!#strings.equals(anews.sep,'')}"
th:text="'$'+${anews.sep}">sep</td>
<td th:if="${!#strings.equals(anews.tp,'')}"
th:text="'$'+${anews.tp}">tp</td>
<td th:if="${!#strings.equals(anews.fop,'')}"
th:text="'$'+${anews.fop}">fop</td>
<td th:if="${!#strings.equals(anews.fp,'')}"
th:text="'$'+${anews.fp}">SP</td>
<td th:if="${!#strings.equals(anews.sip,'')}"
th:text="'$'+${anews.sip}">SP</td>
<td th:if="${!#strings.equals(anews.sp,'')}"
th:text="'$'+${anews.sp}">SP</td>
</tr>
</th:block>
</tbody>
</table>
I even tried to add the <th> block in the header. But it didn't work.
I would appreciate your help.
Update:
I have added the following code in the header section:
<script>
$(document).ready(function() {
$('#searchResultTable').dataTable();
});
</script>
Here you can find the links and scripts in the header:
<script
src="https://cdn.datatables.net/1.10.20/css/dataTables.bootstrap4.min.css"></script>
<link
href="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.min.css"
rel="stylesheet">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<link
href="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.css"
rel="stylesheet">
<script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.js"></script>
<script
src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
You can find the screenshot of the result here:
Update 2: If I remove the th:block section and add some simple elements, it works.
Inside the <thead> row you have 9 cells, let's assume this is what you want;
Inside <tbody> you have 2 rows:
The first row has 9 cells, but the first cell has colspan=3, which will give you 11 cells in total according to your <thead> row. This should be fixed in either remove 2 empty cells or remove colspan.
The second row has 9 cells, but the last 7 cells are conditional and you may not have them at all or have some of them. To maintain the proper structure of the table you should remove the condition from cells and always have them.
Basically your code may look like ... (I might miss something as I didn't try this code, but you'll get an idea)
<table id="searchResultTable">
<thead>
<tr>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<th:block th:each="element, iterStat : ${searchResult}">
<tr>
<td class="underline font-weight-bold" colspan="3"
data-toggle="collapse" th:data-target="|.demo${iterStat.count}|"
th:text="${element.key}"></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr th:id="'demo'+ ${iterStat.index}"
th:class="|accordian-body collapse demo${iterStat.count}|"
th:each="anews : ${element.value}">
<td class="boldanditalic" th:text="${anews.getDate()}">Date</td>
<td class="boldanditalic" th:text="${anews.user.getFullname()}">
writer</td>
<td th:text="${!#strings.equals(anews.fp,'')}? ${'$'+ anews.fp} : ''">fp</td>
<td th:text="${!#strings.equals(anews.sep,'')}? ${'$'+ anews.sep} : ''">sep</td>
<td th:text="${!#strings.equals(anews.tp,'')}? ${'$'+ anews.tp} : ''">tp</td>
<td th:text="${!#strings.equals(anews.fop,'')}? ${'$'+ anews.fop} : ''">fop</td>
<td th:text="${!#strings.equals(anews.sp,'')}? ${'$'+ anews.sp} : ''">sp</td>
<td th:text="${!#strings.equals(anews.sip,'')}? ${'$'+ anews.sip} : ''">sip</td>
<td th:text="${!#strings.equals(anews.fp,'')}? ${'$'+ anews.fp} : ''">fp</td>
</tr>
</th:block>
</tbody>
</table>
I am creating a timeline with an html table. I want to colorize the data cells in a row corresponding to the lifespan of the person represented in a row. That is to say, if the person lived from 1835 to 1910, all of those columns would be colorized. Those prior to 1835, and those after 1910 would not.
I currently simply have a placeholder tilde as the contents of the "year" cells:
<tr>
<th scope="row">John Marshall Clemens</th>
<td>Father</td>
<td>~</td>
<td>~</td>
<td>~</td>
<td>~</td>
The ultimate effect should look something like this (assuming John Marshall was alive at least from 1794 through 1797:
You can do it using CSS classes
.bg {
background-color: orangered;
}
<table>
<tr>
<th scope="row">John Marshall Clemens</th>
<td>Father</td>
<td class="bg">~</td>
<td class="bg">~</td>
<td class="bg">~</td>
<td class="bg">~</td>
</tr>
</table>
i hope this helps if its static.
.bgColor{
background-color: #ff7f28;
color: #ff7f28;
}
<html>
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
</head>
<body>
<table class="table table-bordered text-center">
<thead>
<tr>
<th>Name</th>
<th>Relation</th>
<th>1794</th>
<th>1795</th>
<th>1796</th>
<th>1797</th>
</tr>
</thead>
<tbody>
<tr>
<td>jhon</td>
<td>fater</td>
<td colspan="4" class="bgColor">~</td>
</tr>
<tr>
<td>mark</td>
<td>self</td>
<td>~</td>
<td>~</td>
<td>~</td>
<td>~</td>
</tr>
<tr>
<td>livy</td>
<td>wife</td>
<td>~</td>
<td>~</td>
<td>~</td>
<td>~</td>
</tr>
</tbody>
</table>
</body>
</html>
If you dont want to use css,
<tr>
<th scope="row">John Marshall Clemens</th>
<td bgcolor="orange">Father</td>
<td bgcolor="orange">~</td>
<td bgcolor="orange">~</td>
<td bgcolor="orange">~</td>
https://www.w3schools.com/tags/att_td_bgcolor.asp
But I suggest reading and using CSS
https://www.w3schools.com/css/css_background.asp
I have a Flask application that takes a bunch of input from the users, processes it and generates a table/matrix of values.
I want to display this table to the user with different colors for different cells. At the time of the table generation I know what color I want the cell to be.
I am using Bootstrap4. Can this be done using Bootstrap?
The class attribute can be used in the table row to get the required colors.
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<tr class="active">
<td>ABC</td>
<td>10</td>
</tr>
<tr class="danger">
<td>DEF</td>
<td>20</td>
</tr>
<tr class="info">
<td>GHI</td>
<td>30</td>
</tr>
<tr class="success">
<td>JKL</td>
<td>40</td>
</tr>
<tr class="warning">
<td>MNO</td>
<td>50</td>
</tr>
</tbody>
</table>
In Bootstrap 4, you can give color to the table rows by adding bg classes to the <tr> elements like;
<tr class="bg-success">...</tr>
<tr class="bg-warning">...</tr>
<tr class="bg-danger">...</tr>
Take a look at bootstrap documentation of tables which explains it in detail
This is the way to add color on cells
<!doctype html>
<html lang="en">
<head>
<title>Table Color</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<table class="table">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody class="table-hover">
<tr>
<td>Default</td>
<td>Defaultson</td>
<td>def#somemail.com</td>
</tr>
<tr class="table-primary">
<td>Primary</td>
<td>Joe</td>
<td>joe#example.com</td>
</tr>
<tr class="table-success">
<td>Success</td>
<td>Doe</td>
<td>john#example.com</td>
</tr>
<tr class="table-danger">
<td>Danger</td>
<td>Moe</td>
<td>mary#example.com</td>
</tr>
<tr class="table-info">
<td>Info</td>
<td>Dooley</td>
<td>july#example.com</td>
</tr>
<tr class="table-warning">
<td>Warning</td>
<td>Refs</td>
<td>bo#example.com</td>
</tr>
<tr class="table-active">
<td>Active</td>
<td>Activeson</td>
<td>act#example.com</td>
</tr>
<tr class="table-secondary">
<td>Secondary</td>
<td>Secondson</td>
<td>sec#example.com</td>
</tr>
<tr class="table-light">
<td>Light</td>
<td>Angie</td>
<td>angie#example.com</td>
</tr>
<tr class="table-dark text-dark">
<td>Dark</td>
<td>Bo</td>
<td>bo#example.com</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
More about table
table documentation bootstrap 4
I have little problem in my web page. I use bootstrap 4 and in table box I set inside other table as in the picture below. How to make the height of table inside the same as the hieght of the box (td)?
html:
<table class="table table-bordered">
<tbody>
<tr>
<td>REVERT</td>
<td>
<table class="table" style="height: 100%">
<tbody>
<tr>
<td>Name</td>
<td>LONG TEXT</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
Browser:
Because the table have CSS attribute margin-bottom: 20px, you need to add an override CSS to remove this attribute:
<table class="table table-bordered">
<tbody>
<tr>
<td>REVERT</td>
<td>
<table class="table table-no-margin" style="height: 100%">
<tbody>
<tr>
<td>Name</td>
<td>LONG TEXT</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
<style>
.table-no-margin { margin: 0 }
</style>
This is happening because your nested table has a margin-bottom of 1rem(default bootstrap css). override it that's it.
Working example
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<table class="table table-bordered">
<tbody>
<tr>
<td>REVERT</td>
<td>
<table class="table" style="height: 100%; margin-bottom:0px;">
<tbody>
<tr>
<td>Name</td>
<td>LONG TEXT</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
This question has been asked many times before, but I'll answer it specifically for your scenario. It's not an issue of removing padding/margins.
In order to get a 100% height table, it's container must also be 100% height. So in this case set the containing td to height: 100%...
Demo on Codeply
<td>REVERT</td>
<td style="padding:0;height: 100%;">
<table class="table" style="height: 100%">
<tbody>
<tr>
<td>Name</td>
<td>LONG TEXT</td>
</tr>
</tbody>
</table>
</td>