Colored Boxes!
div
elementsstyle
attribute to specify
width
, height
)position
: static
, relative
, absolute
)top
, left
)background-color
) <div style="background-color: black; width: 300px;
height: 300px; position: relative;">
<div style="background-color: blue; width: 100px;
height: 100px; top: 50px; left: 50px; position:
absolute;"></div>
</div>
Writing style = "..."
for every element is cumbersome!
style
attributesstyle
attribute valuesSetting style
manually is not good style!
… if only there was a better way…
Cascading Style Sheets (CSS): specify style
attributes for many elements on a page:
h1
, p
, div
)class="some-class"
)id="some-id"
)Change to CSS styles affect all elements matching a prescribed pattern
Example from before
Rather than setting style
attribute directly, specify semantics
class
attribute to give names to the types of boxes <div class="outer-box">
<div class="inner-box"></div>
<div class="inner-box"></div>
</div>
All boxes (div
elements):
position: absolute;
Outer box:
width: 300px;
height: 300px;
background-color: black;
Inner boxes:
width: 100px;
height: 100px;
Two methods:
In .html
head:
<head>
<style>
/* style goes here */
</style>
</head>
A separate file, say style.css
, then add reference in head
:
<head>
<link rel="stylesheet" href="style.css">
</head>
div
elements in the document
div {
position: absolute;
}
class="outer-box"
.outer-box {
background-color: black;
width: 300px;
height: 300px;
}
class="inner-box"
.inner-box {
width: 100px;
height: 100px;
}
Each inner box has own:
top
, left
)background-color
)CSS can style element by id!
Give inner boxes unique ids:
<div id="blue-box" class="inner-box"></div>
<div id="red-box" class="inner-box"></div>
#blue-box {
background-color: blue;
top: 50px;
left: 50px;
}
#red-box {
background-color: red;
top: 150px;
left: 100px;
}
stylish-boxes.html
id
beats class
beats tag p { color: red; }
p { color: blue;}
<p>What color is this text</p>
Elements can have multiple class
es, but id
should be unique
<div id="red-box" class="inner-box special-box"></div>
Go forth and make a stylish website!
Color, Three Ways:
Content Warning: Massive oversimplifications coming up!!!
Physics $\implies$ Perception
“Natural” light comprised of different wavelengths in different proportions
Perception of color determined by the amount each color receptor is stimulated
Question. How do color monitors/projectors create so many colors?
So far:
A color that can be represented on a computer screen is represented by three values:
Color is a three-dimensional object!
In HTML: rgb(red, green, blue)
red
, green
, blue
are integers from 0 to 255.Manipulation of r, g, b
color values is not intuitive
red
, green
, blue
have natural physical interpretationsred
, green
, blue
do not have natural perceptual interpretations (at least to me)Question. What are the RGB values of the color above?
Used predefined colors:
<div class="flag">
<div style="background-color: red;" class="stripe"></div>
<div style="background-color: orange;" class="stripe"></div>
<div style="background-color: yellow;" class="stripe"></div>
<div style="background-color: green;" class="stripe"></div>
<div style="background-color: blue;" class="stripe"></div>
<div style="background-color: purple;" class="stripe"></div>
</div>
Question. What do you think of HTML’s color choices?
Make a rainbow with 8 stripes!
Dowload rainbow-eight.html
to get started