Dr. Greg Bernstein
August 22nd, 2021
What kinds of things, if any, have you styled? What techniques have you used, if any, for styling?
A rule consists of:
File FirstCSS.html
(our HTML file):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Our First CSS Example</title>
<link rel="stylesheet" href="first.css">
</head>
<body>
<h1>A CSS Example</h1>
<p>Quite ugly styling.</p>
</body>
</html>
File first.css
:
/* CSS comment, No end of line // coments. */
/* Rules for two different elements */
h1 {background-color: skyblue}
p {color: green}
Browser snapshot:
From MDN:
The browser converts HTML and CSS into the DOM (Document Object Model). The DOM represents the document in the computer’s memory. It combines the document’s content with its style. The browser displays the contents of the DOM.
From MDN:
There are three ways to style with CSS from best to worst(?) practice:
<link>
tag to reference it.<style>...</style>
element in the headstyle
attribute on an element.File SecondCSS.html
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Our Second CSS Example</title>
<style>
/* Inline CSS style sheet, with CSS comment */
h1 {background-color: skyblue}
p {color: green}
</style>
</head>
<body>
<h1>A CSS Example</h1>
<p>Quite ugly styling.</p>
<p>But we aren't worried about that yet.</p>
</body>
</html>
File ThirdCSS.html
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Our Third CSS Example</title>
</head>
<body>
<h1 style="background-color: skyblue">A CSS Example</h1>
<p style="color: green">Quite ugly styling.</p>
<p style="color: blue">Don't use inline styles
if you can help it. 😩</p>
</body>
</html>
Renders as:
right clicking
the reload button.From MDN:
CSS is a declarative language, which makes its syntax fairly easy and straight forward to understand. In addition, it also has a very nice error recovery system that allows you to make mistakes without breaking everything — for example declarations that aren’t understood are generally just ignored.
From MDN:
Important: If a property is unknown or if a value is not valid for a given property, the declaration is wholly ignored by the browser’s CSS engine.
From MDN:
There are more than 300 different properties in CSS and nearly an infinite number of different values. Not all pairs of properties and values are allowed; each property has a specific list of valid values defined for it.
You can look these up at MDN CSS reference.
From MDN:
From MDN:
From MDN:
@charset
and @import
(metadata)@media
or @document
(conditional information, also called nested statements, see below.)@font-face
(descriptive information)Some properties like font, background, padding, border, and margin are called shorthand properties — this is because they allow you to set several property values in a single line…
border
when I want to draw a border around an element for debugging purpose.From MDN:
From MDN:
Pseudo-elements: Match one or more parts of content that are in a certain position in relation to an element, for example the first word of each paragraph, or generated content appearing just before an element.
Combinators: … ways of combining two or more selectors in useful ways for very specific selections. So for example, you could select only paragraphs that are direct descendants of divs, …
<
or >
<ol>
becomes just olExample typeSelectors.css
:
h1 {font-family: sans-serif;
color: darkblue}
p {background-color: bisque}
strong {background-color: coral}
em {background-color: aqua}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Type Selector Example</title>
</head>
<body>
<h1>CSS Type Selector Example</h1>
<p >Quite ugly styling. But that is <strong>not</strong> the
<em>point</em> right now</p>
</body>
</html>
How can we add the example styles to the example web page just shown?
A number of designers have taken up the challenge to making nice CSS “frameworks” based only on tag/type selectors (and semantic HTML)
<ul>
<li class="first done">Create an HTML document</li>
<li class="second done">Create a CSS style sheet</li>
<li class="third">Link them all together</li>
</ul>
.first {
font-weight: bold;
}
/* All the elements with the class "done" are strike through */
.done {
text-decoration: line-through;
}
<p id="polite">"Good morning everyone."</p>
<p id="rude">"Don't bug me until I've had my tea!"</p>
#polite {
font-family: cursive;
}
#rude {
font-family: monospace;
text-transform: uppercase;
}