Speaking of cascading, you may be wondering what happens if different sources have the same rules for an element, but with different values. In that case, there are rules that prioritize styles from different sources. In order of importance, the list looks like this:
The values specified in the style
attribute will be more important than the properties in the <style>
tag, and they will be more important than the default browser styles.
An important feature of CSS is that different types of selectors have different priorities. If styles contradict each other in different selectors, the priority principle comes into play.
Let's look at an example. Let's create a paragraph with the class as red
and the identifier as blue
.
<p id="blue" class="red">Text with the class red and identifier blue</p>
Let's add conflicting styles for class, identifier, and tag:
p {
color: black;
}
.red {
color: red;
}
#blue {
color: blue;
}
What color will the paragraph be? Try running this example in the editor. You'll see that the text is blue. What does this have to do with anything?
Selectors also have priorities. Conventionally, you can put the selectors in the following order of priority:
#blue
).red
)p
)Where 1 is the highest priority and 3 is the lowest.
From these rules it can be understood that the properties specified in the selector by identifier will have a higher priority than the styles in the selector by tag and class. That is why the paragraph in the last example was blue.
This is easy to remember if the selectors are small, but they can be more complex. The selector can be a combination of classes, tags, and so on. For example:
<p class="paragraph color-primary">Paragraph</p>
.paragraph.color-primary {
color: blue;
}
Here, a selector for two classes was used at once. This means that styles will only be applied to an element that has both classes. This separation helps developers pinpoint styles for similar elements. For example:
<p class="paragraph color-primary">Paragraph</p>
<p class="paragraph">Paragraph</p>
<p class="paragraph color-primary">Paragraph</p>
.paragraph {
color: red;
}
.paragraph.color-primary {
color: blue;
}
In this example, all paragraphs with the paragraph
class will have red text, but if the color-primary
class is also set, the color will be blue. Why does this happen? These selectors also have their own priorities. In complex selectors, both the number of occurrences of a particular selector and the total weight are counted.
To determine the priority, you can use a table where each selector is given a "weight":
To find out which selector will have the greater weight, you have to add up all the values obtained using this table.
.paragraph
selector consists of one class, so its weight is 10.paragraph.color-primary
consists of two classes. Its weight is 20.Thus, the properties in the .paragraph.color-primary
selector will have a higher priority than those inside the .paragraph
selector.
This table doesn't tell the whole story, but it can be used in the early stages of learning CSS. With practice, you will learn how to determine the priority of selectors on your own
Add a paragraph to the editor with the class set to the border
and the id as no-border
. Set the following rules:
border
class, set a solid border of any color with a thickness of 1px. This can be done like this: border: 1px solid #000;
.no-border
, cancel the frame. Use the none
value for the border-style
rule.Specify styles for the .border
selector first, and the #no-border
selector second.
Write the styles in the <style>
tag.
If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
Tests are designed so that they test the solution in different ways and against different data. Often the solution works with one kind of input data but doesn't work with others. Check the «Tests» tab to figure this out, you can find hints at the error output.
It's fine. 🙆 One task in programming can be solved in many different ways. If your code passed all tests, it complies with the task conditions.
In some rare cases, the solution may be adjusted to the tests, but this can be seen immediately.
It's hard to make educational materials that will suit everyone. We do our best but there is always something to improve. If you see a material that is not clear to you, describe the problem in “Discussions”. It will be great if you'll write unclear points in the question form. Usually, we need a few days for corrections.
By the way, you can participate in courses improvement. There is a link below to the lessons course code which you can edit right in your browser.
Your exercise will be checked with these tests:
1const { test } = require('tests');
2
3test(({ query, expect }) => {
4 const element = query(document, '#no-border');
5 const style = getComputedStyle(element);
6
7 expect(style).to.have.property('border-width', '1px');
8 expect(style).to.have.property('border-style', 'none');
9});
10
Teacher's solution will be available in: