Is it possible to control which cell's border prevail? - html

Here are four 2x2 tables, each having a single cell with a dark border. I'd like the dark border to stand out but unfortunately the adjoining light borders cover parts of the dark one in somewhat random ways. Is there a way to force a given cell's border not to be covered by adjoining ones?
I thought of using z-index, but unfortunately it doesn't work.
table {
border-collapse: collapse;
margin:auto;
}
td {
padding: 10px;
}
td.dark {
border: 8px solid black;
}
td.light {
border: 8px solid lightgrey;
}
<table>
<tr> <td class="light"> a </td> <td class="light">b</td> </tr>
<tr> <td class="light"> a </td> <td class="dark">b</td> </tr>
</table>
<br>
<table>
<tr> <td class="dark"> a </td> <td class="light">b</td> </tr>
<tr> <td class="light"> a </td> <td class="light">b</td> </tr>
</table>
<br>
<table>
<tr> <td class="light"> a </td> <td class="dark">b</td> </tr>
<tr> <td class="light"> a </td> <td class="light">b</td> </tr>
</table>
<br>
<table>
<tr> <td class="light"> a </td> <td class="light">b</td> </tr>
<tr> <td class="dark"> a </td> <td class="light">b</td> </tr>
</table>

The border-collapse mechanism gets in the way with this. The easiest solution is use absolute postioned ::before or ::after with either a border or a box-shadow to create the border.
The snippet shows both solutions with ::after.
Beware:
Because of the border-collapse, class .dark will still need a border, but its color is irrelevant as it will be overlayed with our custom border
Either ::before or ::after will overlay the content of the table cell making it hard (but not impossible) for the user to select the content.
snippet
body { display: flex; flex-flow: row wrap; gap: 2rem; justify-content: center }
table { border-collapse: collapse; margin:auto }
td { padding: 10px }
/* still need .dark border to trigger border collapse */
td.dark { border: 8px solid black } /* color is irrelevant */
td.light { border: 8px solid lightgrey }
/* Solutions */
/* ::before/::after will overlay the table cell, this may be unwanted behavior */
td:is(.s1,.s2)::after { background-color: CornSilk } /* to show overlay of ::after */
/* Just disable this rule, remove it or use 'transparent' as color value */
:is(.s1,.s2).dark { position: relative } /* new stacking context */
:is(.s1,.s2).dark::after { position: absolute; content: '' } /* relative to .dark */
/* Solution 1, using 'border' */
td.dark.s1::after {
inset: -8px; /* shorthand for top: -8px; right: -8px; bottom: -8px; left: -8px; */
border: 8px solid black
}
/* Solution 2, using 'box-shadow' */
td.dark.s2::after {
inset: 0; /* shorthand for top: 0; right: 0; bottom: 0; left: 0; */
box-shadow: 0 0 0 8px black
}
<table>
<tr> <td class="light"> a </td> <td class="light">b</td> </tr>
<tr> <td class="light"> a </td> <td class="dark">b</td> </tr>
</table>
<table>
<tr> <td class="dark"> a </td> <td class="light">b</td> </tr>
<tr> <td class="light"> a </td> <td class="light">b</td> </tr>
</table>
<table>
<tr> <td class="light"> a </td> <td class="dark s1">b</td> </tr>
<tr> <td class="light"> a </td> <td class="light">b</td> </tr>
</table>
<table>
<tr> <td class="light"> a </td> <td class="light">b</td> </tr>
<tr> <td class="dark s2"> a </td> <td class="light">b</td> </tr>
</table>

