Half border for html table - html

I have a table in my web page that I want to only have half borders.
Example:
The table that I have:
A | B | C
| |
A | B | C
| |
A | B | C
Note:
td { padding-bottom: 5%; }
td#B { border-left: 1px solid black; border-right: 1px solid black}
/* Assuming alphabets A , B and C are ids of each td */
The table that I want:
A | B | C
A | B | C
A | B | C
How can I achieve this?
I am using angularjs' ng-repeat to display the data from a json database for the table.

You can either use an extra border on tr or border-spacing .
The difference can be seen while a border or background is applied to table :
table {
/* demo purpose mainly */
float:left;
margin:1em;
background:lightgray;
box-shadow: 0 0 0 2px green;;
}
.bdtr {
border-collapse:collapse;
}
td {padding:0 1em;}
td + td {
border-left:1px solid;
}
.bdtr tr + tr{
border-top:1em lightgray solid; /* use background-color */
}
.bdtd {
border-spacing:0 1em;
margin:0 1em;
}
<table class="bdtr">
<caption>border on tr</caption>
<tr>
<td>A</td><td>B</td><td>C</td>
</tr>
<tr>
<td>A</td><td>B</td><td>C</td>
</tr>
<tr>
<td>A</td><td>B</td><td>C</td>
</tr>
</table>
<table class="bdtd">
<caption> border-spacing</caption>
<tr>
<td>A</td><td>B</td><td>C</td>
</tr>
<tr>
<td>A</td><td>B</td><td>C</td>
</tr>
<tr>
<td>A</td><td>B</td><td>C</td>
</tr>
</table>
Also, you can use transparent border and shadow to allow a non plain background on table or behind it :
.bdtr {
border-collapse: collapse;
}
td {
padding: 0 1em;
}
.bdtr.bis {
background: linear-gradient(60deg, gray, yellow, purple, pink, lime);
}
.bdtr.bis tr+tr {
border-top: 1em transparent solid;
}
.bdtr.bis tr td+td {
border: none;/* reset from previous demo */
box-shadow: inset 2px 0;
}
<table class="bdtr bis">
<tr>
<td>A</td>
<td>B</td>
<td>C</td>
</tr>
<tr>
<td>A</td>
<td>B</td>
<td>C</td>
</tr>
<tr>
<td>A</td>
<td>B</td>
<td>C</td>
</tr>
</table>

A small table property can do the trick . Use this css in your table css:
table {
border-collapse: separate;
border-spacing: 0 2em;
}
Working snippet:
table {
border-collapse: separate;
border-spacing: 0 2em;
}
td {
border-right: 1px solid #000;
padding: 0 10px;
}
tr td:last-child {
border-right: 0px solid #000;
}
<table>
<tr>
<td>A</td>
<td>B</td>
<td>C</td>
</tr>
<tr>
<td>A</td>
<td>B</td>
<td>C</td>
</tr>
<tr>
<td>A</td>
<td>B</td>
<td>C</td>
</tr>
</table>

Instead of applying the style to the td try to apply it to the tr
And remove the padding on the td
tr {
margin: 10px 0;
display: block;
}

Related

Add specific border styling and spacing that differs for the table, tr and cells

I was wondering if it was possible, with html and/or CSS, to collapse only tr with he table's outline, while having different border styles for the table's outline and trs, and the tds and ths.
I know this is complicated, so if this can make it clearer, here's a drawing of what I'm trying to achieve:
No, border-collapse applies only to the whole table, and it is not a valid property for tr or td elements so you cannot apply it to those to get a different spacing.
However you can “fake” it by adding the cell content into a div and using it for some of the styling:
Apply the outer table styling to the table as normal
Apply the row styling to the top and bottom borders of the th / td cells
Apply the "cell" styling to the divs inside the th & tds.
Working Example:
table {
border: 6px solid lightgray;
border-right-color: gray;
border-bottom-color: gray;
border-collapse: collapse;
}
td {
border-top: 5px solid gray;
}
tr:not(:last-child) td{
border-bottom: 5px solid gray;
}
th .cell,
td .cell {
margin: 5px;
padding: 5px;
border: 2px ridge lightblue;
}
<table>
<tr>
<th><div class="cell">First Name</div></th>
<th><div class="cell">Last Name</div></th>
</tr>
<tr>
<td><div class="cell">John</div></td>
<td><div class="cell">Smith</div></td>
</tr>
<tr>
<td><div class="cell">Jane</div></td>
<td><div class="cell">Doe</div></td>
</tr>
</table>
Found a way by just adding an hr and a minuscule tr between both trs.
hr {
border: 4px outset rgb(207, 172, 179);
width: 443px;
position: absolute;
top: 388px;
left: 35px;
}
tr#mini {
border: none;
padding: 0px;
margin: 0px;margin-top: 0px;
margin-bottom: 0px;
height: 8px;
}
HTML:
<table id="tableau" class="nomaltext">
<tr>
<th>
25g
</th>
<th>
50g
</th>
<th>
75g
</th>
<th>
100g
</th>
<th>
Personnalisé (min. 120g)
</th>
</tr>
<tr id="mini">
<hr>
</tr>
<tr>
<td>
5,99$
</td>
<td>
8,99$
</td>
<td>
13,80$
</td>
<td>
7,40$
</td>
<td>
11¢/gramme
</td>
</tr>
</table>

Why is border collapse and spacing not working when you target only the Table Head (TH) inside a Table?

My HTML:
<table style="width:100%">
<tr>
<th>First name</th>
<th>Last name</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
My CSS:
table, th, td {
margin-top:150px;
margin-bottom:150px;
border:1px solid black;
}
th, td {
padding:15px;
}
th {
text-align:left;
border-collapse:collapse;
}
I want to collapse the borders of the TH only, but it's not working. Border collapse and border spacing aren't working when i target only the TH. I can change the background color and the padding and do other changes to TH only, but border changes seems to not work. Why is that?
Note: Before you tell me how it can be done using other ways, please tell me why THIS way isn't working.
Because border-collapse is a style rule of the table and not of the single cells (td or th). This means that you set it on the table element and all the borders in the table will collapse or separate.
You can mimic the behavior of border-collapse: separate only in td by doing something "hacky" like inserting a div inside tds. Check out the fiddle below:
table {
border-collapse: collapse;
}
th {
border: 1px solid black;
}
td {
padding: 2px;
}
td:first-child {
padding-left: 0;
}
td:last-child {
padding-right: 0;
}
td > div {
height: 100%;
width: 100%;
border: 1px solid black;
}
<table>
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
</thead>
<tbody>
<tr>
<td><div>Cell 1</div></td>
<td><div>cell 2</div></td>
</tr>
<tr>
<td><div>Cell 3</div></td>
<td><div>Cell 4</div></td>
</tr>
</tbody>
</table>
as everybody told you , border-collapse is a rule set for the whole table, it tells how cells should be printed at screen side by sides.
A work around could be to fake borders with a box-shadow.
inside tds :
table {
border-collapse: collapse;
}
th {
border: solid 2px;
box-shadow: inset 0 -2px;
}
td {
border: solid transparent;
box-shadow: inset 0 0 0 2px;
padding:3px;
}
<table>
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
</thead>
<tbody>
<tr>
<td> Cell 1 </td>
<td> Cell 2 </td>
</tr>
<tr>
<td> Cell 3 </td>
<td> Cell 4 </td>
</tr>
</tbody>
</table>
outside th
thead {
padding-bottom: 2px;
}
th {
border: 0;
box-shadow: 0 -2px, inset 0 -2px, 2px 0, -2px 0, 2px -2px, -2px -2px;
padding: 2px;
}
td {
border: solid 2px;
}
<table>
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
</thead>
<tbody>
<tr>
<td> Cell 1 </td>
<td> Cell 2 </td>
</tr>
<tr>
<td> Cell 3 </td>
<td> Cell 4 </td>
</tr>
</tbody>
</table>
The border-collapse property can only be applied to <table> elements - not individual rows or cells
https://developer.mozilla.org/en-US/docs/Web/CSS/border-collapse

