Working with data attributes in CSS

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Thank you so much given such a piece of valuable information about CSS. it's really helpful tips for me! I appreciate the author of this post sharing something special. I am super excited to share that I have found Graphicly!
Graphicly is an all-in-one plugin for graphics management, image cropping, and image optimization. Graphicly lets you easily download and use images from the web,
whether you are on a website or on your WordPress site. With our powerful API, you can create customized libraries of images that integrate seamlessly into your theme’s header.
You can also upload and store your own images directly within the plugin—so there’s less work to do when it comes to finding specific visual assets, images/videos that are relevant and needed by your website users.
as a result of reading this post. I'm getting to save your profile in my favorite list. 🥰
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

We previously looked at using data attributes in JavaScript. These data attributes are a fantastic way to store little bits of information on an element without using custom attributes.
However, we can also use them to style some aspects with specific attribute sets.
To paint a simple picture, let's create elements that hold a specific value like this.
<div data-rating="1">Rating</div>
<div data-rating="5">Rating</div>
As you can see, our rating is only set as the custom attribute.
Let's first look at how we can style this object, which is very easy by simply using its name.
[data-rating] {
color: indigo;
}
With this, the rating text will be purple.
We can also make it more specific and add styling for particular values.
[data-rating='1'] {
color: red;
}
[data-rating='5'] {
color: indigo;
}
And to top it off, we can inject the value with CSS!
[data-rating]::after {
content: attr(data-rating);
}
Which would result in the rating being added after our text.
You can play with these data attributes on the following CodePen.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter