Content and Structure: HTML
Style: CSS
Behavior: JavaScript
p {
color: red;
background-color: yellow;
}
This paragraph uses the style above.
Property | Description |
---|---|
color | color of an element's text |
background-color | background color that will appear behind the element |
p { color: red }
h2 { color: rgb(128, 0, 196); }
h4 { color: #FF8800; }
This paragraph uses the first style above
Color names: aqua, black, blue, fuchsia, gray, green, lime, maroon, navy, olive, purple, red, silver, teal, white, yellow
RGB codes: red, green, and blue values from 0 (none) to 255 (full)
Hex codes: RGB values in base-16 from 00 (0, none) to FF (255, full)
Property | Description |
---|---|
font-family | which font will be used |
font-size | how large the letters will be drawn |
font-style | used to enable/disable italic style |
font-weight | used to enable/disable bold style |
Complete list of font properties |
font-family
p {
font-family: Georgia;
}
h4 {
font-famiy: "Courier New";
}
This paragraph uses the first style above
Enclose multi-world font names in quotes
font-family
p {
font-family: Garamond, "Times New Roman", serif;
}
This paragraph uses the above style
Can specifiy multiple fonts from highest to lowest priority
Generic font names:
If the first font is not found on the user's computer, the next is tried
Generally should specify similar fonts
Placing a generic font name at the end of your
font-family
value ensures that every computer will use a
valid font
font-size
p {
font-size: 14pt;
}
This paragraph uses the above style
Vague font sizes: xx-small, x-small, small, medium, large, x-large, smaller, larger,
Percentage font sizes: 90%, 120%
font-weight
,
font-style
p {
font-style: italic;
font-weight: bold;
}
This paragraph uses the above style
Either of the above can be set to normal to turn them off (e.g., headings)
body {
font-size: 16pt;
}
To apply a style to the entire body of your page, write a selector for the body (saves you from manually applying a style to each element)
/* This is a comment.
It can span many lines in a CSS file. */
p {
background-color: aqua;
color: red;
}
CSS (like HTML) is usually not commented as much as code such as Java
The //
single-line comment is NOT supported in CSS
The <-- ... -->
HTML comment is also
NOT supported in CSS
p, h1, h2 {
color: green;
}
h2 {
background-color: yellow;
}
This paragraph uses the above style.
A style can select multiple elements separated by commas
The individual elements can also have their own styles (like
h2
above)
body { color: green; }
p, h1, h2 { color: blue; font-style: italic; }
h2 { color: red; background-color: yellow; }
This paragraph uses the first style above
When two styles set conflicting values for the same property, the latter style takes precedence
(later we will learn about more specific styles that can override more general styles)
<p>
<a target="_blank" href="http://jigsaw.w3.org/css-validator/">
<img src="http://jigsaw.w3.org/css-validator/images/vcss"
alt="Valid CSS!" />
</a>
</p>
Checks your CSS to make sure it meets the official CSS specifications
More picky than the web browser, which may render malformed CSS correctly
Property | Description |
---|---|
text-align |
alignment of text within its element |
text-decoration |
decorations such as underlining |
text-indent |
indents the first letter of each paragraph |
text-shadow |
a colored shadow near an existing piece of text (CSS3) |
line-height ,
word-spacing ,
letter-spacing |
gaps between the various portions of the text |
Complete list of text properties |
text-align
blockquote { text-align: justify; }
h2 { text-align: center; }
[TO LUKE SKYWALKER] The alliance... will die. As will your friends. Good, I can feel your anger. I am unarmed. Take your weapon. Strike me down with all of your hatred and your journey towards the dark side will be complete.
Can be left
, right
, center
, or
justify
(which widens all full lines of the element so that they occupy its
entire width)
text-decoration
p { text-decoration: underline; }
This paragraph uses the style above
Can also be overline, line through, blink or none
Effects can be combined:
p { text-decoration: overline underline; }
This paragraph uses the style above
text-shadow
p {
font-weight: bold;
text-shadow: 10px 10px gray;
}
This paragraph uses the style above
shadow
is specified as an X-offset, a Y-offset, or an
optional color
list-style-type
property
ol { list-style-type: lower-roman; }
Possible values:
none
: No markerdisc
(default), circle, square
decimal
: 1, 2, 3, etc.decimal-leading-zero
: 01, 02, 03, etc.lower-roman
: i, ii, iii, iv, v, etc.upper-roman
: I, II, III, IV, V, etc.lower-alpha
: a, b, c, d, e, etc.upper-alpha
: A, B, C, D, E, etc.lower-greek
: alpha, beta, gamma, etc.hebrew, armenian, georgian,
cjk-ideographic, hiragana, katakana, hiragena-iroha,
katakana-iroha
Property | Description |
---|---|
background-color |
color to fill background |
background-image |
image to place in background |
background-position |
placement of background image within element |
background-repeat |
how background image should be repeated |
background-attachment |
whether background image scrolls with page |
background-size |
how large the background appears behind the element |
background |
shorthand to set all backgroud properties |
More background properties |
background-image
body {
background-image: url("../images/draft.jpg");
}
This is the first paragraph
This is the second paragraph...
It occupies 2 lines
Background image/color fills the element's content area
background-repeat
body {
background-image: url("../../images/draft.jpg");
background-repeat: repeat-x;
}
This is the first paragraph
This is the second paragraph...
It occupies 2 lines
Can be repeat
(default), repeat-x
,
repeat-y
, or no-repeat
background-position
body {
background-image: url("../images/draft.jpg");
background-position: 370px 20px;
background-repeat: no-repeat;
}
This is the first paragraph
This is the second paragraph...
It occupies 2 lines
Value consists of two tokens, each of which can be top, left,
right, bottom, center,
a percentage, or a length value in px,
pt, etc.
Value can be negative to shift left/up by a given amount
<style>
(BAD)
<head>
<style type="text/css">
p { color: red; font-family: sans-serif; }
h2 { background-color: yellow; }
</style>
</head>
CSS code can be embedded within the head
of an HTML
file
This is bad style; DO NOT DO THIS (why?)
style
attribute (BAD)
<p style="font-family: sans-serif; color: red">
This is a paragraph</p>
This is a paragraph
Higher precedence than embedded or linked styles
Used for one-time overrides and styling a particular element
This is bad style; DO NOT DO THIS (why?)
HTML is for content; what is on the page (heading; list; code; etc.)
CSS is for presentation; how to isplay the page (bold; centered; 20px margin, etc.)
Keeping content separate from presentation is a very important web design principle
If the HTML contains no styles, its entire appearance can be changed
by swapping .css
files
See also: CSS Zen Garden
It's called Cascading Style Sheets because the properties of an element cascade together in this order:
<link>
tag)<style>
tag in
the page header)style
attribute of an HTML
element)
body { background-color: yellow; font-family: sans-serif; }
p { background-color: aqua; color: red; }
a { text-decoration: overline underline; }
h2 { font-weight: bold; text-align: center; }
A styled paragraph. Previous slides are available on the web site.
When multiple styles apply to an element, they are inherited
A more tightly-matching rule can override a more general inherited rule
Not all properties are inherited (notice link's color above)
pseudo-classes
a:link { color: #FF0000; } /* unvisited link */
a:visited { color: #00FF00; } /* visited link */
a:hover { color: #FF00FF; } /* mouse over link */
Class | Description |
---|---|
:active | an activated or selected element |
:focus | an element that has the keyboard focus |
:hover | an element that has the mouse over it |
:link | a link that has not been visited |
:visited | a link that has already been visited |
:first-letter | the first letter of text inside an element |
:first-line | the first line of text inside an element |
:first-child | an element that is the first one to appear inside another |
:nth-child(N) | applies to every Nth child of a given parent |
id
and class
id
id
value per pageid
class
class
class
es<p id="product-12345" class="product">Puppy calendar</p>
<p id="product-133337" class="product">Cat mug</p>
Both paragraphs have the same class (product
), but each has its own ID
Gives you another way to talk about your content in CSS (and later in JS)
#my-id {
/* ... properties ... */
}
.my-class {
/* ... other properties ... */
}
A mnemonic: .java
programs compile into .class
files so...
try to remember dot
(.) class
and hash (#) id
id
or class
?How do you decide whether to use an id
or a class
?
Probably prefer class
. You can only use an id
once per page, so it's good
to be a little stingy with them. class
es are free.
On the other hand, if you know you are making a unique section or page element (e.g.,
submit button), id
is the way to go.
It's easy to just make classes for everything, but don't forget that HTML is made to describe your content.
So, prefer a <p>
tag over a class
named paragraph
.
Ways to group content:
These are descriptive content groups. It's less likely that you'd need to pair
these with a class
(all block level).
<header>
<footer>
<article>
<section>
<aside>
<main>
Also, there are twogeneral
content grouping tags. These are the ones that you probably need to pair with a
class
orid
, though not always.
<div>
-- block level<span>
-- inline levelContext selectors are used to selectively style page elements
Examples:
selector1 selector2 {
properties
}
... applies the given properties to selector2 only if it is inside a selector1 on the page
selector1 > selector2 {
properties
}
... applies the given properties to selector2
only if it is directly inside a selector1
on the page (selector2
tag is immediately inside selector1
with no tags in between)
<p>Shop at <strong>Hardwick's Hardware</strong>...</p>
<ul>
<li>The <strong>best</strong> prices in town!</li>
<li><em><strong>Act</strong></em> while supplies last!</li>
</ul>
li strong { text-decoration: underline; }
Produces:
Shop at Hardwick's Hardware...
<p>Shop at <strong>Hardwick's Hardware</strong>...</p>
<ul>
<li>The <strong>best</strong> prices in town!</li>
<li><em><strong>Act</strong></em> while supplies last!</li>
</ul>
li > strong { text-decoration: underline; }
Produces:
Shop at Hardwick's Hardware...