How can disable inheritance in tailwind? - html

i use tailwind-css for styling my components in Nuxtjs
and my components .
and this is my style classes
.ccard {
#apply bg-white px-2 py-10;
&-header {
#apply relative;
&-btn {
#apply btn btn-circle btn-sm btn-primary px-0 border-0;
}
&-img {
// width: 100%;
// height: 100%;
#apply object-cover w-full;
}
}
&-detail {
#apply text-center pt-2 px-1;
&-cat {
#apply text-sm text-gray-400 mb-2;
}
&-title {
#apply text-lg font-semibold;
}
}
}
and this is my component template
<template>
<div class="ccard">
<div class="ccard-header">
<button class="ccard-header-btn">
<magnify />
</button>
<img :src="data.src" class="ccard-header-img" alt="" />
</div>
<div class="ccard-detail">
<div class="ccard-detail-cat">{{ data.cat }}</div>
<div class="ccard-detail-title">{{ data.title }}</div>
</div>
</div>
</template>
and this is result
i expect the button to be a circle BUT it is not !
i got inspect and it inhertited a min-height from some-where i dont know where it is .
and i think in tailwind child tags inherit their parent tags classes and i think this is not good .
how can i disable heritance in tailwind-css

I think the inheritance in 'tailwind-css' isn't your problem. I have two answers to this question:
1)you have to check to magnify inside the btn, maybe it has a little x-margin or x-padding
2)you have to set specific width and height (w-10 h-10) classes in the icon.
look at this code:
<button class="bg-green-400 rounded-full">
<img class="w-10 h-10 p-2" src="https://img.icons8.com/ios-glyphs/30/000000/search--v1.png"/>
</button>

Related

Issue with custom table in ReactJS

I am trying to create a custom table as per the design and I am using Tailwind CSS for styling. I initially used table tags in HTML, but I was unable to style it properly, so I switched to using divs. However, I am facing an issue with the width size of the table head and row. The width is not consistent due to the content of the cells, can anyone please help me in resolving this issue?
Design:
What I get using div instead of table tags:
code:
<div className="flex flex-col">
<div className='flex text-sm font-bold text-gray-500'>
<div className='w-[170px] mr-2'>ລູກຄ້າ</div>
<div className='w-[140px] mr-2'>ປະກັນໄພ</div>
<div className='w-[120px] mr-2'>ບໍລິສັດ</div>
<div className='w-[90px]'>ວັນທີເລີ່ມຕົ້ນ</div>
<div className='w-[90px]'>ວັນທີສິ້ນສຸດ</div>
<div className='w-[90px]'>ສະຖານະ</div>
<div className='flex-1'>ຄ່າທຳນຽມ</div>
<div className='w-[170px]'></div>
</div>
</div>
<hr />
<div className='max-h-[700px] overflow-y-scroll shadow-sm insuranceReport rounded-b bg-white'>
{
orders?.map((order, index)=>(
<div key={index} className={`flex items-center px-5 ${index%2 == 0 ? 'bg-[#F5FCF5]' : 'bg-[#FFFFFF]'} py-4 text-sm hover:opacity-80`}>
<div className='w-[170px] flex items-center mr-2 font-sans text-sm'>
<div className="w-[2.3rem] h-[2.3rem] mr-2 rounded-full overflow-hidden aspect-square">
<img
src={
GetImageUrl(order?.profile, true)
}
alt={order?.name}
className="object-cover w-full h-full"
onError={({ currentTarget }) => {
currentTarget.onerror = null; // prevents looping
currentTarget.src= IMAGE_NOT_FOUND;
}}
/>
</div>
<div className='flex flex-col'>
<Link className='cursor-pointer hover:underline' to={`/sale/${order?.uuid}`}>
<span>{order?.customer?.name}</span>
</Link>
<span className='text-sm opacity-80'>{order?.customer?.gender}</span>
</div>
</div>
<div className='w-[140px] mr-2 text-sm'>{order?.product}</div>
<div className='w-[120px] mr-2 text-sm'>{order?.company}</div>
<div className='w-[90px] text-sm'>{FormatDate(order?.dateStart)}</div>
<div className='w-[90px] text-sm'>{FormatDate(order?.dateEnd)}</div>
<div className='w-[90px] text-sm'><span className={SaleStatusChecker(order?.status)?.color}>{SaleStatusChecker(order?.status)?.status}</span></div>
<div className='flex-1 text-sm'>₭ {order?.totalPrice?.toLocaleString()}</div>
<div className='flex items-start gap-3 text-sm'>
<button className='px-3 py-1 border-2 rounded-lg outline-none active:opacity-50 text-primary' onClick={()=>navigate(`/sale/${order?.uuid}`)}><FontAwesomeIcon icon={faEye}/> ລາຍລະອຽດ</button>
<button className='px-3 py-1 border-2 rounded-lg outline-none active:opacity-50 text-primary' onClick={()=>handleOpenOrderUpdateModal(order?.uuid)}><FontAwesomeIcon icon={faPenToSquare}/> ແກ້ໄຂ</button>
</div>
</div>
))
}
{
(orders?.length == 0 || orders == undefined || orders == []) && !isFetching ? <Empty/> : ""
}
{
isFetching &&
<LoadingData/>
}
</div>
</div>
I don't want to use flex and set a static width size of the div element. Is there any alternative solution to this issue?
Thank you in advance for your help!

