Tailwind tab underline not complete filling - tailwind-css

As you can see the underline is not complete filling the full length. I couldn't figure out why that is. This is made with tailwindCSS. Does anyone know where to find the full width for the deposit part?
My code:
function MyPage() {
const [currentTab, setCurrentTab] = useState<string>("deposit");
const [inputValue, setInputValue] = useState<string>("");
const [outputValue, setOutputValue] = useState<string>();
function classNames(...classes: string[]) {
return classes.filter(Boolean).join(" ");
}
return(
<div className="text-sm font-medium text-center text-gray-500 border-b border-gray-200 dark:text-gray-400 dark:border-gray-700">
<ul className="flex flex-wrap -mb-px">
<li className="mr-2 ">
<button
onClick={() => {
setCurrentTab("withdrawal"),
currentTab != "withdrawal" ? setOutputValue("") : null;
}}
className={classNames(
currentTab == "withdrawal"
? "text-gray-100 border-gray-100 inline-block p-4 border-b-2 rounded-t-lg active"
: "inline-block p-4 rounded-t-lg active transition-all ease-in duration-100"
)}
>
Withdrawal
</button>
</li>
<li className="mr-2 ">
<button
onClick={() => {
setCurrentTab("deposit"), currentTab != "deposit" ? setOutputValue("") : null;
}}
className={classNames(
currentTab == "deposit"
? " text-gray-100 border-gray-100 inline-block p-4 border-b-2 rounded-t-lg active"
: "inline-block p-4 rounded-t-lg active transition-all ease-in duration-100 "
)}
aria-current="page"
>
Deposit
</button>
</li>
</ul>
</div>
)
}

mr-2 class for button's wrapper element <li className="mr-2"> creates margin on the right side of every li element of the loop - so your button cannot stretch on full width.
By adding last:mr-0 we're simply removing margin for the last child element of the loop. The example for this can be found here
<li className="mr-2 last:mr-0">
Compiled CSS for last:mr-0 would look like
.last\:mr-0:last-child {
margin-right: 0px;
}

Related

Why is z-index not working even after providing the position and the index

I wanted to place an absolute element behind a relative element. I have mentioned the z-index and also the position for the respective elements but its still not working.
This is what i had tried. I am using tailwind css.
const Searchbar: React.FC<SearchbarProps> = ({}) => {
const [toggle, setToggle] = useState(false);
const [dropdown, setDropdownToggle] = useState(false);
return (
<form className={`search flex items-center basis-9/12 pl-5 z-20 relative ${toggle ? 'dark': ''}`} >
{/* ICON */}
<svg className="ico-svg" viewBox="0 0 20 20" width="20">
<path d="M20,8c0-4.4-3.6-8-8-8C7.6,0,4,3.6,4,8c0,1.9,0.6,3.6,1.7,4.9L0,18.6L1.4,20l7.1-7.1l-0.2-0.2 C6.9,11.6,6,9.9,6,8c0-3.3,2.7-6,6-6c3.3,0,6,2.7,6,6c0,3.3-2.7,6-6,6c-0.7,0-1.3-0.1-1.9-0.3L10,13.7l-1.5,1.5l0.4,0.2 c1,0.4,2.1,0.7,3.2,0.7C16.4,16,20,12.4,20,8z"></path>
</svg>
{/* INPUT */}
<input className='basis-9/12 pl-3 h-[48px] text-sm font-normal self-stretch'
onClick={() => setToggle(!toggle)}
type="text" name="search" id="search" placeholder='Search websites, elements, courses...'/>
<div className="dropdown flex items-center justify-end basis-3/12">
{/* TEXT */}
<p className='uppercase mr-9 font-light text-xs'>in</p>
{/* DROPDOWN */}
<button className={`flex items-center h-[48px] justify-between basis-6/12 px-5 ${dropdown ? 'active': ''}`} onClick={(e) => {
e.preventDefault();
setDropdownToggle(!dropdown)
}
}>
<p className="capitalize text-sm font-medium">inspiration</p>
<svg className="dropdown__arrow" viewBox="0 0 20 20" width="12">
<path d="M1.6,4.1c-0.4,0-0.9,0.2-1.2,0.5c-0.7,0.7-0.7,1.7,0,2.4l8.4,8.4c0.7,0.7,1.7,0.7,2.4,0L19.5,7 c0.7-0.7,0.7-1.7,0-2.4c-0.7-0.7-1.7-0.7-2.4,0L10,11.8L2.8,4.6C2.5,4.3,2.1,4.1,1.6,4.1z"></path>
</svg>
</button>
{/* DROPDOWN LIST */}
<div className={`dropdown__list capitalize px-5 pt-2 pb-5 text-sm font-normal ${dropdown ? 'active' : ''}`}>
<ul>
<li className='pt-3'>Inspiration</li>
<li className='pt-3 pl-3'>websites</li>
<li className='pt-3 pl-3'>elements</li>
<li className='pt-3 pl-3'>collections</li>
<li className='pt-3'>blog</li>
<li className='pt-3'>courses</li>
<li className='pt-3'>directory</li>
</ul>
</div>
</div>
{/* SUGGESTIONS */}
<div className="search__suggestions z-10 absolute">
</div>
</form>
);
}
This is how its behaving:
Actual behaviour
Instead of this:
Expected Behaviour
As you can see i have a form tag that is relative places and i also have a div with the class "search__suggestions" that is placed absolutely. I have also provided the appropriate index to the tags, but for some reason its not working. Anyone knows why its behaving this way?

Recommended way of hiding elements until animation is complete Tailwind CSS?

Demo
The text expands in width with the sidebar, when ideally, the transition is much smoother. Maybe delayed so it tricks the eye to thinking it appears only when fully expanded.
How should I think about achieving this?
<div className="flex flex-col items-center space-y-4 w-full mt-8">
<Logo open={open} />
{open && <div className="h-auto w-3/4 text-xs text-slate-400">This looks weird when the line is super long</div>}
....
Should I attach a delay? transition-delay does nothing on the inner div.
I made an over simplified example, hopefully can help finding a good solution.
This one uses a cross delay approach to make the transition smooth when opening and closing the drawer.
There is a state open controls the drawer. On the drawer container:
open ? "w-52 delay-0" : "w-12 delay-300"
And the inside element uses a reversed delay value, something like:
open ? "opacity-100 delay-300" : "opacity-0 delay-0"
This way, during both opening and closing transition, parent and child elements will have properly ordered animation.
Live demo is here: stackblitz
import React, { useState } from "react";
import "./App.css";
const list = ["Option 1", "Option 2", "Option 3", "Option 4", "Option 5"];
const ListItem = ({ open, text, index }) => {
return (
<li className={`bg-teal-100 h-12 flex justify-end`}>
<div
className={`${
open ? "w-24" : "w-12"
} flex justify-center align-center p-3 transition-all duration-300 ease-in-out`}
>
{index + 1}
</div>
<div
className={`${
open ? "opacity-100 delay-300" : "opacity-0 delay-0"
} flex justify-center align-center p-3 flex-1 transition-all duration-300 ease-in-out`}
>
{text}
</div>
</li>
);
};
const SlideDrawer = ({ open }) => {
return (
<div
className={`${
open ? "w-52 delay-0" : "w-12 delay-300"
} absolute left-0 top-0 bottom-0 transition-all duration-300 ease-in-out overflow-hidden flex justify-start bg-pink-200`}
>
<div className={`w-52 flex flex-col justify-start align-end`}>
<div
className={`${
open ? "w-52 p-6 delay-300" : "w-12 p-3 delay-0"
} h-72 flex flex-col justify-start align-center transition-all duration-300 ease-in-out `}
>
<figure
className={`${
open ? "w-40 h-40 delay-300" : "w-6 h-6 delay-0"
} transition-all bg-teal-100 rounded-full duration-300 ease-in-out`}
></figure>
</div>
<ul className="w-full flex flex-col list-none">
{list.map((item, index) => (
<ListItem key={item} open={open} index={index} text={item} />
))}
</ul>
</div>
</div>
);
};
function App() {
const [drawerOpen, setDrawerOpen] = useState(false);
return (
<>
<div className="w-full flex justify-end">
<button
className="m-6 p-3 rounded-lg bg-slate-300"
onClick={() => setDrawerOpen((prev) => !prev)}
>
Open drawer
</button>
</div>
<SlideDrawer open={drawerOpen} />
</>
);
}
export default App;

