Tailwind CSS Navigation Hover Dropdown with Padding - html

I'm attempting to show the sub <ul> list on the first navigation item when the item is hovered:
Everything is working except for sometimes (it's hit and miss) when you are in between the padding of the first line <ul> item and the sub <ul> item, the secondary <ul> will disappear:
How can I keep the secondary navigation list open when I'm navigating from the dropdown to the item list?
JSFiddle
<ul class="w-full">
<li class="dropdown inline px-4 text-purple-500 hover:text-purple-700 cursor-pointer font-bold text-base uppercase tracking-wide">
<a>Dropdown</a>
<div class="dropdown-menu absolute hidden h-auto flex pt-4">
<ul class="block w-full bg-white shadow px-12 py-8">
<li class="py-1"><a class="block text-purple-500 font-bold text-base uppercase hover:text-purple-700 cursor-pointer">Item</a></li>
<li class="py-1"><a class="block text-purple-500 font-bold text-base uppercase hover:text-purple-700 cursor-pointer">Item 2</a></li>
<li class="py-1"><a class="block text-purple-500 font-bold text-base uppercase hover:text-purple-700 cursor-pointer">Item 3</a></li>
<li class="py-1"><a class="block text-purple-500 font-bold text-base uppercase hover:text-purple-700 cursor-pointer">Item 4</a></li>
<li class="py-1"><a class="block text-purple-500 font-bold text-base uppercase hover:text-purple-700 cursor-pointer">Item 5</a></li>
</ul>
</div>
</li>
<li class="inline px-4 text-purple-500 hover:text-purple-700 cursor-pointer font-bold text-base uppercase tracking-wide"><a>Non-Dropdown</a></li>
<li class="inline px-4 text-purple-500 hover:text-purple-700 cursor-pointer font-bold text-base uppercase tracking-wide"><a>Non-Dropdown</a></li>
<li class="inline px-4 text-purple-500 hover:text-purple-700 cursor-pointer font-bold text-base uppercase tracking-wide lg:pr-8"><a>Non-Dropdown</a></li>
</ul>
.dropdown:hover .dropdown-menu {
display: block;
}

Tailwind Group
You can use group and group-hover they are pretty simple and handy
Here is the full code example: tailwind-playgroud
Step 1 add group and relative classes to the div that wraps the dropdown
<li class="group relative dropdown px-4 text-purple-500 hover:text-purple-700 cursor-pointer font-bold text-base uppercase tracking-wide">
<a>Dropdown</a>
Step 2 add group-hover:block to the div that wraps the dropdown links
<div class="group-hover:block dropdown-menu absolute hidden h-auto">
Step 3 add top-0 to the ul that wraps the dropdown links
<ul class="top-0 w-48 bg-white shadow px-6 py-8">
<li class="py-1"><a class="block text-purple-500 font-bold text-base uppercase hover:text-purple-700 cursor-pointer">Item</a></li>
<li class="py-1"><a class="block text-purple-500 font-bold text-base uppercase hover:text-purple-700 cursor-pointer">Item 2</a></li>
<li class="py-1"><a class="block text-purple-500 font-bold text-base uppercase hover:text-purple-700 cursor-pointer">Item 3</a></li>
<li class="py-1"><a class="block text-purple-500 font-bold text-base uppercase hover:text-purple-700 cursor-pointer">Item 4</a></li>
<li class="py-1"><a class="block text-purple-500 font-bold text-base uppercase hover:text-purple-700 cursor-pointer">Item 5</a></li>
</ul>
Step 4 This is last step add display: ['group-hover'] in the tailwind.config.js file inside variants
variants: {
display:['group-hover']
}

3 changes. Here's the jsfiddle
Add relative class to the li tag.
Add top-0 with the dropdown-menu absolute class.
Change the padding of ul, inside the menu, to p-8. Just a minor css

My solution to this issue: transparent div between
Example (assuming mt-2 from dropdown menu to trigger):
<div className="group">
<button>Dropdown button</button>
<div className="hidden group-hover:block -mt-4">
<div className="mt-6 bg-transparent">
<ul>
<li></li>
<li></li>
<ul>
</div>
</div>

Related

AlpineJS TailwindCSS Responsive Navigation with transition

I want to add an x-transition to the element. I want to keep the syntax of html code as much as possible, so I don't know if it is possible to add x-transition on element.
`
<nav
id="nav-main"
x-data="{menuIsOpen : false}"
on:click.away="menuIsOpen = false"
#click.outside="menuIsOpen = false"
x-cloak
>
<button
#click="menuIsOpen = ! menuIsOpen"
:class="{'tham-active': menuIsOpen}"
class="tham tham-e-squeeze tham-w-8 relative md:hidden"
>
<ul
class="md:flex text-lg text-darkGrey font-bold"
:class="{ 'absolute p-6 top-20 left-5 right-5 text-center bg-white drop-shadow-md ': menuIsOpen, 'hidden': !menuIsOpen}"
id="navbar-main"
>
<li
class="px-3 cursor-pointer hover:text-red transition-all"
:class="{'py-3': menuIsOpen}"
>
Features
</li>
<li
class="px-3 cursor-pointer hover:text-red transition-all"
:class="{'py-3': menuIsOpen}"
>
Features
</li>
<li
class="px-3 cursor-pointer hover:text-red transition-all"
:class="{'py-3': menuIsOpen}"
>
Features
</li>
<li
class="px-3 cursor-pointer hover:text-red transition-all"
:class="{'py-3': menuIsOpen}"
>
Features
</li>
<li
class="px-3 cursor-pointer hover:text-red transition-all"
:class="{'py-3': menuIsOpen}"
>
Features
</li>
</ul>
</nav>
`

How to set dropdown menu hidden by default

this is my code for a dropdown, I am using tailwindCSS and pure html,
<li class="relative" x-data="{isOpen:false}">
<button #click="isOpen=!isOpen" class=" block text-sm font-bold outline-none focus:outline-none text-blue-900 " href="#">
SERVICES</button>
<div
class="right-0 p-2 mt-1 bg-white rounded-md shadow lg:absolute"
:class="{'hidden':!isOpen'flex flex-col':isOpen}"
#click.away="isOpen = false">
Categories
Inventories
Brands
</div>
</li>
I want to hide my dropdown by default and is should only open when someone hovers on it or when someone clicks on it. but right now it is still visible even though I have tried hiding it.
I want to be able to do it with tailwind the way I am trying to approach, I know how to do it with CSS,
Please see image for reference. and let know what am I doing wrong.
You have to use pure css on the drop-down container
Example
.drop-down{
display:none;
}
try like below, below example use tailwind CSS
.dropdown:hover .dropdown-menu {
display: block;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/1.0.4/tailwind.min.css" rel="stylesheet"/>
<div class="p-10">
<div class="dropdown inline-block relative">
<button class="bg-gray-300 text-gray-700 font-semibold py-2 px-4 rounded inline-flex items-center">
<span class="mr-1">Dropdown</span>
<svg class="fill-current h-4 w-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path d="M9.293 12.95l.707.707L15.657 8l-1.414-1.414L10 10.828 5.757 6.586 4.343 8z"/> </svg>
</button>
<ul class="dropdown-menu absolute hidden text-gray-700 pt-1">
<li class=""><a class="rounded-t bg-gray-200 hover:bg-gray-400 py-2 px-4 block whitespace-no-wrap" href="#">One</a></li>
<li class=""><a class="bg-gray-200 hover:bg-gray-400 py-2 px-4 block whitespace-no-wrap" href="#">Two</a></li>
<li class=""><a class="rounded-b bg-gray-200 hover:bg-gray-400 py-2 px-4 block whitespace-no-wrap" href="#">Three is the magic number</a></li>
</ul>
</div>
</div>

Background image that spans multiple divs but does not use fixed background position

So I'm creating a website for a business, and I want to have a background image that is on the nav-bar along the top and then when you click the hamburger button and the hidden menu appears it also has that background image. The problem I have is when I use Background Position: fixed; the background image eventually is no longer on the divs and they are left with just the background color. Is there a way to have an image that spans both divs but also don't scroll with the page?
`
.background-image-nav {
background-image: url("/src/images/black-squiggles.png");
background-attachment: fixed;
background-repeat: no-repeat;
background-size: cover;
max-width: 100%;
}
<nav class="background-color-foot background-image-nav shadow-lg box-border w-full max-w-full z-50">
<div class="max-w-6xl mx-auto px-4">
<div class="flex justify-between">
<div class="flex space-x-7">
<!-- Website Logo -->
<a href="#" class="flex items-center py-4 px-2 max-w-[130px]">
<img src="/src/logo_transparent.png" alt="Logo" class="h-8 w-full mr-2" />
<!-- <span class="font-semibold text-white text-lg"
>Design Potato</span
> -->
</a>
</div>
<!-- Primary Navbar items -->
<div class="hidden md:flex items-center space-x-1">
<a
href=""
class="py-4 px-2 text-white-exec border-b-4 border-white hover:text-white-exec font-semibold"
>Home</a
>
<a
href=""
class="py-4 px-2 text-white-exec font-semibold hover:text-white-exec transition duration-300"
>Services</a
>
<a
href=""
class="py-4 px-2 text-white-exec font-semibold hover:text-white-exec transition duration-300"
>Contact Us</a
>
<a
href=""
class="py-4 px-2 text-white-exec font-semibold hover:text-white-exec transition duration-300"
>About Us</a
>
</div>
<div class="md:hidden h-full my-auto">
<button class="mobile-menu-button space-y-2 p-2.5">
<span class="block w-7 h-0.5 bg-white-exec"></span>
<span class="block w-7 h-0.5 bg-white-exec"></span>
<span class="block w-7 h-0.5 bg-white-exec"></span>
</button>
</div>
</div>
</div>
<!-- mobile menu -->
<div
class="hidden md:hidden mobile-menu absolute h-60 w-full z-50 bg-brown-exec background-image-nav pt-5 m-auto rounded-b-md"
>
<ul class="text-center h-full space-y-5 text-2xl">
<li class="">
<a href="" class="text-white-exec border-b-4 hover:text-white-exec border-white font-semibold"
>Home</a
>
</li>
<li>
<a
href=""
class="text-white-exec font-semibold hover:text-white-exec transition duration-300"
>Services</a
>
</li>
<li>
<a
href=""
class="text-white-exec font-semibold hover:text-white-exec transition duration-300"
>Contact Us</a
>
</li>
<li>
<a
href=""
class="text-white-exec font-semibold hover:text-white-exec transition duration-300"
>About Us</a
>
</li>
</ul>
</div>
</nav>
If I set background-attachment to cover the image appears twice, once per div.

Nav also moves when applying margin top in TailWind

When I try to apply margin-top to a container, the container moves, but so does the nav bar, do you guys know what can I do? I am using TailwindCSS, please check the images:
First Image
Second Image
<header>
<nav class="w-full bg-white fixed flex border-b-2 border-opacity-50 z-10">
<a class="" href="#home">
<img class="mx-48 p-2 w-24" src="assets/logo/dfnr.png" alt="Diefonro logo" />
</a>
<ul class="nav-list flex text-gray-500 relative left-40 font-bold mt-1">
<li class="p-3 mx-2 hover:text-gray-800">
Home
</li>
<li class="p-3 mx-1 hover:text-gray-800">
About
</li>
<li class="p-3 mx-2 hover:text-gray-800">
Services
</li>
<li class="p-3 mx-2 hover:text-gray-800">
Portfolio
</li>
<li class="p-3 mx-2 hover:text-gray-800">
Contact
</li>
</ul>
</nav>
</header>
<main>
<div class="container mt-16 mx-auto bg-gray-700" id="home">
<p class="text-3xl">Hi! This is</p>
<h1 class="title gradient-title">DIEFONRO</h1>
<p class="pos-text">Junior Front-End Developer</p>
<button class="hire">HIRE ME</button>
<button class="get-cv">GET CV</button>
<img src="assets/img/Diefonro.png" alt="diefonro's ilustration" class="main-img" />
</div>
</main>
I think adding top-0 to the <nav> will fulfill your wish.

Tailwind - absolute vs relative position dropdown

im having a problem trying to set de position of a dropdownlist. I want it to be on top of everything, but when it comes into a relative positioned element, it just goes behind it. Here is a code example of what I mean.
html example:
<div class="dropdown relative">
<button class="bg-gray-300 text-gray-700 font-semibold py-2 px-4 rounded inline-flex items-center">
<span class="mr-1">Dropdown</span>
<svg class="fill-current h-4 w-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path d="M9.293 12.95l.707.707L15.657 8l-1.414-1.414L10 10.828 5.757 6.586 4.343 8z"/> </svg>
</button>
<ul class="dropdown-menu absolute hidden text-gray-700 pt-1">
<li class=""><a class="rounded-t bg-gray-200 hover:bg-gray-400 py-2 px-4 block whitespace-no-wrap" href="#">One</a></li>
<li class=""><a class="bg-gray-200 hover:bg-gray-400 py-2 px-4 block whitespace-no-wrap" href="#">Two</a></li>
<li class=""><a class="rounded-b bg-gray-200 hover:bg-gray-400 py-2 px-4 block whitespace-no-wrap" href="#">Three is the magic number</a></li>
</ul>
</div>
<div class="dropdown relative">
<button class="bg-gray-300 text-gray-700 font-semibold py-2 px-4 rounded inline-flex items-center">
<span class="mr-1">Dropdown</span>
<svg class="fill-current h-4 w-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path d="M9.293 12.95l.707.707L15.657 8l-1.414-1.414L10 10.828 5.757 6.586 4.343 8z"/> </svg>
</button>
<ul class="dropdown-menu absolute hidden text-gray-700 pt-1">
<li class=""><a class="rounded-t bg-gray-200 hover:bg-gray-400 py-2 px-4 block whitespace-no-wrap" href="#">One</a></li>
<li class=""><a class="bg-gray-200 hover:bg-gray-400 py-2 px-4 block whitespace-no-wrap" href="#">Two</a></li>
<li class=""><a class="rounded-b bg-gray-200 hover:bg-gray-400 py-2 px-4 block whitespace-no-wrap" href="#">Three is the magic number</a></li>
</ul>
</div>
</div>
css:
.dropdown:hover .dropdown-menu {
display: block;
}
live:
https://codepen.io/lcsalt/pen/MWpPvJp
How can I ensure that the list is above everything, no matter what? i tried with z-index but didn't help.
You need to use rule z-index for the <ul> tag. In regulation Tailwind there is a set of classes containing this rule.
By clicking on this link you can see more details.
I added class z-50.
<ul class="dropdown-menu absolute hidden text-gray-700 pt-1 z-50">