Administrivia
Introduction to CSS
Some basic font and color styles along the way
By the end of this week, you should be able to:
First one out now!
You have unlimited attempts to submit before the deadline on lectures.
Use the feedback if you don't get full credit, and review the readings/ask on Piazza if you have any questions!
Creative Project is due Wednesday. Remember to add, commit, push again AND submit by clicking the Turn-in button on this page.
Consider opting in for the CP1 Showcase (checkbox during GitGrade submission process)
and update showcase-options.txt
with the files you want to include/exclude.
Don't forget about the Late Day Challenge!
Click Turn-in by Due Date?:
Click Turn-in between Due Date and Lock Date?:
Click Turn-in after Lock Date?:
Please make sure any file names you reference (e.g. html, css, images) in your HTML are all-lowercase (and have no spaces). Windows will ignore casing, but they will appear as broken links on other systems!
Example, if you have an image under "img/pupper.jpg" relative to your HTML file, the first image will render but the second will appear broken on non-Windows systems:
<img src="img/pupper.jpg" alt="my pupper!" />
<img src="img/pupper.JPG" alt="my pupper!" />
"Division" elements that act as containers solely for styling purposes
Generic containers, do not come with any semantic meaning
div
s are block elementsspan
s are inline elementsReview: What's the difference between block and inline?
May also be used in other semantic tags but must relate to the parent content
<section>
<header>...</header>
</section>
section
elements should almost always have a header
, but a h1-h6 heading is also sufficient.
<blockquote>
<p>
The Web does not just connect machines, it connects people.
</p>
<footer>Tim Berners-Lee</footer>
<blockquote>
nav
Barnav
is often used for a list of navigational links on web pages
nav
BarWhat's wrong with the following choice of p
tags?
<nav>
<p><a href="#">Home</a></p>
<p><a href="koala.html">Koala-tee Facts</a></p>
</nav>
Some students try to use h1
or p
for
nav
element links. Remember that
HTML elements should be chosen based on the meaning of their content,
not on the appearance!
Review: Is the link above an example of a relative or an absolute link?
nav
Solution<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="koala.html">Koala-tee Facts</a></li>
</ul>
</nav>
But what if you don't want the bullet list appearance?
That's where CSS comes in!
nav li {
display: inline;
list-style-type: none;
}
<head>
<meta charset="utf-8" />
<link href="koala-styles.css" rel="stylesheet" />
<title/>Koala Fan Page</title>
</head>
CSS is used to select elements and define styles to change the appearance and layout of information on a web page
Every web browser has its own defaults for different elements (most give headings bold weight, bullets to lists, etc.)
A project using different CSS templates to style the same HTML page.
CSE154 April Fools Theme.
Selectors designate exactly which element(s) to apply styles to
Properties determine the styles that will be applied to the selected element(s) (background-color
, font-size
, etc.)
Each property has a set of values that can be chosen (e.g. 14pt value for font-size property)
There are currently over 200 possible style properties to choose from - use the Chrome inspector for useful autocomplete of property values!
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 |
A great MDN reference page for different color values here!
The Chrome inspector gives you a useful feature to look at different color code representations (e.g. HEX, RGB) as well as opacity settings, eyedropper, etc.)
When you are looking to choose colors for you page, this can be useful to see the effects real-time (then copy/paste the code to set the color in your CSS).
font-family
h4 {
font-family: "Courier New", monospace;
}
p {
font-family: Georgia, serif;
}
This paragraph uses the second style above
Enclose multi-word 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
+
button for each<style>
and </style>
tags.css
document (see example here)Demo
Copy/paste the text inside of the style
tags - you should put this at the top of your external CSS stylesheet
Then use the rules shown, which include the default font specified for your chosen font(s).
Paste the code into your .css
document (see example here)
<head>
...
<link href=styles.css" rel="stylesheet" />
...
</head>
body {
background-color: #AED581;
}
The link
tag is used to define the relationship between HTML and CSS
Use the rel
attribute with a value "stylesheet"
to specify this relationship
Use the href
attribute to identify a path to the CSS file
<head>
...
<link href="styles.css" rel="stylesheet" />
...
</head>
... rest of HTML
<head>
...
<link href="styles.css" rel="stylesheet" />
<link href="koala-styles-custom.css" rel="stylesheet" />
...
</head>
... rest of HTML
body {
background-color: #AED581;
}
Multiple HTML files can share the same CSS, and one HTML file can have multiple CSS files (loaded in order)
Q: What do you think would happen if you have a body style for both styles.css
and koala-styles-custom.css
?
All styles "cascade" from the top of the sheet to the bottom
Another way to include style in your web page, a style sheet in your
<head>
DO NOT DO THIS, but you may see this in code you find online. Why is this poor code quality (think about a more complex website)?
<head>
<link rel="stylesheet" href="precedence.css">
<style>
<!-- internal style sheet, the linked style has color: purple rule -->
p { color: green; }
</style>
</head>
<body>
<p>This is another paragraph</p>
</body>
This is a paragraph
Internal style takes precedence whether it is before or after the linked style in the head
style
attribute (BAD)This is also bad code quality; DO NOT DO THIS
<p style="font-family: sans-serif; color: red">
This is a paragraph
</p>
You will unfortunately see this on some sites like W3Schools
It does render as you would expect though...
This is a paragraph
You may find some resources online helpful to explore different ways to use CSS properties - there are a ton of things! But some are better than others (make sure you understand why these examples are poor use of HTML and CSS).
Understanding good code quality can be extremely valuable in navigating an overwhelming amount of resources on the web today.
A good discussion.
We choose resources that best align with our code quality guidelines, while giving just enough "extra detail" into topics we cover in lecture/section/lab. That said, let us know if you're looking for recommendations on a specific resource!
Remember that all of your work must be your own, and cite any tutorials/resources you may be using to explore features you may be exploring.
Used to select specific HTML elements in CSS
Classifcation | Description | Example |
---|---|---|
Type Selector | Selects an element type | p |
Class Selector | Selects all elements with the given class attribute value | .koala-face |
ID Selector | Selects the with the unique id attribute value | #scientific-name |
NOTE: * selector by itself is bad style (except when used in combination).
body {
background-color: #AED581;
}
Selects an HTML element
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)
id
and class
id
id
can only appear once per pageid
class
class
class
esSee the Pen Class and ID example by Melissa Hovik (@krimsonkoi) on CodePen.
Both paragraphs have the same class (product
), but each has its own ID
<h2>
<span class="koala-face">(*'0'*)</span> Koala Facts! <span class="koala-face">(*'0'*)</span>
</h2>
.koala-face {
font-family: "Roboto Mono", monospace;
}
This is a good use of classes because the styles are shared by both spans
This is a good use of a span because it is used only for styling an inline region of content
<figcaption id="scientific-name">Phascolarctos cinereus</figcaption>
figcaption {
font-weight: bold;
text-align: right;
}
#scientific-name {
font-style: italic;
}
This is a good use of an ID because we don't want to italicize both figcaptions
section {
background-color: #222222; /* dark gray background */
}
header, article, aside, footer, p {
background-color: #E8E8E8; /* light gray background */
}
A ruleset can select multiple elements separated by commas
The individual elements can also have their own styles (like
section
above)
Combinators use the HTML document (DOM) hiearchy to select elements
Classification | Description | Example |
---|---|---|
Descendent Combinator: A B | Selects all B descendents somewhere inside A elements | ul li |
Child Combinator: A > B | Selects all B descendents directly inside A elements | ul > li |
<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>
How would we apply uppercase styling to all strong
elements that are...
li
elements?li
elements?A B
See the Pen Descendent Combinator by Melissa Hovik (@mehovik) on CodePen.
A > B
See the Pen Child Combinator by Melissa Hovik (@mehovik) on CodePen.
This page has a header at the top of the page, and in the Koala Facts section. How could we italicize only the "A Koala-tee Webpage" header directly in the body
, without adding an id?
<body>
<header>
<h1>A Koala-tee Webpage</h1>
</header>
<main>
<aside> <!-- Left sidebar --> </aside>
<section>
<header>
<h2>*('0')* Koala Facts! *('0')*</h2>
</header>
<!-- Koala fact paragraphs -->
<article>
<!-- Koala art gallery -->
</article>
</section>
</main>
<footer><!-- Image citations --></footer>
</body>
body > header {
font-style: italic;
}
id
or class
?
Prefer type selectors over classes and ids when you can - we often style our page elements
based on their purpose in the document (e.g. all section
s might share the same background)
If you have multiple different elements that share styles, then you might consider using a class.
Only use an id
for unique elements that are not already uniquely identified by an element (body
should not have an id since it is always unique).
Extra Practice from Thursday's section
CSBS selector mystery (from past CSE 154 exam)
http://toolness.github.io/css-selector-game/: a great game to practice selectors
https://flukeout.github.io: another game to select "plate" elements with selectors
Use the W3C validators for HTML and CSS (copy/paste option is recommended)
Review the Code Quality Guide for HTML/CSS, and check the spec carefully to make sure you've met all of the requirements
Use Piazza to ask questions
Have fun and be creative!
Remember to add, commit, push and then click the Turn-In button in order to submit your final version