Cascading Style Sheets
Standards & backward compatibility
/* questions? */
How?
<link>
element<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Hello World</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
...
USE this method.
<style>
element<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Hello World</title>
<style type="text/css">
/* ... */
</style>
</head>
<body>
...
DO NOT use this method.
<body>
<h1 style="color: red;">
Hello World
</h1>
<p style="margin: 2em 0;">
Paragraph here
</p>
</body>
DO NOT use this method.
@import
rules@import url(normalize.css);
@import url(layout.css);
@import url(typography.css);
nav {
position: fixed;
top: 0;
left: 0;
height: 3em;
}
DO NOT use this method.
Performs worse than the <link>
!
See don’t use @import.
/* questions? */
property: value
{ declaration; declaration; ... }
/* single-line */
{ display: block; height: 300px }
/* multi-line */
{
display: block;
height: 300px;
}
Semicolons (;
) are only required between declarations.
Pattern matching against document trees
Selectors help you point to (select) specific HTML elements.
Basic simple selectors | Examples |
---|---|
Type selectors | HTML tags: nav , section , header , p |
Class selectors | HTML classes that uses dot scheme in CSS: .ds-body-img |
ID selectors | HTML classes that uses hash scheme in CSS: #data-story-personal |
Universal selector | Selects all-the-HTML-things: * |
Attribute selectors | Selects HTML tag attributes: [title] , [rel~="copyright"] |
Pseudo classes | Adds a CSS-defined class to an HTML element: :hover |
See the Pen Demo basic CSS selection by Chris Lindgren (@lndgrn) on CodePen.
Combinators | Example |
---|---|
Descendent combinator (␣ ) |
.data-story-personal p {...} |
Child combinator (> ) |
ul#resources > li {...} |
Adjacent sibling combinator (+ ) |
h1 + h2 {...} |
General sibling combinator (~ ) |
img ~ p {...} |
See the Pen Demo basic CSS selection by Chris Lindgren (@lndgrn) on CodePen.
HTML
containing block rules.
Containing blocks are established by parent box-context. (See W3C spec.)
Based on these selectors declaration blocks, tell me how the elements will be styled.
JS Bin on jsbin.com::first-line
::first-letter
::before
::after
Type | Example |
---|---|
Location | :link , :visited , :target , ... |
User action | :hover , :focus , ... |
Tree-structural | :first-child , :nth-child() , ... |
:nth-child()
(pseudo-class)/* questions? */
UA, user, author
and !important
See this article on "The Cascade"
#main
.title
article
:hover
See the Pen Using the cascade in CSS by Chris Lindgren (@lndgrn) on CodePen.
What is the color
and text-decoration
of the “Go to post” anchor/link?
/* questions? */
15px
1.25em
rebeccapurple == #663399
url(logo.png)
border-radius: 15px; z-index: 1; order: 3;
line-height: 1.5; flex: 0.618;
width: 80%; font-size: 120%;
lightgreen
, gold
, olivedrab
, etc.#69c
, #abcdef
, #eee
, #123456
...rgb(200, 150, 100)
, hsla(120, 100%, 50%, 0.5)
, ...See more in CSS Color Module Level 4.
em
& rem
vw
, vh
ex
, ch
, etc.
See the Pen Basic heading level hierarchies by Chris Lindgren (@lndgrn) on CodePen.
px
, pt
, in
, cm
, mm
, ...
Extra tidbit: Learn about how pixels are defined -- Physical or pixel?
body {
background: url(../img/starrynight.png);
}
/* questions? */
Test it out with your Inspect Element.
position: default
(normal)position: absolute
position: fixed
position: relative
float: [left, right, none]
fixed
positions an element based on the viewport.
absolute
positions an element based on the closest parent containing block.fixed
& absolute
See the Pen Containing blocks and position properties by Chris Lindgren (@lndgrn) on CodePen.
display
behaviors. Notably, the CSS Grid display.
/**
* style.css
*
* Styles for the basic HTML5 homework.
*
* Index
* - Base
* - Header
* - Article
* - Footer (Need to write/design)
*/
{
);{
) on the same line as the last selector;:
);;
) at the end of all declarations;}
) on its own new line;.selector-1 {
background-color: aliceblue;
background-image: linear-gradient(white, dimgrey),
linear-gradient(black, salmon);
box-shadow: 1px 1px 1px black,
2px 2px 1px 1px salmon inset;
color: black;
font-size: 1.25rem;
padding: 15px;
}
.selector-1 {
background-color: aliceblue;
background-image: linear-gradient(white, dimgrey),
linear-gradient(black, salmon);
box-shadow: 1px 1px 1px black,
2px 2px 1px 1px salmon inset;
color: black;
font-size: 1.25rem;
padding: 15px;
}
article p {
font-size: 1.25rem;
margin: 15px;
padding: 25px;
}
With this new knowledge and guidelines in mind: