HTML table column styling using CSS - html

I have the following HTML table and I need a different styling in the last column:
https://jsfiddle.net/dqa9y1g3/
table {
border-collapse: collapse;
border-right: 0px;
border: 1px solid;
}
td {
border: 1px solid;
}
thead {
border: 2px;
}
th.th {
border: solid;
}
th.th3 {
border-top: 1px dashed;
font-weight: normal;
}
<table>
<thead>
<tr>
<th class="th">h1</th>
<th class="th">h2</th>
<th class="th3">h3</th>
</tr>
</thead>
<tr>
<td>r1c1</td>
<td>r1c2</td>
<td>r1c3</td>
</tr>
<tr>
<td>r2c1</td>
<td>r2c2</td>
<td>r2c3</td>
</tr>
</table>
What is the right way to do it?
This is what I want to get:

If you specifically need to target the last column, you can use this.
You also need to remove the table border, because it will be drawn over the individual th or td border.
table {
border-collapse: collapse;
}
td {
border: 1px solid;
}
thead {
border: 2px;
}
th.th {
border: solid;
}
tr th:last-child,
tr td:last-child {
border: 1px dashed #000;
border-right: 0;
}
<table>
<thead>
<tr>
<th class="th">h1</th>
<th class="th">h2</th>
<th class="th">h3</th>
</tr>
</thead>
<tr>
<td>r1c1</td>
<td>r1c2</td>
<td>r1c3</td>
</tr>
<tr>
<td>r2c1</td>
<td>r2c2</td>
<td>r2c3</td>
</tr>
</table>

You can use the :last-child pseudo class selector. This works because those tds of the last table column are the last child elements of their trs.
I also had to remove the border of the table.
https://developer.mozilla.org/en-US/docs/Web/CSS/:last-child
table {
border-collapse: collapse;
border-right: 0px;
/* border: 1px solid; */
}
td {
border: 1px solid;
}
thead {
border: 2px;
}
th.th {
border: solid;
}
th.th3 {
font-weight: normal;
}
td:last-child, th:last-child {
border: 1px dashed;
border-right: none;
}
<table>
<thead>
<tr>
<th class="th">h1</th>
<th class="th">h2</th>
<th class="th3">h3</th>
</tr>
</thead>
<tr>
<td>r1c1</td>
<td>r1c2</td>
<td>r1c3</td>
</tr>
<tr>
<td>r2c1</td>
<td>r2c2</td>
<td>r2c3</td>
</tr>
</table>

One alternative approach to using a <table> is to deploy CSS Grid.
Working Example:
.my-table {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr;
width: 300px;
height: 180px;
font-size: 24px;
gap: 1px;
}
.my-table-cell {
text-align: center;
line-height: 60px;
outline: 1px solid rgb(0, 0, 0);
}
/* FIRST TWO CELLS */
.my-table-cell:nth-of-type(-n + 2) {
font-weight: 900;
border: 1px solid rgb(0, 0, 0);
}
/* THIRD CELL */
.my-table-cell:nth-of-type(3) {
border-top: 1px dashed rgb(0, 0, 0);
}
/* CELLS ON THE RIGHT */
.my-table-cell:nth-of-type(3n) {
border-bottom: 1px dashed rgb(0, 0, 0);
outline: none;
}
<div class="my-table">
<div class="my-table-cell">h1</div>
<div class="my-table-cell">h2</div>
<div class="my-table-cell">h3</div>
<div class="my-table-cell">r1c1</div>
<div class="my-table-cell">r1c2</div>
<div class="my-table-cell">r1c3</div>
<div class="my-table-cell">r2c1</div>
<div class="my-table-cell">r2c2</div>
<div class="my-table-cell">r2c3</div>
</div>
Further Reading:
https://cssgridgarden.com/
https://css-tricks.com/snippets/css/complete-guide-grid/

Related

CSS Table with custom borders

