Adjacent and General Sibling Combinators
Adjacent and General Sibling Combinators అంటే ఏమిటి?
CSS combinators HTML elements మధ్య ఉన్న సంబంధాన్ని బట్టి elements ను select చేయడానికి ఉపయోగిస్తాము. ఈ పాఠంలో ఒకే parent కింద ఉన్న sibling elements ను select చేయడం నేర్చుకుంటాం.
Adjacent Sibling Combinator (+)
+ combinator ఒక element తరువాత వెంటనే వచ్చే sibling element ను మాత్రమే select చేస్తుంది.
ఉదాహరణ: h2 + p అంటే ప్రతి <h2> తరువాత వెంటనే వచ్చే <p> element.
General Sibling Combinator (~)
~ combinator ఒక element తరువాత అదే parent కింద వచ్చే అన్ని matching sibling elements ను select చేస్తుంది.
ఉదాహరణ: h2 ~ p అంటే <h2> తరువాత వచ్చే అన్ని sibling <p> elements. అవి వెంటనే తరువాత ఉండాల్సిన అవసరం లేదు.
ఇవి ఎలా పనిచేస్తాయి?
h2 + pలో మొదటి<p>మాత్రమే match అవుతుంది, ఎందుకంటే అది<h2>తరువాత వెంటనే ఉంది.h2 ~ pలో<h2>తరువాత ఉన్న అన్ని sibling paragraphs match అవుతాయి.- ఈ elements ఒకే parent కింద ఉండాలి.
- ముందు ఉన్న siblings ను ఇవి select చేయవు; తరువాత ఉన్న siblings ను మాత్రమే select చేస్తాయి.
Syntax
first-element + second-element {
property: value;
}
first-element ~ second-element {
property: value;
}
Simple Explanation
ఒక classroom లో teacher తరువాత వెంటనే కూర్చున్న student ను మాత్రమే ఎంచుకోవడం Adjacent Sibling Combinator లాంటిది. Teacher తరువాత కూర్చున్న మిగతా students అందరినీ ఎంచుకోవడం General Sibling Combinator లాంటిది.
h2 + p వెంటనే వచ్చే paragraph ను మాత్రమే select చేస్తుంది. h2 ~ p అయితే అదే parent కింద h2 తరువాత ఉన్న అన్ని paragraphs ను select చేస్తుంది.
Real-Life Example
ఒక news card లో heading తరువాత వెంటనే వచ్చే summary paragraph కు ప్రత్యేక background ఇవ్వాలనుకుంటే h2 + p ఉపయోగించవచ్చు. Heading తరువాత ఉన్న అన్ని supporting paragraphs కు ఒకే text color ఇవ్వాలనుకుంటే h2 ~ p ఉపయోగించవచ్చు.
Code Examples
Adjacent మరియు General Sibling Combinators Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sibling Combinators</title>
<style>
.article h2 + p {
background-color: #fff3cd;
padding: 10px;
}
.article h2 ~ p {
color: #1d4ed8;
}
</style>
</head>
<body>
<article class="article">
<h2>CSS Combinators</h2>
<p>This paragraph comes immediately after the heading.</p>
<p>This paragraph also comes after the heading.</p>
<p>This is another sibling paragraph.</p>
</article>
</body>
</html>
.article h2 + p heading తరువాత వెంటనే వచ్చే మొదటి paragraph కు background మరియు padding ఇస్తుంది. .article h2 ~ p heading తరువాత ఉన్న అన్ని sibling paragraphs కు blue text color ఇస్తుంది. అందువల్ల మొదటి paragraph కు రెండు rules వర్తిస్తాయి; తరువాతి paragraphs కు general sibling rule మాత్రమే వర్తిస్తుంది.
మొదటి paragraph కు లేత పసుపు background, padding మరియు blue text కనిపిస్తాయి. తరువాతి రెండు paragraphs కు blue text మాత్రమే కనిపిస్తుంది.
Line-by-Line Explanation
.article లోని h2 తరువాత వెంటనే వచ్చే p element ను select చేస్తుంది. ఇది Adjacent Sibling Combinator.
వెంటనే వచ్చే paragraph కు light yellow background ఇస్తుంది.
ఆ paragraph లోని text చుట్టూ లోపలి ఖాళీని పెంచుతుంది.
.article లోని h2 తరువాత వచ్చే అన్ని sibling p elements ను select చేస్తుంది. ఇది General Sibling Combinator.
Match అయిన అన్ని paragraphs లోని text కు blue color ఇస్తుంది.
CSS Combinators
ఇది starting heading. దీని తరువాత ఉన్న sibling paragraphs ను combinator selectors పరిశీలిస్తాయి.
This paragraph comes immediately after the heading.
ఈ paragraph
h2 తరువాత వెంటనే ఉంది. అందువల్ల h2 + p మరియు h2 ~ p రెండింటికీ match అవుతుంది.
This paragraph also comes after the heading.
ఈ paragraph వెంటనే తరువాత లేదు. అందువల్ల
h2 + p కు match కాదు, కానీ h2 ~ p కు match అవుతుంది.
Try It
Try this CSS code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Try Sibling Combinators</title>
<style>
.card h2 + p {
background-color: #e0f2fe;
padding: 8px;
}
.card h2 ~ p {
color: #166534;
}
</style>
</head>
<body>
<div class="card">
<h2>Daily Tip</h2>
<p>This paragraph is immediately after the heading.</p>
<p>This paragraph is another sibling.</p>
</div>
</body>
</html>
Practice
Practice: పై starter code లో మొదటి paragraph కు మాత్రమే light yellow background మరియు padding ఇవ్వండి. <h2> తరువాత వెంటనే వచ్చే <p> ను select చేయడానికి Adjacent Sibling Combinator ఉపయోగించండి. తరువాత heading తరువాత ఉన్న అన్ని paragraphs కు dark blue text ఇవ్వండి.
Hint: .lesson h2 + p ను మొదటి rule కోసం, .lesson h2 ~ p ను అన్ని తరువాతి paragraphs కోసం ఉపయోగించండి.
Common Mistakes
h2 + p బదులుగా h2 p రాస్తే, heading లోపల ఉన్న paragraph ను వెతుకుతుంది.+ Adjacent Sibling Combinator ను తప్పకుండా ఉపయోగించండి. h2 + p అంటే heading తరువాత వెంటనే వచ్చే sibling paragraph.
h2 ~ p ను ఉపయోగించినప్పుడు ముందున్న paragraphs కూడా select అవుతాయని అనుకోవడం.~ combinator తరువాత వచ్చే siblings ను మాత్రమే select చేస్తుంది. ముందున్న siblings ను select చేయదు.
రెండు elements ఒకే parent కింద ఉండాలి. ఒకే parent లేకపోతే
+ లేదా ~ match కావు.
వెంటనే వచ్చే ఒక్క element కోసం
+ ఉపయోగించండి. తరువాత ఉన్న అన్ని matching siblings కోసం ~ ఉపయోగించండి.
Interview Questions
Adjacent Sibling Combinator అంటే ఏమిటి?
Answer: ఇది + గుర్తుతో రాస్తారు. ఒక element తరువాత వెంటనే వచ్చే matching sibling element ను మాత్రమే select చేస్తుంది.
General Sibling Combinator ఏ గుర్తుతో రాస్తారు?
Answer: General Sibling Combinator ను ~ గుర్తుతో రాస్తారు. ఇది మొదటి element తరువాత అదే parent కింద వచ్చే అన్ని matching siblings ను select చేస్తుంది.
h2 + p మరియు h2 ~ p మధ్య తేడా ఏమిటి?
Answer: h2 + p వెంటనే వచ్చే మొదటి paragraph ను మాత్రమే select చేస్తుంది. h2 ~ p అయితే h2 తరువాత ఉన్న అన్ని sibling paragraphs ను select చేస్తుంది.
Sibling combinators పనిచేయడానికి ముఖ్యమైన నియమం ఏమిటి?
Answer: Select చేయాల్సిన elements ఒకే parent కింద siblings గా ఉండాలి. ఇవి తరువాత వచ్చే siblings ను మాత్రమే select చేస్తాయి.
Exam Questions
వెంటనే వచ్చే sibling element ను select చేయడానికి ఏ combinator ఉపయోగిస్తారు?
Answer:
Adjacent Sibling Combinator, అంటే +.
అన్ని తరువాతి sibling paragraphs ను select చేయడానికి సరైన selector ఏది: h2 + p లేదా h2 ~ p?
Answer:
h2 ~ p సరైన selector.
h2 + p లో p element ఎప్పుడు match అవుతుంది?
Answer:
p element అదే parent కింద ఉండి, h2 తరువాత వెంటనే వచ్చినప్పుడు match అవుతుంది.
Quick Revision
+Adjacent Sibling Combinator.~General Sibling Combinator.+వెంటనే వచ్చే ఒక matching sibling ను మాత్రమే select చేస్తుంది.~తరువాత వచ్చే అన్ని matching siblings ను select చేస్తుంది.- రెండు elements ఒకే parent కింద siblings గా ఉండాలి.
- ఈ combinators ముందున్న siblings ను select చేయవు.
