Padding not working for table component in Tailwind - tailwind-css

I am trying to add padding for table head and table rows in Tailwind based React App.
Here's my code:
import React from "react";
import "./styles.css";
import "./styles/tailwind-pre-build.css";
export default function App() {
return (
<table className="w-full px-2 rounded-t-md">
<thead className="text-white bg-blue-600 text-left border border-red-600 p-2">
<tr className="w-full p-2">
<th className="font-medium">App Name</th>
<th className="font-medium">Owner</th>
<th className="font-medium">Date Created</th>
<th className="font-medium">Scopes</th>
<th className="font-medium">Actions</th>
</tr>
</thead>
<tbody>
<tr className="p-2">
<td>Test App</td>
<td>Shivam Sahil</td>
<td>20 May 2022, 19:58 AM</td>
<td className="break-words">all.create all.update all.read</td>
<td>Edit</td>
</tr>
</tbody>
</table>
);
}
For some reason the padding doesn't seem to happen at all. I tried to inspect and check but even when adding padding in styles it won't show up, can someone help me understand what wrong am I doing here?
Here's the live sandbox:https://codesandbox.io/s/tailwind-css-and-react-forked-xwvdue?file=/src/App.js:0-884

You can adjust the padding inside the cells, so instead of <tr> you need set padding for all <th>, <td> tags.
<table className="w-full rounded-t-md">
<thead className="text-white bg-blue-600 text-left border border-red-600">
<tr className="w-full">
<th className="font-medium p-2">App Name</th>
<th className="font-medium p-2">Owner</th>
<th className="font-medium p-2">Date Created</th>
<th className="font-medium p-2">Scopes</th>
<th className="font-medium p-2">Actions</th>
</tr>
</thead>
<tbody>
<tr>
<td className="p-2">Test App</td>
<td className="p-2">Shivam Sahil</td>
<td className="p-2">20 May 2022, 19:58 AM</td>
<td className="break-words p-2">all.create all.update all.read</td>
<td className="p-2">Edit</td>
</tr>
</tbody>
</table>
OR
Instead of adding the p-2 class everywhere using the Tailwind Utilities, you will get the same result.
#tailwind base;
#tailwind components;
#tailwind utilities;
#layer utilities {
td,
th {
#apply p-2;
}
}

Related

How to add space between table rows+ tailwind?

