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.
Related
I followed the tutorial perfectly and made a website while using the live server extension and it was applying. I use Chrome, Edge and Opera to check the .html file itself but no style was applied in the matter of fact it still shows as applied in the live server extension
The files
index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link href="/dist/output.css" rel="stylesheet" />
</head>
<body class="m-0 bg-blue-500 p-0">
<nav class="m-0 h-full w-full space-x-4 border bg-blue-400 p-6">
<h1 class="text-3xl font-bold drop-shadow-md md:filter-none">
Safer Internet
</h1>
<div
class="absolute top-7 right-14 space-x-10 stroke-2 font-extrabold hover:stroke-black"
>
<button
class="position inline-block text-right duration-700 hover:text-white"
>
Home
</button>
<button
class="position inline-block text-right duration-700 hover:text-white"
>
More
</button>
<button
class="position inline-block text-right duration-700 hover:text-white"
>
About
</button>
</div>
</nav>
<div>
<p
class="absolute left-64 top-2/4 translate-x-2/4 translate-y-2/4 text-center text-xl font-bold"
>
To be able to know how to be safe on the internet click the button
bellow.
</p>
<div
class="relative top-96 flex translate-y-2/3 items-center justify-center text-center"
>
<button
class="relative top-9 w-1/6 rounded-xl bg-blue-900 p-4 font-extrabold duration-700 hover:bg-black hover:text-white"
>
More
</button>
</div>
</div>
</body>
</html>
tailwind.config.js:
module.exports = {
content: ["./src/**/*.{html,js}", "./public/*.html"],
theme: {
extend: {},
},
plugins: [],
}
style.css:
#tailwind base;
#tailwind components;
#tailwind utilities;
I tried everything and it didn't work and I expected the styles to apply on my browser.
Proper approach to follow when using Tailwind-CLI
1. Know about your file structure. Use:
public
|_ tailwind_base.css
👆 File from which the output.css is produced
#tailwind base;
#tailwind components;
#tailwind utilities;
|_ output.css
src
|_ index.html
👆 Link with the output.css using
<link href="../public/output.css" rel="stylesheet" />
Watch it as
npx tailwindcss -i ./public/tailwind_base.css -o ./public/output.css --watch
Specify your html/js in tailwind.config.js
module.exports = {
content: ["./src/**/*.{html,js}"],
theme: {
extend: {},
},
plugins: [],
};
Use tailwindcss classes happily in your index.html file 😇
Sorry this may be badly described but will try to describe this as best as I can:
What I'm trying to do is put so the Redux dropdown menu dropdown element is an element in the menu (next to about/Signout as shown in the below screenshot).
For some reason the dropdown element is not coming up at all in the menu, but does work in the index file.
The code for the menu is below,
Dropdown.js
import * as DropdownMenu from '#radix-ui/react-dropdown-menu';
export default function dropdownUserProfileMenu (){
return (
<>
<DropdownMenu.Root>
<DropdownMenu.Trigger className='trigger'>Settings</DropdownMenu.Trigger>
<DropdownMenu.Content>
<DropdownMenu.Item>New Item</DropdownMenu.Item>
<DropdownMenu.Item>New Item 2</DropdownMenu.Item>
<DropdownMenu.Item>New Item 3</DropdownMenu.Item>
<DropdownMenu.Item>New Item 4</DropdownMenu.Item>
</DropdownMenu.Content>
</DropdownMenu.Root>
</>
)
};
Menu.js
<div className='w-screen mr-3 h-16 drop-shadow-md rounded-sm bg-white'>
<div className='container px-4 md:px-0 h-full mx-auto flex justify-between items-center shadow-md'>
<div className='text-primaryBlack'>
<Link href='/'><p className='pl-4'>Student sharing site</p></Link>
</div>
<div>
<ul id='menuElements' className={`right-0 px-10 py-16 text-primaryBlack z-50 flex mx-auto flex-row space-x-4 items-center`}>
<Link href='/about'><li>About</li></Link>
<li><dropdownUserProfileMenu></dropdownUserProfileMenu></li>
<Link href='/api/auth/signout'><li onClick={e => {
e.preventDefault()
signOut()
}}>Signout</li>
</Link>
</ul>
</div>
</div>
</div>
// </div>
(Ignore the signin/signout methods, am using NextAuth specifically but this is not needed to get this working)
Hope helping for this is possible!
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>
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>
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.