I am styling table for my website. I want to have a table where first TR doesn't have border while others TR and their TD's have a border.
Code: http://jsfiddle.net/azq6xfnr/ or here:
.table2 {
border: 1px solid black;
border-collapse: collapse;
text-align: center;
}
.table2 .header {
background-color: #d8ff93;
color: #126f06;
border: 0;
}
.table2 td {
border: 1px solid #53f673;
padding: 10px;
}
.table2 tr:not(.header):nth-child(odd) {
background-color: #3cde53;
}
<table class="table2">
<tr class="header">
<td>Lp.</td>
<td>Column 1</td>
<td>Column 2</td>
</tr>
<tr>
<td>1</td>
<td>Row 1</td>
<td>Row 1</td>
</tr>
<tr>
<td>2</td>
<td>Row 2</td>
<td>Row 2</td>
</tr>
<tr>
<td>3</td>
<td>Row 3</td>
<td>Row 3</td>
</tr>
<tr>
<td>4</td>
<td>Row 4</td>
<td>Row 4</td>
</tr>
<tr>
<td>5</td>
<td>Row 5</td>
<td>Row 5</td>
</tr>
</table>
Use the CSS first-child selector. Here is a fiddle http://jsfiddle.net/r8p061hs/ or http://jsfiddle.net/r8p061hs/1/
.table2 {
border: 1px solid black;
border-collapse: collapse;
text-align: center;
}
.table2 .header {
background-color: #d8ff93;
color: #126f06;
border: 0;
}
.table2 td {
border: 1px solid #53f673;
padding: 10px;
}
.table2 tr:not(.header):nth-child(odd) {
background-color: #3cde53;
}
.table2 tr:first-child {
border: 1px solid #53f673;
}
.table2 tr:first-child td {
border: none;
}
<table class="table2">
<tr class="header">
<td>Lp.</td>
<td>Column 1</td>
<td>Column 2</td>
</tr>
<tr>
<td>1</td>
<td>Row 1</td>
<td>Row 1</td>
</tr>
<tr>
<td>2</td>
<td>Row 2</td>
<td>Row 2</td>
</tr>
<tr>
<td>3</td>
<td>Row 3</td>
<td>Row 3</td>
</tr>
<tr>
<td>4</td>
<td>Row 4</td>
<td>Row 4</td>
</tr>
<tr>
<td>5</td>
<td>Row 5</td>
<td>Row 5</td>
</tr>
</table>
I think css pseudo class :first-child could help you: http://www.w3schools.com/cssref/sel_firstchild.asp
You need to remove the border from both your table, and the cells in the .header row, no need to use :first-child or :first-of-type as you've given the row the class header
Demo Fiddle
.table2 {
border-collapse: collapse;
border-spacing:0;
text-align: center;
/* remove the border from here */
}
.table2 .header td{
border:none; /* and from here */
}
As an alternative to the other answers, if your intention is that the first row be styled this way to be a header row, you could also consider using the more semantic <thead> grouping with <th> elements, if that's practical. You could then class them (advisable) or just rely on the tag names (less advisable due to selector performance, but still possible).
By then grouping subsequent rows within a <tbody>, you could also simplify your alternate row colouring selector, as you would be able to avoid the :not pseudo-selector.
Example of adjusted code:
<table class="table2">
<thead class="header">
<tr><th>Lp.</th><th>Column 1</th><th>Column 2</th></tr>
</thead>
<tbody>
<tr><td>1</td><td>Row 1</td><td>Row 1</td></tr>
<tr><td>2</td><td>Row 2</td><td>Row 2</td></tr>
<tr><td>3</td><td>Row 3</td><td>Row 3</td></tr>
<tr><td>4</td><td>Row 4</td><td>Row 4</td></tr>
<tr><td>5</td><td>Row 5</td><td>Row 5</td></tr>
</tbody>
</table>
Related
I have a table with 2 columns and I want each row to have a gray border-top, a border down the middle separating the columns and the overall table to have a black border. However, the border on the table rows and the border-right on the first column of each row is overlapping the overall tables border. How can I resolve this?
table {
width:350px;
border-collapse:collapse;
table-layout:fixed;
border:2px solid black;
text-align:center;
}
tr {
border-top:2px solid #cecece;
}
thead tr {
border-top:none;
}
th:first-child, td:first-child {
border-right:2px solid #cecece;
}
<table>
<thead>
<tr>
<th>Heading 1</th>
<th>Heading 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Col 1 Row 1</td>
<td>Col 2 Row 1</td>
</tr>
<tr>
<td>Col 1 Row 2</td>
<td>Col 2 Row 2</td>
</tr>
<tr>
<td>Col 1 Row 3</td>
<td>Col 2 Row 3</td>
</tr>
</tbody>
</table>
Wrap the table element in a div and add border to the div element instead of the table element.
.wrapper {
width: 350px;
border: 2px solid black;
}
table {
border-collapse: collapse;
table-layout: fixed;
text-align: center;
width: 100%;
}
tr {
border-top: 2px solid #cecece;
}
thead tr {
border-top: none;
}
th:first-child,
td:first-child {
border-right: 2px solid #cecece;
}
<div class="wrapper">
<table>
<thead>
<tr>
<th>Heading 1</th>
<th>Heading 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Col 1 Row 1</td>
<td>Col 2 Row 1</td>
</tr>
<tr>
<td>Col 1 Row 2</td>
<td>Col 2 Row 2</td>
</tr>
<tr>
<td>Col 1 Row 3</td>
<td>Col 2 Row 3</td>
</tr>
</tbody>
</table>
</div>
Okay so i got a really specific css i need to set for the first 3 rows and only on the 2nd td of those 3 rows. is there anyway to target only them in css rules?
right now i am trying like this
here is the render
<tr className={getRowColor(props, l, i)}>
<td>{i + 1}</td>
<td className="info-table-cell{l.info}</td>
</tr>
and here it is the second td in this row that i want to target for the first 3 rows only. And right now i try like this
.info-table-cell td:nth-child(1) {
background-color: #FFF45E;
}
.info-table-cell td:nth-child(2) {
background-color: #DCDCDC;
}
.info-table-cell td:nth-child(3) {
background-color: #FFC933;
}
but that does nothing. Anyone who knows how to do this?
Hope this helps you. I have added a red background color for nd td of first 3 rows. Try it:
table {
width: 100%;
}
table tr td {
border: 1px solid #000;
}
table tr:first-child td:nth-child(2),
table tr:nth-child(2) td:nth-child(2),
table tr:nth-child(3) td:nth-child(2) {
background-color: red;
}
<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
</table>
If i understand you correcly, its indeed possible. There are some problem with your code, your not selecting anything because info-table-cell is the td. Here is my approach:
Html
<table class="your-specific-class">
<tr>
<td class="info-table-cell">Content 1</td>
<td class="info-table-cell">Content 2</td>
</tr>
<tr>
<td class="info-table-cell">Content 1</td>
<td class="info-table-cell">Content 2</td>
</tr>
<tr>
<td class="info-table-cell">Content 1</td>
<td class="info-table-cell">Content 2</td>
</tr>
</table>
CSS
.your-specific-class tr:nth-child(1) td:nth-child(2){
background-color: #FFF45E;
}
.your-specific-class tr:nth-child(2) td:nth-child(2){
background-color: #DCDCDC;
}
.your-specific-class tr:nth-child(3) td:nth-child(2){
background-color: #FFC933;
}
Example here: https://codepen.io/lauritzz77/pen/XoLjXr
Why in the following example do the columns of the header of the table not take the widths defined in the CSS?
I think in Table 1, the browser can not calculate a percentage value and a fixed one.
/************* commun css***********************/
table {
border-collapse: collapse;
table-layout:fixed;
width: 100%;
position: relative;
}
td, th {
border: 1px solid black;
-webkit-box-sizing: border-box;
}
tr{
width: 100%;
position: relative;
}
/************* css table 1***********************/
.table1 .col1 {
width: calc(35% - 10px);
}
.table1 .col2 {
width: calc(40% - 10px);
}
.table1 .col3 {
width: calc(25% - 10px);
}
.table1 .col4 {
width: 30px;
}
/************* css table 2***********************/
.table2 .col1 {
width: calc(30%);
}
.table2 .col2 {
width: calc(20%);
}
.table2 .col3 {
width: calc(50%);
}
<p>
Table 1 : invalid render (Chrome browser)
</p>
<table class="table1">
<thead>
<tr>
<th class="col1">Col 1</th>
<th class="col2">Col 2</th>
<th class="col3">Col 3</th>
<th class="col4">Col 4</th>
</tr>
</thead>
<tbody>
<tr>
<td>Row 1</td>
<td>Row 1</td>
<td>Row 1</td>
<td>Row 1</td>
</tr>
<tr>
<td>Row 2</td>
<td>Row 2</td>
<td>Row 2</td>
<td>Row 2</td>
</tr>
</tbody>
</table>
<p>
Table 2 : valid render (Chrome browser)
</p>
<table class="table2">
<thead>
<tr>
<th class="col1">Col 1</th>
<th class="col2">Col 2</th>
<th class="col3">Col 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>Row 1</td>
<td>Row 1</td>
<td>Row 1</td>
</tr>
<tr>
<td>Row 2</td>
<td>Row 2</td>
<td>Row 2</td>
</tr>
</tbody>
</table>
Does anyone have any advice in this situation?
Thank you in advance
Example : https://jsfiddle.net/3wq65hb4/
Yes, there is a problem with calc() width for tables. But it works for divs. What about making it on divs instead of tables?
Let's look at example for your purposes:
https://jsfiddle.net/3wq65hb4/39/
You can find there:
.row {
display: flex;
}
It's for equal height for columns. Also you can find there:
margin-left: -1px;
It's solution for borders - to avoid double borders like:
Also remember that max-width for your column: 30px means that it always will be 30px even if your content will be wider:
But maybe it's what you want to achieve.
Let me know if that solution is ok for you. And good luck with other htmls :)
Is there a way in CSS to achieve alternate row shading all rows across multiple tbody elements are treated as one group?
So for example:
<tbody>
<tr>...</tr>
</tbody>
<tbody>
<tr>...</tr>
</tbody>
<tbody>
<tr>...</tr>
</tbody>
I know of nth-child, but that would not work here, since if each tbody only has one row, then they would all get coloured the same.
Anyone know of any ways to achieve this behaviour?
Not with CSS...no. nth-of- doesn't work that way. You would need Javascript.
Jquery makes this easy.
$("#my-table tr:even").css("background-color", "#bbbbff");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="my-table">
<tbody>
<tr>
<td>Row 1</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Row 2</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Row 3.1</td>
</tr>
<tr>
<td>Row 3.2</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Row 4.1</td>
</tr>
<tr>
<td>Row 4.2</td>
</tr>
<tr>
<td>Row 4.3</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Row 5</td>
</tr>
</tbody>
</table>
#my-table tbody
{
display: block;
margin: 20px auto;
}
#my-table tbody tr
{
background-color: #d88;
}
#my-table tbody:nth-child(even) tr
{
background-color: #8d8;
}
#my-table tbody:nth-child(even) tr:nth-child(even)
{
background-color: #d88;
}
#my-table tbody:nth-child(odd) tr:nth-child(odd)
{
background-color: #8d8;
}
<table id="my-table">
<tbody>
<tr><td>Row 1</td></tr>
</tbody>
<tbody>
<tr><td>Row 2</td></tr>
<tr><td>Row 2</td></tr>
</tbody>
<tbody>
<tr><td>Row 3</td></tr>
<tr><td>Row 3</td></tr>
<tr><td>Row 3</td></tr>
</tbody>
<tbody>
<tr><td>Row 4</td></tr>
</tbody>
<tbody>
<tr><td>Row 5</td></tr>
</tbody>
</table>
If the number of rows in your html markup are the same in each tbody you can use following CSS solution:
#my-table tbody:nth-child(odd)
{
background-color: red;
}
#my-table tbody:nth-child(even)
{
background-color: yellow;
}
<table id="my-table">
<tbody>
<tr>
<td>Row 1</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Row 2</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Row 3</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Row 4</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Row 5</td>
</tr>
</tbody>
</table>
Otherwise you can consider using CSS3 :nth-child() Selector, example nth-child(2) to select an element at a specific index.
Alternatively you can use JavaScript, answer from #Paulie_D will solve your issue.
#tb1 tr:nth-child(1){
background-color: green;
}
#tb1 tr:nth-child(2){
background-color: red;
}
#tb2 tr:nth-child(1){
background-color: green;
}
#tb3 tr:nth-child(1){
background-color: red;
}
#tb3 tr:nth-child(2){
background-color: green;
}
#tb3 tr:nth-child(3){
background-color: red;
}
#tb4 tr:nth-child(1){
background-color: green;
}
#tb5 tr:nth-child(1){
background-color: red;
}
<table id="my-table">
<tbody id="tb1">
<tr>
<td>Row 1a</td>
</tr>
<tr>
<td>Row 1b</td>
</tr>
</tbody>
<tbody id="tb2">
<tr>
<td>Row 2a</td>
</tr>
</tbody>
<tbody id="tb3">
<tr>
<td>Row 3a</td>
</tr>
<tr>
<td>Row 3b</td>
</tr>
<tr>
<td>Row 3c</td>
</tr>
</tbody>
<tbody id="tb4">
<tr>
<td>Row 4</td>
</tr>
</tbody>
<tbody id="tb5">
<tr>
<td>Row 5a</td>
</tr>
</tbody>
</table>
This question already has answers here:
Alternate table row color using CSS?
(11 answers)
Closed 6 years ago.
I have a table with class table. The only styling so far is the table th. I'd like to use a CSS value to alternate between white and silver for the rows, and hover silver for the entire row. Does anyone have the code for that?
<table class='table'>
<tr>
<th>heading</th>
<th>heading 2</th>
<th>heading 3</th>
</tr>
<tr class='table'>
<td>col 1</td>
<td>col 2</td>
<td>col 3</td>
</tr>
<tr class='table'>
<td>col 1</td>
<td>col 2</td>
<td>col 3</td>
</tr>
<tr class='table'>
<td>col 1</td>
<td>col 2</td>
<td>col 3</td>
</tr>
</table>
That is the html example (as it's written in php)
CSS
.table {
background-color: #FFFFFF;
color: #000000;
}
.table th {
background-color: #333333;
color: #FFFFFF;
}
That's it so far. Looking for the values to use for I'm guessing the table tr css.
Saying different because even/odd doesn't work & it's dynamic php not strict html.
If you've already set the background color of your table to white, you just need to set the alternate row and hover backgrounds, like so:
.table tr {
transition: background 0.2s ease-in;
}
.table tr:nth-child(odd) {
background: silver;
}
.table tr:hover {
background: silver;
cursor: pointer;
}
Additionally, you probably don't need to repeat the table class on each row, FWIW. You can just target those rows using .table tr as I have done. If you're trying to make sure the table header and body styles don't interfere with each other, it's more semantic and just cleaner to wrap those elements in a thead and tbody:
<table class='table'>
<thead>
<tr>
<th>heading</th>
<th>heading 2</th>
<th>heading 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>col 1</td>
<td>col 2</td>
<td>col 3</td>
</tr>
<tr>
<td>col 1</td>
<td>col 2</td>
<td>col 3</td>
</tr>
<tr>
<td>col 1</td>
<td>col 2</td>
<td>col 3</td>
</tr>
</tbody>
</table>
You can achieve this with a little bit css, just use the n-th child selector, like this:
HTML:
<table class="alternate">
<tr>
<td>Row Col 1 </td>
<td>Row Col 2 </td>
</tr>
<tr>
<td>Row Col 1 </td>
<td>Row Col 2 </td>
</tr>
<tr>
<td>Row Col 1 </td>
<td>Row Col 2 </td>
</tr>
<tr>
<td>Row Col 1 </td>
<td>Row Col 2 </td>
</tr>
</table>
CSS:
.alternate tr:nth-child(2n) {
background-color: silver;
}
.alternate tr {
background-color: white;
}
.alternate tr:nth-child(2n):hover, .alternate tr:hover {
background-color: grey;
}
And here is a working fiddle, I hope that is what you were looking for.