Center Text and put Icon on the right with Tailwind CSS - html

I try to build a sidebar menu with Tailwind CSS. But I am not able to center the icon in same height like the text of the LI-tag.
<ul id="sidemenu" class="text-center border-b-2 border-gray-200">
<li class="border-t-2 border-gray-200">
<div href="#" class="block cursor-pointer block py-3 relative">
<span class="">Title 1</span>
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 absolute right-1 top-1/2" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7" />
</svg>
</div>
<ul class="bg-gray-50 border-t-2 border-gray-100">
<li class="py-2">Sub-Item-1</li>
<li class="py-2">Sub-Item-2</li>
<li class="py-2">Sub-Item-3</li>
</ul>
</li>
<li class="border-t-2 border-gray-200">
<span class=" block py-3 cursor-pointer">Title 2</span>
</li>
<li class="border-t-2 border-gray-200">
<span class=" block py-3 cursor-pointer">Title 3</span>
</li>
</ul>
Here is a example how it looks now: https://jsfiddle.net/j6y3sznq/

The class .top-1\/2 is missing the transform property consider adding transform: translateY(-50%) to align it to the center.
.top-1\/2 {
transform: translateY(-50%);
}
Updated fiddle here
Solution two: Tailwind class name. Just add transform -translate-y-2/4 to the svg element.
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/2.2.15/tailwind.min.css">
<ul id="sidemenu" class="text-center border-b-2 border-gray-200">
<li class="border-t-2 border-gray-200">
<div href="#" class="block cursor-pointer block py-3 relative">
<span class="">Title 1</span>
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 absolute right-1 top-1/2 transform -translate-y-2/4" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7" />
</svg>
</div>
<ul class="bg-gray-50 border-t-2 border-gray-100">
<li class="py-2">Sub-Item-1</li>
<li class="py-2">Sub-Item-2</li>
<li class="py-2">Sub-Item-3</li>
</ul>
</li>
<li class="border-t-2 border-gray-200">
<span class=" block py-3 cursor-pointer">Title 2</span>
</li>
<li class="border-t-2 border-gray-200">
<span class=" block py-3 cursor-pointer">Title 3</span>
</li>
</ul>
Updated fiddle here

Related

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.

How to set width and height of inline svg's

How can I set the whidth and height of my inline svg?
Why is it not working in my case?
<link href="https://cdn.jsdelivr.net/npm/bootstrap#4.6.2/dist/css/bootstrap.min.css" rel="stylesheet" />
<svg style="display: none">
<symbol id="checkmark" viewBox="0 0 24 24">
<path stroke="#6CB21F" fill="#6CB21F" d="M9 16.2L4.8 12l-1.4 1.4L9 19 21 7l-1.4-1.4L9 16.2z"></path>
</symbol>
</svg>
<div style="max-width:200px;">
<div class="card shadow w-100 border-info mb-5 rounded-lg">
<div class="card-body pt-0">
<ul class="price-benefits list-unstyled">
<li class="d-flex flex-row mb-3"><svg height="24" width="24" class="mr-2"><use href="#checkmark"></use></svg> Test Test Test Test Test Test Test Test Test Test</li>
<li class="d-flex flex-row mb-3"><svg height="24" width="24" class="mr-2"><use href="#checkmark"></use></svg> Test</li>
<li class="d-flex flex-row mb-3"><svg height="24" width="24" class="mr-2"><use href="#checkmark"></use></svg> Test Test Test Test Test Test Test Test Test Test</li>
<li class="d-flex flex-row mb-3"><svg height="24" width="24" class="mr-2"><use href="#checkmark"></use></svg> Test</li>
</ul>
</div>
</div>
</div>
Flex is shrinking your icons. In order to avoid this create a CSS class like:
.shrink-0 {
flex-shrink: 0;
}
and it to your <svg> like this
<svg height="24" width="24" class="mr-2 shrink-0">
It isn't about the SVG, it's about flex it shrinks the SVG to give more width to other elements.
Use min-width in that case.
<link href="https://cdn.jsdelivr.net/npm/bootstrap#4.6.2/dist/css/bootstrap.min.css" rel="stylesheet" />
<svg style="display: none">
<symbol id="checkmark" viewBox="0 0 24 24">
<path stroke="#6CB21F" fill="#6CB21F" d="M9 16.2L4.8 12l-1.4 1.4L9 19 21 7l-1.4-1.4L9 16.2z"></path>
</symbol>
</svg>
<div style="max-width:200px;">
<div class="card shadow w-100 border-info mb-5 rounded-lg">
<div class="card-body pt-0">
<ul class="price-benefits list-unstyled">
<li class="d-flex flex-row mb-3"><svg height="24" width="24" style="min-width: 24px" class="mr-2"><use href="#checkmark"></use></svg> Test Test Test Test Test Test Test Test Test Test</li>
<li class="d-flex flex-row mb-3"><svg height="24" width="24" style="min-width: 24px" class="mr-2"><use href="#checkmark"></use></svg> Test</li>
<li class="d-flex flex-row mb-3"><svg height="24" width="24" style="min-width: 24px" class="mr-2"><use href="#checkmark"></use></svg> Test Test Test Test Test Test Test Test Test Test</li>
<li class="d-flex flex-row mb-3"><svg height="24" width="24" style="min-width: 24px" class="mr-2"><use href="#checkmark"></use></svg> Test</li>
</ul>
</div>
</div>
</div>
Here's an article about that: https://css-tricks.com/making-width-and-flexible-items-play-nice-together/

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">