lecture03
lecture03
Layout
Lecture 3
Today’s Agenda
Properties determine the styles that will be applied to the selected element(s)
(background-color, font-size, etc.)
Each property has a set of values that can be chosen (e.g. 14pt value for font-size
property)
There are currently over 200 possible style properties to choose from - use the Chrome
inspector for useful autocomplete of property values!
A good discussion.
Understanding the Good vs. Bad
We choose resources that best align with our code quality guidelines, while giving just
enough "extra detail" into topics we cover in lecture/section/lab. That said, let us know if
you're looking for recommendations on a specific resource!
Remember that all of your work must be your own, and cite any tutorials/resources you
may be using to explore features you may be exploring.
The DOM
For example:
● Every HTML tag (<p>, <em>, <body>, etc.) are part of the DOM, and have a place in a tree
hierarchy.
● They all have a parent (<body>'s is <html>) and almost always have a child (<title> is a child
of <head>).
● You don't build a DOM, but the browser does, which allows you to access it.
CSS Selectors
Used to select specific HTML elements in CSS
Example (CodePen)
The Document Object Model (DOM)
How the browser represents the hierarchy of a page - very useful when thinking about
selectors!
We'll return to this when we introduce JavaScript, where we can dynamically access/modify
element "nodes" in the DOM tree.
For those of you interested in accessibility on the web, there is also a related concept known as
Accessibility Tree
The “Cascade” of CSS
All styles "cascade" from the top of the sheet to the bottom
● When you add a second file, conflicting rules are overwritten
● Take care not to override styles if possible
● If you have styles specific to a page, use a second sheet (usually linked after the
shared one)
Styles "cascade" together when there are conflicting selectors but different properties.
Top, to bottom, lower rules will overwrite
previous ones
HTML CSS
<p id="text"> p {
Hello World! color: blue;
Given the HTML and CSS to </p> }
the right, what color will the
p {
"Hello World!" paragraph be? color: red;
}
now?
But what if the selector is different?
CSS
There are two other important concepts here:
p {
1. Specificity color: blue;
2. Inheritance }
#text {
color: red;
}
CSS Specificity (try saying this 5 times fast)
Given an element, and two (or more) different HTML CSS
selectors that would "select" it, which rules would <p id="text"> #text {
actually apply? Hello World! color: blue;
</p> }
p {
color: red;
What will "Hello World!" look like }
now?
With different selectors, which one is more specific?
As the browser reads through your CSS source, it calculates a score for each selector. You'll rarely, if
ever, see these numbers, but they might look something like 0223 or 1000.
When looking at an HTML element and trying to decide what it should look like, it gathers all the
selectors that might apply, sorts by their specificity score, and applies some style in order!
Specificity Scores (you don't have to memorize these)
h1 0 0 0 1 0001
h1 + p::first-letter 0 0 0 3 0003
#text 0 1 0 0 0100
Source: https://developer.mozilla.org/en-
US/docs/Learn/CSS/Building_blocks/Cascade_and_inheritance#specificity_2
Calculator: https://specificity.keegan.st/
CSS Specificity and Cascading
HTML CSS
Note that I've been specifically saying
<p id="text"> #text {
"selector" rather than property.
Hello World! color: blue;
</p> }
p {
color: red;
text-decoration: underline;
}
What will "Hello World!" look like
now?
CSS Inheritance
Some properties, like color or HTML CSS
text-decoration are inheritable. <div> #text {
That is: if there's no conflict, an <p id="text"> color: blue;
element will inherit the value for that Hello World! }
property from its parent. </p>
</div> div {
color: red;
text-decoration: underline;
}
What will "Hello World!" look like
now?
CSS and Layout
Layout Techniques in CSS
● Block vs. Inline and nesting in HTML
● Box Model (margin/padding/border)
● Flex
● Positioning
● Float (less common today, but still good to know)
These are what we expect you to focus on, roughly in order of prioritization
Default Dimensions of Block and Inline Elements
Height:
● Block and inline elements normally have the height of their content
Width:
● Inline elements have the width of their content
● Block elements have a width that spans the width of their parent
#css {
width: 80%;
<p id="css"> height: 60px;
CSS is <strong>really</strong> great! }
</p>
strong {
/* We still get the inline display, but with
<p id="html">HTML is pretty cool too.<p> width/height */
display: inline-block;
width: 120px; /* works! */
height: 50px; /* works! */
}
More about width and height
Both can be set using length units (px, % are most common - when doing page layout,
generally want to do % unless you know you have elements that are a fixed width)