#Add attribute value dynamic
3 messages · Page 1 of 1 (latest)
Hi @still heron. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
In Astro, you can dynamically add classes to an element using the class:list utility attribute. This attribute allows you to combine classes on an element dynamically. Here's how you can use it:
---
const { guildId, path } = Astro.props;
const isActive = ("/guild/" + guildId) === path;
---
<a href={"/guild/" + guildId} class:list={['text-gray-900', 'dark:text-white', 'hover:underline', { 'active': isActive }]}>Guild Overview</a>
In this example, the active class will be added to the list of classes if the isActive variable is truthy. If isActive is falsy, the active class will not be included.