Introduction to CSS and How It Connects to HTML

🎨 Styling the web with Cascading Style Sheets

CSS (Cascading Style Sheets) is the language used to describe the presentation and design of a web page written in HTML. It controls layout, colors, fonts, spacing, and overall visual appearance, making web pages look attractive and user-friendly.

Here you’ll learn what CSS is, how it works alongside HTML, and the ways to connect CSS to HTML documents.


πŸ“ What is CSS Used For?

  • Setting colors, fonts, and text styles
  • Creating layouts with grids, flexbox, and positioning
  • Adding spacing, margins, and padding
  • Styling responsive designs for different devices
  • Animating elements and transitions
  • Controlling the look of HTML elements consistently

🧱 CSS Basics

CSS consists of selectors and declarations. Selectors target HTML elements, and declarations define style properties and values.

Example CSS rule:

h1 {
color: blue;
font-size: 24px;
}
  • This targets all <h1> elements and sets their text color to blue and font size to 24 pixels.

πŸ”Ή How CSS Connects to HTML

There are three main ways to apply CSS to HTML:

MethodDescriptionExample
Inline CSSStyles directly inside an HTML tag<p style="color: red;">Hello</p>
Internal CSSCSS inside <style> tags in the HTML <head><style> p { color: red; } </style>
External CSSSeparate .css file linked in HTML<link rel="stylesheet" href="styles.css">

πŸ“Œ Connecting CSS to HTML Example

External CSS File (styles.css):

body {
background-color: #f0f0f0;
font-family: Arial, sans-serif;
}

h1 {
color: #333333;
}

HTML File:

<!DOCTYPE html>
<html>
<head>
<title>Styled Page</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Welcome to my website</h1>
<p>This page is styled with CSS.</p>
</body>
</html>

πŸ”Ž Important CSS Concepts

ConceptDescription
SelectorsTarget HTML elements (h1, .class, #id)
PropertiesStyle aspects like color, margin, font-size
CascadeRules for which styles apply when multiple rules conflict
Box ModelModel of element layout: content, padding, border, margin
Media QueriesApply different styles based on screen size or device

🧩 Why Use CSS Separately from HTML?

  • Keeps structure (HTML) and style (CSS) cleanly separated
  • Enables reuse of styles across multiple pages
  • Easier to maintain and update website design
  • Improves page load performance by caching CSS files

πŸ’‘ CSS Syntax Example

/* Select all paragraphs */
p {
font-size: 16px;
line-height: 1.5;
color: #666666;
}

πŸ“š Summary Table

FeatureDescription
LanguageCascading Style Sheets (CSS)
PurposeStyle and layout of HTML content
Connection MethodsInline, internal, external CSS
Key ConceptsSelectors, properties, cascade, box model
BenefitsSeparation of concerns, maintainability

πŸ”œ What’s Next?

Mastering CSS is essential for creating visually appealing and responsive websites. Explore layout techniques like Flexbox and Grid, and dive into animations and transitions in our HTML & CSS Essentials course!