Highlight rows across two adjacent tables with CSS - html

I have a layout with two adjacent tables. When a row in one table is hovered, I'd like to apply the same style to the cells in the rows across both tables.
Is this possible with css only?
table{
display: inline-block;
}
td{
border:1px solid grey;
}
table#lhs tr:hover{
background-color:green;
}
table#lhs tr:hover {
background-color:green;
}
<table id="lhs">
<tr>
<td>lhs row 1</td>
</tr>
<tr>
<td>lhs row 2</td>
</tr>
<tr>
<td>lhs row 3</td>
</tr>
</table>
<table id="rhs">
<tr>
<td>rhs row 1</td>
</tr>
<tr>
<td>rhs row 2</td>
</tr>
<tr>
<td>rhs row 3</td>
</tr>
</table>

I came up with this, which seems pretty close to what you were asking for:
table {
display: inline-block;
}
div {
overflow:hidden;
float:left;
}
td {
border:1px solid grey;
}
table#lhs tr:hover {
background-color:green;
}
table#lhs tr:hover {
background-color:green;
}
#lhs td:hover::before {
background-color: green;
content:'\00a0';
position: absolute;
width: 10000px;
z-index: -1;
}
td {
position: relative;
}
<div>
<table id="lhs">
<tr>
<td>lhs row 1</td>
</tr>
<tr>
<td>lhs row 2</td>
</tr>
<tr>
<td>lhs row 3</td>
</tr>
</table>
<table id="rhs">
<tr>
<td>rhs row 1</td>
</tr>
<tr>
<td>rhs row 2</td>
</tr>
<tr>
<td>rhs row 3</td>
</tr>
</table>
</div>

Related

Two rows of a table with different number of cells, but equal width?

I am trying to make a table with two rows. Each row has a different number of cells. Each cell on both rows should be equal in width, and each row should be equal width as well. Hopefully I've explained that correctly.
Here is what I've done so far: -
HTML: -
<table class="top-table">
<tr>
<td width="14%">2007-2016</td>
<td width="14%">2017</td>
<td width="14%">2018</td>
<td width="14%">2019</td>
<td width="14%">2020</td>
<td width="14%">2021</td>
<td width="14%">2022</td>
</tr>
</table>
<table class="bottom-table">
<tr>
<td width="11%">Event 1</td>
<td width="11%">Event 2</td>
<td width="11%">Event 3</td>
<td width="11%">Event 4</td>
<td width="11%">Event 5</td>
<td width="11%">Event 6</td>
<td width="11%">Event 7</td>
<td width="11%">Event 8</td>
<td width="11%">Event 9</td>
</tr>
</table>
CSS: -
td {
border: 1px solid;
border-color: blue;
}
.top-table {
width: 100%;
}
.bottom-table {
width: 100%;
}
I've tried doing it with one table, but couldn't figure it out, which is why I made two separate tables instead. Surely there should be something easier? The nearest I could find it this fiddle, but that's using divs.
td {
border: 1px solid;
border-color: blue;
}
.top-table {
width: 100%;
}
.bottom-table {
width: 100%;
}
<table class="top-table">
<tr>
<td width="14%">2007-2016</td>
<td width="14%">2017</td>
<td width="14%">2018</td>
<td width="14%">2019</td>
<td width="14%">2020</td>
<td width="14%">2021</td>
<td width="14%">2022</td>
</tr>
</table>
<table class="bottom-table">
<tr>
<td width="11%">Event 1</td>
<td width="11%">Event 2</td>
<td width="11%">Event 3</td>
<td width="11%">Event 4</td>
<td width="11%">Event 5</td>
<td width="11%">Event 6</td>
<td width="11%">Event 7</td>
<td width="11%">Event 8</td>
<td width="11%">Event 9</td>
</tr>
</table>
Does this work for you?
td {
border: 1px solid;
border-color: blue;
}
.top-table {
width: 100%;
}
.bottom-table {
width: 100%;
}
<table class="top-table">
<tr>
<td width="14%">2007-2016</td>
<td width="14%">2017</td>
<td width="14%">2018</td>
<td width="14%">2019</td>
<td width="14%">2020</td>
<td width="14%">2021</td>
<td width="14%">2022</td>
</tr>
<tr>
<td width="11%">Event 1</td>
<td width="11%">Event 2</td>
<td width="11%">Event 3</td>
<td width="11%">Event 4</td>
<td width="11%">Event 5</td>
<td width="11%">Event 6</td>
<td width="11%">Event 7</td>
<td width="11%">Event 8</td>
<td width="11%">Event 9</td>
</tr>
</table>
There is another way to do this with a table, by using the flexbox. It is important to insert the <tbody> because some browsers will insert it if it is missing. For the correct flex table the <tbody> is required.
Each row can have a different number of cells. By adjusting the percentage .td-7 (100% : 7) and .td-9 (100% : 9) you will get the correct width of the cells per row.
.ftable, .ftable tr {
display: flex;
flex-flow: row nowrap;
width: 100%;
text-align: left;
box-sizing: border-box;
}
.ftable tbody, .ftable td {
display: block;
flex: 0 0 100%;
width: 100%;
box-sizing: border-box;
}
.ftable td {
vertical-align: top;
text-align: inherit;
text-align: -webkit-match-parent;
}
tr.td-7 td {
max-width: 14.285%;
}
tr.td-9 td {
max-width: 11.111%;
}
/** example borders **/
.ftable {
border-top: 1px solid silver;
border-left: 1px solid silver;
}
.ftable td {
border-right: 1px solid silver;
border-bottom: 1px solid silver;
font: normal 14px/1.3 sans-serif;
padding: 2px 5px 4px 5px;
}
<table class="ftable">
<tbody>
<tr class="top-table td-7">
<td>2007-2016</td>
<td>2017</td>
<td>2018</td>
<td>2019</td>
<td>2020</td>
<td>2021</td>
<td>2022</td>
</tr>
<tr class="bottom-table td-9">
<td>Event 1</td>
<td>Event 2</td>
<td>Event 3</td>
<td>Event 4</td>
<td>Event 5</td>
<td>Event 6</td>
<td>Event 7</td>
<td>Event 8</td>
<td>Event 9</td>
</tr>
<tbody>
</table>
I think I've figured it out. This allows me to add an extra "event" cell, and that row will still span the above "date" row. The only thing is the size of the first date cell - I'm not sure why it's wider than the rest.
body {
font-family: 'Open Sans', sans-serif;
text-align: center;
line-height: 1.5;
margin: auto;
padding: 0;
}
th {
background-color: #B3B3B3;
border: 2px solid;
}
td {
border: 1px solid;
padding: 10px;
text-align: center;
}
.nav-date,
.nav-event {
margin-left: auto;
margin-right: auto;
width: 75%;
}
<table class="nav-date">
<tr>
<th>2007-2016</th>
<th>2017</th>
<th>2018</th>
<th>2019</th>
<th>2020</th>
<th>2021</th>
<th>2022</th>
</tr>
</table>
<table class="nav-event">
<tr>
<td>Event 1</td>
<td>Event 2</td>
<td>Event 3</td>
<td>Event 4</td>
</tr>
</table>

How do i make a table in html with first row having 1 column only and other rows having more than 1

I have tried to make this work but it always adds an extra column in the first row even when i clearly stated it has only 1 column. What i want to make is like this : this is what i want to make
But this is what i get like this
The only way to make it like the first picture is by using 2 tables which is what i used but is there no way to do it with 1 table ?
My code :my code for the second picture
Use the colspan attribute.
td {
border: solid black 1px;
height: 20px;
}
<table>
<tr>
<td colspan="3">colspan = 3</td>
</tr>
<tr>
<td>colspan = 1</td>
<td>colspan = 1</td>
<td>colspan = 1</td>
</tr>
<tr>
<td colspan="3">colspan = 3</td>
</tr>
</table>
You can use the colspan attribute.
The colspan attribute in HTML specifies the number of columns a cell should span. It allows the single table cell to span the width of more than one cell or column.
Below is a working code snippet which looks almost similar to your requirement.
table, tr, td, th {
border: 1px solid black;
border-collapse: collapse;
}
table {
width: 100%;
}
.row1, .row2 {
height: 100px;
}
.row2 {
vertical-align: top;
}
.row1, .row3 {
text-align: center;
}
<table>
<tr class="row1">
<td colspan="3">Your Name</td>
</tr>
<tr class="row2">
<td>Course 1</td>
<td>Course 2</td>
<td>Course 3</td>
</tr>
<tr colspan="3" class="row3">
<td colspan="3">Social Media accounts</td>
</tr>
</table>

Make 1 table look like 2 on top of each other