I'm stuck with a simple table design as i'm from backend I need a table like this
What I have right now
table {
border-collapse: collapse;
width: 100%;
}
tr {
border: none;
}
.first{
width: 40%;
font-family: Quasimoda;
font-size: 0.75em;
color: #1D1D1E;
}
.middle{
width: 60%;
font-family: Quasimoda;
font-size: 0.75em;
color: #1D1D1E;
}
.last{
width: 40%;
font-family: Quasimoda;
font-size: 0.75em;
color: #1D1D1E;
}
td {
border-bottom: 1px solid #ddd;
border-top: 1px solid #ddd;
padding: 10px;
}
<table>
<tr>
<td class="University">January</td>
<td class="middle ">the progress bar will be placed here </td>
<td class="last">870</td>
</tr>
<tr>
<td class="first">January</td>
<td class="middle ">the progress bar will be placed here </td>
<td class="last">560</td>
</tr>
</table>
Please help. I have tried with multiple html table attributes all gone in vain.
Set the thead tr element bottom border, and the right border of the .first element:
table {
border-collapse: collapse;
width: 100%;
}
th {
text-align: left;
border-bottom: 1px solid #ddd;
}
td {
padding: 10px;
font-family: Quasimoda;
font-size: 0.75em;
color: #1D1D1E;
}
.first {
border-right: 1px solid #ddd;
}
.middle {
width: 100%;
}
<table>
<thead>
<tr>
<th colspan="3">Unique values:</th>
</tr>
</thead>
<tbody>
<tr>
<td class="first">January</td>
<td class="middle ">the progress bar will be placed here </td>
<td class="last">870</td>
</tr>
<tr>
<td class="first">January</td>
<td class="middle ">the progress bar will be placed here </td>
<td class="last">560</td>
</tr>
</tbody>
</table>
Actually, your picture is not that clear.
I was not clear where you want border actually.
But I try to do this, Run the code snippet and have a look, and let me know if this works for you or not.
.colspan2border
{
border-collapse: collapse;
border-bottom: 1px solid red;
}
.rightBorder
{
border-collapse: collapse;
border-right: 1px solid red;
}
.pr-left1
{
padding-left: 1rem;
}
table
{
border-collapse: collapse;
}
tr
{
border: none;
}
<table>
<tr colspan="2" class='colspan2border'><td>Unique values:</td></tr>
<tr>
<td class="rightBorder pr-left1">University</td>
<td>870</td>
</tr>
<tr>
<td class="rightBorder pr-left1">Custom</td>
<td>560</td>
</tr>
</table>

Border trouble with CSS/HTML; CSS applying table but not borders

