tailwindcss have descendent ignore mix - css

I've been chasing down a problem, getting blur and mix to work well together in Tailwind, so I can have a Navbar component look good regardless of the color of the underlying content.
That is now largely solved for the text, but I have an issue where an anchor element styled as a button is "mixing" when I don't want it to do so.
This is the mix, over a dark background:
That green is what the button should always look like (actually, the text should be white, but that's a different problem). However, when over a light background, it turns pink (or mauve, or something):
The structure looks like this:
<div className="backdrop-blur sticky top-0 left-0">
<header className="backdrop-blur z-[1100] w-full mix-blend-difference bg-black">
<div className="mx-auto">
<div className="py-4 px-8 mx-4">
<nav>
<a href="/">
<div className="text-white">My Site</div>
</a>
<ul className="flex justify-between ml-auto">
<li className="text-white">Products</li>
<li> // I want this to not mix
<a
className="bg-emerald-700 hover:bg-emerald-600 text-white py-2 px-4 rounded-sm shadow-lg shadow-emerald-800"
href="/"
>
About
</a>
</li>
</ul>
</nav>
</div>
</div>
</header>
</div>
Basically, I need some deeply nested element inside a container that is using mix-blend-<whatever> to ignore the mix.
I've tried applying mix-blend-normal to the li element, but no effect.
Any idea how to do this?
CodeSandbox is here.

Related

How to style nested elements based on parent class using Tailwind CSS?

Let's say I want all the links inside white card to be black, and all the links inside orange cards to be white. I can easily do in pure CSS:
.card--primary {
background-color: white;
a {
color: black
}
}
.card--danger {
background-color: orange;
a {
color: white;
}
}
But how can I achieve similar behaviour using Tailwind (or other CSS utility frameworks)?
This is possible in tailwind 3.1 with the addition of the arbitrary variants feature. The way you would do this is by using a selector on the parent div:
<div className="card--primary bg-white [&>a]:text-black" >
<a/>
<a/>
<div/>
<div className="card--danger bg-orange [&>a]:text-white" >
<a/>
<a/>
<div/>
In case of Tailwind CSS
Read the descriptive and a very brief documentation entry here: Using arbitrary variants.
As I can see you need to change the color of all the <a> links no matter how deeply they reside in the <cards>. Use an underscore between & and a selectors - [&_a]:text-black. This translates to:
.card--primary {
/* arbitrarily nested <a> links */
a {
color: black
}
}
On the other hand the Tailwind directive with > between & and a => [&>a]:text-black would result in this css (only the direct child <a> nodes would be styled):
.card--primary {
/* direct-child <a> links */
> a {
color: black
}
}
Recap: the resulting Tailwind HTML for your case:
<div className="card--primary [&_a]:text-black" >
<a></a> !-- will be black
<div>
<a></a> !-- will be black
</div>
<div/>
<div className="card--danger [&_a]:text-white" >
<a></a> !-- will be white
<div>
<a></a> !-- will be white
</div>
<div/>
That's it. I hope it was helpful.
YOu can do this way
<script src="https://cdn.tailwindcss.com"></script>
<div class="relative bg-slate-100 flex min-h-screen flex-col justify-center overflow-hidden bg-gray-50 py-6 sm:py-12 space-y-9">
<div class="relative bg-white px-6 pt-10 pb-8 shadow-xl ring-1 ring-gray-900/5 sm:mx-auto sm:max-w-lg sm:rounded-lg sm:px-10">
<div class="mx-auto max-w-md">
<div class="divide-y divide-gray-300/50">
<div class="space-y-6 py-8 text-base">
<p>White Card with all black links:</p>
<ul class="space-y-4">
<li class="flex items-center">
First Link
</li>
<li class="flex items-center">
Second Link
</li>
<li class="flex items-center">
Third Link
</li>
</ul>
</div>
</div>
</div>
</div>
<div class="relative bg-orange-700 px-6 pt-10 pb-8 shadow-xl ring-1 ring-gray-900/5 sm:mx-auto sm:max-w-lg sm:rounded-lg sm:px-10">
<div class="mx-auto max-w-md">
<div class="divide-y divide-gray-300/50">
<div class="space-y-6 py-8 text-base text-white">
<p>Orange Card with all white links:</p>
<ul class="space-y-4">
<li class="flex items-center">
First Link
</li>
<li class="flex items-center">
Second Link
</li>
<li class="flex items-center">
Third Link
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
I see where each of you are coming from, but if you are asking how to do it strictly in Tailwind:
<div class="bg-white text-black">
<a></a>
<a></a>
</div>
<div class="bg-black text-white">
<a></a>
<a></a>
</div>
Everyone else's answer seemed a little bloated.
In tailwind v3 you can have arbitrary group selectors. This allows you to specify a how a certain element behaves when nested beneath a parent class.
This is useful when you have a templated element needs to appear different under certain scenarios, but doesn't know anything about its parent context.
It requires the addition of the .group class, but then you can do something like this:
<div class="card">
<a href="#" class="text-black group-[.card--danger]:text-white">
Normal Card
</a>
</div>
<div class="card group card--danger bg-orange-600">
Danger Card
</div>
Here's an example on Tailwind's playground: https://play.tailwindcss.com/IQ4W4BVdjw

How to make parent width fit content when a child has a negative transform translateX?

This is an example snippet showcasing what I'm trying to achieve. (please make sure to scroll down, as the 2nd example might be hidden by tailwind warning)
In the 2nd example, as you can see, the parent box width remains the same and doesn't shrink down to fit the content.
I believe absolute positioning is probably the best to achieve the badge position that I want, however, using absolute positioning will have a similar issue of the parent not adjusting to content size.
/*
* These styles are just a workaround to fix tailwind/daisyui styles
* as for some reason daisyui doesn't override tailwind when using CDN.
*/
.img-link {
padding:0!important;
border:none!important;
}
<script src="https://cdn.tailwindcss.com"></script>
<link href="https://cdn.jsdelivr.net/npm/daisyui#2.14.1/dist/full.css" rel="stylesheet"/>
<h1 class="mb-2">Original box</h1>
<div class="inline-block bg-rose-400">
<a class="btn btn-ghost normal-case text-l p-0 border-0 img-link" href="/">
<img src="https://picsum.photos/200" class="h-full" alt="madlabz - Wild Rift Builds" />
</a>
<span class="badge">badge</span>
</div>
<h1 class="mt-6 mb-2">Badge has `tranform: translateX(-50%)`</h1>
<div class="inline-block bg-rose-400">
<a class="btn btn-ghost normal-case text-l p-0 border-0 img-link" href="/">
<img src="https://picsum.photos/200" class="h-full" alt="madlabz - Wild Rift Builds" />
</a>
<span class="badge -translate-x-1/2 ">badge</span>
</div>
<div style="height:8rem;"><!-- extra space so that content is scrollable and isn't hidden by tailwind warning --></div>

Text behind image in Tailwind CSS

I want to do this:
But my image stay in right side of div, like that:
Soo I don't know why it's happening, that's my code:
<div class="flex h-full justify-center items-center">
<div>
<h2 class="font-bold text-2xl text-gray-700">Page Not Found πŸ•΅πŸ»β€β™€οΈ</h2>
<h3 class="mt-3 text-gray-500">Oops! πŸ˜– The requested URL was not found on this server.</h3>
<div class="lg:grid justify-center mt-4">
<BreezeButton
class="
inline-table
w-full
items-center
bg-purple-600
text-white text-xs
font-bold
hover:bg-purple-600 hover:shadow-purple
mt-3
"
:href="route('dashboard')"
>
Back to home
</BreezeButton>
</div>
</div>
<div>
<img src="../../Assets/Img/error404.svg" alt="" />
</div>
</div>
You flexed the wrapper div so it seems to have placed the items in a row by default. Use "flex-col" alongside that in order to place your items in a column.
As #xavi3r mentioned you need to use flex-col, as a reminder, Tailwind uses a mobile-first breakpoint, so you need to set one of the breakpoints to change it to row for large devices as follows lg:flex-row or any other breakpoint!!

How to change the layout of a section class with Tailwind CSS based on viewport width?

I am struggling with the execution of the following idea and would be really glad to get support:
I have created this article preview section which looks great on small screen (mobile) devices:
<section class="w-full md:w-2/3 flex flex-col items-center px-3">
<article class="w-full flex flex-col shadow my-4">
<div class="bg-white flex flex-col justify-start p-6">
<div class="max-w"><a
href="#>"
class="text-blue-700 hover:text-blue-500 text-sm font-bold uppercase pb-4">#Tag #Tagg #TAGGG</a>
</div>
<a href="#"
class="text-3xl font-bold hover:text-gray-700 pb-4">Article title</a>
<p href="#" class="text-sm pb-3">By author, published at 12.03.2021
</p>
<div class="aspect-w-16 aspect-h-9">
<a href="#">
<img src="https://images.unsplash.com/photo-1614730321146-b6fa6a46bcb4?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MnwxMTc3M3wwfDF8c2VhcmNofDIzfHxlYXJ0aHxlbnwwfHx8fDE2Mjc1NTc2ODI&ixlib=rb-1.2.1&q=80&w=2000">
</a>
</div>
<a href="#"
class="pb-6 prose max-w-none">Here comes a teaser or an excerpt of the article text...</a>
<div><a href="#"
class="uppercase hover:font-bold text-gray-700 hover:text-black"><mark>Read more… <i
class="fas icon-arrow-right"></i></mark></a></div>
</div>
</article>
</section>
Now I want to change the layout on bigger screens (md: = #media (min-width: 768px) &) to this one:
Sadly my knowledge is too limited to achieve my idea. What needs to be added/changed to achieve it?
Link to the Playground
The simplest method to achieve this would be having 2 images and hiding 1 of them based on the screensize.
So on big screens you would hide the left aligned image with these classes:
hidden md:block
md:block overwrites hidden.
And the inline image gets hidden on big screens:
md:hidden
I made you a playground (also cleaned up unneccesary classes and tags a bit)
I left aligned the image with grid and grid-cols. There definitely are other methods to do this.

Navbar inside flex item oveflow issue

I am using tailwind css vanilla and I want to create a navbar inside the flex item that I have assigned as the visual page header content placeholder.
I encounter a problem while working on this, I have added padding all around with p-8 to the navbar element and I have notice that the navbar is overflowing over the flex item that is holding it.
I do not understand why is this behavior happening because I have added overflow-hidden to the flex item that is holding the navbar. Padding is not considered to be content?
I have tried different combination with display property and also with positioning, all without any result, therefore I need some help.
This is what I have done so far.
<link href="https://unpkg.com/tailwindcss#2.0.3/dist/tailwind.min.css" rel="stylesheet"/>
<div class="container mx-auto px-4 h-screen">
<div class="flex flex-col h-full content-start">
<div class="flex-grow w-full overflow-hidden bg-gradient-to-r from-purple-400 via-pink-500 to-red-500">
<nav class="p-8">
<a class="" href="#">
Home
</a>
<a class="" href="#">
Products
</a>
<a class="" href="#">
Discounts
</a>
<a class="" href="#">
Customers
</a>
</nav>
</div>
<div class="flex-grow my-px px-px w-full overflow-hidden bg-gradient-to-r from-purple-400 via-pink-500 to-red-500 h-full">
<!-- Column Content -->
2
</div>
<div class="flex-grow my-px px-px w-full overflow-hidden bg-gradient-to-r from-purple-400 via-pink-500 to-red-500 h-5">
<!-- Column Content -->
3
</div>
</div>
</div>
Thank you!
L.E. If you shrink the browser, the when the flex column item shrinks it overlaps the previous item. I have shrieked the browser window and I have notice that element 1 of the flex box is going under element 2 and if I go further I'm left only with element 3, the other 2 are going behind the last element 3 of the flex-box.

Resources