Learn the fundamentals of CSS and master the art of selecting elements
CSS (Cascading Style Sheets) is a stylesheet language used to describe the presentation of a document written in HTML. CSS controls the layout, colors, fonts, and overall visual appearance of web pages.
A CSS rule consists of a selector and a declaration block:
selector {
property: value;
property: value;
}
h1 {
color: blue;
font-size: 24px;
text-align: center;
}
Explanation:
h1 - Selector (targets all <h1> elements)
color: blue; - Declaration (property: value)Selectors are used to target HTML elements that you want to style.
Selects all elements of a specific type.
p {
color: green;
}
/* Selects all <p> elements */
Selects elements with a specific class attribute (uses a dot).
.highlight {
background-color: yellow;
}
/* Selects all elements with class="highlight" */
Selects a unique element with a specific ID (uses a hash).
#header {
font-size: 32px;
}
/* Selects the element with id="header" */
Selects all elements on the page.
* {
margin: 0;
padding: 0;
}
/* Selects all elements */
Applies the same styles to multiple selectors.
h1, h2, h3 {
color: navy;
font-family: Arial;
}
/* Selects all h1, h2, and h3 elements */
Applied directly to an HTML element using the style attribute.
<p style="color: red; font-size: 18px;">This is inline CSS</p>
Placed inside a <style> tag in the HTML document's <head>.
<head>
<style>
p {
color: blue;
}
</style>
</head>
Stored in a separate .css file and linked to HTML.
<head>
<link rel="stylesheet" href="styles.css">
</head>
This paragraph is styled with CSS properties.
This uses a class selector.
Q1: What does CSS stand for?
Q2: Which selector is used to select elements with a specific class?
Q3: What is the correct CSS syntax?
Q4: Which is the correct way to select an element with id="demo"?
Q5: Which CSS property is used to change text color?
selector { property: value; }
p)
.classname)
#idname)
* to select all elements