How to implement a bg-images slider with tailwind css?

Can someone advise or provide an example on how to implement a background image slider with tailwind css?
To set a single image i have to do as follow:
module.exports = {
content: [
"./pages/**/*.{js,ts,jsx,tsx}",
"./components/**/*.{js,ts,jsx,tsx}",
],
theme: {
extend: {
backgroundImage: {
'my_bg_image' : "url('../public/bg.png')",
}
},
},
plugins: [],
}
then return a div as follow:
return (
<div
className="bg-scroll bg-[url('/bg.png')] h-[972px]"
>
</div>
)
What if i want this image change, is it possible?
Mainly you will need Carousel wrapper, Slider indicators , Slider controls.
Refer to this :
https://tailwind-elements.com/docs/standard/components/carousel/
OR you can use this:
html file:
<body>
<section>
<div class="relative">
<ul id="slider">
<li class="h-[50vh] relative">
<img class="h-full w-full object-cover" src="./img1.jpg" alt="">
</li>
<li class="h-[50vh] relative hidden">
<img class="h-full w-full object-cover" src="./img2.jpg" alt="">
</li>
<li class="h-[50vh] relative hidden">
<img class="h-full w-full object-cover" src="./img3.jpg" alt="">
</li>
</ul>
<div class="absolute px-5 flex h-full w-full top-0 left-0">
<div class="my-auto w-full flex justify-between">
<button onclick="prev()" class="bg-white p-3 bg-opacity-70 shadow-lg rounded-full">Button 1</button>
<button onclick="next()" class="bg-white p-3 bg-opacity-70 shadow-lg rounded-full">Button 2</button>
</div>
</div>
</div>
</section>
<script src="./main.js"></script>
</body>
js file:
curr_slide = 1;
sliderElemnt = document.getElementById('slider');
total_slides = sliderElemnt.childElementCount;
// console.log(total_slides);
function next() {
if (curr_slide < total_slides) {
curr_slide++;
slide_show();
}
}
function prev() {
if (curr_slide > 1) {
curr_slide--;
slide_show();
}
}
function slide_show() {
slides = document.getElementById('slider').getElementsByTagName('li');
for (let index = 0; index < total_slides; index++) {
const element = slides[index];
if(curr_slide == index+1){
element.classList.remove('hidden');
}else{
element.classList.add('hidden');
}
}
}

how to apply smooth transition on the element using tailwind css

