Descendant Combinator
Descendant Combinator అంటే ఏమిటి?
Descendant Combinator అనేది ఒక parent element లోపల ఉన్న అన్ని matching descendant elements ను select చేయడానికి ఉపయోగించే CSS selector పద్ధతి.
దీనిలో రెండు selectors మధ్య ఒక ఖాళీ space ఉంటుంది. ఉదాహరణకు .card p అంటే card class ఉన్న element లోపల ఉన్న అన్ని <p> elements.
ఎందుకు ఉపయోగిస్తాం?
- ఒక నిర్దిష్ట section లోని elements కు మాత్రమే style ఇవ్వడానికి.
- అదే HTML tag page లో ఇతర చోట్ల ఉన్నా, అవసరమైన parent లోపల ఉన్న వాటిని మాత్రమే target చేయడానికి.
- HTML structure ఆధారంగా CSS ను స్పష్టంగా నిర్వహించడానికి.
Syntax
ancestor descendant {
property: value;
}
ఇక్కడ ancestor parent లేదా పై స్థాయి element ను సూచిస్తుంది. descendant దాని లోపల ఉన్న target element ను సూచిస్తుంది. ఈ రెండు selectors మధ్య తప్పనిసరిగా space ఉండాలి.
ముఖ్యమైన విషయం
Descendant Combinator ఒక element లోపల ఏ స్థాయిలో ఉన్న matching elements నైనా select చేస్తుంది. అంటే target element parent కు వెంటనే లోపల ఉండాల్సిన అవసరం లేదు; మధ్యలో మరొక element ఉన్నా అది select అవుతుంది.
Code Example
ఈ ఉదాహరణలో .article p selector, article class ఉన్న section లోని paragraphs కు మాత్రమే style ఇస్తుంది. .article బయట ఉన్న paragraph కు ఆ style వర్తించదు.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Descendant Combinator</title>
<style>
.article p {
color: darkblue;
background-color: #eaf2ff;
padding: 8px;
}
</style>
</head>
<body>
<section class="article">
<h1>CSS Selectors</h1>
<div>
<p>ఈ paragraph article section లోపల ఉంది.</p>
</div>
</section>
<p>ఈ paragraph article section బయట ఉంది.</p>
</body>
</html>
ఎలా చదవాలి?
.article p ను “article class ఉన్న element లోపల ఉన్న ప్రతి paragraph” అని చదవాలి. HTML లో <p> ఒక <div> లోపల ఉన్నా, ఆ <div> article లోపల ఉన్నందున paragraph select అవుతుంది.
Simple Explanation
Descendant Combinator లో రెండు selectors మధ్య space ఉంటుంది.
.menu a అంటే menu class ఉన్న element లోపల ఉన్న అన్ని links. .card span అంటే card లోపల ఉన్న అన్ని <span> elements.
ఈ selector page మొత్తం ఉన్న elements ను కాకుండా, ఒక నిర్దిష్ట parent లోపల ఉన్న matching elements ను మాత్రమే style చేస్తుంది.
Real-Life Example
ఒక website లో ఒక పెద్ద building ను .building class ఉన్న container గా ఊహించండి. ఆ building లోని అన్ని rooms ను paragraphs గా ఊహిస్తే, .building p selector ఆ building లోపల ఉన్న rooms కు మాత్రమే ఒకే విధమైన గుర్తింపు ఇవ్వడంలా పనిచేస్తుంది. ఇతర buildings లోని rooms కు ఆ style వర్తించదు.
Code Examples
Descendant Combinator తో ఒక section లోని paragraphs ను style చేయడం
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Descendant Combinator</title>
<style>
.article p {
color: darkblue;
background-color: #eaf2ff;
padding: 8px;
}
</style>
</head>
<body>
<section class="article">
<h1>CSS Selectors</h1>
<div>
<p>ఈ paragraph article section లోపల ఉంది.</p>
</div>
</section>
<p>ఈ paragraph article section బయట ఉంది.</p>
</body>
</html>
.article p selector, article class ఉన్న section లోపల ఉన్న paragraph ను select చేస్తుంది. Paragraph ఒక <div> లోపల ఉన్నా, అది article లోపల descendant కాబట్టి style పొందుతుంది. బయట ఉన్న paragraph కు style రాదు.
The paragraph inside the article section appears dark blue with a light blue background and padding. The paragraph outside the article section keeps the browser's default styling.
Line-by-Line Explanation
.article class ఉన్న element లోపల ఉన్న అన్ని <p> elements ను select చేస్తుంది. మధ్యలో ఉన్న space descendant relationship ను సూచిస్తుంది.
Select అయిన paragraphs లోని text కు dark blue రంగు ఇస్తుంది.
Select అయిన paragraphs వెనుక light blue background ను ఉంచుతుంది.
Paragraph text మరియు దాని background edge మధ్య 8 pixels లోపలి ఖాళీని ఇస్తుంది.
ఈ descendant selector యొక్క declaration block ఇక్కడ ముగుస్తుంది.
ఇది
article class ఉన్న parent section. దీని లోపల ఉన్న matching paragraphs selector ద్వారా target అవుతాయి.
ఈ
<div> paragraph మరియు article section మధ్యలో ఉంది. అయినా paragraph article లోపల ఉన్నందున descendant selector దాన్ని select చేస్తుంది.
ఈ paragraph article section లోపల ఉంది. అందువల్ల
.article p style దీనికి వర్తిస్తుంది.
ఈ paragraph article section బయట ఉంది. అందువల్ల
.article p selector దీనిని select చేయదు.
Try It
Try this CSS code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Descendant Combinator Practice</title>
<style>
.notice p {
color: green;
background-color: #eefbea;
padding: 8px;
}
</style>
</head>
<body>
<section class="notice">
<h2>Important Notice</h2>
<div>
<p>This paragraph is inside the notice.</p>
</div>
</section>
<p>This paragraph is outside the notice.</p>
</body>
</html>
Practice
Practice: పై code లో .profile section లోపల ఉన్న paragraph text ను blue color లో చూపించండి. .profile మరియు p మధ్య space ఉపయోగించే descendant selector రాయండి. బయట ఉన్న paragraph మారకూడదు.
Hint: Selector ను .profile p రూపంలో రాయండి. తరువాత color: blue; declaration ఉపయోగించండి.
Common Mistakes
.profilep అని రాయడం.Descendant Combinator కోసం parent selector మరియు descendant selector మధ్య తప్పనిసరిగా space పెట్టాలి:
.profile p.
p .profile అని selector order ను మార్చడం.ముందుగా ancestor లేదా parent selector, తరువాత దాని లోపల ఉన్న descendant selector రావాలి. సరైన రూపం
.profile p.
p మాత్రమే ఉపయోగించడం.ఒక నిర్దిష్ట container లోని paragraphs కు మాత్రమే style కావాలంటే container class తో కలిపి
.profile p వంటి descendant selector ఉపయోగించాలి.
Descendant selector మధ్యలో ఇతర elements ఉన్నా పనిచేస్తుంది.
.profile లోపల <div> లో ఉన్న <p> కూడా select అవుతుంది.
Interview Questions
CSS లో Descendant Combinator అంటే ఏమిటి?
Answer: ఒక ancestor లేదా parent element లోపల ఉన్న matching descendant elements ను select చేసే selector ను Descendant Combinator అంటారు. రెండు selectors మధ్య space ఉంటుంది.
.menu a selector ఏమి select చేస్తుంది?
Answer: ఇది menu class ఉన్న element లోపల ఉన్న అన్ని anchor links ను select చేస్తుంది.
Descendant Combinator లో target element వెంటనే parent లోపల ఉండాలా?
Answer: లేదు. Target element మధ్యలో ఇతర elements ఉన్నా, అది ancestor లోపల ఉంటే select అవుతుంది.
Exam Questions
Descendant selector కు సరైన syntax ఏది? A) .boxp B) .box > p C) .box p
Answer:
సరైన సమాధానం C) .box p. రెండు selectors మధ్య ఉన్న space descendant relationship ను సూచిస్తుంది.
ఒక .content section లోని paragraphs కు మాత్రమే red color ఇవ్వడానికి CSS రాయండి.
Answer:
.content p { color: red; }
Descendant selector మరియు page-wide element selector మధ్య ప్రధాన తేడా ఏమిటి?
Answer:
Page-wide selector అయిన p అన్ని paragraphs ను target చేస్తుంది. Descendant selector అయిన .content p మాత్రం content class ఉన్న element లోపల ఉన్న paragraphs ను మాత్రమే target చేస్తుంది.
Quick Revision
- Descendant Combinator లో రెండు selectors మధ్య ఒక space ఉంటుంది.
.parent pఅంటే parent లోపల ఉన్న అన్ని paragraphs.- Descendant element వెంటనే parent లోపల ఉండాల్సిన అవసరం లేదు.
- Ancestor selector ముందుగా, descendant selector తరువాత రాయాలి.
- ఒక నిర్దిష్ట section లోని elements కు మాత్రమే style ఇవ్వడానికి ఇది ఉపయోగపడుతుంది.
