Featured
- Get link
- X
- Other Apps
Day 11: Introduction to CSS & Syntax
🎨 Day 11: Introduction to CSS & Syntax
Today we begin CSS — the language that styles HTML. You’ll learn the basics (what CSS is), how the cascade works, selector basics, how to include CSS, and the concept of specificity and inheritance.
1. What is CSS?
CSS (Cascading Style Sheets) controls how HTML elements are displayed — colors, spacing, layout, fonts, and responsive behavior.
2. Three ways to add CSS
- Inline: directly in the element with the
styleattribute (quick tests; avoid for maintainability). - Internal: inside a
<style>block in the document head (useful for single-page style overrides). - External: link a separate
.cssfile using<link rel="stylesheet" href="style.css">— recommended for real projects.
3. CSS rule syntax
selector {
property: value;
property2: value2;
}
4. Cascade, Specificity & Inheritance (why some rules win)
Cascade: multiple sources (browser, external, internal, inline) merge into final styles. Order matters — later rules can override earlier ones.
Specificity (priority):
- Inline styles (e.g.,
style="...") — highest priority - ID selectors (e.g.,
#header) - Class / attribute / pseudo-class selectors (e.g.,
.btn,[type="text"],:hover) - Element selectors (e.g.,
p,h1) & pseudo-elements (lowest)
Example of specificity override:
p { color: blue; } /* element selector */
.intro { color: green; } /* class selector -> higher specificity */
#main p { color: red; } /* id selector -> even higher specificity */
5. Important: !important
Avoid using !important unless absolutely necessary — it overrides specificity and makes maintenance harder.
Try it yourself — CSS basics
Edit the CSS inside the <style> block and click Run Code. This demo shows internal CSS, inline, and specificity behavior.
Practice Tasks
- Create a CSS file and move the internal styles into it (practice linking external CSS).
- Create three selectors (element, class, id) that style the same element and observe specificity order.
- Try removing inline style and see the cascade result.
Popular Posts
📘 Day 36: Understanding the DOM (Document Object Model)
- Get link
- X
- Other Apps
Day 27: Bootstrap Mini Project – Responsive Portfolio Page
- Get link
- X
- Other Apps
Comments
Post a Comment