Here’s a quick recap of yesterday:
http://sta.mn/32f
Images are embedded via the <img>
element:
<img src="http://placekitten.com/400/250">
The src
is an image URL, which can be either fully
qualified (with the http://
prefix, domain, etc.) or a
relative path to an image file (src="images/kitten.jpg"
).
By default, images have no width or height until the browser loads them. This can sometimes result in jarring layout changes.
The width
and height
attributes can be used to tell the browser how big the image is before it’s loaded.
<img src="http://placekitten.com/300/200"
width="300" height="200">
Scalable Vector Graphics are the Internet’s vector format.
They encode shapes rather than pixels:
<svg width="250" height="250">
<g transform="translate(125,125)">
<circle fill="yellow" r="125"></circle>
<circle fill="cyan" r="100"></circle>
<circle fill="magenta" r="75"></circle>
</g>
</svg>
See also: <img src="image.svg">
http://cssdeck.com/labs
CSS was created to separate substance
(your content) from style (its appearance).
Before CSS, HTML mostly looked like this:
<p><font size="10">This is a paragraph with <font color="red">red text</font>.</font></p>
But with CSS you can make rules that define
the style for different elements:
<style>
p { font-size: 10px; }
b { color: red; }
</style>
<p>This is a paragraph with <b>red text</b>.</p>
selector(s) {
property: value;
}
Multiple selectors are separated by a comma:
h1, h2 {
font-style: italic;
}
You can have as many properties as you want in a rule. To make
all <h2>
elements red and centered, for instance:
h2 {
color: red;
text-align: center;
}
text color | color: red; |
background color | background-color: yellow; |
border | border: 4px dashed green |
text shadow | text-shadow: 0 0 5px blue |
Some properties accept multiple values, for instance:
font
is a shorthand that can set many font-related properties at once.border
and background
also serve as
shorthand for a collection of related properties.margin
and padding
take 1, 2, 3 or 4 values:top right bottom left
top right bottom
(left = right)top right
(bottom = top, left = right)top
(top = right = bottom = left)Most property values can be expressed in terms of
different measurement units.
unit | description | e.g. |
---|---|---|
px |
absolute pixels | 20px |
% |
percentage of the parent value | 200% |
em |
relative to the parent font-size | .5em |
rem |
relative to the root element’s value | 2rem |
vw/vh |
percentage of the viewport width/height | 50vw |
Note: the value 0
can be specified without units.
Colors can also be expressed in different formats:
format | description | e.g. |
---|---|---|
name |
W3C named colors | blue , salmon |
#rgb |
hexadecimal red, green, blue (RGB) |
#f3c , #00dead |
rgb(r,g,b) |
decimal RGB | rgb(64,128,255) |
hsl(h,s,l) |
hue, saturation, value | hsl(180,50%,50%) |
transparent |
.. | transparent |
There’s also rgb()
and hsla()
to control alpha, or opacity.
size | font-size: 50px |
family | font-family: "Helvetica Neue" |
weight | font-weight: bold |
style | font-style: italic |
everything! | font: italic bold 60px “Helvetica Neue” |
text-align | text-align: center |
padding | padding: 20px |
margin | margin: 40px |
float | float: right |
display |
display: inline display: block (also: display: none )
|
The CSS position
property, combined with
top
, left
, bottom
and right
, allows you to
arbitrarily position elements on the page:
relative |
position: relative;
top: -20px;
|
absolute |
position: absolute;
left: 50px;
top: 100%;
|
fixed |
position: fixed;
right: 0;
bottom: 0;
|
opacity |
opacity: .5 opacity: .2
|
transform |
transform: rotate(-15deg)
|
As browsers render each element on the page,
they keep track of their current position on the
screen, or the flow.
Floating elements change the flow of text and other elements around them, and can break up long runs of text.
(Relatively positioned) elements don’t change the flow of other elements, whereas (absolutely positioned) ones are removed from the flow and placed elsewhere.
CSS selectors are expressions that match different types of elements in your document. Here are some examples:
selector | description | examples |
---|---|---|
element |
matches elements with this name | h2 |
.class |
matches elements with this class attribute |
.red |
#id |
matches the element with this id attribute |
#chapter1 |
CSS selectors may be made more specific by separating
multiple expressions with a space between them to
indicate their descendency:
section h2 { /* <h2> within a <section> */ }
.chapter p { /* <p> within a class="chapter" */ }
ul ol li { /* <li> in a <ul> in another <ul> */ }
You can target direct descendants
by adding a >
between selectors:
section > h2 { /* <h2> with a <section> parent */ }
.cars > li { /* <li> with a class="cars" parent */ }
.people > .person { /* "person" with a "people" parent */ }
A special class of selector suffixes can be used to
style elements based on contextual criteria:
:first-child
only matches the first child element:last-child
only matches the last child:nth-child(n)
only matches the nth child (starting at 1),
or n can be odd
or even
There are 3 ways to add CSS to your document:
<head>
:
<link rel="stylesheet" href="style.css">
<style>
element in the <head>
:
<style>
/* your styles here */
</style>
style
attribute to any element:
<h2 style="color: red;">Red!</h2>
At some point you’re going to need to get
your work out of CSS Deck and into GitHub.
For simple sites, I recommend the following structure:
/
(project root)index.html
.html
/css
.css
/js
.js
Note: index.html
is a common naming
convention for the file that should show up in a directory listing.
Crack open Atom and create a new
HTML file with the following contents:
<!DOCTYPE html>
<html>
<head>
<title>Your Page Title</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<!-- HTML here -->
</body>
</html>
Save this as index.html
in your project directory.
In Atom, right-click the file list in the left pane
and select Add File.
Type css/style.css
in the Atom prompt.
Your workflow looks like this from here on out:
index.html
in Chrome by either:file://
⌘R
(Mac) or F5
(Windows)Going back and forth between your text editor
and a web browser can be tedious.
Thankfully, Apple and Google made us
a very nice tool called the Web Inspector.
In Chrome (and other modern web browsers)
you can right-click on any part of
web page to inspect its HTML element:
This opens Chrome’s element inspector:
The left pane is your HTML.
And the right pane is your CSS.
You can change stuff in the inspector and Chrome
will update the page automatically!
In the top-right corner there’s a section that looks like this:
Right-click the image above and select Inspect Element.
Then, click inside the element.style { }
area to enter CSS editing mode. You can also
edit any of the other styles defined below.
The changes you make here are temporary. If you
refresh the page they will disappear.
If you want to save them, you will need to either:
So-called “live-coding” tools are great for
testing ideas quickly, with less back-and-forth:
But once you get comfortable with the Web Inspector,
you may never use one of these again!