I want to make 1 table to look like 2 tables where they are on top of each other with a little space between them. Each table has the same number of columns, but the text they contain can differ. And each table can contain many rows. I need this because i need columns of both tables always to be the same width. How do i achieve this? I need that Empty row's side borders to hide
<table>
<tr> <!-- First table rows --> </tr>
<td>text</td>
<td>text</td>
<tr> <!-- Empty space between tables --> </tr>
<tr> <!-- Second table rows --> </tr>
<td>text</td>
<td>text</td>
Just use a single <td> element with a specific height as a separator, and use border-collapse to mimic what you're looking for:
table {
border-collapse: collapse;
}
table td {
border: 1px solid #000;
}
table td.separator {
border: none;
height: 40px;
}
<table>
<tr>
<td>text</td>
<td>text</td>
</tr>
<tr>
<td>text</td>
<td>text</td>
</tr>
<tr>
<td colspan="2" class="separator"></td>
</tr>
<tr>
<td>text</td>
<td>text</td>
</tr>
<tr>
<td>text</td>
<td>text</td>
</tr>
</table>
You can use css for this. border-spacing
Change 45px for sizing
table {
border-collapse: collapse;
}
th {
background-color: red;
Color:white;
}
th, td {
width:150px;
text-align:center;
border:1px solid black;
padding:5px
}
.geeks {
border-right:hidden;
}
.gfg {
border-collapse:separate;
border-spacing:0 45px;
}
h1 {
color:green;
}
<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
<center>
<h2>Row spacing in a table</h2>
<table>
<tr>
<th>Employee ID</th>
<th>Name</th>
<th>Gender</th>
<th>Age</th>
</tr>
</table>
<table class = "gfg">
<tr>
<td class = "geeks">10001</td>
<td>Thomas</td>
<td>M</td>
<td>32</td>
</tr>
<tr>
<td class = "geeks">10002</td>
<td>Sally</td>
<td>F</td>
<td>28</td>
</tr>
<tr>
<td class = "geeks">10003</td>
<td>Anthony</td>
<td>M</td>
<td>24</td>
</tr>
</table>
</center>
</body>
</html>
You could use something like shown below. The colspan="4" on table-spacing td specifies how many columns the cell should span.
Advice
However if the data is really different from each other I would recommend actually using two different tables instead of two . It make it easier for screen readers to distinct the data from each other. To improve this further you can use table headers to improve your accessibility even more.
Source: https://www.w3schools.com/tags/tag_thead.asp
.table {
border-collapse: collapse;
}
.table-spacing td {
border: none;
height: 15px; /* Increase/descrease for white-space between 'tables' */
}
td {
padding: 6px;
border: 1px solid black;
}
<table class="table">
<tr>
<td> Cel 1</td>
<td> Cel 2</td>
<td> Cel 3</td>
<td> Cel 4</td>
<tr>
<tr>
<td> Cel 5</td>
<td> Cel 6</td>
<td> Cel 7</td>
<td> Cel 8</td>
<tr>
<tr class="table-spacing"><td colspan="4"></td></tr>
<tr>
<td> Cel 1</td>
<td> Cel 2</td>
<td> Cel 3</td>
<td> Cel 4</td>
<tr>
<tr>
<td> Cel 5</td>
<td> Cel 6</td>
<td> Cel 7</td>
<td> Cel 8</td>
<tr>
</table>

Adding continuous border to only header and footer of table

