Dynamic CSS border depends on number of contents - html

I'm not sure what is this called, and already searched it on google but doesnt found what I want,
but here is what I want to looks like
Can someone help me to achieve this? or maybe what is this called?
and if there are only 1 content, then no border at all
and, I'm using div not table tag

It's taken from codepen but modified as per your requirements.
page.html
<table class="table">
<tbody class="table-body">
<tr class="row">
<td class="cell"><span>Cool</span></td>
</tr>
</tbody>
</table>
<table class="table" style="margin-top:30px;">
<tbody class="table-body">
<tr class="row">
<td class="cell"><span>Cool</span></td>
<td class="cell"><span>Cool</span></td>
</tr>
</tbody>
</table>
<table class="table" style="margin-top:30px;">
<tbody class="table-body">
<tr class="row">
<td class="cell"><span>Cool</span></td>
<td class="cell"><span>Cool</span></td>
</tr>
<tr class="row">
<td class="cell"><span>Cool</span></td>
<td class="cell"><span>Cool</span></td>
</tr>
<tr class="row">
<td class="cell"><span>Cool</span></td>
</tr>
</tbody>
</table>
<table class="table" style="margin-top:30px;">
<tbody class="table-body">
<tr class="row">
<td class="cell"><span>Cool</span></td>
<td class="cell"><span>Cool</span></td>
</tr>
<tr class="row">
<td class="cell"><span>Cool</span></td>
<td class="cell"><span>Cool</span></td>
</tr>
</tbody>
</table>
page.css
.cell {
border-collapse: collapse;
border: 1px solid #000;
padding: 5px;
}
.cell span{
background-color: #000;
color: #fff;
display: block;
padding: 10px;
}
.table {
border-collapse: collapse;
border-style: hidden;
}
.table-head > .row {
border-collapse: collapse;
border: 2px solid pink;
}
See the screenshot
http://prnt.sc/pdptlg

try checking out this example, it shows a table with inner borders only, without the outer borders.
.cell {
border-collapse: collapse;
border: 1px solid pink;
padding: 5px 10px;
}
.table {
border-collapse: collapse;
border-style: hidden;
}
.table-head > .row {
border-collapse: collapse;
border: 2px solid pink;
}
<table class="table">
<tbody class="table-body">
<tr class="row">
<td class="cell">Cool</td>
<td class="cell">Cool</td>
</tr>
<tr class="row">
<td class="cell">Cool</td>
<td class="cell">Cool</td>
</tr>
</tbody>
</table>
Taken from https://codepen.io/sirinity/pen/dDsLx

One way only using pure css. Hope this help
.container {
display: flex;
flex-wrap: wrap;
width: 150px;
}
.wrapper {
width: 50px;
height: 50px;
padding: 5px;
border: 0 solid;
}
.content {
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
color: white;
background-color: #030;
}
.wrapper:nth-of-type(odd) {
border-right-width: 1px;
}
.wrapper:nth-of-type(odd), .wrapper:nth-of-type(even) {
border-bottom-width: 1px;
}
.wrapper:last-child, .wrapper:nth-last-of-type(-n+2):not(:nth-child(even)) {
border-bottom-width: 0;
}
<div class="container">
<div class="wrapper"><div class="content">1</div></div>
<div class="wrapper"><div class="content">2</div></div>
<div class="wrapper"><div class="content">3</div></div>
<div class="wrapper"><div class="content">4</div></div>
<div class="wrapper"><div class="content">5</div></div>
<div class="wrapper"><div class="content">6</div></div>
<div class="wrapper"><div class="content">7</div></div>
</div>

You can do it with css pseudo-classes
More info here: Css pseudo-classes
Yout HTML dosnt change:
<table class="table">
<tbody class="table-body">
<tr class="row">
<td class="cell">Cool</td>
<td class="cell">Cool</td>
</tr>
<tr class="row">
<td class="cell">Cool</td>
<td class="cell">Cool</td>
</tr>
</tbody>
</table>
in css:
.table {
border-collapse: collapse;
border-style: hidden;
}
.cell {
padding: 5px 10px;
}
.row:nth-of-type(1n+1) {
border-bottom: 2px solid pink;
}
.cell:nth-of-type(2n-1) {
border-right: 2px solid pink;
}
Explanation:
Just skip first row to set top border:
.row:nth-of-type(1n+1) {
border-bottom: 2px solid pink;
}
and then, set border only to odd cells:
.cell:nth-of-type(2n-1) {
border-right: 2px solid pink;
}
Code here
Hope it helps you.
Edit: Sorry but i didnt notice that you are not using table tags, Hải Bùi response is the way for that, my css only works properly with tables.