I'm applying an external CSS to an HTML project to include borders in
formatting for a table. Everything is applying to my table except the
borders no matter what.
I have tried applying table {}, as well as table, th, td {}
table,
th,
td {
border-collapse: collapse;
border: 2px #022D41;
background-color: #DAECF3;
text-align: center;
margin: auto;
table {
border-collapse: seperate;
width: 2;
background-color: "#1AA6B7";
border: 2px "#FE424D";
text-align: center;
margin: auto;
/*aligning table to the center*/
}
th {
border: 3px "#FE424D";
background-color: "#022D41";
color: #DAECF3;
}
td {
border: 3px "#FE424D";
}
<table border="4">
<tr>
<th>Company</th>
<th>Location</th>
<th>Dates Employed</th>
<th>Title/Duties</th>
</tr>
<tr>
<td>Mercury Systems</td>
<td>Hudson, NH</td>
<td>May 20, 2019 - <br>Current</td>
<td>Continous<br> Improvement<br> Intern</td>
</tr>
<tr>
<td>Manchester<br> Public Schools</td>
<td>Manchester, NH</td>
<td>January 2017 - <br>August 2018</td>
<td>Para-Professional</td>
</tr>
<tr>
<td>Penobscot<br>Indian Island</td>
<td>Old Town, ME</td>
<td>November 2015 - <br>January 2017</td>
<td>Youth Program<br>Coordinator</td>
</tr>
</table>
Trying to do a dotted/solid border around and between the table.
You must add the style of the border as the other person already said, but the order it's {size} {style} {color}.
The two main reasons your code isn't working are: You've forgotten to close the first table rule and the order of the arguments for the border rule.
Eg: border: 2px solid #FFFFFF. And you must not use the color as "#FFFFFF" (remove the quotes mark).
table,
th,
td {
border-collapse: collapse;
border: 2px solid #022D41;/* add the border style (solid) */
background-color: #DAECF3;
text-align: center;
margin: auto;
} /* You've forgot to close this rule */
table {
border-collapse: seperate;
width: 2;
background-color: #1AA6B7; /* remove the "" */
border: 2px solid #FE424D; /* remove the "" and add the border-style */
text-align: center;
margin: auto; /*aligning table to the center*/
}
th {
border: 3px solid #FE424D; /* remove the "" and add the border-style */
background-color: "#022D41"; /* remove the "" */
color: #DAECF3; /* you're using the same backgorund-color as the text color */
color: #000;
}
td {
border: 3px solid #FE424D; /* add the border style and remove the "" */
}
<table border="4">
<tr>
<th>Company</th>
<th>Location</th>
<th>Dates Employed</th>
<th>Title/Duties</th>
</tr>
<tr>
<td>Mercury Systems</td>
<td>Hudson, NH</td>
<td>May 20, 2019 - <br>Current</td>
<td>Continous<br> Improvement<br> Intern</td>
</tr>
<tr>
<td>Manchester<br> Public Schools</td>
<td>Manchester, NH</td>
<td>January 2017 - <br>August 2018</td>
<td>Para-Professional</td>
</tr>
<tr>
<td>Penobscot<br>Indian Island</td>
<td>Old Town, ME</td>
<td>November 2015 - <br>January 2017</td>
<td>Youth Program<br>Coordinator</td>
</tr>
</table>
https://developer.mozilla.org/en-US/docs/Web/CSS/border
I believe you need to add solid or dotted to the border property to make it appear. In your case, you would need:
border: solid 2px "#FE424D";
for a solid border, or
border: dotted 2px "#FE424D";
for a dotted one.
Example:
.table_dark {
font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
font-size: 14px;
width: 640px;
text-align: left;
border-collapse: collapse;
background: #311B92;
border: 7px solid red;
}
.table_dark th {
color: #EDB749;
border-bottom: 1px dotted #37B5A5;
border-right: 1px dotted #37B5A5;
padding: 12px 17px;
}
.table_dark td {
color: #CAD4D6;
border-bottom: 1px dotted #37B5A5;
border-right: 1px dotted #37B5A5;
padding: 7px 17px;
}
.table_dark tr:last-child td {
border-bottom: none;
}
.table_dark td:last-child {
border-right: none;
}
.table_dark tr:hover td {
text-decoration: underline;
}
<table class="table_dark">
<tr>
<th>Company</th>
<th>Location</th>
<th>Dates Employed</th>
<th>Title/Duties</th>
</tr>
<tr>
<td>Mercury Systems</td>
<td>Hudson, NH</td>
<td>May 20, 2019 - <br>Current</td>
<td>Continous<br> Improvement<br> Intern</td>
</tr>
<tr>
<td>Manchester<br> Public Schools</td>
<td>Manchester, NH</td>
<td>January 2017 - <br>August 2018</td>
<td>Para-Professional</td>
</tr>
<tr>
<td>Penobscot<br>Indian Island</td>
<td>Old Town, ME</td>
<td>November 2015 - <br>January 2017</td>
<td>Youth Program<br>Coordinator</td>
</tr>
</table>

How to color all 4 sides of a TD cell border via CSS in an HTML table?

I have this code where I am trying to color all 4 sides of a TD cell with red, but if you run the code, only the bottom and the right border are getting color (in Mozilla Firefox). Is there a way to color all 4 borders?
#selections_table table {
border-collapse: collapse;
}
#selections_table td,
th {
border: 1px solid black;
padding: 3px 4px 3px 4px;
}
<div id="selections_table">
<table>
<tbody>
<tr>
<th>#</th>
<th>Model</th>
</tr>
<tr>
<td>1</td>
<td style="border-color:red">XXX-8</td>
</tr>
</tbody>
</table>
</div>
This question/answer does not help: CSS Border declare 4 sides, color, width, in one line
If there is a way to style it via a class, that will be better than using a an inline style command.
Change your inline style to style="border:1px double red;":
<div id="selections_table">
<table>
<tbody>
<tr>
<th>#</th>
<th>Model</th>
</tr>
<tr>
<td>1</td>
<td style="border:1px double red;">XXX-8</td>
</tr>
</tbody>
</table>
</div>
A little trick, create an ::after to the td you want to add the border.
#selections_table table {
border-collapse: collapse;
}
#selections_table td,
th {
border: 1px solid black;
padding: 3px 4px 3px 4px;
position:relative;
}
#selections_table td.border-red::after{
content: "";
position: absolute;
top: -1px;
left: -1px;
right: -1px;
bottom: -1px;
border: 1px solid red;
}
<div id="selections_table">
<table>
<tbody>
<tr>
<th>#</th>
<th>Model</th>
</tr>
<tr>
<td>1</td>
<td class="border-red">XXX-8</td>
</tr>
</tbody>
</table>
</div>
https://jsfiddle.net/ym82a0k7/
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid red;
}
</style>
</head>
<body>
<div id="selections_table">
<table>
<tbody>
<tr>
<th>#</th>
<th>Model</th>
</tr>
<tr>
<td>1</td>
<td>XXX-8</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
First, you don't need to declare td here:
#selections_table td,th {
border: 1px solid black;
padding: 3px 4px 3px 4px;
}
So,
#selections_table th {
border: 1px solid black;
border-bottom: 1px solid red;
padding: 3px 4px 3px 4px;
}
td {
border: 1px solid red;
}
This is another option (by increasing the pixel, it is not a good practice but another option)
#selections_table th {
border: 1px solid black;
padding: 3px 4px 3px 4px;
}
td {
border: 2px solid red;
}
or just like this:
#selections_table th {
border: 1px solid black;
padding: 3px 4px 3px 4px;
}
td {
border: 1px double red;
}

