How to select an element based on its data attribute in CSS?
使用属性选择器可基于data属性选中元素,如[data-type="submit"]精确匹配值,[data-highlight]匹配属性存在,[data-category*="news"]、[data-category^="top"]、[data-category$="post"]分别匹配包含、开头、结尾的值。

To select an element based on its data attribute in CSS, you use the attribute selector syntax. Data attributes are custom attributes prefixed with data-, and they can be targeted directly in your CSS rules.
Select by Exact Data Attribute Value
If you want to target an element that has a specific data attribute with a precise value, use the following format:
[data-your-attribute="value"] { /* styles */ }For example, to style a button with data-type="submit":
[data-type="submit"] { background-color: green; color: white; }Select by Presence of Data Attribute
You can apply styles if a data attribute exists, regardless of its value:
[data-highlight] { font-weight: bold; }This will match any element that has the data-highlight attribute, no matter the value.
Select by Partial or Pattern-Based Values
CSS also supports matching partial values using modifiers:
-
[data-category*="news"]– matches if the value contains "news" anywhere -
[data-category^="top"]– matches if the value starts with "top" -
[data-category$="post"]– matches if the value ends with "post"
These are helpful when working with dynamic or multiple values in data attributes.
Using data attributes in CSS gives you a clean way to style elements without affecting presentation classes. Just make sure not to overuse them for visual states that could be handled with regular classes.
Basically, just use square brackets with the full data- name and optional value matching — it's reliable and widely supported.
The above is the detailed content of How to select an element based on its data attribute in CSS?. For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undress AI Tool
Undress images for free
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undresser.AI Undress
AI-powered app for creating realistic nude photos
ArtGPT
AI image generator for creative art from text prompts.
Stock Market GPT
AI powered investment research for smarter decisions
Hot Article
Popular tool
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
20521
7
13634
4
CSS mobile performance optimization_Use will-change to inform transition attributes in advance
Mar 12, 2026 am 11:15 AM
Will-change should only be declared for transform and opacity attributes that will change frequently and can trigger synthesis; avoid abusing invalid attributes such as all, left, top, etc., which must be added/removed dynamically, and be used with caution in scrolling containers. Mobile terminals need to take into account compatibility and memory limitations.
How to make a simple fixed bottom toolbar with CSS_Set bottom:0 through position:fixed
Mar 10, 2026 pm 02:12 PM
The main reason why bottom:0 does not take effect is that the ancestor element triggers transform/will-change/filter to create a new containing block, so that the fixed element is positioned relative to it rather than the viewport; dynamic changes in the iOS Safari address bar cause occlusion; fixed elements need to be given way with padding-bottom; z-index failure is often caused by the parent creating a cascading context.
How to avoid content overflow when using CSS with float_Set the box-sizing of the box model and ensure that the sum of the percentages does not exceed 100%
Mar 12, 2026 pm 12:00 PM
If the floating element cannot open the parent container, BFC needs to be triggered. Overflow:hidden is commonly used; box-sizing:border-box must be set on the floating element itself; the percentage exceeds 100% due to whitespace characters, border/padding and pixel rounding; in IE, pay attention to the box-sizing prefix and margin parsing bugs.
How to set a transparent frosted sidebar with CSS_using rgba color through background-filter
Apr 03, 2026 pm 03:57 PM
In Safari, the background-filter needs to be prefixed with -webkit- and the background is opaque to take effect; the blur value cannot be 0; the size of the blurred area affects performance more than the numerical value; text needs to be enhanced with contrast and strokes to ensure readability.
How to adjust the picture display ratio in CSS_define the object-fit attribute in style
Mar 13, 2026 pm 01:09 PM
object-fit:cover is the most common choice - it maintains the aspect ratio, crops excess parts, and ensures the container is completely filled.
How to make a simple jitter tip with CSS_using keyframes and rotation
Apr 03, 2026 pm 03:42 PM
Shake animation should use translateX() to achieve left and right displacement instead of rotate(); it needs to be combined with @keyframes to define a 0%/25%/50%/75%/100% offset sequence, and the offset is controlled within ±2px~±6px; animation-fill-m must be added ode:forwards (or use both), select cubic-bezier(.36,.07,.19,.97) for easing; the old animation must be cleared and forced to rearrange before JS is triggered; use will-change with caution on the mobile terminal, and only add translateZ(0) to ensure hardware acceleration.
How to implement navigation signs with floating dynamic effects in CSS_using relative positioning and transition
Mar 12, 2026 am 11:51 AM
The main reason why CSShover animation is not smooth is that the transition is not written in the default state or acts on non-animable properties; the transition should be placed in a non-hover style, and transform should be used instead of left/relative. Pay attention to mobile hover invalidation and browser compatibility issues.
How does CSS exclude specific types of elements_Use the not pseudo-class selector to filter css
Mar 10, 2026 pm 02:57 PM
:not() only supports a single simple selector, disabling composite writing methods such as descendants/descendants; to exclude multiple conditions, you need to write repeatedly, such as: not(.a):not([b]); when mixed with :nth-child, it will still match the original sequence number, and will be rearranged after non-filtering.





