By the end of today, you will be able to:
The goal of CSS is to take over all the formatting of a website,
which used to be done with now-obsolete HTML tags
like <center>
. Modern websites are formatted using
CSS, so that the same content (HTML) might be rendered different ways
depending on the device (laptop, notebook, phone) and the CSS
rules. A tour de force example of that
is csszengarden.com, in which
the same content is rendered in shockingly different ways.
In short, if you find yourself wanting to change the appearance of your page by changing the HTML, you're thinking about it the wrong way. On the other hand, we will sometime have to modify the HTML in order to provide the containers and hooks to allow the CSS to work, including classes and IDs.
The basics of CSS are:
If all of the following are specifying the same property value for an element, which one wins?
external style file
document level style tag
inline style attribute
It's up to the user
Why should we use external style files?
an external style file can apply to all the pages of a site
an external style file gives you one place to specify a rule
an external style file reduces redundancy
all of the above
In addition, an external style file can be cached by a browser, which can speed up the loading of subsequent pages.
How do we specify an external style file?
<style src="mystyle.css">
<link rel="stylesheet" href="mystyle.css">
<a rel="stylesheet" href="mystyle.css">
<external rel="stylesheet" src="mystyle.css">
How do we specify a document level style element?
<style> </style>
<style type="text/css"> </style>
<link rel="stylesheet"> </link>
<document> </document>
How do we specify an inline rule to make an emphasis tag be bold?
This is <em style="font-weight: bold">really</em> important.
This is <em class="font-weight: bold">really</em> important.
This is <em class="font-style: bold">really</em> important.
This is <em style="font-style: bold">really</em> important.
Using this jsfiddle on cascading CSS, which has an
external CSS file, do the following steps. Note that you can type your
CSS into the box at the upper right; that's the same as typing it into
a <style>
element.
As we learned in the last exercise, sometimes we need to be a bit more focussed with our CSS selectors than just saying every occurrence of some tag will appear the same way. For example, it's common for hyperlinks in the nav bar to look different from ordinary hyperlinks.
How do we specify that hyperlinks inside the nav bar are to be teal and 30px bold Verdana?
navbar a { color: teal; font: 30px bold Verdana; }
nav a { color: teal; font: 30px bold Verdana; }
navbar > a { color: teal; font: 30px bold Verdana; }
nav > a { color: teal; font: 30px bold Verdana; }
Here's an example in action:
In the exercise, we also used an inline style attribute to style a particular element. But you know that inline styles are frowned upon (deprecated). Instead, we should use a ...
class
id
div
span
The <DIV>
tag is used to create a generic box
(technically, a block element) that you can put other elements
into. Then, you can use CSS to style the elements. Similarly,
a <SPAN>
is a generic inline container.
By putting an ID or a CLASS onto an element, any element (not just DIV and SPAN), you can style it specially.
The difference between ID and CLASS is:
only one thing can have a particular CLASS
only one thing can have a particular ID
they are interchangeable
IDs avoid class-discrimination
Which is most likely to have be ID rather than a class:
important
best
optional
highlight
How would you create a CSS rule such that elements with the class "optional" are in a smaller, gray font?
optional { font-size: smaller; color: gray}
.optional { font-size: smaller; color: gray}
#optional { font-size: smaller; color: gray}
class=optional { font-size: smaller; color: gray}
How would you apply that CSS rule to a paragraph?
<p id="optional">lorem ipsum</p>
<p style="optional">lorem ipsum</p>
<p class="optional">lorem ipsum</p>
none of above
Why not name the class gray
rather than optional
?
One of the many powerful ways to use CSS selectors is to use more than one, namely nested selectors. For example, as we saw before, to style hyperlinks with the nav, we can do:
nav a { color: teal; font: 30px bold Verdana; }
This idea can be combined with classes and ids in lots of powerful ways.
Suppose you want the font of paragraphs inside the legal disclaimer to be 5px silver. How would you do that?
#disclaimer p { font-size: 5px; color: silver}
p .disclaimer { font-size: 5px; color: silver}
.disclaimer p { font-size: 5px; color: silver}
p #disclaimer { font-size: 5px; color: silver}
Suppose you want hyperlinks that are inside something that is optional to lack the usual underlining. How would you do that?
a optional { text-decoration: none}
a .optional { text-decoration: none}
.optional a { text-decoration: none}
optional a { text-decoration: none}
Let's turn to fonts next.
Which of the following is not a font property?
style
weight
height
family
size
color
Fonts can be difficult to work with because
They have to be on your computer
They have to be on the user's computer
They have to be on the server
All of the above
Using this JSfiddle on CSS, do the following:
h3
elements green and underlined
em
elements red
The Romantic Movement
introDIV be 24px.
wheat
.
week
be khaki
.
optional
optional stuff gray
Song of Myself?
author
photos?
We hope that after these activities you have a good understanding of:
Will be posted later, visit again after .