Ways to Add CSS: Inline, Internal and External
Ways to Add CSS
HTML page కు CSS styles జోడించడానికి మూడు ప్రధాన మార్గాలు ఉన్నాయి: Inline CSS, Internal CSS మరియు External CSS.
1. Inline CSS
Inline CSS ను HTML element యొక్క style attribute లో నేరుగా రాస్తాము. ఇది ఒక ప్రత్యేక element కు మాత్రమే style ఇవ్వడానికి ఉపయోగపడుతుంది.
ఉదాహరణ: <p style=”color: blue;”>Hello</p>
2. Internal CSS
Internal CSS ను అదే HTML file యొక్క <head> భాగంలో ఉన్న <style> element లో రాస్తాము. ఒకే web page లోని అనేక elements కు style ఇవ్వడానికి ఇది ఉపయోగకరం.
3. External CSS
External CSS లో styles ను ప్రత్యేకమైన .css file లో రాస్తాము. ఆ file ను HTML file లోని <head> భాగంలో <link> element ద్వారా కలుపుతాము.
External CSS పెద్ద websites లో ఎక్కువగా ఉపయోగిస్తారు, ఎందుకంటే ఒకే CSS file ను అనేక HTML pages ఉపయోగించవచ్చు.
ఏ పద్ధతిని ఎప్పుడు ఉపయోగించాలి?
- Inline: ఒకే element కు చిన్న, ప్రత్యేకమైన style అవసరమైనప్పుడు.
- Internal: ఒకే HTML page కు మాత్రమే styles అవసరమైనప్పుడు.
- External: అనేక pages కు ఒకే design ఇవ్వాల్సినప్పుడు. ఇది సాధారణంగా ఉత్తమమైన పద్ధతి.
CSS priority గురించి చిన్న గమనిక
ఒకే element కు Inline, Internal మరియు External styles అన్నీ ఉంటే, సాధారణంగా Inline style కు ఎక్కువ priority ఉంటుంది. అయినప్పటికీ, code నిర్వహణ సులభంగా ఉండటానికి Inline CSS ను అవసరమైనప్పుడు మాత్రమే ఉపయోగించాలి.
Simple Explanation
CSS రాయడానికి మూడు మార్గాలను గుర్తుంచుకోండి:
- Inline CSS: style ను element లోనే రాయడం.
- Internal CSS: <style> element లో రాయడం.
- External CSS: ప్రత్యేక .css file లో రాసి <link> ద్వారా కలపడం.
చిన్న పరీక్ష కోసం Inline CSS ఉపయోగించవచ్చు. ఒక page కోసం Internal CSS ఉపయోగించవచ్చు. నిజమైన projects లో reusable మరియు maintainable code కోసం External CSS సాధారణంగా ఉత్తమ ఎంపిక.
Real-Life Example
ఒక పాఠశాల notice board ను ఊహించుకోండి. ఒకే notice కు marker తో నేరుగా రంగు వేయడం Inline CSS లాంటిది. ఒకే board మీద ఉన్న notices అన్నింటికీ ఒకే design rule పెట్టడం Internal CSS లాంటిది. పాఠశాలలోని అన్ని boards కోసం ఒకే design guide తయారు చేయడం External CSS లాంటిది.
Code Examples
Inline CSS
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Inline CSS</title>
</head>
<body>
<h1 style="color: darkblue;">Inline CSS Example</h1>
<p style="font-size: 18px; color: #333333;">This style is written inside the HTML element.</p>
</body>
</html>
ఇక్కడ CSS ను style attribute లో నేరుగా రాశాము. ప్రతి style చేసిన element కు మాత్రమే ఆ ప్రభావం ఉంటుంది.
The heading appears dark blue. The paragraph appears larger with dark gray text.
Internal CSS
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Internal CSS</title>
<style>
h1 {
color: darkgreen;
}
p {
font-size: 18px;
color: #333333;
}
</style>
</head>
<body>
<h1>Internal CSS Example</h1>
<p>These styles are written inside the style element.</p>
</body>
</html>
<style> element లో రాసిన rules ఈ page లోని matching elements కు వర్తిస్తాయి. ఇక్కడ అన్ని <h1> elements dark green గా, అన్ని <p> elements పెద్ద dark gray text గా కనిపిస్తాయి.
The heading appears dark green. The paragraph appears larger with dark gray text.
External CSS
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>External CSS</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>External CSS Example</h1>
<p>The design comes from a separate CSS file.</p>
</body>
</html>
/* Save the following CSS in a separate file named styles.css */
body {
background-color: #f2f7ff;
}
h1 {
color: purple;
}
p {
color: #333333;
}
HTML file లోని <link> element, styles.css అనే external file ను కలుపుతుంది. ఈ ఉదాహరణలో HTML భాగాన్ని ఒక file గా, CSS భాగాన్ని styles.css అనే మరో file గా save చేయాలి. రెండు files ఒకే folder లో ఉంటే browser CSS ను load చేస్తుంది.
The page has a very light blue background, a purple heading, and dark gray paragraph text.
Line-by-Line Explanation
Inline CSS Example
ఈ line లో
style attribute ఉపయోగించి h1 element కు dark blue color ఇచ్చాము. ఈ style ఈ h1 element కు మాత్రమే వర్తిస్తుంది.
ఇది Internal CSS ప్రారంభమయ్యే స్థానం. సాధారణంగా దీనిని HTML document లోని
<head> భాగంలో ఉంచుతాము.
ఇది h1 selector. Page లోని h1 elements ను ఈ rule target చేస్తుంది.
ఈ declaration h1 text కు dark green రంగు ఇస్తుంది. color property తర్వాత colon, value తర్వాత semicolon ఉన్నాయి.
ఈ closing brace h1 rule ముగిసిందని browser కు తెలియజేస్తుంది.
ఇది Internal CSS block ముగింపు.
ఈ line external CSS file ను HTML page కు కలుపుతుంది. href లో ఉన్న styles.css file పేరు మరియు path ను browser ఉపయోగిస్తుంది.
External CSS file లోని ఈ selector HTML page లోని h1 elements కు styles ఇవ్వడానికి ఉపయోగపడుతుంది.
ఈ declaration h1 text ను purple గా చూపిస్తుంది.
Try It
Try this CSS code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Try CSS Methods</title>
<style>
p {
color: darkgreen;
font-size: 18px;
}
</style>
</head>
<body>
<h1 style="color: navy;">Try Different CSS Methods</h1>
<p>This paragraph uses internal CSS.</p>
</body>
</html>
Practice
Practice: పై starter code లో h1 కు Inline CSS ఉపయోగించి navy color ఇవ్వండి. తరువాత Internal CSS లో p text ను dark green గా మార్చి, font-size ను 20px గా చేయండి.
Hint: h1 opening tag లో style=”color: navy;” రాయండి. <style> block లోని p rule కు color మరియు font-size declarations జోడించండి.
Practice: Internal CSS లోని p rule లో color value ను darkblue గా మార్చి browser లో output ను గమనించండి.
Hint: color property యొక్క value ను మాత్రమే మార్చండి; property name ను మార్చకండి.
Common Mistakes
Inline CSS కోసం HTML element లో style attribute ఉపయోగించాలి: <p style=”color: red;”>.
Internal CSS కోసం <style> element ను సాధారణంగా <head> లో ఉంచి, </style> తో సరిగ్గా ముగించండి.
సరైన link element ఇలా ఉండాలి: <link rel=”stylesheet” href=”styles.css”>.
CSS file ఉన్న సరైన path ను href లో ఇవ్వండి లేదా రెండు files ను ఒకే folder లో ఉంచండి.
సరైన syntax ఉపయోగించండి: color: blue; మరియు font-size: 18px;.
Interview Questions
CSS ను HTML కు జోడించే మూడు మార్గాలు ఏమిటి?
Answer: Inline CSS, Internal CSS మరియు External CSS.
Inline CSS అంటే ఏమిటి?
Answer: ఒక HTML element యొక్క style attribute లో నేరుగా రాసే CSS ను Inline CSS అంటారు. ఇది సాధారణంగా ఆ element కు మాత్రమే వర్తిస్తుంది.
Internal CSS ఎక్కడ రాస్తారు?
Answer: HTML document లోని head భాగంలో ఉన్న style element లో Internal CSS రాస్తారు.
External CSS ఎందుకు ఉపయోగకరం?
Answer: CSS ను ప్రత్యేక file లో ఉంచి అనేక HTML pages కు ఒకే design ను వర్తింపజేయవచ్చు. Code reuse మరియు maintenance సులభమవుతాయి.
ఒకే element కు Inline మరియు Internal styles రెండూ ఉంటే సాధారణంగా ఏది ముందుకు వస్తుంది?
Answer: సాధారణంగా Inline CSS కు Internal CSS కంటే ఎక్కువ priority ఉంటుంది.
Exam Questions
ఒక HTML element కు నేరుగా style ఇవ్వడానికి ఏ CSS పద్ధతిని ఉపయోగిస్తారు?
Answer:
Inline CSS.
సరైన Internal CSS syntax ను రాయండి.
Answer:
<style> h1 { color: blue; } </style>
External CSS file ను HTML కు కలపడానికి ఏ HTML element ఉపయోగిస్తారు?
Answer:
<link> element.
చాలా HTML pages కు ఒకే styles ఉపయోగించాల్సి ఉంటే ఏ పద్ధతి ఉత్తమం?
Answer:
External CSS.
Quick Revision
- Inline CSS ను HTML element యొక్క style attribute లో రాస్తారు.
- Internal CSS ను HTML head లోని style element లో రాస్తారు.
- External CSS ను ప్రత్యేక .css file లో రాసి link element ద్వారా కలుపుతారు.
- చిన్న ప్రత్యేక మార్పులకు Inline CSS ఉపయోగించవచ్చు.
- ఒకే page కోసం Internal CSS ఉపయోగించవచ్చు.
- అనేక pages కోసం External CSS సాధారణంగా ఉత్తమమైన పద్ధతి.
- External CSS file పేరు మరియు path href లో సరిగ్గా ఇవ్వాలి.
