Selector Lists and Specificity Preview
Selector Lists అంటే ఏమిటి?
ఒకే CSS rule ను చాలా selectors కు apply చేయాలంటే selectors ను comma (,) తో వేరు చేసి ఒక selector list గా రాయవచ్చు.
ఉదాహరణకు h1, h2, p అంటే ఈ మూడు selectors కు ఒకే style apply అవుతుంది. ప్రతి selector తనకు సరిపోయే HTML elements ను select చేస్తుంది.
Selector List Syntax
h1, h2, .notice {
color: darkblue;
}
ఇక్కడ h1, h2, మరియు .notice మూడు వేర్వేరు selectors. Comma వాటిని ఒకే rule లో కలుపుతుంది.
Specificity Preview
ఒక HTML element కు ఒకటి కంటే ఎక్కువ CSS rules match అయితే, ఏ rule కు ఎక్కువ priority ఉందో browser నిర్ణయిస్తుంది. ఈ priority ని specificity అంటారు.
- Element selector, ఉదాహరణకు
p, తక్కువ specificity కలిగి ఉంటుంది. - Class selector, ఉదాహరణకు
.notice, element selector కంటే ఎక్కువ specificity కలిగి ఉంటుంది. - Attribute selector, ఉదాహరణకు
[data-type="warning"], class selector కు సమానమైన category లో ఉంటుంది. - ID selector, ఉదాహరణకు
#main-notice, class లేదా attribute selector కంటే ఎక్కువ specificity కలిగి ఉంటుంది.
Specificity ఎక్కువగా ఉన్న rule సాధారణంగా apply అవుతుంది. Specificity ఒకటే అయితే, తర్వాత రాసిన rule గెలుస్తుంది. ఇది specificity యొక్క basic preview మాత్రమే; తరువాత దీనిని మరింత వివరంగా నేర్చుకుంటారు.
Browser ఎలా పని చేస్తుంది?
Browser ముందుగా ప్రతి selector ఏ HTML element కు match అవుతుందో చూస్తుంది. ఒకే element కు అనేక rules match అయితే, selectors యొక్క specificity ని పోల్చి final style ను ఎంచుకుంటుంది.
Simple Explanation
ఒకే style ను అనేక elements కు ఇవ్వాలంటే selector list ఉపయోగించండి. Selectors ను comma తో separate చేయాలి.
ఉదాహరణకు h1, .title అంటే h1 elements మరియు title class ఉన్న elements రెండింటికీ అదే style వస్తుంది.
ఒక element ను element selector, class selector, మరియు ID selector కలిసి target చేస్తే, సాధారణంగా ID selector కు ఎక్కువ specificity ఉంటుంది. అందువల్ల ID rule యొక్క value కనిపిస్తుంది.
Real-Life Example
ఒక school notice board లో main heading, subheading, మరియు warning notice అన్నింటినీ ఒకే నీలం రంగులో ఉంచాలనుకుందాం. ప్రతి దానికి విడిగా rule రాయడం బదులు h1, h2, .warning అనే selector list ఉపయోగించవచ్చు.
అయితే ఒక specific notice కు ప్రత్యేకమైన రంగు కావాలంటే, దానికి class లేదా ID selector ఉపయోగించవచ్చు. Specific selector సాధారణ selector కంటే ఎక్కువ priority పొందుతుంది.
Code Examples
Selector List మరియు Specificity ఉదాహరణ
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Selector Lists</title>
<style>
h1, h2, .notice {
color: #174a7c;
}
p {
color: #444444;
}
.notice {
background-color: #fff3cd;
padding: 12px;
}
#important-notice {
color: #9b1c1c;
}
</style>
</head>
<body>
<h1>School Information</h1>
<h2>Today's Notice</h2>
<p class="notice">Bring your project file tomorrow.</p>
<p id="important-notice" class="notice">The library closes at 4 PM.</p>
</body>
</html>
h1, h2, .notice selector list వల్ల heading elements మరియు notice class ఉన్న paragraph కు ఒకే text color వస్తుంది. .notice rule background color మరియు padding ఇస్తుంది. చివరి paragraph కు .notice మరియు #important-notice రెండూ match అవుతాయి. ID selector కు ఎక్కువ specificity ఉండటంతో ఆ paragraph text color ఎరుపుగా కనిపిస్తుంది.
The h1 and h2 headings are dark blue. Both notice paragraphs have a light yellow background and padding. The first notice paragraph has dark blue text, while the paragraph with id important-notice has dark red text.
Line-by-Line Explanation
h1, h2, మరియు .notice ను comma తో కలిపి selector list తయారు చేశాం. ఈ మూడు selectors కు ఒకే rule apply అవుతుంది.
Selector list లో ఉన్న ప్రతి matching element యొక్క text color ను dark blue గా మార్చుతుంది.
ప్రతి paragraph element ను select చేసే element selector ఇది.
అన్ని paragraphs కు మొదట dark gray text color ఇస్తుంది.
notice class ఉన్న అన్ని elements ను select చేస్తుంది. Class selector, element selector కంటే ఎక్కువ specificity కలిగి ఉంటుంది.
notice paragraphs కు light yellow background ఇస్తుంది.
Notice text చుట్టూ లోపలి ఖాళీని 12 pixels గా ఉంచుతుంది.
important-notice ID ఉన్న ఒక specific element ను select చేస్తుంది. ID selector కు class మరియు element selectors కంటే ఎక్కువ specificity ఉంటుంది.
ఈ ID ఉన్న paragraph యొక్క text color ను dark red గా మార్చుతుంది. ఇది .notice rule కంటే ఎక్కువ specificity కలిగి ఉండటం వల్ల ఈ color కనిపిస్తుంది.
Bring your project file tomorrow.
ఈ paragraph కు notice class ఉంది. అందువల్ల selector list మరియు .notice rule రెండూ దీనికి apply అవుతాయి.
The library closes at 4 PM.
ఈ paragraph కు class మరియు ID రెండూ ఉన్నాయి. రెండూ match అయినా, ID selector కు ఎక్కువ specificity ఉండటం వల్ల ID rule యొక్క dark red color చివరగా కనిపిస్తుంది.
Try It
Try this CSS code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Try Selector Lists</title>
<style>
h1, h2, .highlight {
color: #155e75;
}
.highlight {
background-color: #cffafe;
padding: 10px;
}
#special-item {
color: #b91c1c;
}
</style>
</head>
<body>
<h1>Weekly Tasks</h1>
<h2>Important Items</h2>
<p class="highlight">Complete the CSS exercise.</p>
<p id="special-item" class="highlight">Submit the assignment today.</p>
</body>
</html>
Practice
Practice: Selector list ఉపయోగించి h1 మరియు h2 రెండింటి text color ను blue గా మార్చండి. తరువాత #featured selector తో Desk Lamp paragraph text color ను dark red గా మార్చండి.
Hint: h1, h2 అని comma తో రాయండి. ID selector కోసం #featured ఉపయోగించండి. తరువాత browser లో run చేసి రెండు rules ఎలా పని చేస్తున్నాయో చూడండి.
Common Mistakes
h1 h2 అని రాయడం.రెండు independent selectors కు ఒకే rule ఇవ్వాలంటే
h1, h2 అని comma ఉపయోగించాలి. Space ఉపయోగిస్తే అది descendant selector అర్థం ఇస్తుంది.
చివరి selector తర్వాత comma పెట్టకండి. సరైన syntax:
h1, h2, .notice.
Class కు
.card, ID కు #featured అని రాయాలి. HTML లో ఉన్న class లేదా id value తో spelling కూడా match కావాలి.
ముందుగా specificity చూడండి. ID selector సాధారణంగా class మరియు element selectors కంటే ఎక్కువ specificity కలిగి ఉంటుంది.
ప్రతి selector HTML లోని నిజమైన element, class, లేదా ID ను target చేస్తున్నదో తనిఖీ చేయండి.
Interview Questions
CSS selector list అంటే ఏమిటి?
Answer: ఒకే CSS rule ను అనేక selectors కు apply చేయడానికి comma తో కలిపిన selectors సమూహాన్ని selector list అంటారు. ఉదాహరణకు h1, h2, p.
Selector list లో comma యొక్క పని ఏమిటి?
Answer: Comma selectors ను independent selectors గా వేరు చేస్తుంది. ప్రతి selector తనకు match అయ్యే elements ను select చేస్తుంది.
Specificity అంటే ఏమిటి?
Answer: ఒక element కు అనేక CSS rules match అయినప్పుడు ఏ rule కు ఎక్కువ priority ఉందో నిర్ణయించే విధానాన్ని specificity అంటారు.
Element selector మరియు ID selector లో ఏది ఎక్కువ specificity కలిగి ఉంటుంది?
Answer: ID selector ఎక్కువ specificity కలిగి ఉంటుంది. సాధారణంగా ID selector, class లేదా element selector పై priority పొందుతుంది.
ఒకే specificity ఉన్న రెండు rules ఉంటే ఏది apply అవుతుంది?
Answer: సాధారణంగా CSS లో తర్వాత రాసిన rule apply అవుతుంది.
Exam Questions
క్రింది selectors ను ఒక selector list గా రాయండి: h1, h2, మరియు .title.
Answer:
సరైన syntax: h1, h2, .title
క్రింది code లో ఏ color కనిపిస్తుంది? p { color: blue; } మరియు #message { color: red; }, అలాగే paragraph కు id="message" ఉంది.
Answer:
Red color కనిపిస్తుంది, ఎందుకంటే ID selector కు element selector కంటే ఎక్కువ specificity ఉంటుంది.
క్రింది code లో comma మరియు space మధ్య తేడా ఏమిటి: h1, p మరియు h1 p?
Answer:
h1, p అంటే h1 మరియు p రెండూ select అవుతాయి. h1 p అంటే h1 లోపల ఉన్న p elements మాత్రమే select అవుతాయి.
Class selector ను ఎలా రాస్తారు?
Answer:
Class selector ను dot తో రాస్తారు. ఉదాహరణ: .card.
Quick Revision
- ఒకే style ను అనేక selectors కు ఇవ్వడానికి comma-separated selector list ఉపయోగించండి.
h1, h2, .noticeలో ప్రతి selector independent గా పని చేస్తుంది.- Space మరియు comma ఒకే అర్థం ఇవ్వవు.
- Specificity CSS rules మధ్య priority ని నిర్ణయిస్తుంది.
- సాధారణ క్రమం: element కంటే class/attribute ఎక్కువ, class/attribute కంటే ID ఎక్కువ specificity.
- Specificity ఒకటే అయితే తర్వాత రాసిన rule సాధారణంగా గెలుస్తుంది.
