Hey there! Iβm Vikas Sankhla, your friendly full-stack dev bro π and founder of Web Codder. Today, letβs talk about how to make websites beautiful using CSS β in the easiest way possible.
Imagine HTML as the skeleton of a web page. CSS is like the clothes, colors, and makeup that make it look cool and stylish. π
π§΅ What is CSS?
CSS stands for Cascading Style Sheets.
It controls how elements look on a web page β like colors, spacing, fonts, sizes, and layout.
Think of HTML as what you see, and CSS as how you see it.
Example:
<p>This is a paragraph.</p>
With CSS:
p {
color: blue;
font-size: 18px;
}
Now the paragraph is blue and bigger! π
π¦ CSS Box Model β Layout Magic
Every HTML element is a box. This box has 4 layers:
- Content β The actual text or image
- Padding β Space inside the box, around the content
- Border β The line around the box
- Margin β Space outside the box
Box Model Diagram π¦
+-------------+
| Margin |
| +--------+ |
| | Border | |
| |+------+| |
| ||Padding|| |
| ||Content|| |
| |+------+| |
| +--------+ |
+-------------+
Example:
div {
margin: 10px;
padding: 20px;
border: 2px solid black;
}
βοΈ Styling Text and Fonts
You can make your text beautiful with CSS.
Basic Properties:
h1 {
color: red;
font-size: 32px;
font-family: Arial, sans-serif;
line-height: 1.5;
}
color
: Text colorfont-size
: Size of the textfont-family
: Style of the fontline-height
: Space between lines
π Tip: Use Google Fonts to explore cool fonts!
π§ CSS Selectors β Target Like a Pro
Selectors help us decide which element to style.
Selector | Example | Meaning |
---|---|---|
Element | p {} | All <p> tags |
Class | .title {} | Elements with class=”title” |
ID | #main {} | Element with id=”main” |
Example:
<p class="info">This is blue text.</p>
.info {
color: blue;
}
βοΈ Specificity β Who Wins the Style Fight?
Sometimes, multiple styles apply to the same element. Who wins?
π― Specificity decides which CSS rule is stronger.
Specificity Ranking:
- Inline styles β
style="color:red"
- ID selectors β
#id
- Class selectors β
.class
- Element selectors β
p, h1
π Higher specificity wins!
π§° Inline, Internal, and External CSS
There are 3 ways to apply CSS:
Method | Where it goes | Example |
---|---|---|
Inline | Inside an HTML tag | <p style="color: red"> |
Internal | In a <style> block | <style>p {color: blue;}</style> |
External | Linked .css file | <link href="style.css" rel="stylesheet"> |
π‘ Pro Tip: Always use external CSS for big projects.
π§ͺ Try It Yourself!
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1 class="title">Hello World</h1>
<p>This is a paragraph.</p>
</body>
</html>
/* styles.css */
.title {
color: purple;
font-size: 36px;
}
p {
color: green;
font-size: 18px;
}
π Summary
- CSS makes websites beautiful π¨
- Use box model to control layout
- Style text with fonts, colors, and spacing
- Use selectors to target elements
- Specificity decides which style wins
π£ Final Tip from Vikas Bhaiya π¬
CSS is super fun once you play with it! Try changing colors, spacing, and fonts.
Build your own small project and style it like a boss. π§βπ¨π»
And hey, if you learned something new today:
β
Subscribe to Web Codder on YouTube
π https://www.youtube.com/@web_codder/?sub_confirmation=1
π§ Read more articles on Automation & Web Dev
π https://webcodder.dev/category/automation/
π¬ Join our WhatsApp Community
π https://webcodder.dev/whatsapp
Letβs grow and code together! π