Related

Content inside a table cell doesn't take the whole space

I created this table:
table {
}
thead {
background-color: black;
color: white;
}
tbody, tr, td {
border: 1px solid black;
}
.cell-first-column {
width: 200px;
height: 70px;
}
.cell, th {
width: 100px;
height: 70px;
}
.td-cell {
height: 70px;
border: 1px solid black;
}
.chart {
position: relative;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
margin: 0
}
.chart-inner {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background-color: gold;
}
.cell-text {
z-index: 10;
}
<table>
<thead>
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
<th>Number</th>
</tr>
</thead>
<tr>
<td><div class="cell-first-column">Alfreds Futterkiste</div></td>
<td><div class="cell">Maria Anders</div></td>
<td><div class="cell">Germany</div></td>
<td><div class="cell">1234567</div></td>
</tr>
<tr>
<td><div class="cell-first-column">Centro comercial</div></td>
<td><div class="cell">Francisco Chang</div></td>
<td><div class="cell">Mexico</div></td>
<td><div class="cell">1234567</div></td>
</tr>
<tr>
<td><div class="cell-first-column">Opla</div></td>
<td class="td-cell">
<div class="chart">
<div class="chart-inner" style="height: 90%"></div>
<div class="cell-text">Charles Boule</div>
</div>
</td>
<td class="td-cell">
<div class="chart">
<div class="chart-inner" style="height: 30%"></div>
<div class="cell-text">France</div>
</div>
</td>
<td class="td-cell">
<div class="chart">
<div class="chart-inner" style="height: 50%"></div>
<div class="cell-text">1234567</div>
</div>
</td>
</tr>
</table>
inside the third row, the cells are colored and to do that I use absolute position.
It works but I don't understand why the yellow doesn't take the entire cell space, there is a white margin between the yellow and the cell border.
Margins are 0.
How it is now vs How I would like it to be:
Why? How can I solve?
Thanks a lot
Set padding of td to 0 should fix the issue.
The correct way:
<table cellpadding="0">

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>

Table headers position:sticky and border issue