See if this helps:
According to CSS guidelines this is a defined behavior of border-collapse.
If collapsing borders have the same style and width, but differ in
color, then from most to least preferred: cell, row, row group,
column, column group, table.
If come from same type of element, such as two rows then color is
taken from borders that are the topmost and the leftmost.
table {
margin: auto;
}
td {
padding: 10px;
}
td.dark {
border: 8px solid black;
}
td.light {
border: 8px solid lightgrey;
}
<table>
<tr>
<td class="light"> a </td>
<td class="light">b</td>
</tr>
<tr>
<td class="light"> a </td>
<td class="dark">b</td>
</tr>
</table>
<br>
<table>
<tr>
<td class="dark"> a </td>
<td class="light">b</td>
</tr>
<tr>
<td class="light"> a </td>
<td class="light">b</td>
</tr>
</table>
<br>
<table>
<tr>
<td class="light"> a </td>
<td class="dark">b</td>
</tr>
<tr>
<td class="light"> a </td>
<td class="light">b</td>
</tr>
</table>
<br>
<table>
<tr>
<td class="light"> a </td>
<td class="light">b</td>
</tr>
<tr>
<td class="dark"> a </td>
<td class="light">b</td>
</tr>
</table>
OR you can have your own <td> styling for each row <td> element.

Related

How to force html to give the width of a column you want?

I am working on a project where I need to coloring columns of an html table based on how much percentage of a total that data takes up. For example if Alice has 3 apples and 1 orange, that row should be 75% colored for apple, and the other 25% for orange. And if Bob has 2 apples and 2 oranges is should be colored 50/50. My expertise isn't in HTML/CSS, so I am very confused with how my html is working.
If do only one row of a table I get the correct answer, but when I add rows, they couple together and don't have the same proportions.
<table style="width: 100%">
<tr>
<td width="10%" bgcolor="#4a5666"> </td>
<td width="80%" bgcolor="#304aa6"> </td>
<td width="10%" bgcolor="#4a5666"> </td>
</tr>
<tr>
<td width="6%" bgcolor="#4a5666"> </td>
<td width="16%"bgcolor="#304aa6"> </td>
<td width="76" bgcolor="#4a5666"> </td>
</tr>
</table>
Am I doing something wrong, and is there a way I can fix it?
Thanks
Coupled Rows
Check this solution out:
<html>
<head>
<style type="text/css">
.mytable {
border-collapse: collapse;
width: 100%;
background-color: white;
}
.mytable-head {
border: 1px solid black;
margin-bottom: 0;
padding-bottom: 0;
}
.mytable-head td {
border: 1px solid black;
}
.mytable-body {
border: 1px solid black;
border-top: 0;
margin-top: 0;
padding-top: 0;
margin-bottom: 0;
padding-bottom: 0;
}
.mytable-body td {
border: 1px solid black;
border-top: 0;
}
.mytable-footer {
border: 1px solid black;
border-top: 0;
margin-top: 0;
padding-top: 0;
}
.mytable-footer td {
border: 1px solid black;
border-top: 0;
}
</style>
</head>
<body>
<table class="mytable mytable-head">
<tr>
<td width="25%">25</td>
<td width="50%">50</td>
<td width="25%">25</td>
</tr>
</table>
<table class="mytable mytable-body">
<tr>
<td width="50%">50</td>
<td width="30%">30</td>
<td width="20%">20</td>
</tr>
</table>
<table class="mytable mytable-body">
<tr>
<td width="16%">16</td>
<td width="68%">68</td>
<td width="16%">16</td>
</tr>
</table>
<table class="mytable mytable-footer">
<tr>
<td width="20%">20</td>
<td width="30%">30</td>
<td width="50%">50</td>
</tr>
</table>
</body>
</html>
Credits to #TokPhobia for this answer (html table cell width for different rows)
tables have columns. The width of a column is constant. Use separate tables
table{
box-sizing:unset;
border-spacing:0;
}
<table style="width: 100%;">
<tr>
<td width="10%" bgcolor="#4a5666"> </td>
<td width="80%" bgcolor="#304aa6"> </td>
<td width="10%" bgcolor="#4a5666"> </td>
</tr>
</table>
<table style="width: 100%;">
<tr>
<td width="6%" bgcolor="#4a5666"> </td>
<td width="16%"bgcolor="#304aa6"> </td>
<td width="76%" bgcolor="#4a5666"> </td>
</tr>
</table>
You're printing two rows of table in a wrong way, try to use col and row class in your tag. That's how it'll be responsive also won't need to add the width too.