Border between two table cells in HTML/CSS

I am rewriting existing servlet to make it look better. The tables are very complex and I thought it would look nicer if there is no border between two cells. I failed to define such CSS rule. Then I tried to change cell border color. I have failed again. There are probably some CSS rule priority issues that I cannot handle.
This is what I get on current Chrome and what I want to achieve:
Minimum reproducible code is there: http://jsbin.com/rokabaliti
<html>
<head><style>
table { border-collapse: collapse;}
table, th, td { border: 1px solid black;}
th, td {
padding: 5px;
text-align: left; vertical-align: top;
}
tr.all { background-color: palegreen; }
tr.other { background-color: beige; }
td.chain { border: 1px solid red; }
td.target { border-left: none; }
</style></head>
<body>
<table class='rule'>
<tr class="all"><td>XX</td></tr>
<tr class="other">
<td>YY</td>
<td class='target'>ZZ</td></tr>
<tr class="other">
<td>AA</td>
<td class='chain'>BB</td>
</tr>
</table>
</body>
</html>
Setting border on td makes border all around on td and border-collapse: collapse; just overlap two borders it doesn't remove border, so you need to remove border of both columns.
.other td:first-child{
border-right:0;
}
.other td:last-child{
border-left:0;
}
Layout 1
table {
border-collapse: collapse;
}
table,
th,
td {
border: 1px solid black;
}
th,
td {
padding: 5px;
text-align: left;
vertical-align: top;
}
tr.all {
background-color: palegreen;
}
tr.other {
background-color: beige;
}
.other td:first-child{
border-right:0;
}
.other td:last-child{
border:1px solid red;
}
<table class='rule'>
<tr class="all">
<td>XX</td>
</tr>
<tr class="other">
<td>YY</td>
<td class='target'>ZZ</td>
</tr>
<tr class="other">
<td>AA</td>
<td class='chain'>BB</td>
</tr>
</table>
Layout 2
table {
border-collapse: collapse;
}
table,
th,
td {
border: 1px solid black;
}
th,
td {
padding: 5px;
text-align: left;
vertical-align: top;
}
tr.all {
background-color: palegreen;
}
tr.other {
background-color: beige;
}
.other td:first-child{
border-right:0;
}
.other td:last-child{
border-left:0;
}
<table class='rule'>
<tr class="all">
<td>XX</td>
</tr>
<tr class="other">
<td>YY</td>
<td class='target'>ZZ</td>
</tr>
<tr class="other">
<td>AA</td>
<td class='chain'>BB</td>
</tr>
</table>
The border-collapse CSS property determines whether a table's borders
are separated or collapsed. In the separated model, adjacent cells
each have their own distinct borders. In the collapsed model, adjacent
table cells share borders.
MDN
Yo can try,
table { border-collapse: collapse;}
table, th, td { border-left: 1px solid black; border-bottom: 1px solid black;}
th, td {
padding: 5px;
text-align: left; vertical-align: top;
}
tr.all { background-color: palegreen; }
tr.all td { border: 1px solid black; }
tr.other { background-color: beige; }
td.chain { border: 1px solid red; }
td.target { border: 1px solid red; }
Variant #1: https://jsfiddle.net/a7p2dp3o/
<style>
table { border-collapse: collapse;}
table, th, td { border: 1px solid black;}
th, td {
padding: 5px;
text-align: left; vertical-align: top;
}
tr.all { background-color: palegreen; }
tr.other { background-color: beige; }
td.chain { border-left: 0px; }
td.target { border-left: 0px; }
</style>
<table class="rule">
<tbody><tr class="all"><td>XX</td></tr>
<tr class="other">
<td style="border-right: 0px;">YY</td>
<td class="target">ZZ</td></tr>
<tr class="other">
<td style="border-right: 0px;">AA</td>
<td class="chain">BB</td>
</tr>
</tbody>
</table>
Variant #2: https://jsfiddle.net/3u2cno6f/1/
<style>
table { border-collapse: collapse;}
table, th, td { border: 1px solid black;}
th, td {
padding: 5px;
text-align: left; vertical-align: top;
}
tr.all { background-color: palegreen; }
tr.other { background-color: beige; }
td.chain { border-color: red; }
td.target { border-color: red; }
</style>
<table class="rule">
<tbody><tr class="all"><td>XX</td></tr>
<tr class="other">
<td style="border-right: 0px;">YY</td>
<td class="target">ZZ</td></tr>
<tr class="other">
<td style="border-right: 0px;">AA</td>
<td class="chain">BB</td>
</tr>
</tbody>
</table>
Expected result 1
table {
border-collapse: collapse;
}
table,
th,
td {
border: 1px solid black;
}
th,
td {
padding: 5px;
text-align: left;
vertical-align: top;
}
tr.all {
background-color: palegreen;
}
tr.other {
background-color: beige;
}
td.chain {
border: 1px solid red;
}
td.target {
}
.other>td:first-child{
border-right: 1px solid red;
}
.other>td:last-child{
border: 1px solid red;
}
<table class='rule'>
<tr class="all">
<td>XX</td>
</tr>
<tr class="other">
<td>YY</td>
<td class='target'>ZZ</td>
</tr>
<tr class="other">
<td>AA</td>
<td class='chain'>BB</td>
</tr>
</table>
Expected result 2
<table class='rule'>
<tr class="all">
<td>XX</td>
</tr>
<tr class="other">
<td>YY</td>
<td class='target'>ZZ</td>
</tr>
<tr class="other">
<td>AA</td>
<td class='chain'>BB</td>
</tr>
</table>
table {
border-collapse: collapse;
}
table,
th,
td {
border: 1px solid black;
}
th,
td {
padding: 5px;
text-align: left;
vertical-align: top;
}
tr.all {
background-color: palegreen;
}
tr.other {
background-color: beige;
}
.other>td:not(.target) {
border: none;
}
.target{
border-left:none;
border-bottom:none;
}

How to separate row in table using border

I have a table in which I have to separate a row using border as in image below.
As you can see, Border separator is having a space left-right side and not fully touched to table border.
I tried giving padding,margin but nothing worked.
tr {
border-bottom: 1px solid blue;
padding: 10px; // not working
margin: 10px; // not working
}
https://jsfiddle.net/alpeshprajapati/s934Lpbx/
What is the way to achieve this?
CSS
table {
border: 1px solid black;
border-collapse: collapse;
}
thead {
background: black;
color: white;
}
th {
width: 100px;
}
tr {
float: left;
border-bottom: 1px solid blue;
width: 90%;
margin: 0 10px;
}
td{
width: 32%;
float: left;
}
Try this:
table {
border: 1px solid black;
border-collapse: collapse;
}
thead {
background: black;
color: white;
}
th {
width: 100px;
}
tr {
// border-bottom: 1px solid blue;
}
td{
padding:5px 10px;
}
.border{
background:skyblue;
width:100%;
height:2px;
}
<table>
<thead>
<th>Th1</th>
<th>Th2</th>
<th>Th3</th>
</thead>
<tbody>
<tr>
<td>TD1</td>
<td>TD2</td>
<td>TD3</td>
</tr>
<tr>
<td colspan="3">
<div class="border"></div>
</td>
</tr>
<tr>
<td>TD1</td>
<td>TD2</td>
<td>TD3</td>
</tr>
<tr>
<td colspan="3">
<div class="border"></div>
</td>
</tr>
<tr>
<td>TD1</td>
<td>TD2</td>
<td>TD3</td>
</tr>
</tbody>
</table>
To increase the length of the border you have to increase the width of the div that is containing it.