I have a table that I'm trying to get sticky headers, following this article. Except my table style has the headers with a border at the top and bottom.
The part I do not understand is that the top/bottom borders applied to the th scroll away with the rest of the table instead of staying with the "stuck" header cells. Is there anything that can be done about this?
Working sample can be found here: https://codepen.io/smlombardi/pen/MNKqQY?editors=1100#0
let string = ''
console.log("oj")
for(let i=0; i<100; i++) {
string += `
<tr>
<td>Malcolm Reynolds</td>
<td>Captain</td>
<td>9035749867</td>
<td>mreynolds</td>
</tr>
`
}
document.getElementsByTagName('tbody')[0].innerHTML += string
.table-sticky-container {
height: 200px;
overflow-y: scroll;
border-top: 1px solid green;
border-bottom: 1px solid green;
}
.table-sticky {
th {
position: sticky;
top: 0;
z-index: 2;
border-top: 1px solid red !important;
border-bottom: 2px solid red !important;
}
}
<div class="table-sticky-container">
<table class="table table-sticky">
<thead class="thead-light">
<tr>
<th scope="col">Name</th>
<th scope="col">Title</th>
<th scope="col">ID</th>
<th scope="col">Username</th>
</tr>
</thead>
<tbody>
<tr>
<td>Malcolm Reynolds</td>
<td>Captain</td>
<td>9035749867</td>
<td>mreynolds</td>
</tr>
</tbody>
</table>
</div>
You can add
.table {
border-collapse: separate;
}
But unfortunately it looks bad, a better solution will be adding a workaround using a pseudo-element.
th:after,
th:before {
content: '';
position: absolute;
left: 0;
width: 100%;
}
th:before {
top: -1px;
border-top: 1px solid red;
}
th:after {
bottom: -1px;
border-bottom: 2px solid red;
}
.table-sticky-container {
height: 200px;
overflow-y: scroll;
border-top: 1px solid green;
border-bottom: 1px solid green;
}
.table-sticky th {
position: -webkit-sticky;
position: sticky;
top: 0;
z-index: 2;
}
th:after,
th:before {
content: '';
position: absolute;
left: 0;
width: 100%;
}
th:before {
top: -1px;
border-top: 1px solid red;
}
th:after {
bottom: -1px;
border-bottom: 2px solid red;
}
<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css'>
<div class="table-sticky-container">
<table class="table table-sticky">
<thead class="thead-light">
<tr>
<th scope="col">Name</th>
<th scope="col">Title</th>
<th scope="col">ID</th>
<th scope="col">Username</th>
</tr>
</thead>
<tbody>
<tr>
<td>Malcolm Reynolds</td>
<td>Captain</td>
<td>9035749867</td>
<td>mreynolds</td>
</tr>
<tr>
<td>Zoe Washburne</td>
<td>First Officer</td>
<td>8908980980</td>
<td>zwashburne</td>
</tr>
<tr>
<td>Kaylee Frye</td>
<td>Engineer</td>
<td>6678687678</td>
<td>kfrye</td>
</tr>
<tr>
<td>Malcolm Reynolds</td>
<td>Captain</td>
<td>9035749867</td>
<td>mreynolds</td>
</tr>
<tr>
<td>Zoe Washburne</td>
<td>First Officer</td>
<td>8908980980</td>
<td>zwashburne</td>
</tr>
<tr>
<td>Kaylee Frye</td>
<td>Engineer</td>
<td>6678687678</td>
<td>kfrye</td>
</tr>
</tbody>
</table>
</div>
The second solution
.table {
border-collapse: collapse;
}
.table-sticky thead th {
position: -webkit-sticky;
position: sticky;
top: 0;
z-index: 2;
box-shadow: inset 0 1px 0 red, inset 0 -2px 0 red;
}
You can add
.table {
border-collapse: separate;
border-spacing: 0;
}
Don't use border-collapse and draw the lines as follows :
td, th {
border-bottom: 1px solid grey;
border-right: 1px solid grey;
}
table {
border-spacing: 0px;
border-left: 1px solid grey
}
th {
background-color:#c5e8ec;
position: sticky;
position: -webkit-sticky;
border-top: 1px solid grey;
top:0;
}
Another working solution (tested at latest Chrome and FF) - wrap th/td content with divs and use those divs borders instead of cell's borders.
<div class="wrapper">
<table>
<thead>
<tr>
<th>
<div class="cell">head1</div>
</th>
<th>
<div class="cell">head2</div>
</th>
<th>
<div class="cell">head3</div>
</th>
<th>
<div class="cell">head4</div>
</th>
<th>
<div class="cell">head5</div>
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<div class="cell">1</div>
</td>
<td>
<div class="cell">1</div>
</td>
<td>
<div class="cell">1</div>
</td>
<td>
<div class="cell">1</div>
</td>
<td>
<div class="cell">1</div>
</td>
</tr>
<tr>
<td>
<div class="cell">1</div>
</td>
<td>
<div class="cell">1</div>
</td>
<td>
<div class="cell">1</div>
</td>
<td>
<div class="cell">1</div>
</td>
<td>
<div class="cell">1</div>
</td>
</tr>
<tr>
<td>
<div class="cell">1</div>
</td>
<td>
<div class="cell">1</div>
</td>
<td>
<div class="cell">1</div>
</td>
<td>
<div class="cell">1</div>
</td>
<td>
<div class="cell">1</div>
</td>
</tr>
</tbody>
</table>
</div>
table {
border-collapse: collapse;
table-layout: fixed;
width: 500px;
}
.wrapper {
height: 150px;
overflow: auto;
}
td, th {
width: 150px;
height: 50px;
text-align: center;
font-size: 32px;
padding: 0;
}
th {
position: sticky;
top: 0;
left: 0;
background-color: #fff;
}
.cell {
border: 1px solid black;
height: 100%;
}
td .cell {
border-top: none;
border-left: none;
}
th .cell {
border-left: none;
}
td:first-child .cell,
th:first-child .cell {
border-left: 1px solid black;
}

Remove border spacing from every 2n element in table

