div sticky header not working in TailwindCSS - css

I'm trying to create a sticky header with TailwindCSS but can't seem to make it work.
I've read the docs and seems that i only need to add sticky top-0 to my div to make it sticky, but it doesn't work.
I've tried to clean up my code as best as I could for the sake of readability, but if you are intrested in the entire code you can find it here.
import { Logo, ToolbarButton, IconLink, Countdown } from "#lib/atoms";
import { faWhatsapp, faFacebook } from "#fortawesome/free-brands-svg-icons";
import {
faHandHoldingHeart,
faHeadphonesAlt,
} from "#fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "#fortawesome/react-fontawesome";
export const Toolbar = () => {
return (
<div className="flex flex-col drop-shadow-2xl">
<div className="h-16 flex items-center justify-center bg-rs-secondary">
<div className="container flex justify-between">
<div className="flex gap-4 items-center">
<IconLink
icon={faFacebook}
href="https://www.facebook.com/estereo.sulamita"
/>
<IconLink icon={faWhatsapp} href="https://wa.link/logvtp" />
</div>
<Countdown />
</div>
</div>
{/* I want this div to be sticky */}
<div className="sticky top-0 h-20 flex justify-center bg-white">
<div className="container flex items-center h-full justify-between">
<Logo />
<div className="flex">
<ToolbarButton>Inicio</ToolbarButton>
<ToolbarButton>Videos</ToolbarButton>
<ToolbarButton>Colaboradores</ToolbarButton>
<ToolbarButton
className="group"
hoverBackgroundColor="hover:bg-black"
primary={true}
>
<FontAwesomeIcon
icon={faHandHoldingHeart}
className="group-hover:text-white w-4"
/>
</ToolbarButton>
<ToolbarButton
backgroundColor="bg-rs-primary"
hoverBackgroundColor="hover:bg-black"
textColor="text-white"
primary={true}
>
Reproducir
<FontAwesomeIcon icon={faHeadphonesAlt} className="w-4 ml-3" />
</ToolbarButton>
</div>
</div>
</div>
</div>
);
};
The above code renders the following component:
Also, you can find a deployed version of my app here.
I'd like to achieve something like this with my header component.
Any help is much appreciated.

sticky element should be direct child of the scrollable element.
In your case scrollable element is the root __next container. Basically it means that you need to remove this div <div className="flex flex-col drop-shadow-2xl"> and replace it with React.Fragment so both of your headers, Countdown and second Header, would be direct children of this scrollable div.

Related

Add Z-index Using tailwind CSS in Next JS

I want my heading to display over the background image. I am trying to use z-index but it is not working. Here is my code. Any help will be very grateful.
<div
className="
bg-banner
bg-cover
opacity-60
h-screen
flex flex-col
space-y-0
items-center
justify-center
text-center
overflow-center
-z-[20]
"
>
<h1 className="">
<span>{text}</span>
<Cursor cursorColor="green" />
</h1>
</div>

Why does my website has extra space in its right corner only in its mobile version?

So I have created a website(deployed on Vercel) for practice and built it responsive by having breakpoints for different screen sizes. But now, when I see the same website's dashboard page on my mobile, I see some extra unwanted space in its right-top corner and in its index page's desktop view; the footer is, for some reason floating above the bottom of the screen. I have pictures below of desktop view of the footer and dashboard page as shown in Edge and as shown in Chrome Android.
I have built it using React and Tailwind CSS. My website's link is
Dashboard link - https://build2-eight.vercel.app/dashboard
Index Page - https://build2-eight.vercel.app/
And in my development server, neither of the issues were encountered.
The code is:-
/* Index.css custom tailwind classes */
#layer components{
.cardMainPage{
box-shadow:0px 0px 15px rgb(0,0,0,0.32);
#apply flex flex-col items-center justify-start text-2xl gap-5 p-2 py-3 rounded-md bg-white;
}
.Icons{
#apply bg-[#aff0cc] rounded-full h-16 md:h-8 w-16 md:w-8 flex flex-row justify-center items-center cursor-pointer hover:bg-white;
}
.HeaderIcons{
#apply bg-[#aff0cc] p-2 h-8 w-8 rounded-sm hover:bg-white cursor-pointer;
}
}
React Code : -
/* Footer React code */
import React from 'react';
export default function Footer() {
return (
<div className='bg-[#5cdb95] text-[#05386b] w-full text-md flex items-center justify-center'>
<div className='text-center'>
Made with <b className='text-red-600 text-lg'>♥</b> by <a href="/" className=' underline hover:no-underline'>Soumya Deep Sarkar</a>
</div>
</div>
)
}
Dashboard code->
/* Dashboard page code */
import React from "react";
import Header from "../header/index";
import Footer from "../footer/index";
import { GrBitcoin, GrGamepad } from "react-icons/gr";
import { SiCodingninjas } from "react-icons/si";
import { FiSearch } from "react-icons/fi";
import { FcSettings, FcBusinessman } from "react-icons/fc";
import { IoMdNotifications } from "react-icons/io";
import {GrChat} from "react-icons/gr"
export default function Dashboard() {
return (
<div className="flex flex-col h-screen">
<Header />
<div className="flex flex-row h-full">
<div
id="left-side-menu"
className="p-2 px-1 bg-back py-4 flex justify-between h-full flex-col"
>
<div className=" flex flex-col gap-3">
<span className="Icons">
<GrBitcoin />
</span>
<span className="Icons">
<GrGamepad />
</span>
<span className="Icons">
<SiCodingninjas />
</span>
</div>
<div className="flex flex-col gap-3">
<span className="Icons">
<FcSettings />
</span>
<span className="Icons">
<FcBusinessman/>
</span>
</div>
</div>
<div id="center-menu" className="flex flex-col w-full">
<div className="bg-[#20d876] w-full flex flex-row justify-between items-center px-4">
<span className="HeaderIcons my-1"><IoMdNotifications className="text-yellow-500"/></span>
<form className="w-full flex flex-row items-center justify-center p-1">
<span className="relative flex items-center">
<input type="text" className="border-2 px-2 rounded-md border-text "/>
<span className="absolute right-1 cursor-text"><FiSearch/></span>
</span>
</form>
<span className="HeaderIcons"><GrChat className="text-yellow-500"/></span>
<div>
</div>
</div>
</div>
<div id="right-side-menu"></div>
</div>
<Footer />
</div>
);
}
I have figured it out. The textbox(search one) is causing the issue in case of the dashboard header. To fix it I just have to define its width to 100% and it worked out.
Regarding the footer issue. I had to define the minimum height of the window. So I set min height to 100vh.
In tailwind css, for the dashboard header I just put w-full in the className attribute and for the footer I put min-h-screen in the parent div.