guys I am new to tailwind and trying to add space between table rows.
<table className="table-auto w-full shadow-md mt-5 rounded">
<thead className="bg-base-200 text-left text-gray-700 tracking-wider">
<tr>
<th className="p-4 ">Chapter Number</th>
<th className="p-4 ">Chapter Name</th>
<th className="p-4 ">Added at</th>
<th className="p-4 ">Status</th>
</tr>
</thead>
<tbody>
{chapters.map((chapter) => (
<tr className="bg-card mt-6 rounded" key={chapter.chapterNumber}>
<td className="p-4">{chapter.chapterNumber}</td>
<td className="p-4">{chapter.chapterName}</td>
<td className="p-4">{chapter.addedAt}</td>
<td className="p-4">{!chapter.published && 'Not published'}</td>
</tr>
))}
</tbody>
</table>
This does not add space between the table rows.
So,I have tried with mt-6 on each rows. It has no effect.
I have seen a similar question and used the answer here and have added border-spacing and border-seperate.
So, now my table row has these classes.
<table className="table-auto w-full shadow-md mt-5 border-spacing-2 border-separate rounded">
But this results in adding space around all the elements.
I do not understand why table row behaves this way and does not take the margin with mt-6.
But if i replace the rows with a div, it applies the margin top.
eg:
<div className="">
<th className="p-4 ">Chapter Number</th>
<th className="p-4 ">Chapter Name</th>
<th className="p-4 ">Added at</th>
<th className="p-4 ">Status</th>
</div>
<div className="mt-6 bg-card">
<th className="p-4 ">1</th>
<th className="p-4 ">Chapter Name</th>
<th className="p-4 ">04/2/2022</th>
<th className="p-4 ">Not published</th>
</div>
<div className="mt-6 bg-card">
<th className="p-4 ">1</th>
<th className="p-4 ">Chapter Name</th>
<th className="p-4 ">04/2/2022</th>
<th className="p-4 ">Not published</th>
</div>
First you need to split the borders with Tailwind border-separate property on the table tag, then add border-spacing-y-3 where: y is the axis and number is the height between row.
Utilities for controlling the spacing between table borders. Tailwind documentation
<script src="https://cdn.tailwindcss.com"></script>
<table class="table-auto w-full shadow-md mt-5 rounded bg-black border-separate border-spacing-y-3">
<thead class="text-left text-gray-500 tracking-wider">
<tr>
<th class="p-4">Chapter Number</th>
<th class="p-4">Chapter Name</th>
<th class="p-4">Added at</th>
<th class="p-4">Status</th>
</tr>
</thead>
<tbody class="">
<tr class="bg-card rounded text-gray-200 bg-neutral-900">
<td class="p-4">60001</td>
<td class="p-4"></td>
<td class="p-4">6/21/2022</td>
<td class="p-4">Not published</td>
</tr>
<tr class="bg-card rounded text-gray-200 bg-neutral-900">
<td class="p-4">60001</td>
<td class="p-4"></td>
<td class="p-4">6/21/2022</td>
<td class="p-4">Not published</td>
</tr>
</tbody>
</table>
Use border-seperate and border-spacing-y-4 to add vertical magin only.
<script src="https://cdn.tailwindcss.com"></script>
<div class="bg-black">
<table class="table-auto w-full shadow-md rounded border-separate border-spacing-y-4">
<thead class="text-white text-left bg-gray-900 tracking-wider">
<tr>
<th class="p-4 ">Chapter Number</th>
<th class="p-4 ">Chapter Name</th>
<th class="p-4 ">Added at</th>
<th class="p-4 ">Status</th>
</tr>
</thead>
<tbody class="">
<tr class="bg-stone-800 mt-6 text-white rounded" key={chapter.chapterNumber}>
<td class="p-4">{chapter.chapterNumber}</td>
<td class="p-4">{chapter.chapterName}</td>
<td class="p-4">{chapter.addedAt}</td>
<td class="p-4">{!chapter.published && 'Not published'}</td>
</tr>
<tr class="bg-stone-800 mt-6 text-white rounded" key={chapter.chapterNumber}>
<td class="p-4">{chapter.chapterNumber}</td>
<td class="p-4">{chapter.chapterName}</td>
<td class="p-4">{chapter.addedAt}</td>
<td class="p-4">{!chapter.published && 'Not published'}</td>
</tr>
<tr class="bg-stone-800 mt-6 text-white rounded" key={chapter.chapterNumber}>
<td class="p-4">{chapter.chapterNumber}</td>
<td class="p-4">{chapter.chapterName}</td>
<td class="p-4">{chapter.addedAt}</td>
<td class="p-4">{!chapter.published && 'Not published'}</td>
</tr>
<tr class="bg-stone-800 mt-6 text-white rounded" key={chapter.chapterNumber}>
<td class="p-4">{chapter.chapterNumber}</td>
<td class="p-4">{chapter.chapterName}</td>
<td class="p-4">{chapter.addedAt}</td>
<td class="p-4">{!chapter.published && 'Not published'}</td>
</tr>
</tbody>
</table>
</div>

How in tailwindcss table hide column on small devices?

With tailwindcss 2 I want to hide some columns in the table on small devices using sm:hidden:
<table class="table-auto">
<thead class="bg-gray-700 border-b-2 border-t-2 border-gray-300">
<tr>
<th class="py-2">Name</th>
<th class="py-2">Active</th>
<th class="py-2">Type</th>
<th class="py-2 sm:hidden">Category</th>
<th class="py-2 sm:hidden">Mailchimp Id</th>
<th class="py-2"></th>
</tr>
</thead>
<tbody>
<tr>
<td class="">
Name content
</td>
<td class="">
Active content
</td>
<td class="">
Typecontent
</td>
<td class=" sm:hidden">
Category content
</td>
<td class="sm:hidden">
mailchimp content
</td>
I expected that on devices 640px and smaller 2 columns would be hidden, but failed.
Which syntax is correct?
Thanks
Tailwind uses a mobile first breakpoint system, meaning, if you use hidden class, it will affect all the screen sizes. But if you attach a prefix such as sm, md, lg, xl and 2xl it will target the corresponding screen size and above it. You can read more about it here.
For example, using sm:hidden will only hide the element on sm and above screen size. So, you could combine it together such as, hidden md:table-cell it will not show on screen size lower than sm breakpoint.
Here you have an example.
<table class="whitespace-nowrap">
<thead>
<tr>
<th>Name</th>
<th>Active</th>
<th class="hidden md:table-cell">Type</th>
<th class="hidden md:table-cell">Category</th>
<th class="hidden lg:table-cell">Mailchip</th>
<th class="hidden lg:table-cell">other</th>
</tr>
</thead>
<tbody>
<tr>
<td>Name content</td>
<td>Active content</td>
<td class="hidden md:table-cell">Type content only in md</td>
<td class="hidden md:table-cell">Category content only in md</td>
<td class="hidden lg:table-cell">Mailchip content only in lg</td>
<td class="hidden lg:table-cell">other content only in lg</td>
</tr>
</tbody>
</table>
And here you have all the display properties to play around with a bit more. (Remember to combine it with the hidden property + the responsive variants sm:, md:, lg:, xl:, 2xl:)
as a more general answer, not regarding table cells, another display type can be chosen to overwrite hidden.
https://tailwindcss.com/docs/display
example:
<p class="hidden lg:flex">Only visible on larger than lg</p>