css rotate text vertically across table columns

I have a matrix which, because of the subjects names, is stretching off screen
I wish to create a training matrix with the abilities on the left, colored cells for the acquired skill, with the subjects name vertically across the columns.
I have tries using bootstrap and css rules to rotate text, but I cant get the text to float above the grid
apologies, this is what I have tried :
<style>
.rotate {
transform: rotate(90deg);
width: 8rem;
}
</style>
</head>
<body>
<div class="row">
<div class="col">
<table class="table">
<tr> <td> </td>
<tr> <td> <p class="text-nowrap bd-highlight rotate ">Bruce Wayne. </p> </td>
<tr> <td> <p class="text-nowrap bd-highlight rotate ">Clark Kent.</p> </td>
<tr> <td> <p class="text-nowrap bd-highlight rotate ">Barry Allen.</p> </td>
</tr>
<tr> <td> Detective</td> <td class="table-success"> </td> <td class="table-success"></td> <td class="table-success"></td> </tr>
<tr> <td> can fly</td> <td class="table-success"> </td> <td class="table-success"></td> <td class="table-success"></td> </tr>
<tr> <td> runs fast</td> <td class="table-success"> </td> <td class="table-success"></td> <td class="table-success"></td> </tr>
<tr> <td> plays well</td> <td class="table-success"> </td> <td class="table-success"></td> </tr>
</table>
all the vertical text stacks to the left of the screen.
Is this what you wanted?
.table {
background: grey;
border-spacing: 5px;
border: none;
width: 500px;
}
.rotate {
transform: rotate(90deg);
width: 8rem;
}
td {
width: 25%;
border: 2px solid black;
padding: 10px;
position: relative;
}
.vertical {
transform: rotate(90deg);
position: absolute;
top: 200%;
left: 0;
right: 0;
color: #fff;
font-weight: bold;
font-size: 18px;
text-transform: uppercase;
white-space: nowrap;
}
.
<body>
<div class="row">
<div class="col">
<table class="table">
<tr>
<td> Detective</td>
<td class="table-success"><span class="vertical"> Bruce Wayne</span> </td>
<td class="table-success"><span class="vertical"> clark Kent</span></td>
<td class="table-success"><span class="vertical"> barry allen</span></td>
</tr>
<tr>
<td> can fly</td>
<td class="table-success"> </td>
<td class="table-success"></td>
<td class="table-success"></td>
</tr>
<tr>
<td> runs fast</td>
<td class="table-success"> </td>
<td class="table-success"></td>
<td class="table-success"></td>
</tr>
<tr>
<td> plays well</td>
<td class="table-success"> </td>
<td class="table-success"></td>
<td class="table-success"></td>
</tr>
</table>
You haven't given your code how you did it but I give you a solution. Hope this is useful for you.
.rotate{
transform: rotate(-90deg);
}
<table>
<tr>
<td class="rotate">BRUCE</td>
<td class="rotate">CLARK</td>
<td class="rotate">BARRY</td>
</tr>
</table>

Left padded table rows

