0

Is there a way to define css selector properties dynamically ?

For example how to define "color" property of ".some-style" selector with the value got from the backend server?

<style>
    .some-style {
        color: #ffc050;
    }
</style>
1
  • please give more details about your use case Commented Nov 19, 2020 at 14:15

1 Answer 1

1
  1. Create a dynamic style in your top-most element in your template.
  2. Assign the backend response of your properties to a computed function.
  3. Set style lang to lang='scss' then use CSS varialbe function var() to set the values.

Example

<template>
    <div :style="cssProps">
        <div class="some-style">
            Hello Mars
        </div>
    </div>
</template>

<script>
export default {
computed: {
    cssProps() {
        // backend response with your css values
        let backendResponseObject = {
            fontColor: "black", // you can use rgb or hex
            backgroundColor: "White" // you can use rgb or hex
        }

        properties = {
            "--brand-base": backendResponseObject.color,
            "--brand-primary": backgroundColor.hex,
        };

        return properties;
    }
}
}
</script>

<style lang="scss">
.some-style {
    color: var(--brand-base);
    background: var(--brand-primary);
}
</style>
Sign up to request clarification or add additional context in comments.

1 Comment

Set style lang to lang='scss' then Actually you don't have to use scss. just that I've always used this in <style lang="scss">

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.