Inheritance of CSS properties
Let's say we have a paragraph, and in it some text in the tag i
:
<p>
This is some <i>text</i> in paragraph.
</p>
Let's color the paragraphs red:
p {
color: red;
}
As a result, not only the paragraph text will be colored, but also the text of the i
tag:
The thing is that the property color
is inherited. This means that if a parent tag has a certain color, then the descendants will have the same color. Not all CSS properties are inherited, but many (of those that you already know - all are inherited).
If desired, however, you can override the parent property by specifying a child selector. For example, let's set the paragraph to red and the italic to blue:
<p>
This is some <b>text</b> in paragraph.
</p>
p {
color: red;
}
i {
color: blue;
}
:
The order of selectors in CSS code does not matter in this case. If you swap the i
selector and the p
selector, everything will work the same:
<p>
This is some <b>text</b> in paragraph.
</p>
i {
color: blue;
}
p {
color: red;
}
:
After studying the code, tell me what color the text in the tag b
will be:
<p>
This is some <b>text</b> in paragraph.
</p>
p {
color: blue;
}
After studying the code, tell me what color the text in the tag b
will be:
<p>
This is some <b>text</b> in paragraph.
</p>
p {
color: blue;
}
b {
color: red;
}
After studying the code, tell me what color the text in the tag b
will be:
<p>
This is some <b>text</b> in paragraph.
</p>
b {
color: red;
}
p {
color: blue;
}
After studying the code, tell me what color the text in the li
tag will be:
<ul>
<li>text</li>
<li>text</li>
<li>text</li>
</ul>
ul {
color: blue;
}
After studying the code, tell me what color the text in the li
tag will be:
<ul>
<li>text</li>
<li>text</li>
<li>text</li>
</ul>
ul {
color: blue;
}
li {
color: red;
}
After studying the code, tell me what color the text in the tag i
will be:
<ul>
<li>text <i>italic</i> text</li>
<li>text <i>italic</i> text</li>
<li>text <i>italic</i> text</li>
</ul>
ul {
color: blue;
}
li {
color: red;
}
i {
color: green;
}
After studying the code, tell me what size the text will be in the i
tag:
<ul>
<li>text <i>italic</i> text</li>
<li>text <i>italic</i> text</li>
<li>text <i>italic</i> text</li>
</ul>
i {
font-size: 30px;
}
ul {
font-size: 20px;
}
li {
color: red;
font-weight: bold;
}