π What Is CSS?
CSS (Cascading Style Sheets) is a styling language used to control the appearance of HTML content. While HTML gives you the structure (headings, paragraphs, buttons, images), CSS defines how they should look β colors, layout, spacing, animations, responsiveness, and more.
Note
β HTML = Structure
β JavaScript = Interactivity
π― Why Do We Use CSS?
- π¨ To add colors, fonts, spacing, and visual design
- π± To make websites responsive on all screen sizes
- π§± To control layout (Grid, Flexbox, Positioning)
- π₯ To create animations, transitions, and effects
- β»οΈ To separate structure (HTML) from design (CSS)
π¦ How CSS Works
CSS uses selectors to target elements and apply rules. A rule is made of a selector + declaration block.
Basic CSS Rule
selector {
property: value;
}Example: Make all paragraphs blue:
Example
p {
color: blue;
}π Adding CSS to HTML
1οΈβ£ Inline CSS
Inline CSS
<p style="color: red;">Hello World</p>Note
2οΈβ£ Internal CSS
Internal CSS
<style>
h1 {
color: purple;
}
</style>3οΈβ£ External CSS (Best Practice)
External CSS
<link rel="stylesheet" href="styles.css" />π¨ CSS Properties Youβll Use Every Day
- color β Text color
- background β Background color or image
- font-size β Text size
- margin β Spacing outside elements
- padding β Spacing inside elements
- border β Lines around elements
- width / height β Element dimensions
π CSS Selectors Overview
- Element Selector: p
- Class Selector: .btn
- ID Selector: #header
- Universal Selector: *
- Attribute Selector: [type="text"]
- Pseudo-class Selector: :hover
- Pseudo-element Selector: ::before
π The βCascadingβ Part of CSS
The word cascading means styles are applied in order of priority:
| Priority | Source |
|---|---|
| 1οΈβ£ Highest | Inline styles |
| 2οΈβ£ | IDs |
| 3οΈβ£ | Classes, pseudo-classes, attribute selectors |
| 4οΈβ£ | Elements |
| 5οΈβ£ Lowest | Universal selector * |
Note
π± CSS for Responsive Websites
CSS lets websites adapt to different devices using @media queries.
Media Query Example
@media (max-width: 600px) {
body {
background: lightgray;
}
}π§± Modern CSS Tools
- Flexbox β One-dimensional layout
- Grid β Two-dimensional layout
- Variables (custom properties): --brand-color
- Animations & transitions
- Container queries
- Nesting (like SCSS)
β¨ Example: Styling a Button
CSS Button
.btn {
background: royalblue;
color: white;
padding: 10px 20px;
border-radius: 6px;
border: none;
cursor: pointer;
}
.btn:hover {
background: navy;
}β οΈ Common Mistakes Beginners Make
- β Using inline CSS everywhere
- β Forgetting to link your stylesheet
- β Not using classes (overusing IDs)
- β Writing overly specific selectors
- β Not understanding the cascade & specificity
Note
π₯ Summary
CSS is the language that turns your HTML into a beautiful, responsive, modern webpage. By learning selectors, the box model, colors, layout systems, and responsive techniques, you gain full control over how your UI looks.
Learn more βMDN CSS Docs π