ID Selectors
ID Selectors అంటే ఏమిటి?
ID Selector ఒక ప్రత్యేకమైన HTML element ను select చేసి దానికి CSS styles apply చేయడానికి ఉపయోగిస్తారు.
ID selector ను CSSలో # symbol తో రాస్తారు. ఉదాహరణకు #main-title selector, id="main-title" ఉన్న element ను select చేస్తుంది.
ID Selector ఎందుకు ఉపయోగిస్తారు?
- ఒక ప్రత్యేకమైన element కు మాత్రమే style ఇవ్వడానికి ఉపయోగిస్తారు.
- HTMLలో id value సాధారణంగా unique గా ఉండాలి.
- ఒకే id ను ఒక pageలో అనేక elements కు ఇవ్వడం నివారించాలి.
ID Selector ఎలా పనిచేస్తుంది?
ముందుగా HTML elementకు id attribute ఇస్తాం. తరువాత CSSలో ఆ id పేరుకు ముందు # symbol పెట్టి selector రాస్తాం.
<h1 id="main-title">Welcome to CSS</h1>
#main-title {
color: navy;
}
ఇక్కడ #main-title selector, id="main-title" ఉన్న <h1> element పై మాత్రమే navy color apply చేస్తుంది.
Class Selectorతో తేడా
Class Selectorను అనేక elements కు ఒకే style ఇవ్వడానికి ఉపయోగించవచ్చు. ID Selector మాత్రం ఒక ప్రత్యేకమైన element కోసం ఉపయోగించడం మంచిది. Classకు . symbol, IDకు # symbol ఉపయోగిస్తాం.
Simple Explanation
ID Selector అనేది ఒక ప్రత్యేకమైన HTML elementను గుర్తించే CSS selector. HTMLలో id="profile-box" ఉంటే, CSSలో దాన్ని #profile-boxగా select చేస్తాం.
Syntax:
#id-name {
property: value;
}
ఒక pageలో ఒకే id పేరు ఒక elementకు మాత్రమే ఇవ్వండి. ID పేరులో spaces ఉపయోగించకుండా meaningful name ఇవ్వడం మంచిది.
Real-Life Example
ఒక officeలో ప్రతి employeeకు unique employee ID ఉంటుంది. ఆ ID ద్వారా ఒక నిర్దిష్ట employeeని గుర్తించవచ్చు. అదే విధంగా HTMLలో ఒక ప్రత్యేకమైన elementకు unique id ఇస్తే, CSSలో ID Selector ద్వారా దానిని target చేయవచ్చు.
Code Examples
ఒక ప్రత్యేకమైన headingకు ID Selector ఉపయోగించడం
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ID Selector Example</title>
<style>
#main-title {
color: navy;
background-color: #e8f1ff;
padding: 12px;
}
#special-note {
color: #7a2800;
font-weight: bold;
}
</style>
</head>
<body>
<h1 id="main-title">Welcome to CSS</h1>
<p>This paragraph has normal styling.</p>
<p id="special-note">This is an important note.</p>
</body>
</html>
id="main-title" ఉన్న headingను #main-title selector select చేస్తుంది. అలాగే id="special-note" ఉన్న paragraphకు #special-note selector styles apply చేస్తుంది. IDs వేర్వేరు కాబట్టి ప్రతి rule తనకు సంబంధించిన ఒక్క elementను target చేస్తుంది.
The heading has navy text, a light blue background, and padding. The first paragraph has normal styling. The important note is dark orange and bold.
Line-by-Line Explanation
Welcome to CSS
ఈ lineలో headingకు main-title అనే unique id ఇచ్చాం. CSSలో ఈ idను target చేయడానికి #main-title ఉపయోగిస్తాం.
This is an important note.
ఈ paragraphకు special-note అనే id ఇచ్చాం. అందువల్ల ఈ ప్రత్యేక paragraphను ఇతర paragraphs నుండి వేరుగా style చేయవచ్చు.
ఇది ID Selector ప్రారంభం. # తర్వాత main-title రాయడం ద్వారా అదే id ఉన్న element select అవుతుంది.
main-title id ఉన్న headingలో text color navyగా మారుతుంది.
ఆ headingకు light blue background color ఇస్తుంది.
heading text చుట్టూ లోపలి ఖాళీని 12 pixelsగా ఇస్తుంది.
ఇది special-note id ఉన్న paragraphను select చేసే ID Selector.
special-note paragraph textకు dark orange color apply చేస్తుంది.
ఆ paragraph textను boldగా చూపిస్తుంది.
Try It
Try this CSS code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Try ID Selectors</title>
<style>
#page-title {
color: darkgreen;
}
#message {
background-color: #fff3cd;
padding: 10px;
}
</style>
</head>
<body>
<h1 id="page-title">My Web Page</h1>
<p id="message">This message uses an ID selector.</p>
</body>
</html>
Practice
Practice: పై codeలో id="profile-card" ఉన్న sectionకు royalblue border మరియు light blue background ఇవ్వండి.
Hint: CSSలో #profile-card selectorను ఉపయోగించి border మరియు background-color values మార్చండి.
Common Mistakes
profile-card అని రాయడం.ID selector ముందు తప్పనిసరిగా # symbol పెట్టాలి:
#profile-card.
HTMLలోని id value, CSSలోని # తర్వాతి పేరు ఒకేలా ఉండాలి. ఉదాహరణకు
id="main-title"కు #main-title ఉపయోగించాలి.
ఒక idను సాధారణంగా ఒకే unique elementకు ఇవ్వండి. అనేక elementsకు ఒకే style కావాలంటే Class Selector ఉపయోగించండి.
#main title వంటి id పేరులో space ఉపయోగించడం.ID పేర్లలో spaces ఉపయోగించవద్దు.
main-title లేదా mainTitle వంటి పేర్లు ఉపయోగించండి.
Interview Questions
CSSలో ID Selector అంటే ఏమిటి?
Answer: ఒక ప్రత్యేకమైన HTML elementను select చేసి style చేయడానికి ఉపయోగించే selectorను ID Selector అంటారు.
ID Selector syntax ఏమిటి?
Answer: ID Selector syntax #id-name. ఉదాహరణకు #header.
HTML id attribute మరియు CSS ID Selector మధ్య సంబంధం ఏమిటి?
Answer: HTMLలో ఇచ్చిన id valueను CSSలో # symbol ముందు పెట్టి select చేస్తాం. id="box"కు CSS selector #box.
ఒకే idను అనేక elementsకు ఇవ్వవచ్చా?
Answer: ఇవ్వడం సాధ్యమైనా, ఒక id uniqueగా ఉండాలి. అనేక elementsకు style ఇవ్వాలంటే Class Selector ఉపయోగించడం సరైన పద్ధతి.
Exam Questions
ID Selectorను గుర్తించడానికి CSSలో ఏ symbol ఉపయోగిస్తారు?
Answer:
# symbol.
id=”main-box” ఉన్న elementను select చేసే CSS selector ఏది?
Answer:
#main-box
ID Selectorను సాధారణంగా ఎప్పుడు ఉపయోగిస్తారు?
Answer:
ఒక ప్రత్యేకమైన లేదా unique HTML elementకు style ఇవ్వాల్సినప్పుడు ఉపయోగిస్తారు.
క్రింది వాటిలో సరైన ID Selector ఏది: .title, #title, title?
Answer:
#title సరైన ID Selector.
Quick Revision
- ID Selector ముందు # symbol ఉపయోగించాలి.
- HTMLలోని id value మరియు CSS selector పేరు ఒకేలా ఉండాలి.
- ID ఒక unique elementను target చేయడానికి ఉపయోగిస్తారు.
- ఒకే idను అనేక elementsకు ఇవ్వకండి.
- అనేక elementsకు ఒకే style కావాలంటే Class Selector ఉపయోగించండి.
