Welcome to our CSS Tutorial! In this lesson, we'll dive into the world of Cascading Style Sheets, a powerful tool that enhances the visual appeal of your web pages. Let's get started!
CSS is a style rules language that helps you design and layout websites. It separates the design part from the content, making it easier to maintain and update.
A CSS rule has three parts:
Let's see this in action:
/* Selector */
h1 {
/* Declarations */
color: red; /* Property: color, Value: red */
font-size: 2em; /* Property: font-size, Value: 2em */
}In the example above, we've selected the h1 HTML element and set its color to red and font size to 2em.
There are three ways to apply CSS to your HTML:
<style> section within the HTML file.Each method has its pros and cons. External styles are preferred for larger projects, while inline styles are useful for small, quick changes.
Selectors help you target specific HTML elements. Here are some common CSS selectors:
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.myClass {
color: blue;
}
#myId {
font-size: 2em;
}
</style>
</head>
<body>
<h1 class="myClass" id="myId">Hello, World!</h1>
</body>
</html>In this example, the h1 element is both a class and an id selector, making it styled by both rules.
The Box Model is a visual representation of HTML elements as rectangular boxes. It consists of content, padding, border, and margin.
_________
| Content |
|_________|
| | |
| Padding| Border|
| | |
|_________|
| | |
| Margin | |
| | |
___________
Understanding the Box Model is crucial for designing responsive websites.
Flexbox and Grid are powerful CSS layout methods that help you create complex, responsive designs.
Flexbox allows you to align, distribute, and wrap elements with ease. Here's a simple example:
.container {
display: flex;
}
.container > div {
flex: 1;
}<div class="container">
<div>Box 1</div>
<div>Box 2</div>
<div>Box 3</div>
</div>In this example, the container div is set to be a flex container, and the children divs are set to have equal widths.
Grid is a highly flexible layout method that allows you to create complex, responsive grids.
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
.container > div {
background-color: lightblue;
}<div class="container">
<div>Box 1</div>
<div>Box 2</div>
<div>Box 3</div>
<div>Box 4</div>
<div>Box 5</div>
</div>In this example, the container div is set to be a grid with three equal columns.
Responsive design ensures that your website looks good on any device. You can use media queries, viewport, and flexible units to create responsive designs.
Media queries allow you to apply different styles based on the device's characteristics.
@media only screen and (max-width: 600px) {
h1 {
font-size: 1.5em;
}
}In this example, the h1 font size will change to 1.5em when the screen width is 600px or less.
What does CSS stand for?
What is the Box Model in CSS?