CSS Attribute Selectors
In this page:
[attribute] { }
[attribute="value"] { }
[attribute~="value"] { }
[attribute^="start"] { }
[attribute$="end"] { }
[attribute*="contains"] { }
Attributes की Presence को Target करना
Basic attribute selector (attribute key को brackets [attribute] में wrap करके defined) किसी भी ऐसे element को target करता है जिसके पास specified attribute है, उसकी value चाहे जो भी हो।
उदाहरण: Targeting Presence of Attributes
<style>
[title] {
cursor: help;
}
</style>
<span title="Extra info">Hover for a tooltip cursor</span>
Exact Attribute Values को Target करना
आप [attribute="value"] syntax इस्तेमाल करके एक specific attribute को एक exact value के साथ target कर सकते हैं। यह अलग-अलग form input types को style करने के लिए बेहद उपयोगी है, जैसे text fields को buttons से अलग करना।
उदाहरण: Targeting Exact Attribute Values
<style>
[type="text"] {
border: 1px solid gray;
}
</style>
<input type="text">
<input type="submit" value="Go">
Word Matches (~=) को Target करना
Word match selector [attribute~="value"] किसी भी ऐसे element को target करता है जिसकी attribute value में एक space-separated list के अंदर specified word शामिल हो। यह similar metadata strings share करने वाले element groups को target करने के लिए उपयोगी है।
उदाहरण: Targeting Word Matches (~=)
<style>
[class~="featured"] {
border: 2px solid gold;
}
</style>
<div class="card featured">Matches "featured" as a whole word</div>
Ends-With Patterns ($=) को Target करना
Ends-with selector [attribute$="value"] उन elements को target करता है जिनकी attribute value एक specified string pattern पर खत्म होती है। यह download links को उनके file formats के आधार पर style करने के लिए बेहद उपयोगी है।
उदाहरण: Targeting Ends-With Patterns ($=)
<style>
a[href$=".pdf"] {
color: red;
}
</style>
<a href="report.pdf">Download report.pdf</a>
Starts-With Patterns (^=) को Target करना
Starts-with selector [attribute^="value"] उन elements को target करता है जिनकी attribute value एक specified string pattern से शुरू होती है। यह आमतौर पर secure 'https://' protocols check करके external links को internal links से अलग style करने के लिए इस्तेमाल होता है।
उदाहरण: Targeting Starts-With Patterns (^=)
<style>
a[href^="https://"] {
color: green;
}
</style>
<a href="https://example.com">Secure external link</a>
- अपनी CSS selectors में attribute values को double quotes में wrap करना भूल जाना।
- values के अंदर partial text matches search करने के लिए word match selectors (~=) इस्तेमाल करना।
- string patterns की spelling गलत करना, जो matching के दौरान highly case-sensitive होते हैं।
- Attribute selectors elements को उनके HTML attributes की presence, exact value, या partial value patterns के आधार पर target करते हैं।
- [attribute="value"] से exact matches, [attribute$="value"] से ending patterns, और [attribute^="value"] से starting patterns target करें।
- form inputs, file downloads, और external link behaviors को automatically साफ़ तरीके से format करने के लिए इन selectors का इस्तेमाल करें।
Standard CSS attribute selectors हर modern web browser द्वारा natively supported हैं।
Chapter Quiz — Complete all 22 topics to unlock
0/22 topics done
Complete these topics first:
- CSS Combinators
- CSS Pseudo-classes
- CSS Pseudo-elements
- CSS Opacity
- CSS Navigation Bar
- CSS Vertical Navbar
- CSS Horizontal Navbar
- CSS Dropdowns
- CSS Advanced Dropdowns
- CSS Image Gallery
- CSS Image Sprites
- CSS Attribute Selectors
- CSS Forms
- CSS Counters
- CSS Specificity
- CSS Specificity Hierarchy
- CSS Variables
- CSS !important
- CSS Box Sizing
- CSS Media Queries
- CSS Pointer Events
- CSS Outline