This exercise illustrates how we can use multiple selectors to create more concise code. Are there additional tactics for creating less repetitive stylesheets?
Answer
Yes there are! One common way to keep our CSS concise is via proper utilization of “inheritance”. Within CSS, the computed values of many properties get inherited from ancestor elements. We can use this fact to reduce the number of declarations we write. For example, if I want most of the text on my page to be 20px I can declare font-size: 20px on the html selector. As <html> is the root element and the font-size property is inheritable, all text for this page will be 20px. In cases where we want to deviate from this 20px norm, we can use a selector with a higher specificty to target the element whose font-size we want to override.
I have a question about this exercise. In this exercise we used multiple selectors in order to change the font of P and h5 to georgia. Yet we already had a command to make P Arial. It’s the first rule of the css sheet: p {font-family: Arial;} . Does this mean that h5, p { font-family: Georgia;} overrides the previous code?
A type selector rule is the least specific of all rules, and hence the most pervasive. Like water, it creeps everywhere.
We want type selectors to be the weakest, or least important so that we can build upon their foundation. In some cases that means making an alteration or two to suit a special need. The options are navigate (as in, traverse) to the target nodes, or give them a specific container, or give them a class. Purpose is the guiding force in this decision.
This is where the water begins to get murky. There is already evidence of a diversity of choices.
Consider,
p.class
vs.
p .class
vs.
.class p
To begin to answer your question we need to be certain that their differences and application purposes are clear.
The universal selector * selects all the elements in an HTML document. The html selector selects the <html> element. Some properties are inheritable (E.g. the color property), therefore, some declarations applied the html CSS rule will apply to its descendants. Similar to the html selector, the body selector selects the <body> element. Again, some properties are inheritable so some declarations in the body CSS rule will apply to the <body> element’s descendants
Because of the amount of memory it uses. The DOM balloons to an enormous size so the selector rules can be applied to every element known, not just those in the document.
If there is any user interaction, even CSS dynamically such as :hover the entire page has to be redrawn.