What is CSS ?
→ CSS stands for Cascading
style sheet
Cascading meaning
→ Used to design HTML
structure
What is HTML
Analogy
:- CSS ko aap esse samajh sakte hai ki ek sundar dikhane wale technique. Jese ki ek skeleton (jo CID mai dikh te ho aur Biology lab mai to zaroor dekhen hoge) bhayanak lagta hai na, usse HTML kehte hai web ki language mai, uske upar skin ke layer laga denge to sundar lagega na, jo ek human ki tarah dikhega, ussi skin ko web ki language mai CSS kehte hai.
WHY CSS ?
→ For making the HTML structure beautiful.
→ Nowadays, we don't use plain CSS. Instead, we use Bootstrap and Tailwind CSS, which are frameworks for CSS.
HOW TO USE CSS ? :-
We can use CSS in three ways.
Inline CSS
Internal CSS
External CSS
- Inline CSS :-
→ Inline CSS is added directly where we want to apply the style.
<h1 style="color:blue;">This is Inline CSS</h1>
It is inline CSS, where an h1 tag has its text color set to blue.
→ Inline CSS is applied to only a single element.
2. Internal CSS :-
→ Internal CSS is added in the head section of HTML file
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: '#fff';
}
h1 {
color: cyan;
}
</style>
</head>
<body>
<h1>This is a Internal CSS</h1>
</body>
</html>
It is Internal CSS define with a style tag in the head section. iska matlab ye nehi ki aap style section ko body mai nehi dal sakte, ye galat nehi hoga but ye recommend nehi hai kyuki iska kuchh reason hai
Why we prefer style tag in head section but not in body section
smooth experience
milna chahiye. Agar page pehle plain text ke saath load ho aur phir design badle, to wo user ko achha nahi lagega. Page ka design aur layout ek saath dikhna chahiye, jo tabhi possible hai jab CSS <head>
me ho.3. External CSS :-
→ External CSS is used by creating a file with a .css extension and linking it in the head section of an .html file using a link tag.
Summary of this Diagram
index.html
, which is an HTML file and serves as the entry point for the browser. Another section is a CSS file named style.css
, and the third section is a browser, which displays the output (essentially a simple website).In the index.html
file, there is a <link>
tag with a rel
attribute set to stylesheet
, which links to style.css
using a relative file path (./style.css
).There are two types of file paths absolute file path and relative file path.Priority of CSS Styling:
The first priority is Inline CSS, followed by Internal CSS, and finally External CSS.
Summary Of the Diagram:-
The diagram consists of three sections: index.html, style.css, and the Browser.
1. index.html: An HTML file that serves as the entry point to the browser.
2. style.css: An external CSS file connected to the HTML file via the <link> tag.
3. Browser: The output screen where the simple website is displayed.
The diagram has three types of CSS:
→ External CSS: In the external CSS file, the h1 text color is set to red. Here h1 is known as Selector.
→ Internal CSS: In the internal CSS (written inside the <style> tag in the <head> section), the h1 text color is set to blue.
→ Inline CSS: In the inline CSS (directly written in the h1 tag), the h1 text color is set to white.
In the browser, the h1 text appears white because inline CSS has the highest priority among all CSS styles.
By default, the h1 text color is black, which is the browser’s default style. However, default styles may vary across different browsers. Since inline CSS overrides both internal and external CSS, the h1 text color displays as white in the browser.
Analogy
Let us know about Selector :-
In general term Selector meaning who selects.
In CSS there are 5 types of Selectors.
Simple Selector based on ID, Class and Name
Combinator Selector
Pseudo-class Selector
Pseudo-elements Selector
Attribute Selector
We will learn about Selectors in next article.
It's all about the basics of CSS. In one article, it's impossible to cover everything about CSS.