Welcome to our comprehensive CSS tutorial! Today, we'll dive into the world of Cascading Style Sheets, a powerful tool that lets you customize and enhance the look of your web pages. Let's get started!
CSS stands for Cascading Style Sheets. It's a language used to style web pages, separating the design from the content. This allows for a more organized and flexible approach to web development.
Using CSS offers several benefits:
There are three ways to add CSS to your web pages:
style attributes directly to your HTML elements.<style> tags in the <head> section of your HTML document.<link> tag in the <head> section of your HTML document.Let's explore the external method, as it's the most common approach for larger projects.
styles.css).<link> tag:<head>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>Now, let's write some CSS rules!
A CSS rule consists of a selector and declaration(s). Here's an example:
selector {
property: value;
}The selector identifies the HTML element(s) you want to style, while the property is the style you want to apply, and value is the value for that property.
You can select elements based on their type, class, or id. Here are some examples:
<p> elements:p {
color: blue;
}.my-class):<p class="my-class">This text will be blue!</p>
.my-class {
color: blue;
}#my-id):<p id="my-id">This text will be blue!</p>
#my-id {
color: blue;
}There are countless CSS properties to style your web pages, but here are some essential ones:
color: Sets the text color.background-color: Sets the background color.font-size: Sets the text size.margin: Adds space around the element.padding: Adds space within the element.Let's create a simple web page with an external CSS file:
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="styles.css">
<title>CSS How To Add</title>
</head>
<body>
<h1>Welcome to CodeYourCraft!</h1>
<p class="highlight">This text will be green!</p>
</body>
</html>styles.css
body {
background-color: #f0f0f0;
}
h1 {
color: navy;
font-size: 2em;
}
.highlight {
color: green;
}Which method do we use to link an external CSS file to our HTML document?
Keep learning and practicing, and soon you'll be a CSS master! 🚀