CSS (Cascading Style Sheets) is used to style and layout web pages. With CSS, you can control the color, fonts, spacing, and layout of HTML elements.
CSS can be added directly to an HTML element using the style
attribute.
<p style="color:blue;">This is a blue paragraph.</p>
You can include CSS rules in the <style>
tag inside the <head>
section of an HTML document.
<style>
p {
color: blue;
}
</style>
The most common way to add CSS is by linking to an external stylesheet. You can do this by using the <link>
tag in the <head>
section of your HTML file.
<link rel="stylesheet" href="styles.css">
CSS selectors are patterns used to select elements on the web page that you want to style.
p {
color: red;
}
#
symbol.
#myElement {
font-size: 20px;
}
.
symbol.
.myClass {
background-color: yellow;
}
Selectors and properties are the foundation of CSS. Selectors target HTML elements, while properties define how those elements are styled.
Common selectors include:
p
for all paragraphs..
.#
.
p { color: blue; } /* Element selector */
.my-class { font-size: 18px; } /* Class selector */
#my-id { background-color: yellow; } /* ID selector */
Properties define the appearance of elements. Common properties include:
color
: Text colorbackground-color
: Background colorfont-size
: Text sizemargin
: Space outside an elementpadding
: Space inside an element
p {
color: red;
font-size: 20px;
padding: 10px;
}
Create a webpage using CSS selectors and properties to style the headings, paragraphs, and other elements. Use a mix of class and ID selectors to practice.