How can I make a table row's background color surpass its text?

Setting a table row's background color to grey, you'd get a table row that would look like this
+-------------------+--------------------+------------------------+---------------+
|Col1 | Col2 | Col3 | Numeric Column|
+------------ ------+--------------------+------------------------+---------------+
I want to be able to pad the left and right most texts however to get an effect like this
+----------------+------------------+----------------------+---------------+
| Col1 | Col2 | Col3 | Numeric Column|
+----------------+------------------+----------------------+---------------+
Is it possible? I want this effect for both rows and headers
Table HTML and CSS Below:
table {
border-collapse: separate;
border-spacing: 0 1rem;
font-size: 85%;
}
th {
letter-spacing: 1px;
padding-left: 1rem;
border: 0;
}
td {
background-color: grey;
border-radius: 5px;
border: 0;
}
<table class="content-table">
<thead>
<tr>
<th><strong>Name</strong></th>
<th><strong>Age</strong></th>
<th><strong>Height</strong></th>
<th><strong>Location</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td>Stephen Curry</td>
<td>27</td>
<td>1,91</td>
<td>Akron, OH</td>
</tr>
<tr>
<td>Klay Thompson</td>
<td>25</td>
<td>2,01</td>
<td>Los Angeles, CA</td>
</tr>
</tbody>
</table>
Edit:
Found an image by ueno on Dribbble that shows the effect I'm trying to achieve. In the image below, you can see that the table rows' background color slightly pass the text whereas on my table (2nd image), it does not. I understand that I can set text-align: center as some people are saying in the comments however I prefer the left align with just a bit of padding.
I'm not sure if this is what you mean but here is how you can center the text in the row.
An example plunker :
table {
border-collapse: separate;
border-spacing: 0 1rem;
font-size: 85%;
}
th {
letter-spacing: 1px;
padding-left: 1rem;
border: 0;
}
tr {
letter-spacing: 1px;
padding-left: 1rem;
border: 0;
}
td {
border-radius: 5px;
border: 0;
}
.text-center{
text-align: center;
}
tbody tr:nth-child(odd) {
background-color: grey;
}
<table class="content-table">
<thead>
<tr>
<th><strong>Name</strong></th>
<th class="text-center"><strong>Age</strong></th>
<th class="text-center"><strong>Height</strong></th>
<th><strong>Location</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td>Stephen Curry</td>
<td class="text-center">27</td>
<td class="text-center">1,91</td>
<td>Akron, OH</td>
</tr>
<tr>
<td>Klay Thompson</td>
<td class="text-center">25</td>
<td class="text-center">2,01</td>
<td>Los Angeles, CA</td>
</tr>
<tr>
<td>Klay Thompson</td>
<td class="text-center">25</td>
<td class="text-center">2,01</td>
<td>Los Angeles, CA</td>
</tr>
</tbody>
</table>
As you can see i have made an class called .text-center and attached it to the columns I have understood you want to be centered. You can create same classes for -right and -left alignment. And changed some tags as
th to tr in tbody. I hope this is what you mean.
Not quite sure if this is what you were trying to get but this achieves the effect you have shown in the ueno picture above.
table {
border-collapse: collapse;
width:100%;
padding-left:20px;
padding-right:20px;
}
td, th {
padding: 12px;
}
tr:nth-child(even){background-color: #f2f2f2;}
th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: white;
color: black;
<table class="content-table">
<thead>
<tr>
<th><strong>Name</strong></th>
<th><strong>Age</strong></th>
<th><strong>Height</strong></th>
<th><strong>Location</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td>Stephen Curry</td>
<td>27</td>
<td>1,91</td>
<td>Akron, OH</td>
</tr>
<tr>
<td>Klay Thompson</td>
<td>25</td>
<td>2,01</td>
<td>Los Angeles, CA</td>
</tr>
<tr>
<td>Stephen Curry</td>
<td>27</td>
<td>1,91</td>
<td>Akron, OH</td>
</tr>
<tr>
<td>Klay Thompson</td>
<td>25</td>
<td>2,01</td>
<td>Los Angeles, CA</td>
</tr>
</tbody>
</table>

Table with transparent border only on tbody

table
{
background-color: lime;
border-collapse: collapse;
}
tr
{
border-width: 0 0 1px 0;
border-style: solid;
}
tr:last-child
{
border: none; // so the last child from thead and tbody dont have border
}
<table>
<thead>
<th>Rank</th>
<th>Player</th>
<th>Pts</th>
</thead>
<tbody>
<tr>
<td>1</td>
<td>player1</td>
<td>50</td>
</tr>
<tr>
<td>2</td>
<td>player2</td>
<td>40</td>
</tr>
<tr>
<td>3</td>
<td>player3</td>
<td>30</td>
</tr>
<tr>
<td>4</td>
<td>player4</td>
<td>40</td>
</tr>
</tbody>
</table>
Now, I want a transparent border between the rows, but only the rows within tbody, but not between thead and tbody.
First, I tried
table {
border-collapse: collapse;
tr {
border-width: 0 0 1px 0;
border-style: solid;
}
tr:last-child {
border: none; // so the last child from thead and tbody dont have border
}
}
In the case, I get the border on the element I wanted, but it's black and not transparent.
Then I tried with border-spacing:
table {
border-spacing: 0 1px;
tr:last-child {
border: none;
border-spacing: none; //those two don't seem to work
}
}
Now I have transparent borders, but there are borders before and after the thead as well, which I can't eliminate.
So, I have now either:
1. border only in tbody but not between thead and first data row(good), but the borders are not transparent(bad)
or
2. transparent border(good), but unwanted border between thead and first data row(bad).
Is there a way to combine this so I have transparent border, but NOT between thead and first data row?
edit:
I want the border to be full transparent, but as soon as I set the border-color with rgba(0,0,0,0), the border "disappears". Ok, it doesn't really disappeares, but take the background-color from td(the lightgrey color, which is a rgba value as well) and I have no idea why.
use border-color: rgba(0,0,0,.5);
<!DOCTYPE html>
<html>
<head>
<style>
table
{
background:yellow;
border-spacing: 0;
}
tbody tr:not(:last-of-type) td
{
border-width: 0 0 5px 0;
border-style: solid;
border-color:black;
border-color: rgba(0,0,0,.5);
}
</style>
</head>
<body>
<table>
<thead>
<th>Rank</th>
<th>Player</th>
<th>Pts</th>
</thead>
<tbody>
<tr>
<td>1</td>
<td>player1</td>
<td>50</td>
</tr>
<tr>
<td>2</td>
<td>player2</td>
<td>40</td>
</tr>
<tr>
<td>3</td>
<td>player3</td>
<td>30</td>
</tr>
<tr>
<td>4</td>
<td>player4</td>
<td>40</td>
</tr>
</body>
</html>
The problem is that your table has a background color. If you make the border fully transparent, it is transparent, but it won't show you what's behind the table. You're seeing the table's background-color. Also, since you're using a table, any reason you don't just use border-spacing instead?
But rgba(0,0,0,0) for the border color could work as well I suppose.
table
{
border-spacing: 0 20px;
}
tr
{
background-color: lime;
}
tr:last-child
{
border: none; // so the last child from thead and tbody dont have border
}
<table>
<thead>
<th>Rank</th>
<th>Player</th>
<th>Pts</th>
</thead>
<tbody>
<tr>
<td>1</td>
<td>player1</td>
<td>50</td>
</tr>
<tr>
<td>2</td>
<td>player2</td>
<td>40</td>
</tr>
<tr>
<td>3</td>
<td>player3</td>
<td>30</td>
</tr>
<tr>
<td>4</td>
<td>player4</td>
<td>40</td>
</tr>
</tbody>
</table>
Edit:
A way to achieve this is to use an :after on the tr of the thead, so you can "simulate" a border.
thead {
background: lime;
tr:after {
background: lime;
content: "";
height: 2px;
position: absolute;
left: 56px;
top: 77px;
width: calc(100% - 111px);
}
}
With this, you can place the "border" on top of the space between thead and the tbody. Obviously, this may not be the best solution, but I couldn't find another way.
Example: https://jsfiddle.net/qapmqfau/6/
Added an empty table row after each row
<!DOCTYPE html>
<html>
<head>
<style>
table
{
border-spacing: 0;
border-collapse:collaspe;
}
th
{
background:pink;
}
tr:nth-child(even)
{
background:transparent;
height:2px;
}
tr:nth-child(odd)
{
background:green;
}
</style>
</head>
<body>
<table>
<thead>
<th>Rank</th>
<th>Player</th>
<th>Pts</th>
</thead>
<tbody>
<tr>
<td>1</td>
<td>player1</td>
<td>50</td>
</tr>
<tr></tr>
<tr>
<td>2</td>
<td>player2</td>
<td>40</td>
</tr>
<tr></tr>
<tr>
<td>3</td>
<td>player3</td>
<td>30</td>
</tr>
<tr></tr>
<tr>
<td>4</td>
<td>player4</td>
<td>40</td>
</tr>
</body>
</html>

convert Table vertical Header

I have a table like this
<table>
<thead>
<tr>
<th>Numbers</th>
<th>Alphabet</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>a</td>
</tr>
<tr>
<td>2</td>
<td>b</td>
</tr>
</tbody>
</table>
Normally, the first column will show Number 1 2(top to bottom) and the second column is Alphabet a b
Now, I'd like to convert <thead> to vertical and <tbody> <tr> to vertical so that Number 1 2 will be in a horizontal line and Alphabet a b will be in another horizontal one.
CSS
thead {
float: left;
}
thead th {
display: block;
}
tbody {
float: right;
}
The thead turns to vertical, but tbody tr doesn't.
Does anyone know how to get it work?
THANKS
Not sure why just don't change the table layout, but I guess you have your reasons.
Anyway this was not an easy one :)
Here's the CSS code
table{
display:block;
padding: 0px;
border-collapse:collapse;
border-spacing:0;
margin-top: 0px;
margin-right: auto;
margin-bottom: 0px;
margin-left: auto;
}
thead{
display: block;
float: left;
margin: 0px;
padding: 0px;
width: 100px;
}
tbody{
margin: 0px;
padding: 0px;
}
tbody tr {
float:left;
}
th, td{
display:block;
padding: 5px;
margin: 0px;
}
thead > tr th:nth-child(odd) {
display:block;
float:left;
}
thead > tr th:nth-child(even) {
display:block;
float:left;
}
tbody > tr td:nth-child(odd) {
display:block;
}
tbody > tr td:nth-child(even) {
display:block;
float:right;
}
and here's the demo with yout HTML table structure:
http://jsfiddle.net/darkosss/83kVc/
Hope this helps
why dont you use this kind of structure if your table table is not going to change dynamically.
<table>
<tr>
<th>Numbers</th>
<td>1</td>
<td>1</td>
</tr>
<tr>
<th>Alphabet</th>
<td>a</td>
<td>a</td>
</tr>
</table>
My friend, in this condition, don't use thead and tbody. Do like this,
<table>
<tr>
<th>Numbers</th>
<td>1</td>
<td>2</td>
</tr>
<tr>
<th>Alphabet</th>
<td>a</td>
<td>b</td>
</tr>
</table>