Angular material: table inside mat-expansion not aligned to other table

Im added to Angular website for table ,that table col is not align ,any one know how to do that correctly ?
Thanks
<div class="table-responsive ">
<table class="table" style="width: 100%">
<thead>
<tr>
<th scope="col" width="50">ID</th>
<th scope="col" width="300">Book Name</th>
<th scope="col" width="250">Total Book</th>
<th scope="col" width="250">Date</th>
<th scope="col" width="250">Remarks</th>
<th scope="col" width="250">Booking Date</th>
<th scope="col" width="250">Booking Status</th>
</tr>
</thead>
</table>
</div>
<mat-accordion>
<mat-expansion-panel (opened)="panelOpenState = true; openContent(order)"
(closed)="panelOpenState = false">
<mat-expansion-panel-header [collapsedHeight]="customCollapsedHeight"
[expandedHeight]="customExpandedHeight">
<table class="table ">
<tbody>
<tr>
<td class="tb-td-txt" width="50">12</td>
<td class="tb-td-txt" width="300">ABC</td>
<td class="tb-td-txt" width="250">100}</td>
<td class="tb-td-txt" width="250">10-11-2018</td>
<td class="tb-td-txt" width="250">Jhone Doe</td>
<td class="tb-td-txt" width="250">10-05-2019</td>
<td class="tb-td-txt" width="250">Completed</td>
</tr>
</tbody>
</table>
</mat-expansion-panel-header>
</mat-accordion>
There are a few things...
the lower table is a child of .mat-expansion-panel, so for the table outside it, we mimic the padding:0 24px
but there is also a down arrow for mat-expansion-indicator so we gotta increase the padding on the right side by 8px
next, we text-align center for the bottom table (which is inside the mat-expansion) so that the effect is similar
I have also put a red borders on the <td> so that you can see this
relevant CSS:
th, td{border:1px solid red;}
.table-responsive>.table{padding:0 32px 0 24px;}
mat-expansion-panel-header .table td{text-align: center}
complete working stackblitz here

Bulma table rows not spanning full width

I have a is-fullwidth Bulma table in my angular7 app which works fine. But now I need to add a angular component inside each row, and td tags lies inside that component. But when I do that, table rows not spans to its full width.
<table class="table is-fullwidth">
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let row of rows">
<app-table-row [details]="row">
</app-table-row>
</tr>
</tbody>
</table>
table-row.component.html
<td>
<div *ngIf="!showField"> {{ details.name }} </div>
<div *ngIf="showField" class="field">
<div class="control">
<input [value]="details.name" class="input" type="text">
</div>
</div>
</td>
<td>{{ details.address }}</td>
This outputs the following
if I remove the angular component and add tds normally, it spans correctly. Like this.
Why is it not work when angular component added? And how should I fix it?
The reason this is happening is probably because the td elements are not direct children of the tr, but children of the app-table-row component.
Not sure that it would make sense in your case, but you can change the selector property in the app-table-row component to select the tr itself, like so:
#Component({
selector: 'tr[app-table-row]',
...
})
export class AppTableRowComponent {}
And use it like this:
<table class="table is-fullwidth">
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let row of rows" app-table-row [details]="row">
</tr>
</tbody>
</table>
This way the app-table-row is not a separate node in the DOM.

How to style a link inside a table with Bootstrap's helping classes?

I´m sure it's an easy issue to solve but I'm unable to find the right way to do it.
I have a web with Bootstraps (hero-bootstrap template).
I have a link in a table and I'm unable to apply a helping class like "text-success" to the link.
Check the fiddle: https://jsfiddle.net/9dt5zo74/2/
<div class="table-responsive">
<table class="table table-condensed table-hover table-striped text-center">
<thead>
<tr class="active">
<th class="text-center">text</th>
<th class="text-center">text</th>
<th class="text-center">text</th>
<th class="text-center">text</th>
</tr>
</thead>
<tbody>
<tr>
<td><a class="text-success" href="#">something should work and it doesnt</a></td>
<td>text</td>
<td>text</td>
<td>text</td>
</tr>
</tbody>
</table>
</div>
<p class="text-success">that works</p>
As you will see, the class works for elements outside the table.
style in line 6400
table a:not(.btn),
.table a:not(.btn) {
color: #fff;
text-decoration: underline;
}
gets higher priority

Resources