CSS Introduction
What is CSS?
CSS stands for Cascading Style Sheets. CSS describes how HTML elements are to be displayed on screen, paper, or in other media. CSS saves a lot of work. It can control the layout of multiple web pages all at once.
Why Use CSS?
CSS is used to:
- Style HTML elements
- Control the layout of multiple pages
- Create responsive designs
- Add animations and transitions
- Improve the visual appearance of websites
CSS Syntax
A CSS rule consists of a selector and a declaration block:
p {
color: blue;
font-size: 16px;
}
The selector points to the HTML element you want to style. The declaration block contains one or more declarations separated by semicolons. Each declaration includes a CSS property name and a value, separated by a colon.
How to Add CSS
1. Inline CSS
Use the style attribute inside HTML elements:
<p style="color: blue;">This is a blue paragraph.</p>
2. Internal CSS
Use a <style> element in the <head> section:
<head>
<style>
p {
color: blue;
font-size: 16px;
}
</style>
</head>
3. External CSS
Link to an external CSS file using the <link> element:
<head>
<link rel="stylesheet" href="styles.css">
</head>
CSS Selectors
CSS selectors are used to find (or select) HTML elements you want to style:
/* Element selector */
p {
color: red;
}
/* ID selector */
#header {
background-color: blue;
}
/* Class selector */
.intro {
font-size: 18px;
}
CSS Comments
Comments are used to explain code and are ignored by browsers:
/* This is a single-line comment */
/*
This is a
multi-line comment
*/
CSS Cascading Order
When multiple CSS rules apply to the same element, the following order of priority applies:
- Inline styles (highest priority)
- External and internal style sheets
- Browser default
Next Steps
Now that you understand the basics of CSS, you're ready to learn about CSS Selectors in more detail.