I want the image to be at the top left corner but I am not able to do it in tailwind and next

I want to add my image to the top right corner but I am not able to add it. Pls help me in finding the error.
import Image from "next/image";
function Header() {
return (
{/* left */}
<div className='relative flex items-center h-10 cursor-pointer my-auto grid-cols-3 bg-black'>
{/* objectfit prevents from stretch of the image*/}
<Image
src='https://links.papareact.com/qd3'
layout="fill"
objectFit="contain"
objectPosition="left"
/>
</div>
{/* Middle */}
<div></div>
{/* Right */}
<div></div>
</header>
)
}
export default Header
Simply change the objectPostion: "right". Your image will be placed right
You have used flex so you can align to right by adding justify-end. Also you can make layout=responsive. And add className = "w-20 h-6" to either or a wrapping the image which gives the image proper size.
Here's the working code
<script src="https://cdn.tailwindcss.com"></script>
<div class='relative flex items-center justify-end h-10 cursor-pointer my-auto grid-cols-3 bg-black'>
<Image
src='https://links.papareact.com/qd3'
layout="responsive"
objectFit="contain"
objectPosition="right"
class="h-6 w-20 mr-2"
/>
</div>
<div></div>
<div></div>

Allow Component to Overflow to next line

I am trying to get a map of a component to go to the next line (see yellow arrow). However, right now instead of going below it is squishing the Component (the Cards). I have made bg-colors to help assist. Any guidance would be great!
<div className="border-2 h-screen bg-pink-300 ">
<div className="flex justify-end px-10 ">
<button className="border-2 border-secondary p-2 rounded-lg hover:border-opacity-20">Add Item +</button>
</div>
<div className="flex overflow-x-auto bg-green-500 ">
{data.map((data) => (
<MenuCard title={data.title} ingredients={data. ingredients}
category={data.category} onSpecial={data.onSpecial} />
))}
</div>
</div>
Add the flex-wrap class into the MenuCard's parent div element. Also remove the overflow-x-auto class as this will make the container scroll vertically.
Should look like this.
<div className="flex flex-wrap bg-green-500 ">
{data.map((data) => (
<MenuCard title={data.title} ingredients={data. ingredients}
category={data.category} onSpecial={data.onSpecial} />
))}
</div>
You might also need to add flex-shrink-0 class in each MenuCard instances if it tries to shrink.

Align items centrally on devices sizes =< small

I have a card component that renders like the first image below on screen sizes above small, mobile devices I have the component set to flex-wrap. When flex-wrap is active then my image is pushed to the left of the card even though it's container is set to w-full and I have tried to center with justify-center. I am trying to centre the image only when devices are small and under. I have tried setting sm: justify-center which doesn't work. Anyone got ideas on how I can refactor to get this functionality to work? Thanks
import React from "react"
export default function FeatureCard(props) {
return (
<div className="flex flex-row flex-wrap w-2/3 h-auto bg-gray-200 p-2 rounded-lg">
<div className="flex xl:w-1/3 lg:w-1/3 md:w-1/3 sm:w-full xs:w-full">
<img src={props.image} />
</div>
<div className="flex xl:w-2/3 lg:w-2/3 md:w-2/3 sm:w-full xs:w-full text-center self-center justify-center font-bold">
<ul>
{props.features.map(feature => (
<li>{feature}</li>
))}
</ul>
</div>
</div>
)
}
<div className="flex flex-row flex-wrap w-full h-auto justify-center -py-2">
<div className="flex xl:w-1/2 lg:w-1/2 md:w-full sm:w-full xs:w-full h-auto justify-center py-2">
<FeatureCard
features={[
"Modern Website Design",
"Progressive Web Applications",
"Content Management Systems",
"JAMstack",
]}
image={"https://img.icons8.com/color/96/000000/monitor.png"}
/>
</div>
</div>
So if I understand correctly, you want the props.image centered on small screens?
What if you added something like this to the <div className="flex flex-row flex-wrap w-full h-auto justify-center -py-2"> div:
#media (max-width: 600px) {
flex-direction: column;
justify-content: center;
align-items: center;
}
What it basically does, is changing the flex direction to column instead of row when the screen is smaller than 600px, which in tailwind-css probably translates to:
sm:flex-col sm:justify-center sm:items-center

Resources