How to style nested elements based on parent class using Tailwind CSS? - 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

Related

tailwindcss have descendent ignore mix

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.

Tailwind CSS technique for a horizontal line with words in the middle [duplicate]

I want to create a <hr> divider using Tailwind CSS, but instead of the horizontal rule spanning the entire width of the page unbroken, I want to add some text in the middle.
For example:
----------------------------------- Continue -----------------------------
I can't find anything like this in the documentation. How can I achieve this effect?
If necessary, I can change the HTML to something other than an <hr> element. That was just the only way I knew how to create a horizontal rule.
You can use this HTML syntax to create what you want :
<div class="relative flex py-5 items-center">
<div class="flex-grow border-t border-gray-400"></div>
<span class="flex-shrink mx-4 text-gray-400">Content</span>
<div class="flex-grow border-t border-gray-400"></div>
</div>
See here the result: https://play.tailwindcss.com/65JULZ5XES
Try this instead...
<script src="https://cdn.tailwindcss.com"></script>
<div class="relative py-4">
<div class="absolute inset-0 flex items-center">
<div class="w-full border-b border-gray-300"></div>
</div>
<div class="relative flex justify-center">
<span class="bg-white px-4 text-sm text-gray-500">Continue</span>
</div>
</div>
Example
https://play.tailwindcss.com/Yx4OmAlBsv
Yes, you can do this:
<script src="https://cdn.tailwindcss.com"></script>
<h1 class="text-center overflow-hidden before:h-[1px] after:h-[1px] after:bg-black
after:inline-block after:relative after:align-middle after:w-1/4
before:bg-black before:inline-block before:relative before:align-middle
before:w-1/4 before:right-2 after:left-2 text-xl p-4">Heading
</h1>
Try this.
Need to adjust the height, margin, bg-color, etc. to fit your website.
https://play.tailwindcss.com/FzGZ1fMOEL
<div class="h-5 border-b-4 border-black text-2xl text-center">
<span class="bg-white px-5">your text</span>
</div>

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.

Tailwind: how to fit image inside navbar parent div

I have a simple header in which I want to insert a profile image. However profile image doesn't fit the header and I'm not sure how to approach it in a responsive manner. I don't want to hardcore the width/height of the image in any manner, but rather I want it to always fit the height of the header, with some paddings.
My code is:
<div class="flex flex-wrap items-center justify-start
m-3 p-2 pl-6 ">
<span class="title-text light-text text-xl">Some Title</span>
<span class="pl-10">Some link</span>
<span class="pl-10">Some link</span>
<span class="pl-10">Some link</span>
<span class="pl-10">Some link</span>
<div class="rounded">
<img class= "relative rounded-full border
border-gray-100 shadow-sm
max-w-max h-full " src="../../assets/dog.jpeg" />
</div>
</div>
Without image it looks like this:
But with image it looks like this:
However I want it to look:
How do I achieve it, using Tailwind?
You can use object-fit:cover, note that you will have to set the with and the height of the img to 100%.
.rounded{
display: flex;
align-items: center;
justify-content: center;
}
.rounded img {
height: 100%;
width: 100%;
object-fit: cover;
}
here is the link for tailwind-css meaning you can use this class object-cover only
<div class="rounded">
<img class= "object-cover relative rounded-full border
border-gray-100 shadow-sm
max-w-max h-full " src="../../assets/dog.jpeg" />
</div>
Give the fixed height to the nav and the image container,
<div class="flex flex-wrap items-center justify-start px-4 bg-gray-800 text-white h-20">
<span class="title-text light-text text-xl">Some Title</span>
<span class="pl-10">Some link</span>
<span class="pl-10">Some link</span>
<span class="pl-10">Some link</span>
<span class="pl-10">Some link</span>
<div class="h-16 ml-auto">
<img class= "rounded-full border border-gray-100 shadow-sm h-full" src="../../assets/dog.jpeg" />
</div>
</div>
Kinda late but I've just been working on this and the solution for me was to set:
image height className=h-8 to control sizing
container margins to control positioning className=ml-auto.
I'm using tailwind in react, so full code is:
import GitHubLogo from '../media/GitHubLogo.svg';
const tools = [
'Transcribe',
'Reverse complement',
'Translate',
'Primer check',
'Primer design',
];
export const Navbar = () => (
<nav className="flex w-11/12 p-2 mx-auto rounded bg-coral-std">
<h1 className="text-4xl btn"> welcome</h1>
{tools.map((tool) => (
<a
href={`/#${tool}`}
className="inline-flex px-6 py-2 rounded hover:bg-slate-200/20"
key={tool}
>
{tool}
</a>
))}
<a href="https://github.com/ccozens/mol-bio-tools/" target='_' className="ml-auto">
<img
src={GitHubLogo}
alt="GitHub logo"
className="h-8 pr-1 mx-auto"
>
</img>
Source code</a>
</nav>
);
Note the mx-auto flag in the image class centres the image over the image label; this isn't impacting the right positioning.

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