I have make a nav bar using tailwind css in next.js it is responsive and working fine.
every thing is working fine but when i click the burger button it shows the list appears instantly but i want to show the bottom (un-order list) on small screen size with smooth transition and i have no idea how to make the transition smooth
the solution of the problem i want is to make this list smoothly visible on the screen
`
import React, { useEffect, useState } from "react";
import { MenuAlt1Icon } from "#heroicons/react/outline";
function Header() {
const [isOpen, setisOpen] = React.useState(false);
const [size, setSize] = useState(0);
function handleClick() {
setisOpen(!isOpen);
}
useEffect(() => {
setSize(window.innerWidth);
window.addEventListener("resize", handleSize);
return () => window.removeEventListener("resize", handleSize);
}, []);
const handleSize = () => {
setSize(window.innerWidth);
};
`
Above is the javascript code
and below is the jsx and tailwind
return (
<header>
<nav className={` shadow-md px-5 ${isOpen && size < 640 && "pb-3"}`}>
<button
type="button"
className={`${
size >= 640 ? "hidden" : "inline-block h-12 focus:outline-none"
}`}
onClick={handleClick}
>
<MenuAlt1Icon className="h-6 w-6" />
</button>
<ul
className={` ${
size >= 640
? " flex h-12 items-center space-x-2 "
: `${
isOpen
? `block space-y-2 border-t-2 border-gray-50 pt-2 transition duration-500 ease-linear`
: `hidden`
}`
}`}
>
<li>element 1</li>
<li>element 2</li>
<li>element 3</li>
<li>element 4</li>
</ul>
</nav>
</header>
);
}
export default Header;
If you want a top to down dropdown animation, try to read: Animating max-height with CSS transitions
You can try to use these tailwind classes transition-all max-h-screen max-h-0.
Now don't be like me. By applying the class md:hidden to the navbar and hamburger you don't have to check if it clicked again to show the ul or navlinks. What you should instead do is add a negative -translate-x-full to make it display off screen and when the hamburger is clicked add a translate-x-0 to make it show by sliding into the screen. If not the sidebar won't be animated
do this
<ul className={`md:hidden flex flex-col fixed left-0 w-3/4 h-screen top-[60px] bg-green-300 items-center justify-around transition-all ease-in-out duration-200 ${isNavExpanded ? "translate-x-0 " : "-translate-x-full"}`}>
{links.map((item) => (
<li key={`link-${item}`} className="nav-link">
<a href={`#${item}`} className="">
{item}
</a>
</li>
))}
</ul>
instead of this
{isNavbarExpanded && (<ul className={`md:hidden flex flex-col fixed left-0 w-3/4 h-screen top-[60px] bg-green-300 items-center justify-around transition-all ease-in-out duration-200 ${isNavExpanded ? "translate-x-0 " : "-translate-x-full"}`}>
{links.map((item) => (
<li key={`link-${item}`} className="nav-link">
<a href={`#${item}`} className="">
{item}
</a>
</li>
))}
</ul>)}
Try this,
I used Tailwind Responsive Design way in official Doc.
so you can adjust sm: or md: in the className
import { useState } from "react";
import { MenuAlt1Icon } from "#heroicons/react/outline";
export default function Header() {
const [isOpen, setisOpen] = useState(false);
return (
<header>
<nav className={"shadow-md px-5 pb-3 sm:pb-0"}>
<button
type="button"
className={"inline-block h-12 focus:outline-none sm:hidden"}
onClick={() => setisOpen(!isOpen)}
>
<MenuAlt1Icon className="w-6 h-6" />
</button>
<ul className={`"flex flex-col sm:flex-row w-full h-auto justify-between space-x-2 space-y-2" ${isOpen === false && "hidden sm:flex"}`}>
<li>element 1</li>
<li>element 2</li>
<li>element 3</li>
<li>element 4</li>
</ul>
</nav>
</header>
);
}
Happy coding :)

Display Block not moving to next line

I'm trying to make a responsive nav bar with tailwind. I want to list my nav links with a single item in each row when I open the navigation menu. Right now, they are all on one row. I'm using tailwind:
function Navbar() {
const [isOpen, setIsOpen] = useState(false);
return (
<header>
<div className="flex flex-col max-w-screen-xl px-4 mx-auto md:items-center md:justify-between md:flex-row md:px-6 lg:px-8">
<div className="p-4 flex flex-row items-center justify-between">
<Link href="/">
<a className="text-lg font-semibold tracking-widest text-gray-900 uppercase rounded-lg dark-mode:text-white focus:outline-none focus:shadow-outline">
Header
</a>
</Link>
<button
className="md:hidden rounded-lg focus:outline-none focus:shadow-outline"
onClick={() => {
setIsOpen(!isOpen);
}}
>
Menu
</button>
</div>
<nav className={isOpen ? "flex" : "md:flex hidden"}>
<NavLink pathTo="/" displayText="Home" />
<NavLink pathTo="/" displayText="Page 1" />
<NavLink pathTo="/" displayText="Page 2" />
</nav>
</div>
</header>
);
}
export default Navbar;
My NavLink component is as follows:
import React from "react";
import Link from "next/link";
interface NavItem {
pathTo: string;
displayText: string;
}
function NavLink(item: NavItem) {
return (
<Link href={item.pathTo}>
<a className="block lg:p-4 px-4 py-2 mt-2 border-b-2 border-white text-sm bg-transparent dark-mode:bg-transparent dark-mode:hover:border-red-400 dark-mode:focus:border-red-400 dark-mode:focus:text-white dark-mode:text-gray-200 md:mt-0 md:ml-4 hover:text-gray-900 focus:text-gray-900 hover:border-red-400 focus:border-red-400">
{item.displayText}
</a>
</Link>
);
}
export default NavLink;
What am I missing? I want the NavLinks to have one item on each row when opened from the menu. I thought display:block would do it, but it seems not to. Why's that?
You need to add a flex direction to your nav links. Adding flex-col should do the trick.

Resources