CSS Tutorial: Introduction Chapter
What is CSS?
CSS (Cascading Style Sheets) is a style sheet language used for describing the presentation of a document written in HTML or XML. It controls the layout, colors, fonts, and overall design of a web page, enabling developers to create visually appealing and user-friendly websites.
Why Learn CSS?
CSS is essential for web development because:
- Separation of Concerns: It separates content (HTML) from presentation, making the code easier to maintain.
- Responsive Design: CSS enables websites to adapt seamlessly to different screen sizes.
- Custom Styling: It allows for creative control over a site’s appearance, from animations to sophisticated layouts.
- Efficiency: Shared styles across multiple pages reduce redundancy and improve performance.
Key Features of CSS
- Cascading Rules:
CSS rules cascade, meaning styles applied later can override earlier ones. - Selectors:
CSS provides powerful selectors to target HTML elements precisely. - Responsive Design:
Media queries and flexible layouts make your site mobile-friendly. - Animation and Effects:
CSS animations and transitions enhance interactivity.
How CSS Works
CSS works by applying styles to HTML elements using:
- Inline CSS: Styling directly within an HTML element.
<p style="color: red;">This is red text.</p>
- Internal CSS: Defined within a
<style>
tag in the<head>
of an HTML document.<style> p { color: blue; } </style>
- External CSS: Defined in a separate
.css
file and linked to an HTML file.<link rel="stylesheet" href="styles.css">
Basic Structure of a CSS Rule
selector {
property: value;
}
- Selector: Specifies the HTML element(s) to style.
- Property: Defines what to style (e.g.,
color
,font-size
). - Value: Specifies the appearance (e.g.,
red
,16px
).
Example:
h1 {
color: green;
font-size: 24px;
}
Example of CSS in Action
HTML File (index.html
):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Example</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Welcome to CSS!</h1>
<p>CSS makes web pages beautiful.</p>
</body>
</html>
CSS File (styles.css
):
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
margin: 0;
padding: 0;
}
h1 {
color: #4CAF50;
text-align: center;
padding: 20px 0;
}
p {
color: #555;
font-size: 18px;
text-align: center;
}
Advantages of Using CSS
- Flexibility: Change the look of a site without altering its HTML structure.
- Reusability: Apply the same styles across multiple pages.
- Efficiency: Faster page loading by reducing HTML code duplication.
Tools to Start with CSS
- Code Editors: Visual Studio Code, Sublime Text, Atom.
- Browsers: Chrome, Firefox, Safari (with built-in developer tools).
- CSS Frameworks: Bootstrap, Tailwind CSS (for advanced projects).
This introductory chapter gives you a foundation to begin your CSS journey. In the next chapter, we’ll dive deeper into CSS Selectors and Properties to start creating stunning designs.