tailwind css change color of button on focus

i have two buttons, blue and red.
i want to set when red button is clicked/focused blue button should also turn red.
how can i achieve it using tailwind css.
<link href="https://unpkg.com/tailwindcss#^2/dist/tailwind.min.css" rel="stylesheet"/>
<button class="focus:bg-red-700 bg-red-400 font-bold px-4 py-4 rounded-lg">
RED
</button>
<button class=" bg-blue-400 font-bold px-4 py-4 rounded-lg">
BLUE
</button>
Tailwind don't has the css combinators. Guess, you have two way to achieve this.
Create an extra css file and add this line
button.bg-red-400:focus ~ button.bg-blue-400 {
background-color: rgba(185, 28, 28, 1);
}
button.bg-red-400:focus~button.bg-blue-400 {
background-color: rgba(185, 28, 28, 1);
}
<link href="https://unpkg.com/tailwindcss#^2/dist/tailwind.min.css" rel="stylesheet" />
<button class="focus:bg-red-700 bg-red-400 font-bold px-4 py-4 rounded-lg">RED</button>
<button class="bg-blue-400 font-bold px-4 py-4 rounded-lg">BLUE</button>
You can create your own custom class with #Variants documentation, but not with a link from CDN.
Before using the CDN build, please note that many of the features that make Tailwind CSS great are not available without incorporating Tailwind into your build process. documentation
Solution with javascript
// Select all elements with `bg-blue-400` class
const blueBox = document.querySelectorAll('.bg-blue-400');
const changeColor = ev => {
// Define button in the DOM
const button = ev.target.closest('button');
// Check, if the button was clicked
if (button) {
// Chenge color in the DIVs
for (const box of blueBox) {
box.classList.add('bg-red-400');
box.classList.remove('bg-blue-400');
}
return;
}
// If clicked outside, the color will switch back
for (const box of blueBox) {
box.classList.add('bg-blue-400');
box.classList.remove('bg-red-400');
}
};
document.addEventListener('click', changeColor);
<link href="https://unpkg.com/tailwindcss#^2/dist/tailwind.min.css" rel="stylesheet" />
<div>
<button class="one bg-red-400 font-bold px-4 py-4 rounded-lg">RED</button>
<div class="bg-blue-400 font-bold px-4 py-4 rounded-lg">BLUE</div>
</div>
<div class="two bg-blue-400 font-bold px-4 py-4 rounded-lg">APPLY COLOR HERE</div>

Can you resolve my problem the link is not working but when i copy and paste the link from console it.s working?

