CSS (Cascading Style Sheets) works by selecting HTML elements and applying styles to them. Every CSS rule has two main parts: the selector and the declaration block.
A CSS rule looks like this:
selector {
property: value;
}
p{
color:red;
font-size:18px;
font-weight:600;
}
This makes all <p> elements blue and sets their font size.
CSS offers different ways to select elements depending on your need.
Selects all HTML elements with a specific tag name.
h1 {
color: blue;
font-size: 25px;
}
Targets elements with a specific class. Class names begin with a dot (.).
.title {
font-weight: bold;
}
Selects an element with a unique ID. ID selectors start with a hash (#).
#header {
background: lightgray;
}
Applies styles to all elements.
* {
margin: 0;
padding: 0;
}
Used to style multiple selectors at once.
h1, h2, h3 {
color: navy;
}
Selects elements based on HTML attributes.
input[type="text"] {
border: 1px solid #333;
}
Used for special states like hover, focus, active, first-child, etc.
a:hover {
color: red;
}
Targets specific parts of an element such as first letter or before/after content.
p::first-letter {
font-size: 30px;
}
Selects elements inside another element.
div p {
color: purple;
}
Selects direct children only.
ul > li {
font-size: 16px;
}