I have a table that looks like this:
And every row has a hidden details field:
So I want to remove border spacing from row and its details row.. How can I do that?
this is my HTML:
<table class="table message-table">
<thead>
<tr>
<th>Title</th>
<th>Date</th>
<th>Action</th>
<th></th>
</tr>
</thead>
<tbody>
<ng-container *ngFor="let message of messages | paginate: config">
<tr>
<td>{{message.title}}</td>
<td>{{message.created | date:'longDate'}}</td>
<td (click)="message.collapsed = !message.collapsed; makeMessageSeen(message);" [attr.aria-expanded]="!message.collapsed" aria-controls="collapseExample">{{message.collapsed ? 'More' : 'Less'}}</td>
<td></td>
</tr>
<tr id="collapseExample" [ngbCollapse]="message.collapsed">
<td>{{message.text}}</td>
<td colspan="3"></td>
</tr>
</ng-container>
</tbody>
</table>
and this is my SCSS:
.messages {
background-color: $color-background-main;
min-height: 17rem;
overflow-x: auto;
padding-top: 2rem;
.message-table {
border-collapse: separate;
border-spacing: 0 0.4rem;
thead {
th {
border: none;
font-size: 0.6rem;
color: #9b9b9b;
text-transform: uppercase;
padding-top: 0;
padding-bottom: 0;
&:first-child {
width: 70%;
padding-left: 1rem;
}
}
}
}
tbody {
tr {
box-shadow: $main-shadow;
background-color: white;
&.selected {
box-shadow: $shadow-selected;
}
td:first-child {
padding-left: 1rem;
}
}
td {
background-color: white;
border: none;
padding-top: 0;
padding-bottom: 0;
padding-right: 0;
height: 2.5rem;
vertical-align: middle;
table-layout: fixed;
&:first-child {
border-top-left-radius: 5px;
border-bottom-left-radius: 5px;
}
&:last-child {
border-top-right-radius: 5px;
border-bottom-right-radius: 5px;
padding-right: 0;
width: 2rem;
}
}
}
}
How can I fix this? I've tried making tr a top border but nothing happened... What are other solutions for my problem?
UPDATE 1
Adding codepen of a simple example : https://codepen.io/anon/pen/prxgOe
UPDATE 2
I want to remove spacing between title and details tr elements ONLY!
Here is a quick fix using borders.
table {
border-collapse: collapse;
border-spacing: 0 0.4rem;
}
tr {
background-color: #ccc;
}
.title {
border-top: 5px solid #fff;
}
.details {
/* display: none; */
}
<table>
<thead>
<tr>
<th>Title</th>
<th>Created</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr class="title">
<td>title1</td>
<td>2017-01-01</td>
<td>More</td>
</tr>
<tr class="details">
<td>this is text1</td>
<td colspan="2"></td>
</tr>
<tr class="title">
<td>title2</td>
<td>2017-01-01</td>
<td>More</td>
</tr>
<tr class="details">
<td>this is text2</td>
<td colspan="2"></td>
</tr>
<tr class="title">
<td>title3</td>
<td>2017-01-01</td>
<td>More</td>
</tr>
<tr class="details">
<td>this is text3</td>
<td colspan="2"></td>
</tr>
</tbody>
</table>
I think you want like this
table {
border-collapse: collapse;
border-spacing: 0 0.4rem;
}
tr {
background-color: #ccc;
display: table-row;
}
table {
border-collapse: collapse;
border-spacing: 0 0.4rem;
}
tr {
background-color: #ccc;
display: table-row;
}
.title:first-child {
border-top: 7px solid #fff;
}
.title {
border-top: 12px solid #fff;
}
tbody tr:nth-child(2n) {
position: relative;
}
tbody tr:nth-child(2n)::after {
bottom: 0;
box-shadow: 0 2px 2px 0 #ebebeb;
content: "";
height: 100%;
left: 0;
position: absolute;
right: 0;
width: 100%;
}
<table>
<thead>
<tr>
<th>Title</th>
<th>Created</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr class="title">
<td>title1</td>
<td>2017-01-01</td>
<td>More</td>
</tr>
<tr class="details">
<td>this is text1</td>
<td colspan="2"></td>
</tr>
<tr class="title">
<td>title2</td>
<td>2017-01-01</td>
<td>More</td>
</tr>
<tr class="details">
<td>this is text2</td>
<td colspan="2"></td>
</tr>
<tr class="title">
<td>title3</td>
<td>2017-01-01</td>
<td>More</td>
</tr>
<tr class="details">
<td>this is text3</td>
<td colspan="2"></td>
</tr>
</tbody>
</table>
*ngFor has an option to detect odd and even rows:
<div *ngFor="let message of messages; let odd=odd; let even=even>..</div>
then you can use odd or even values to set style/class as so:
<div [class.evenClass]="event" [class.oddClass]="odd">...</div>
and in the stylesheet, define .evenClass and .oddClass style as needed.
P.S.You have a table layout, you need to adapt above to your case
#hunzaboy and #ankitapatel answers were kind of good for me, but eventually I came with different solution which is probably not the best one, but it works like a charm... It does not break the ui scalability or anything else...
So I just added div elements in each of td elements so my HTML now looks like this:
<tbody>
<ng-container *ngFor="let message of messages | paginate: config">
<tr>
<td [class.unseen]="!message.seen" [class.seen]="message.seen">{{message.title}}</td>
<td [class.unseen]="!message.seen" [class.seen]="message.seen">{{message.created | date:'longDate'}}</td>
<td class="details-button" (click)="message.collapsed = !message.collapsed; makeMessageSeen(message);" [attr.aria-expanded]="!message.collapsed" aria-controls="collapseExample">{{message.collapsed ? 'More' : 'Less'}}</td>
<td></td>
</tr>
<tr id="collapseExample" [ngbCollapse]="message.collapsed">
<td>
<div class="text-container">{{message.text}}</div>
</td>
<td colspan="3">
<div class="empty-container"></div>
</td>
</tr>
</ng-container>
</tbody>
and then I added this SCSS to my file:
#collapseExample {
td {
padding: 0;
}
.text-container {
background-color: #fff;
margin-top: -23px;
padding-left: 1rem;
border-radius: 0.2rem;
height: 100%;
padding-bottom: 1rem;
}
.empty-container {
background-color: #fff;
height: 100%;
margin-top: -20px;
width: 100%;
}
}