the link is not working <Link href={/blog/detail?id=${blog._id}}.
{ data && data.map((blog) => (
<div className="col-12 col-lg-3 mb-6 mb-lg-0" key={blog._id}>
{/* Blog Card */}
<div className="card border-0 bg-transparent">
{/* <div className="position-absolute bg-white shadow-primary text-center p-2 rounded ml-3 mt-3">15
<br/>July
</div> */}
{/*<img className="card-img-top shadow rounded" src={`${blog.productImages[0]}`} alt="Image"/>*/}
<Link href={`/blog/detail?id=${blog._id}`}><img className="card-img-top shadow rounded_img" style={{height: "170px", width: "253px"}} src={BASE_URL+blog.productImages[0]} alt="Img"/></Link>
In HTML Links use the <a> element not link, try replacing your last line with:
<a href={`/blog/detail?id=${blog._id}`}><img className="card-img-top shadow rounded_img" style={{height: "170px", width: "253px"}} src={BASE_URL+blog.productImages[0]} alt="Img"/></a>
See here:
https://www.w3schools.com/tags/tag_a.asp
Also, try replacing className with class throughout your code.

Navbar transition in tailwindcss

I have a navbar and I want that when someone clicks on the button, it will be open with transition in tailwindcss.
const bar_btn = document.getElementById('BAR-BTN');
const mobileMenu = document.getElementById('mobileMenu')
bar_btn.addEventListener(
'click',
function() {
mobileMenu.classList.toggle('w-0')
mobileMenu.classList.toggle('h-0')
}
)
<nav>
<div>
<button id='BAR-BTN'><i class="fas fa-bars fa-2x"></i></button>
</div>
<ul class="transition-transform ease-linear h-0 w-0 delay-1000 duration-1000"
id="mobileMenu">
<li>test-1</li>
<li>test-2</li>
<li>test-3</li>
</ul>
</nav>
This is my HTML and javascript code and it doesn't work.
I could not find a way to give transition to height property(which we can in pure css) so i used scale to give a transition effect
See if it helps
const bar_btn = document.getElementById('BAR_BTN');
const mobileMenu = document.getElementById('mobileMenu')
bar_btn.addEventListener('click',function()
{
mobileMenu.classList.toggle('transform');
}
);
<link href="https://unpkg.com/tailwindcss#^2/dist/tailwind.min.css" rel="stylesheet"/>
<nav>
<div>
<button id="BAR_BTN" class="px-10 py-2 mt-10 mx-10 w-1/4 hover:bg-green-300 bg-green-200 transition-all">button</button>
</div>
<ul id="mobileMenu" class="mx-10 p-5 border-4 border-black overflow-hidden text-xl w-1/4 origin-top duration-300 scale-y-0" >
<li>Test-1</li>
<li>Test-2</li>
<li>Test-3</li>
</ul>
</nav>
I have the same problem just now and I solve it.
// .tsx
navigationRef.current?.classList.toggle('active');
<div ref={navigationRef} className=' w-[80px] ... duration-500 transition-all ' />
// index.css
#tailwind base;
#tailwind components;
#tailwind utilities;
#layer components {
.active {
width: 300px !important;
}
}
The effect as follow:
No need scale and the effect may be not as expected.

Bootstrap button shifted to the right outside containing div

I've got a very simple bootstrap page that has a div with a column of buttons in it. For no apparent reason the buttons are shifted slightly to the right:
The containing div (as highlighted above) has no padding or margin:
and the buttons have the same margin applied on both sides:
associated html:
<div class="cinema-list" style="max-width: 700px;" class="m-auto">
<h2>Select Cinema</h2>
<div class="form-group">
<label for="cinemaSearchInput">Search</label>
<input
type="text"
class="form-control"
id="cinemaSearchInput"
placeholder="Cinema name..."
[(ngModel)]="searchString"
>
</div>
<div *ngIf="cinemaList != null">
<button
*ngFor="let cinema of cinemaList"
type="button"
class="btn btn-primary cinema-button m-1 w-100"
(click)="selectCinema(cinema)"
>
<span>{{cinema.name}}</span>
<i
*ngIf="isFavoriteCinema(cinema)"
class="fa fa-heart pl-1 heart"
aria-hidden="true"></i>
</button>
</div>
<div
*ngIf="cinemaList == null"
class="d-flex align-items-center pt-2">
<span class="spinner-border text-primary mr-2"></span>
<span>Loading...</span>
</div>
</div>
and scss:
.cinema-list{
.cinema-button{
display: block;
}
.cinema:hover{
background-color: $primary;
}
}
I can't see any reason why the buttons are shifted. Is this a bug with bootstrap?
This is boostrap 4.3.1
Thanks
It is because you added class m-1 in the button. please remove that class from it.
Replace
class="btn btn-primary cinema-button m-1 w-100"
With
class="btn btn-primary cinema-button w-100"
Hope this will help you.
Please use my-1 and mx-0 class instead of m-1 class.
<button
*ngFor="let cinema of cinemaList"
type="button"
class="btn btn-primary cinema-button my-1 mx-0 w-100"
(click)="selectCinema(cinema)"
>
<span>{{cinema.name}}</span>
<i
*ngIf="isFavoriteCinema(cinema)"
class="fa fa-heart pl-1 heart"
aria-hidden="true"></i>
</button>
Alok Mali's answer fixes the issue.
I can't see any reason why the buttons are shifted. Is this a bug with bootstrap?
No, this is how CSS is designed.
This is the root cause why this is happen.
In Bootstrap by default all elements have box-sizing:border-box;
This is done by reset.scss
*,
*::before,
*::after {
box-sizing: border-box; // 1
}
For border-box the width and height properties of an element include the content, padding, and border, but do not include the margin. In your case you have a margin of .25em when you applied class m-1, thats why your element (button in this case) overlflowed.
More read of box-sizing here
Hope this helps.