1

In my style component I have a class that changes the backgound color of a button as follows:

<style>
.button:global(:nth-child(2)) {
    background-color: red;
  }
</style>

My issue is that I don't want the nth-child hard-coded, I would like it to be a variable, as seen below.

<script>
let x = 5
</script>

<style>
.button :global(:nth-child(x)) {
    background-color: red;
  }
</style>

Is this even possible?

1
  • I'm not going to convert this into an answer, someone else can, but here's the first result on DuckDuckGo for "svelte variables in style tag": svelte.school/tutorials/…, and it seems to be what you need Commented Sep 3, 2021 at 1:49

1 Answer 1

1

No it's not possible, the only thing that is possible to use the style attribute to set properties (or custom properties) on an element, but these can not be used as an argument to nth-child

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.