I’m learning tailwind using react and i can’t seem to make the navbar horizontal. It even disappears on the Ui at some point. how would you edit the following code to be horizontal?
const Navbar = () => {
return (
<nav className="z-50 bg-white">
<div className="h-10vh flex justify-between lg:py-5 px-20 py -20 border-b">
<div className="flex items-center flex-1">
<h2 className="text-3xl font-bold text-pink-500 "> BOWANA </h2>
</div>
<div>
<ul className="flex gap-8 mr-16 text-[18px]">
<Link to="/"><li>Home</li></Link>
<Link to="/"><li>Blogs</li></Link>
<Link to="/"><li>Services</li></Link>
<Link to="/"><li>Book An Appointment</li></Link>
<Link to="/"><li>Contact Us</li></Link>
</ul>
</div>
</div>
</nav>
);
};
export default Navbar;
I expected a horizontal nav bar, it is vertical
3
Answers
To make your navbar horizontal, need to change the flex direction of the parent container. Currently, the flex direction is set to the default value of flex-col (column), that’s why items are stacked vertically. To make it horizontal, set it to flex-row.
Live preview:
https://stackblitz.com/edit/vitejs-vite-agz5wk?file=src%2FNavbar.tsx
I resolved the code issue and also defined the black color for
li
and for anchor/routing you need to read about React Routes and in react routes library you will seeLink
Read more:
https://reactrouter.com/en/main/components/link
You will need to swap your
<Link>
and<li>
nesting. You can also eliminate the<div>
surrounding your<ul>
.