What is the method for setting a data attribute with CSS?

In my CSS, I have the following:

.map-menu .category.selected {
    background-image: url("../img/category-selected.png");
}

However, I am trying to apply this style to elements with certain data attributes. For example, I can target a CSS element for a specific data attribute like this:

[data-id='1']   {
    width: 400px;
}

I'm struggling to figure out how to target both the category in the CSS and the data attribute together. I tried combining them like this, but it's not working:

.map-menu .category.selected  [data-id='1']  {
    background-image: url("../img/category-selected1.png");
}

Can anyone provide guidance on how to achieve this? I don't have much experience with data attributes.

Here is an example of the HTML structure where this styling should be applied:

<div class="category selected" data-id="1">Art Galleries</div>

UPDATE: I managed to solve this by being more specific with my selector:

div.selected[data-id='5']   {
 background-image: url("../img/category-selected.png") !important;
}

Answer №1

This CSS code snippet is fully functional and an interactive example can be found on JSFiddle:

[data-id]{color: red;}
[data-id="1"]{color: blue;}

Answer №2

Breaking News: I have cracked the code! All you need to do is provide a clear and specific style directive like so:

div.highlighted[data-id='5']   {
background-image: url("../img/category-highlighted.png") !important;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to have a content's columns adjust to the size of an image?

I'm currently struggling with a layout issue. I have an image on the left and want the content columns on the right (title, lorem...) to adjust their height to match the image. How can I achieve this? I'm using Bootstrap v5.1.1 Current image: ...

Enhance the appearance of a row on the table with a lumin

Is there a way to create a glowing effect around a single row in a table, similar to the style shown in this example? Here's the code that can help achieve this effect, sourced from CSS/HTML: Create a glowing border around an Input Field .glowing-bo ...

Style the modal body to display as a block on both desktop and mobile devices using CSS

My goal is to ensure that the modal-body is vertically aligned for optimal visibility on both mobile phones and PCs. I am utilizing bootstrap for this purpose. <div class="modal-body" style="display: inline-block;"> <div> ...

How to ensure that two rows in Vuetify evenly split the screen into two equal halves

I'm struggling with what seems like a simple task. All I want is for the upper half of the v-main to be taken up by one v-row, and the lower half by another v-row. If the content of either row overflows, it should be scrollable. You can view the MWE ...

Tips for styling a PHP file using CSS and styling more than one element:1. Begin by creating a

I am currently attempting to style a php file using a linked stylesheet, but I am encountering some difficulties. At the moment, I have the , it will affect either the font or the border.</p> Is there a way to apply multiple styles to elements on a ...

Content missing after centered banner

Seeking help! I need to figure out how to center text after a container. Whenever I try, it ends up either behind the banner picture or misaligned. I want both the banner and the text example to be centered, but placing the text in the right position has b ...

center a "button" vertically

I have created a transparent "button" and positioned it on a full-width background image using the class within an <a> tag. I want the button to be centered responsively on the image, achieving horizontal centering but not quite vertical alignment (m ...

Switch the color of the final letter

Sample code snippet: <p class="test">string</p> Looking to dynamically change the color of the last letter, specifically the letter "g", using CSS only, without relying on JavaScript. I am displaying the string character by character and nee ...

Adjusting the zoom feature in CSS3

I'm looking to have my React app display as if the user has changed the zoom level to 90%. I attempted to tackle this issue using CSS. body { transform: scale(0.9); } However, this method did not result in the screen appearing similar to wh ...

Adjust the dimensions of the label element

Even after utilizing the width property in the label tag, I am unable to adjust the size of the 'Categories' box within my dropdown menu created using slide. Snippet of HTML code `<div class="nav-1"> <label for="touch&qu ...

Problem with Adjusting Bootstrap Carousel Caption Placement per Slide

Trying to customize the position of each Bootstrap Carousel slide in This Demo. The code snippet below modifies the caption positioning: .carousel-caption { top: 0; bottom: auto; } However, I need to adjust the positions of other slides' ca ...

Firebug displays the class name and location twice in the right panel

When examining a div labeled with .mlm-clearfix, I noticed that Firebug was displaying this class name and its URL twice in the right panel. The style declarations given for the Easy Clear Method in relation to this class are as follows: .mlm-clearfix:bef ...

Top Tip for conditionally rendering a styled component inside or outside a wrapper, depending on the screen width

Can anyone help me with this coding question? I'm currently trying to determine the most efficient way to conditionally place the code inside or outside of the wrapper. However, I am unsure what the best practice for this would be. This process seems ...

Angular JS has a unique feature of a scrollable drop-up menu that allows

https://i.sstatic.net/MOUqO.pngHere is the code snippet for my Dropup component: <div class="dropup"> <button class="btn btn-primary btn-raised dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false> ...

When you hover over the page, the content shifts to reveal a hidden div

Trying to figure out how to show additional content when a photo is hovered over without shifting the rest of the page's content placement? Looking for alternative solutions rather than using margin-top: -22%. Here's a link to the website in que ...

Having trouble with the JSFiddle dropdown button that is unresponsive to

I am currently in the process of developing a test drop-down menu. The open menu button functions correctly, however, the close button is unresponsive to clicking or hovering actions. Upon clicking the open menu button, it should make the other button visi ...

React components may overlap each other due to their elements intersecting

I am currently facing an issue in my React project where the footer is overlapping with the elements at the bottom of the page. To view the live project, you can visit: Although I have researched similar questions on this topic and tried adjusting margin ...

Finding a solution to use webpack style loader to allow the importing of css directly into my components

I am currently experimenting with the GitHub repository at https://github.com/sahat/megaboilerplate with a goal of importing CSS from within my components. Below is the configuration I am working with: const path = require('path'); const webpac ...

Inline-block display causing divs to act unpredictably

I am working with a main div that contains three smaller divs inside. Each of these divs has been assigned a width of 30%, and they are all perfectly centered within the main div. To ensure that the three divs appear side by side, I used the display: inli ...

There is a lack of CSS import in the HTML document

I have organized my files in the following structure: - index.html - css/styles.css This is the HTML code I am using: <!DOCTYPE html> <html lang="en> <head> <title>Bootstrap Case</title> <meta charset="utf-8"> & ...