Fundamentals Flashcards
What is a declaration?
A declaration is made up of a property and a value. For example:
color: black;
A group of declarations inside curly braces is called a declaration block.
Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.
What is a selector?
A declaration block is preceded by a selector (in this case, body):
body { color: black; font-family: Helvetica; }
Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.
What is a css rule or css ruleset?
Together, the selector and declaration block are called a ruleset. A ruleset is also called a rule.
Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.
What are at-rules?
at-rules are language constructs beginning with an “at” symbol, such as @import rules or @media queries.
Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.
When declarations conflict, What does the cascade consider to resolve the conflict?
- Stylesheet origin - Where the styles come from. Your styles are applied in conjunction with the browser’s default styles.
- Selector specificity - Which selectors take precedence over which.
- Source order - Order in which styles are declared in the stylesheet.
Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.
What are the possible origins a stylesheet can have?
- author styles - The stylesheets you add to your web page. Highest priority.
- user stylesheet - Some browsers let users define a user stylesheet. This is considered a third origin, with a priority between user agent and author style.
- user agent styles - Styles added by the browser. Lowest priority
Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.
What are !important
declarations?
A declaration can be marked important by adding !important
to the end of the declaration, before the semicolon:
color: red !important;
Important declarations are treated as a higher-priority origin. The overall order of preference, in decreasing order, is this:
- Author important
- Author
- User agent
Adding !important
gives the declaration a specificity above inline-style.
Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.
What is CSS specificity?
According to MDN, Specificity is the means by which browsers decide which CSS property values are the most relevant to an element and, therefore, will be applied. Simply put, if two CSS selectors apply to the same element, the one with higher specificity is used.
What are inline-styles?
Styles added to an element using its style
attribute, the declarations are applied only to that element. These are, in effect, “scoped” declarations, which override any declarations applied from your stylesheet or a tag.
The style attribute accepts a list of declarations separated by semicolons (;
).
To override inline declarations in your stylesheet, you’ll need to add !important
to the declaration.
Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.
How does the browser evaluate the specificity of a css rule?
The browser evaluates specificity in two parts:
- inline-stiles - By default have higher specificity than any other styles.
- selector specificity - Determined by the selectors (elaborated in another card)
Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.
How does the browser evaluate the specificity of a selector?
- If a selector has more IDs, it wins. - ID selectors
- If that results in a tie, the selector with the most classes wins. - Class selectors
- If that results in a tie, the selector with the most tag names wins. - Tag selectors (also known as type selectors)
- If the origin and the specificity are the same, then the declaration that appears later in the stylesheet—or appears in a stylesheet included later on the page—takes precedence.
NOTE: Pseudo-class selectors (for example, :hover
) and attribute selectors (for example, [type="input"]
) each have the same specificity as a class selector.
The universal selector (*
) and combinators (>
, +
, ~
) have no effect on specificity.
Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.
Which order do we need to apply to overwrite all the styles of a link?
This listing shows styles for links on a page in the “correct” order.
a:link { color: blue; text-decoration: none; } a:visited { color: purple; } a:hover { text-decoration: underline; } a:active { color: red; }
The cascade is the reason this order matters: given the same specificity, later styles override earlier styles.
A helpful mnemonic to remember this order is LoVe/HAte—link, visited, hover, active.
Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.
What is a cascaded value aka computed value?
The browser follows these three steps — origin, specificity, and source order to resolve every property for every element on the page. A declaration that “wins” the cascade is called a cascaded value.
There’s at most one cascaded value per property per element.
Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.
What are inheritance values?
When an element has no cascaded value for a given property, it may inherit one from an ancestor element.
Not all properties are inherited (We wouldn’t want a passing its border down to every descendant element.), however. By default, only certain ones are.
In general, these are the properties you’ll want to be inherited. They are primarily properties pertaining to text: color, font, font-family, font-size, font-weight, font-variant, font-style, line-height, letter-spacing, text-align, text-indent, text-transform, white-space, and word-spacing
.
Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.
What does the special keyword inherit
do to a property value?
Will cause the element to inherit that value from its parent.
Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.