Knight
Knight

Reputation: 573

Vue - getting data attribute value in css

Is there any option to get data attribute value in Vue css?

<template>
   <p data-background="purple"> TEST </p>
</template>

<style lang="scss">
p {
  background: attr(data-background); //error
  &:after {
     background: attr(data-background); //error
  }
}
</style>

Upvotes: 1

Views: 2941

Answers (1)

Quentin
Quentin

Reputation: 3289

You could use CSS variables for this case.

new Vue({
  el: '#app',
  data: {
    elStyle: {
      '--background': 'lightblue',
    }
  }
});
p:after {
  content: 'A pseudo element';
  background: var(--background, red); // Red is the fallback value
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <p :style="elStyle"></p>
</div>

Upvotes: 1

Related Questions