I would like a table where some cells have broken borders and others have continuous borders. For example, here is the html for a four column, 12 row table:
<table>
<thead>
<tr class="header">
<th id="blank_cell"></th> <!-- blank -->
<th>first_c</th>
<th>second_c</th>
<th>third_c</th>
</tr>
</thead>
<tbody>
<tr>
<th>row one</th>
<td>1,1</td>
<td>1,2</td>
<td>1,3</td>
</tr>
<tr>
<th>row two</th>
<td>2,1</td>
<td>2,2</td>
<td>2,3</td>
</tr>
<tr>
<th>row three</th>
<td>3,1,</td>
<td>3,2</td>
<td>3,3</td>
</tr>
<tr>
<th>row four</th>
<td>4,1</td>
<td>4,2</td>
<td>4,3</td>
</tr>
<tr>
<th>row five</th>
<td>5,1</td>
<td>5,2</td>
<td>5,3</td>
</tr>
<tr>
<th>row six</th>
<td>6,1</td>
<td>6,2</td>
<td>6,3</td>
</tr>
<tr>
<th>row seven</th>
<td>7,1</td>
<td>7,2</td>
<td>7,3</td>
</tr>
<tr>
<th>row eight</th>
<td>8,1</td>
<td>8,2</td>
<td>8,3</td>
</tr>
<tr>
<th>row nine</th>
<td>9,1</td>
<td>9,2</td>
<td>9,3</td>
</tr>
<tr>
<th>row ten</th>
<td>10,1</td>
<td>10,2</td>
<td>10,3</td>
</tr>
</tbody>
<tfoot>
<tr>
<th>End</th>
<td>e_one</td>
<td>e_two</td>
<td>e_three</td>
</tr>
</tfoot>
</table>
I would like the thead cells to have a continuous border along the bottom (except for the first, blank cell), whilst maintaining a broken border on the other rows.
Here is some CSS, it creates broken borders along the bottom of the rows (except the leftmost cells of each row).
body {
font-family:Arial,Verdana,sans-serif;
color:#111111;
}
table {
width:450px;
}
td,th {
padding:7px 10px 10px 10px;
}
thead th {
border-bottom:4px solid #111111;
}
tbody th {
border-left:2px solid #111111;
border-right:4px solid #111111;
}
tbody td {
border-bottom:2px solid #111111;
}
th {
text-transform:uppercase;
letter-spacing:0.1em;
word-spacing:0.3em;
text-align:left;
}
#blank_cell {
border:none;
}
tr:hover {
background-color:#c3e6e5;
}
I want the very first row - the header row - to have a continuous, unbroken line, whereas I want the others to remain as they are (i.e. broken). All I can find, so far, is styling that is applied to the whole table: for example, I can't seem to collapse borders or set zero spacing on just the cells in the thead part of the table. So if I make the borders continuous, it is applied to the entire table.
You'll probably have to put it in two different tables, the first containing the content of your tr.header, with cellspacing="0" and the second with the default value. However, that means that you'll have to add CSS to keep the width's consistent, and that may be a problem depending on what you are putting into the table(s). If that isn't a problem, here is the code with all columns set to 25% width:
(and a JSFiddle)
<body>
<table cellspacing="0">
<thead>
<tr class="header">
<th id="blank_cell"></th> <!-- blank -->
<th>first_c</th>
<th>second_c</th>
<th>third_c</th>
</tr>
</thead>
</table>
<table>
<tbody>
<tr>
<th>row one</th>
<td>1,1</td>
<td>1,2</td>
<td>1,3</td>
</tr>
<tr>
<th>row two</th>
<td>2,1</td>
<td>2,2</td>
<td>2,3</td>
</tr>
<tr>
<th>row three</th>
<td>3,1,</td>
<td>3,2</td>
<td>3,3</td>
</tr>
<tr>
<th>row four</th>
<td>4,1</td>
<td>4,2</td>
<td>4,3</td>
</tr>
<tr>
<th>row five</th>
<td>5,1</td>
<td>5,2</td>
<td>5,3</td>
</tr>
<tr>
<th>row six</th>
<td>6,1</td>
<td>6,2</td>
<td>6,3</td>
</tr>
<tr>
<th>row seven</th>
<td>7,1</td>
<td>7,2</td>
<td>7,3</td>
</tr>
<tr>
<th>row eight</th>
<td>8,1</td>
<td>8,2</td>
<td>8,3</td>
</tr>
<tr>
<th>row nine</th>
<td>9,1</td>
<td>9,2</td>
<td>9,3</td>
</tr>
<tr>
<th>row ten</th>
<td>10,1</td>
<td>10,2</td>
<td>10,3</td>
</tr>
</tbody>
<tfoot>
<tr>
<th>End</th>
<td>e_one</td>
<td>e_two</td>
<td>e_three</td>
</tr>
</tfoot>
</table>
</body>
body {
font-family:Arial,Verdana,sans-serif;
color:#111111;
}
table {
width:450px;
}
td,th {
padding:7px 10px 10px 10px;
}
thead th {
border-bottom:4px solid #111111;
}
tbody th {
border-left:2px solid #111111;
border-right:4px solid #111111;
}
tbody td {
border-bottom:2px solid #111111;
width: 25%;
}
th {
text-transform:uppercase;
letter-spacing:0.1em;
word-spacing:0.3em;
text-align:left;
width: 25%;
}
#blank_cell {
border:none;
}
tr:hover {
background-color:#c3e6e5;
}

How to create two table rows containing different columns?

I want to keep the same width of <tr> of the two rows.
The first <tr> contains two <td> the second row contains only one <td> but as it is now, the second row is not with same width as the first one (see my script pleas)
How to keep the first row as it is and make the second one 100 width ?
table{
width:100%;
}
table tr td{
width:100%;
border:1px solid black;
border-collapse: collapse;
}
.second-row{
width:100%;
}
<table>
<tr class="first-row">
<td>hello</td>
<td>world</td>
</tr>
<tr class="second-row">
<td>hello world</td>
</tr>
</table>
use colspan
table{
width:100%;
}
table tr td{
width:100%;
border:1px solid black;
border-collapse: collapse;
}
.second-row{
width:100%;
}
<table>
<tr class="first-row">
<td>hello</td>
<td>world</td>
</tr>
<tr class="second-row">
<td colspan="2">hello world</td>
</tr>
</table>