How to properly format tables using css

I want the outer borders of my table be dashed, while the inner borders be solid. So I made these css codes for my normal-table, but whole table border is solid.
.zulu-post .zulu-content .normal-table{
color: #444444;
border: 1px dashed #444444;
}
.zulu-post .zulu-content .normal-table td, .normal-table tr{
padding: 10px;
border: 1px solid #444444;
}
<table border="1" cellpadding="1" cellspacing="1" class="normal-table" style="width:500px;">
<tbody>
<tr>
<td>Table Name</td>
</tr>
<tr>
<td>Make sure that Stylesheet Classes is normal-table</td>
</tr>
<tr>
<td>Text Here...</td>
</tr>
</tbody>
</table>
This is one way of doing what you want:
Basically you add border left and top to all <td> tags and than remove the border from the sides of the table, and you use dashed border on <table>.
.normal-table {
color: #444444;
border: 1px dashed #444444;
}
.normal-table td {
padding: 10px;
border-left: 1px solid #444444;
border-top: 1px solid #444444;
}
.normal-table td:first-child {
border-left: none;
}
.normal-table tr:first-child td {
border-top: none;
}
<table cellpadding="1" cellspacing="0" class="normal-table" style="width:500px;">
<tbody>
<tr>
<td>Table Name</td>
</tr>
<tr>
<td>Make sure that Stylesheet Classes is normal-table</td>
</tr>
<tr>
<td>Text Here...</td>
</tr>
</tbody>
</table>
1st make use of border-collpase:collapse, this collapse the table border to single border then do styling part for table, tbody, tr and such.
.normal-table {
border: 2px solid red;
border-collapse: collapse;
}
tr {
border: 2px dashed red;
}
<table border="1" cellpadding="1" cellspacing="1" class="normal-table" style="width:500px;">
<tbody>
<tr>
<td>Table Name</td>
</tr>
<tr>
<td>Make sure that Stylesheet Classes is normal-table</td>
</tr>
<tr>
<td>Text Here...</td>
</tr>
</tbody>
</table>
You can use divs as an alternative:
.container {
width: 100%;
border: medium dashed darkgray;
display: table;
}
.cell {
width: 30%;
border: thin solid red;
height: 50px;
display: table-cell;
}
<div class='container'>
<div class='cell'></div>
<div class='cell'></div>
<div class='cell'></div>
</div>