I am trying to build a dropdown with tailwind CSS(v2.2.15) group and group-hover classes. It works fine when I use them in HTML directly. But when I use it in custom class with #apply it doesn't work.
Custom classes:
.dropdown-container {
#apply group inline-block relative;
}
.dropdown-list {
#apply absolute
hidden
-left-16
top-0
rounded-lg
text-sm
group-hover:block;
}
but if I use the group in HTML directly it works fine. I also extend the group hover for display in tailwind.config.js
tailwind.config.js
variants: {
extend: {
display: ['group-hover']
}
},
Is it possible to use this feature in custom class
This doesn't answer your exact question, but tailwind provide a huge bunch of pre-built components you can just cut and paste to save reinventing the wheel: https://tailwindcomponents.com/components/dropdowns
I had issues using group-hover inside #apply, so i just used vanilla css
.parent-class:hover .child-class {
#apply your-tailwind-classes
}
If you want to use custom classes when using group-hover inside the template:
in css file
#tailwind utilities;
#layer utilities {
.hover-small { #apply your--tailwind-classes; // or normal css }
.hover-large { #apply your--tailwind-classes; // or normal css }
}
in html just use group-hover with these custom utilities
<span class="handle transition duration-300
rounded-full group-hover:custom-hover-small">
</span>
In my case i needed dynamic class depending on the size prop
<span class="handle transition duration-300 rounded-full"
:class="`group-hover:hover-${size}`">
</span>
but for that to work i had to add these specific classes in tailwind.config.js safelist so its pre-generates it
safelist: ['group-hover:hover-small', 'group-hover:hover-large'],
Related
I am working in an Angular 14 app using MDBootstrap 5 and I am building some base components. For this example I am trying to build the List Group.
I have a list-group parent component:
#Component({
selector: 'list-group',
standalone: true,
imports: [CommonModule],
template: `
<ul class="list-group list-group-flush">
<ng-content></ng-content>
</ul>
`,
styles: [],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ListGroupComponent {
}
Along with a list-group-item child component:
#Component({
selector: 'list-group-item',
standalone: true,
imports: [CommonModule],
template: `
<li class="list-group-item">
<ng-content></ng-content>
</li>
`,
styles: [],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ListGroupItemComponent {
}
And I am using them in a card component template:
<list-group>
<list-group-item>
<span class="text-muted">Id: </span>
<span>placeholder</span>
</list-group-item>
<list-group-item>
<span class="text-muted">Name: </span>
<span>placeholder</span>
</list-group-item>
...
</list-group>
The issue is that when using content projection like this, the bootstrap class for list-group-flush does not get applied correctly, or at all. It should remove the top border from the first element and the bottom border from the last element.
If I get rid of the projection and instead place the list items directly inside the list group, then it will apply the css properly.
Additionally, if I try something like adding the following css to the list-group:
::ng-deep li:first-of-type {
border-top-width: 0;
}
::ng-deep li:last-of-type {
border-bottom-width: 0;
}
It will remove the top and bottom border from every element in the list.
I dont understand whats happening and am at a loss here as to why the css will not get applied and am wondering if anyone else has encountered such an issue when using content projection with MDBootstrap or another UI library.
Research Update
This is not the ideal solution, as i want to be able to just apply the list-group-flush class and use its styling instead of having to override and apply my own.
However, by adding the following to the list-group-item component:
:host {
border-top: 0;
border-left: 0;
border-right: 0;
border-bottom: 1px solid rgba(0,0,0,.125);
}
:host:last-of-type {
border-bottom: 0;
}
It has accomplished what I wanted, and it will suffice for now. For anyone else on this path, this article was very insightful.
I would still like any feedback though if there are solutions to get the bootstrap class to apply to my projected content.
I wanna ask something about pseudo-element using tailwind, so before going through into my main problem I wanna show my code using CSS
.text-location {
display: flex;
gap: 1.625rem;
}
.text-location::after {
content: url('image/arrow-down-icon.svg'); <= example image
display: inline-block;
width: 100%;
height: 100%;
}
and the result is like this:
it's working and nothing something wrong when I used in CSS, but when I'm going through using tailwind the content is not showing anything, any wrong with my code? Or I must do something different what I have been made? I hope anyone can help and tell me where I made the mistake...Thank you before and have a nice day, bellow my code:
<label class="font-poppins text-sm font-light leading-[0.875rem] text-[#969696] flex gap-[1.625rem] after:content-[url('image/arrow-down-icon.svg')] after:inline-block after:h-full after:w-full">Location</label>
And the result:
You can use item-center class here and use text-black to make them look similar. You have to use custom fonts as popins is not supported by default.
You can refer here
Below is my code
https://play.tailwindcss.com/0wks3noHUe
You can define your content in the tailwind.config.js
theme: {
extend: {
content: {
'arrowDownIcon': 'url("../src/arrow-down-icon.svg")',
'arrowUpIcon': 'url("../src/arrow-up-icon.svg")',
},
fontSize: {
...
You can render it using the following className. Make sure to include an inline-block and width.
<label className="after:content-arrowDownIcon after:inline-block after:w-8">Learn More</label>
You can also apply a hover state like this hover:after:content-arrowUpIcon
<label className="hover:after:content-arrowUpIcon after:content-arrowDownIcon after:content-arrowBlack after:inline-block after:w-8">Learn More</label>
I'm working with TailwindCSS and I created entity like this:
#layer screens {
#responsive {
.flex-row-around {
#apply flex flex-row justify-around;
}
.flex-col-around {
#apply flex flex-col justify-around;
}
}
}
I've generated stylesheet and now I'm trying to use it:
<div class="flex-col-around md:flex-row-around">
...
</div>
But my styles are not added. What is done wrong?
TailwindCSS does not have a screens layer. Looks like you're trying to create a new utility, in which case you'd want to use the utilities layer.
#layer utilities {
#responsive {
.flex-row-around {
#apply flex flex-row justify-around;
}
.flex-col-around {
#apply flex flex-col justify-around;
}
}
}
Here's the docs for reference:
https://tailwindcss.com/docs/functions-and-directives#layer
P.S. If you want to use the new just-in-time compilation, it will generate the responsive utilities automatically without needed to specify them. This feature is currently experimental, but I have been using it in production for a few months now without issue.
#layer utilities {
.flex-row-around {
#apply flex flex-row justify-around;
}
.flex-col-around {
#apply flex flex-col justify-around;
}
}
https://tailwindcss.com/docs/just-in-time-mode#enabling-jit-mode
I'd like to put a conditional styling on a primeng 'p-overlayPanel' element. I have tried:
<p-overlayPanel [styleClass]="#{(bean.comment) ? 'style1' : 'style2'}">, but it's not working.
[ng-class]="bean.comment ? 'style1' : 'style2'" - this is not working either.
Styleclass works only without a condition like so:
<p-overlayPanel [styleClass]="style1"> // html file
p-overlayPanel .style1.ui-overlay { background-color: yellow; } // css file
While [ng-class] doesn't work at all (but works fine on vanilla JS elements). Have I missed something? My questions are following:
Is 'ng-class' not working for some of the elements from ngPrime collection?
How to correctly conditionally apply 'styleClass' for p-overlayPanel element?
I'm using Angular 8.
styleClass accept string as a css class or list of classes and apply to the elemnt at that already have a list of these classes overlaypanel ui-widget ui-widget-content ui-corner-all ui-shadow
so if you want to change the background color you have to do it like this
.style1.ui-overlaypanel{
background-color: red;
}
.style2.ui-overlaypanel{
background-color: green;
}
you have to add the class to the global style file not the component style file and if you use style property the value will pass to ngStyle directive.
demo 🚀
🦑 overlaypanel.ts
Updated 🌟
you can use ngClass but the style must be change like the example below , because now the css classes will apply to the element directly.
.style1 .ui-overlaypanel{
background-color: red;
}
.style2 .ui-overlaypanel{
background-color: green;
}
demo 🥈
You can use [ngClass] like this:
<input pInputText [ngModel]="vendor.iban" name="pIban" #pIban="ngModel" (click)="some(pIban)" class="col-md-7 ui-inputtext ui-widget ui-state-default ui-corner-all " [ngClass]="{'errorVendor': vendor.iban=='' && pIban.touched}" />
I have a Home component with this inside:
<alert type="info">Hello from ng2-bootstrap</alert>
Inside my home.style.scss, I have this:
:host .alert {
background-color: green;
}
which should change the background color to green, but it does not.
The above css code will produce this style:
[_nghost-wjn-3] .alert[_ngcontent-wjn-3] {
background-color: green;
}
and the final HTML looks like this:
<home _nghost-wjn-3="">
<div _ngcontent-wjn-3="" class="card-container">
<alert _ngcontent-wjn-3="" type="info" ng-reflect-type="info">
<div class="alert alert-info" role="alert" ng-reflect-initial-classes="alert" ng-reflect-ng-class="alert-info">
Hello from ng2-bootstrap Sat Sep 17 2016
</div>
</alert>
</div>
</home>
I don't know what the problem is here, but I think the selector is wrong. I'd like the final selector to be:
[_nghost-wjn-3] .alert
instead of:
[_nghost-wjn-3] .alert[_ngcontent-wjn-3]
Or in other words, why is there no _ngcontent-wjn-3 attribute on <div class="alert">...</div>?
Maybe I'm doing the whole thing wrong. What I'm trying to achieve is to customize the CSS of the individual bootstrap components (<alert> in the code above) as provided by the ng2-bootrap library (https://github.com/valor-software/ng2-bootstrap) inside my custom components (<home> in the code above).
I'm using the default view encapsulation (emulated) in the home component.
How can I do that please?
I figured it out myself. This is what I was looking for:
:host /deep/ .alert {
background-color: green;
}
The above code will produce the following:
[_nghost-wjn-3] .alert {
background-color: green;
}
This way I can modify the default styles of a bootstrap class (.alert, in this case) inside of my component <home>.
Source: https://angular.io/docs/ts/latest/guide/component-styles.html
You need:
[_nghost-wjn-3] alert[_ngcontent-wjn-3]
Instead of:
[_nghost-wjn-3] .alert[_ngcontent-wjn-3]
If you go and check your structure, alert tag has the ngcontent... attribute, not his div child with alert class.