CSS Selectors
CSS Selectors
CSS Selectors
HTML
Menu Log in
CSS Selectors
‹ Previous Next ›
CSS Selectors
CSS selectors are used to "find" (or select) the
HTML elements you want to style.
Example
Here, all <p> elements on the page will be center-
aligned, with a red text color:
p {
text-align: center;
color: red;
}
Try it Yourself »
Example
The CSS rule below will be applied to the HTML
element with id="para1":
#para1 {
text-align: center;
color: red;
}
Try it Yourself »
ADVERTISEMENT
Example
In this example all HTML elements with
class="center" will be red and center-aligned:
.center {
text-align: center;
color: red;
}
Try it Yourself »
Example
In this example only <p> elements with
class="center" will be red and center-aligned:
p.center {
text-align: center;
color: red;
}
Try it Yourself »
Example
In this example the <p> element will be styled
according to class="center" and to class="large":
Try it Yourself »
Example
The CSS rule below will affect every HTML element
on the page:
* {
text-align: center;
color: blue;
}
Try it Yourself »
h1 {
text-align: center;
color: red;
}
h2 {
text-align: center;
color: red;
}
p {
text-align: center;
color: red;
}
Example
In this example we have grouped the selectors from
the code above:
h1, h2, p {
text-align: center;
color: red;
}
Try it Yourself »
Exercise:
Set the color of all <p> elements to red.
<style>
{
red;
}
</style>
Submit Answer »
* * Selects all
elements
‹ Previous Next ›
ADVERTISEMENT
NEW
We just launched
W3Schools videos
Explore now
COLOR PICKER
Get certified
by completing
a CSS
course today!
school
w3 s
2
CE
02
TI 2
R
FI
ED .
Get started
CODE GAME
Play Game
ADVERTISEMENT
Report Error
Spaces
Pro
Buy Certificate
Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
W3.CSS Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial
Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
W3.CSS Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
Angular Reference
jQuery Reference
Top Examples
HTML Examples
CSS Examples
JavaScript Examples
How To Examples
SQL Examples
Python Examples
W3.CSS Examples
Bootstrap Examples
PHP Examples
Java Examples
XML Examples
jQuery Examples
Get Certified
HTML Certificate
CSS Certificate
JavaScript Certificate
Front End Certificate
SQL Certificate
Python Certificate
PHP Certificate
jQuery Certificate
Java Certificate
C++ Certificate
C# Certificate
XML Certificate
FORUM | ABOUT