Chapter 11 (Introducing CSS)
Chapter 11 (Introducing CSS)
Introducing CSS
Content 2
3 CSS Rules
CSS Intro 3
• Cascading Style Sheets (CSS) is the W3C standard for defining the presentation of
documents written in HTML, and in fact, any XML language.
• Presentation refers to the way the document is delivered to the user, whether shown on
a computer screen, displayed on a cell phone, printed on paper, or read aloud by a
screen reader.
CSS Benefits 4
• Precise type and layout controls: You can achieve print-like precision using CSS.
There is even a set of properties aimed specifically at the printed page.
• Less work: You can change the appearance of an entire site by editing one style
sheet. This also ensures consistency of formatting throughout the site.
• More accessible sites: When all matters of presentation are handled by CSS, you
can mark up your content meaningfully, making it more accessible for non-visual or
mobile devices
CSS Power 5
• When used to its full potential, CSS is a robust and powerful design tool.
• It is proof of the power in keeping CSS separate from HTML, and presentation
separate from structure.
CSS Rule 6
CSS Implementation 7
• Embedded style sheets: It is placed in a document via the style element, and its
rules apply only to that document. The style element must be placed in the head of the
document.
• Inline styles: You can apply properties and values to a single element by using the
style attribute in the element itself
CSS Implementation… 8
• Embedded style sheets: It is placed in a document via the style element, and its
rules apply only to that document. The style element must be placed in the head of the
document.
• Inline styles: You can apply properties and values to a single element by using the
style attribute in the element itself
CSS Big Concepts 9
• There are a few big ideas that you need to get your head around to be comfortable
with how Cascading Style Sheets behave.
Inheritance
• Styled HTML elements pass down certain style properties to the elements they
contain.
CSS Big Concepts… 10
• There are a few big ideas that you need to get your head around to be comfortable
with how Cascading Style Sheets behave.
Document Structure
• HTML documents have an implicit structure, or hierarchy. For example, the sample
article we’ve been playing with has an html root element that contains a head and a
body, and the body contains heading and paragraph elements.
CSS Big Concepts… 11
• There are a few big ideas that you need to get your head around to be comfortable
with how Cascading Style Sheets behave.
• An element that is directly contained within another element is said to be the child of
that element. Conversely, the containing element is the parent.
CSS Big Concepts… 12
• There are a few big ideas that you need to get your head around to be comfortable
with how Cascading Style Sheets behave.
CSS Priority
• Style information can come from various origins, listed here from highest priority to
lowest. In other words, items higher in the list override items below.
• If you don’t apply any style information to a web page, it renders according to the
browser’s internal style sheet. We’ve been calling this the default rendering; the
W3C calls it the user agent style sheet.
• Individual users can apply their own styles as well (the user style sheet, also called the
reader style sheet), which override the default styles in their browser. However, if the
author of the web page has attached a style sheet (the author style sheet), that
overrides both the user and the user agent styles.
• The only exception is if the user has identified a style as “important,” in which case that
style will override all competing styles
CSS Big Concepts… 13
• There are a few big ideas that you need to get your head around to be comfortable
with how Cascading Style Sheets behave.
Specificity
• It is possible for conflicts to arise in which an element is getting style instructions from
more than one rule.
• When two rules in a style sheet conflict, the type of selector is used to determine the
winner. The more specific the selector, the more weight it is given to override
conflicting declarations.
CSS Big Concepts… 14
• There are a few big ideas that you need to get your head around to be comfortable
with how Cascading Style Sheets behave.
Assigning importance
• If you want a rule not to be overridden by a subsequent conflicting rule, include the
!important indicator just after the property value and before the semicolon for that
rule.
CSS Big Concepts… 15
• There are a few big ideas that you need to get your head around to be comfortable
with how Cascading Style Sheets behave.
Rule Order
• After all the style sheet sources have been sorted by priority, and after all the linked
and imported style sheets have been shuffled into place, there are likely to be conflicts
in rules with equal weights. When that is the case, the order in which the rules appear
is important. The cascade follows a “last one wins” rule.
CSS Big Concepts… 16
• There are a few big ideas that you need to get your head around to be comfortable
with how Cascading Style Sheets behave.
• You can apply properties such as borders, margins, padding, and backgrounds to
these boxes, and even reposition them on the page.
CSS Big Concepts… 17
• There are a few big ideas that you need to get your head around to be comfortable
with how Cascading Style Sheets behave.
Grouped Selector
• If you ever need to apply the same style property to a number of elements, you can
group the selectors into one rule by separating them with commas.
CSS Units of Measurement 18
2) Relative Units: Relative units are based on the size of something else, such as the
default text size or the size of the parent element. Relative units are the way to go for
most web measurements, and there are a few options: rem, em, and vw/vh.
CSS Units of Measurement … 19
Browsers’ Developer Tools 20