CSS
Remember our simple page?
<!doctype html>
<html lang="en">
<head>
<title>Hello, world!</title>
</head>
<body>
<h1>Hello, world!</h1>
<p>Here is a very small and simple document.</p>
<h2>Links I like:</h2>
<ul>
<li><a href="http://google.com/">Google</a></li>
<li><a href="http://amazon.com/">Amazon</a></li>
<li><a href="http://netflix.com">Netflix</a></li>
</ul>
</body>
</html>
And remember the document object model (DOM) representation as a tree?
Browsers expose two ways for web
developers to do things with the DOM
Designed to allow separation of content from presentation
Consider <h1>
: it's for indicating the most important heading.
"Important" doesn't imply anything about color, typeface, size, etc.
You could do this...
<h1>
<font size="16" face="Times" color="red">Big Headline</font>
</h1>
But it's extremely limited.
And not a good idea.
Separating presentation info into its own CSS file also makes the HTML far less cluttered and allows for far more control over how things look with less code.
How does CSS work?
RULE: Text in unordered lists should be green!
RULE: The last <li> in a list should be orange.
What a CSS rule looks like
<selector> {
<property-1>: <value-1>;
<property-2>: <value-2>;
}
Universal selector
* {
color: green;
}
Element selector
li {
color: green;
}
Grouping selectors
li, h2, p {
color: green;
}
Selecting descendants
ul li {
color: green;
}
Selecting children
li > a {
color: green;
}
Pseudo classes
a:hover, li:last-child {
color: green;
}
The id
attribute is used to specify a unique identifier for a single HTML element
<a href="email:gl26499@umbc.edu" id="email-link">email me!</a>
The class
attribute is used to specify a class or grouping for one or more HTML elements
<p class="important" >Here's some important text!</p>
<p>Here's some regular tex</p>
<p class="important" >And more important text!</p>
id
is meant for targeting a single elementclass
is meant for targeting multiple elementsRemember: HTML is intended to add semantic meaning to documents, so try to choose you names accordingly.
important
, logo
, warning
, signup-button
abc123
, redText
, paragraph
, bold
ID selectors
button#login {
background-color: #c10547
}
(#c10547
is a hexadecimal color code for magenta)
class selectors
.pagination {
width: 500px;
}
Units of measurement should generally be in pixels (px
). There are others, but they are more complex to work with.
Use a <div> (probably with a class name or id)
<h3>What if you need to <span class="important">arbitrarily
style</span> some text selection?</h3>
View the HTML and CSS here.
Embed directly in the document's <head>
section:
<style>
h1 {
color: #fefefe;
}
p, ul, ol {
font-face: Helvetica;
}
</style>
Link a file in the document's <head>
section:
<link rel="stylesheet" href="style.css">