June 2, 2024
CSS Syntax
CSS uses a simple syntax to define how HTML elements should be styled. A CSS rule consists of a selector and a declaration block:
selector {
property: value;
}
CSS Selectors
Selectors are used to target HTML elements. The most common selectors include:
p {
color: blue;
}
.
) before the class name..myClass {
color: red;
}
#
) before the ID name.#myId {
color: green;
}
Grouping and Nesting Selectors
h1, h2, h3 {
color: purple;
}
.container p {
font-size: 14px;
}
Common Properties
Here are some common CSS properties:
color: blue;
background-color: yellow;
font-family: Arial, sans-serif;
font-size: 16px;
font-weight: bold;
Inheritance and Specificity
Specificity determines which CSS rule is applied when multiple rules target the same element. It's calculated based on the types of selectors used in a rule. The more specific a selector is, the higher its specificity value, and the more precedence it has.
Specificity is calculated using a four-part value (a, b, c, d):
#id
).class
, [attribute]
, :hover
)h1
, ::before
)Examples:
p {
color: blue; /* Specificity: 0, 0, 0, 1 */
}
.intro {
color: green; /* Specificity: 0, 0, 1, 0 */
}
#main {
color: red; /* Specificity: 0, 1, 0, 0 */
}
<p style="color: yellow;">Text</p> /* Specificity: 1, 0, 0, 0 */
Combining Selectors:
p.intro {
color: purple; /* Specificity: 0, 0, 1, 1 */
}
#main .intro {
color: orange; /* Specificity: 0, 1, 1, 0 */
}
Specificity Examples:
Let's consider an HTML element:
<p id="main" class="intro">Hello, World!</p>
CSS Rules:
p {
color: blue; /* Specificity: 0, 0, 0, 1 */
}
.intro {
color: green; /* Specificity: 0, 0, 1, 0 */
}
#main {
color: red; /* Specificity: 0, 1, 0, 0 */
}
#main .intro {
color: orange; /* Specificity: 0, 1, 1, 0 */
}
p.intro {
color: purple; /* Specificity: 0, 0, 1, 1 */
}
Applied Specificity:
1. The p
selector has the lowest specificity (0, 0, 0, 1).
2. The .intro
class selector has higher specificity (0, 0, 1, 0).
3. The #main
ID selector has even higher specificity (0, 1, 0, 0).
4. The combined selector #main .intro
has higher specificity (0, 1, 1, 0) than individual ID or class selectors.
5. The combined selector p.intro
has lower specificity (0, 0, 1, 1) than #main
but higher than .intro
alone.
Result:
The color applied to the paragraph with id="main"
and class="intro"
will be orange
because the #main .intro
rule has the highest specificity.
Exercise:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Specificity Example</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<p id="main" class="intro">Hello, World!</p>
</body>
</html>
p {
color: blue; /* Specificity: 0, 0, 0, 1 */
}
.intro {
color: green; /* Specificity: 0, 0, 1, 0 */
}
#main {
color: red; /* Specificity: 0, 1, 0, 0 */
}
#main .intro {
color: orange; /* Specificity: 0, 1, 1, 0 */
}
p.intro {
color: purple; /* Specificity: 0, 0, 1, 1 */
}
Coming Up Next
In Chapter 3, we'll dive into the box model, exploring margins, padding, borders, and how they affect the layout of your elements.
Please Sign In to post a comment.