Learnify - Your Coding Companion

Mastering the Web, One Lesson at a Time

Introduction to CSS

Welcome to the CSS Introduction lesson! Cascading Style Sheets (CSS) is a fundamental technology for building modern websites. While HTML provides the structure and content of a webpage, CSS is responsible for its presentation and visual appeal.

What is CSS?

CSS stands for Cascading Style Sheets. It's a stylesheet language used to describe the look and formatting of a document written in a markup language like HTML. With CSS, you can control:

Why is CSS Important?

CSS is crucial for several reasons:

How CSS Works: Selectors, Properties, and Values

At its core, CSS works by selecting HTML elements and applying styles to them. This is done through a simple rule structure:

selector {
property: value;
property: value;
}

Let's break this down:

Example: Styling a Paragraph

Let's say we have the following HTML:

<p>This is a paragraph.</p>

We can style this paragraph using CSS to make its text red and change its font size:

p {
color: red;
font-size: 18px;
}

Example Output:

This is a paragraph.

Ways to Include CSS

There are three main ways to add CSS to your HTML document:

1. Inline Styles (Not Recommended for Most Cases)

Applied directly to an HTML element using the style attribute. This is generally discouraged for large projects as it mixes content and presentation.

<p style="color: green; font-weight: bold;">This paragraph has inline styles.</p>

2. Internal Stylesheets

Defined within the <style> tags in the <head> section of your HTML document.

<!DOCTYPE html>
<html>
<head>
<title>Internal CSS Example</title>
<style>
body { background-color: lightblue; }
h1 { color: navy; margin-left: 20px; }
</style>
</head>
<body>
<h1>This is a heading.</h1>
<p>This is a paragraph.</p>
</body>
</html>

3. External Stylesheets (Recommended)

CSS rules are placed in a separate file with a .css extension. This file is then linked to your HTML document using the <link> tag in the <head> section.

styles.css:

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

index.html:

<!DOCTYPE html>
<html>
<head>
<title>External CSS Example</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>This is a heading.</h1>
<p>This is a paragraph.</p>
</body>
</html>

Next Steps

This lesson provides a foundational understanding of CSS. In subsequent lessons, we'll dive deeper into specific CSS properties, selectors, the box model, layout techniques (like Flexbox and Grid), and responsive design principles.

Keep practicing and experimenting! The best way to learn CSS is by doing.