I've been trying to mimic the following table layout using HTML/CSS:
NOTE: It's a table from LibreOffice Writer which I modified using Gimp to show you what I mean.
As you can see, I'd like to add some left padding to some rows to show visually that they are inside a group.
I tried using padding-left of both <td> and <tr>, and a little trick that don't work: applying 'border-left: 14px solid white' to the <tr> and then 'border-left: 15px solid black' to the first <td> in the row. I thought that the border in the <td> would overlap the <tr> border by 1px, but HTML rendering seems not to work that way :)
Also, I tried to do this:
<tr>
<td colspan="9">
GROUP 1
</td>
</tr>
<tr>
<td colspan="9" style="padding-left: 15px">
<table>
<tr>
<td> <!-- # --> </td>
<td> <!-- Id --> </td>
<td> <!-- Field1 --> </td>
(ETC)
<td> <!-- Comment --> </td>
</tr>
</table>
</td>
</tr>
The problem with this approach is that the column lines of the inside the 'group' don't match the ones that are outside so it doesn't look good...
Any suggestion?
Try this. Remove borders from table cells, instead add divs within each table cell with the border:
<tr>
<td colspan="4">
<div class="cell">GROUP 1</div>
</td>
</tr>
<tr>
<td style="padding-left: 15px">
<div class="cell"> col 1</div>
</td>
<td>
<div class="cell"> col 2</div>
</td>
<td>
<div class="cell"> col 3</div>
</td>
<td>
<div class="cell"> col 4</div>
</td>
</tr>
CSS:
div.cell {
border-left: 1px solid #000;
border-right: none;
border-bottom: none;
}
table {
border-collapse: collapse;
border-right: 1px solid #000;
border-bottom: 1px solid #000;
}
table td {
padding: 0;
border-top: 1px solid #000;
border-bottom: 1px solid #000;
}
See example here: https://codepen.io/anon/pen/baMdWP
Suggestion:
.with-padding {
margin-left: 10px;
}
<table class="normal">
...
</table>
<table class="with-padding">
...
</table>
<table class="normal">
...
</table>
Assign padding-left: 15px to every sub-sequent <tr> that is to be displayed as part of the group. It's better to use a class instead of applying inline style.
Try this way.
HTML
<table style="width:100%">
<tr>
<td style="width:25%;">January</td>
<td style="width:25%;">$100</td>
<td style="width:25%;">January</td>
<td style="width:25%;">$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
<td>February</td>
<td>$80</td>
</tr>
<td colspan="9">
GROUP 1
</td>
</table>
<table style="width:95%;margin-left:5%">
<tr>
<td style="width:20%;">January</td>
<td style="width:25%;">$100</td>
<td style="width:25%;">January</td>
<td style="width:25%;">$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
<td>February</td>
<td>$80</td>
</tr>
</table>
<table style="width:100%">
<tr>
<td style="width:25%;">January</td>
<td style="width:25%;">$100</td>
<td style="width:25%;">January</td>
<td style="width:25%;">$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
<td>February</td>
<td>$80</td>
</tr>
</table>
CSS
table,th,td {
border: 1px solid black;
}

How to remove table border on a single row?

I have a table with the following class:-
.multicolor {
border: 1px solid #000000;
}
and for one specific table row I wanted to remove the left and right borders, replacing them with top and bottom so that it looks like one table is ending and another is beginning. Here's how I was trying it and no any luck.
<tr style="background-color:transparent; border-style:solid none solid none; border-width:1px 0px 1px 0px">
<td colspan="7" style="background-color:transparent; border-style:solid none solid none; border-width:1px 0px 1px 0px">
<br></td>
</tr>
The top and the bottom borders are appearing but the side ones remain. Does anyone know if there is a way to override the inherited border property for that row?
try this.
table {
border-collapse:collapse;
}
td {
border:none;
}
You have to set border of your td or th "none";
May be this will help hide cells border using css
hello See the below fiddle as you mentioned the merge row I did this for both border and without border in the rows .hope it helps
<table>
<thead>
<tr>
<th class="col1">1</th>
<th class="col2">2</th>
<th class="col3">3</th>
</tr>
</thead>
<tr class="first">
<td>asdas</td>
<td>asdas</td>
<td >boooo</td>
</tr>
<tr class="second">
<td>asdas</td>
<td>asdas</td>
<td>asdas</td>
</tr>
</table>
see the below fiddle
fiddle demo
try this,working fine
<style>
.border {
border:solid 1px #000;
}
.border-head {
border-bottom:solid 1px #000;
}
</style>
<table width="300" border="0" cellpadding="0" cellspacing="0" class="border" >
<thead>
<tr>
<td class="border-head"> </td>
<td class="border-head"> </td>
<td class="border-head" > </td>
</tr>
</thead>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>

