Skip to content

Commit ed38094

Browse files
Create CSSselectors (#240)
1 parent be3677f commit ed38094

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

Types of CSS/CSSselectors

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
A CSS selector selects the HTML element(s) you want to style.
2+
3+
CSS Selectors
4+
CSS selectors are used to "find" (or select) the HTML elements you want to style.
5+
6+
We can divide CSS selectors into five categories:
7+
8+
Simple selectors (select elements based on name, id, class)
9+
Combinator selectors (select elements based on a specific relationship between them)
10+
Pseudo-class selectors (select elements based on a certain state)
11+
Pseudo-elements selectors (select and style a part of an element)
12+
Attribute selectors (select elements based on an attribute or attribute value)
13+
This page will explain the most basic CSS selectors.
14+
15+
The CSS element Selector
16+
The element selector selects HTML elements based on the element name.
17+
18+
Example
19+
Here, all <p> elements on the page will be center-aligned, with a red text color:
20+
21+
p {
22+
text-align: center;
23+
color: red;
24+
}
25+
The CSS id Selector
26+
The id selector uses the id attribute of an HTML element to select a specific element.
27+
28+
The id of an element is unique within a page, so the id selector is used to select one unique element!
29+
30+
To select an element with a specific id, write a hash (#) character, followed by the id of the element.
31+
32+
Example
33+
The CSS rule below will be applied to the HTML element with id="para1":
34+
35+
#para1 {
36+
text-align: center;
37+
color: red;
38+
}
39+
Note: An id name cannot start with a number!
40+
41+
ADVERTISEMENT
42+
43+
The CSS class Selector
44+
The class selector selects HTML elements with a specific class attribute.
45+
46+
To select elements with a specific class, write a period (.) character, followed by the class name.
47+
48+
Example
49+
In this example all HTML elements with class="center" will be red and center-aligned:
50+
51+
.center {
52+
text-align: center;
53+
color: red;
54+
}
55+
You can also specify that only specific HTML elements should be affected by a class.
56+
57+
Example
58+
In this example only <p> elements with class="center" will be red and center-aligned:
59+
60+
p.center {
61+
text-align: center;
62+
color: red;
63+
}
64+
HTML elements can also refer to more than one class.
65+
66+
Example
67+
In this example the <p> element will be styled according to class="center" and to class="large":
68+
69+
<p class="center large">This paragraph refers to two classes.</p>
70+
Note: A class name cannot start with a number!
71+
72+
The CSS Universal Selector
73+
The universal selector (*) selects all HTML elements on the page.
74+
75+
Example
76+
The CSS rule below will affect every HTML element on the page:
77+
78+
* {
79+
text-align: center;
80+
color: blue;
81+
}
82+
The CSS Grouping Selector
83+
The grouping selector selects all the HTML elements with the same style definitions.
84+
85+
Look at the following CSS code (the h1, h2, and p elements have the same style definitions):
86+
87+
h1 {
88+
text-align: center;
89+
color: red;
90+
}
91+
92+
h2 {
93+
text-align: center;
94+
color: red;
95+
}
96+
97+
p {
98+
text-align: center;
99+
color: red;
100+
}
101+
It will be better to group the selectors, to minimize the code.
102+
103+
To group selectors, separate each selector with a comma.
104+
105+
Example
106+
In this example we have grouped the selectors from the code above:
107+
108+
h1, h2, p {
109+
text-align: center;
110+
color: red;
111+
}
112+
Test Yourself With Exercises
113+
Exercise:
114+
Set the color of all <p> elements to red.
115+
116+
<style>
117+
{
118+
119+
red;}

0 commit comments

Comments
 (0)