How to make a tag with dynamic content full height inside of td tag

I have a table where I load dynamic content, and I want that the content inside the cells is the same height as the cells! Precisely I have "a" elements with some text, inside the td cells. I get a problem when the text in a cell is long and goes at newline, and like you can see in the image the other "a" elements in the table row don't fit the full height of the cell.
See the live Demo
Here is the code
HTML
<table class="table" cellspacing="0">
<thead>
<tr>
<th>Data</th>
<th>Titolo</th>
<th>Sede</th>
<th>Città</th>
</tr>
</thead>
<tbody>
<tr>
<td>12/02/2015
</td>
<td><span class="fixed glyphicon glyphicon-flag"></span>asd
</td>
<td>ada
</td>
<td>asdas
</td>
</tr>
<tr>
<td>04/02/2015
</td>
<td><span class="fixed glyphicon glyphicon-flag"></span>ada
</td>
<td>prova
</td>
<td>asda
</td>
</tr>
<tr>
<td>07/09/2017
</td>
<td><a href="eventi.php?id=12">Evento Lignano <br>
sabbiadoro</a>
</td>
<td>Palazzetto dello Sport
</td>
<td>Perugia
</td>
</tr>
<tr>
<td>09/09/2015
</td>
<td>aaa
</td>
<td>aaa
</td>
<td>aaa
</td>
</tr>
<tr>
<td>09/03/2015
</td>
<td>sfsd
</td>
<td>ada
</td>
<td>dadasd
</td>
</tr>
</tbody>
CSS
table{
width:400px;
}
td a{
background:#ff0;
display: block;
padding:20px;
box-sizing:border-box;
height: 100%;
width: 100%;
}
td{
height:100%;
position: relative;
}
table {
width: 400px;
}
td a {
background: #ff0;
display: block;
padding: 20px;
box-sizing: border-box;
height: 100%;
width: 100%;
}
td {
height: 100%;
position: relative;
}
<table class="table" cellspacing="0">
<thead>
<tr>
<th>Data</th>
<th>Titolo</th>
<th>Sede</th>
<th>Città</th>
</tr>
</thead>
<tbody>
<tr>
<td>12/02/2015
</td>
<td><span class="fixed glyphicon glyphicon-flag"></span>asd
</td>
<td>ada
</td>
<td>asdas
</td>
</tr>
<tr>
<td>04/02/2015
</td>
<td><span class="fixed glyphicon glyphicon-flag"></span>ada
</td>
<td>prova
</td>
<td>asda
</td>
</tr>
<tr>
<td>07/09/2017
</td>
<td><a href="eventi.php?id=12">Evento Lignano <br>
sabbiadoro</a>
</td>
<td>Palazzetto dello Sport
</td>
<td>Perugia
</td>
</tr>
<tr>
<td>09/09/2015
</td>
<td>aaa
</td>
<td>aaa
</td>
<td>aaa
</td>
</tr>
<tr>
<td>09/03/2015
</td>
<td>sfsd
</td>
<td>ada
</td>
<td>dadasd
</td>
</tr>
</tbody>
</table>
Here is a little hack I've used in the past.
You can essentially add a pseudo element to the a element and absolutely position it relative to the closest td element. Since the pseudo elements are essentially children elements of the a element, when you click the pseudo element, you are inadvertently clicking the a element.
Updated Example
td a:after, td a:before {
content: '';
position: absolute;
top: 0; right: 0;
bottom: 0; left: 0;
}
td a:after {
background: #ff0;
z-index: -1;
}
You must define an height of your TD, otherwise the browser doesn't know the 100% of what is to be considered:
td {
height:100px;
position: relative;
}
https://jsfiddle.net/wg6paumw/4/