This is a continuation from HTML 1.
(See p. 71) Be able to:
For displaying pictures in your web page. Example:
<img src="whale.jpg" alt="a blue whale" width="200" height="50"/>img tagsrc attribute specifies the location (URL) of the image filealt attribute gives alternate text
width and height attributes are recommended to speed up loading of the web page and avoid jitter (otherwise, the browser can’t figure out how much space to allocate for the images until the image files are downloaded from the web server).XML rules: attribute values should always be quoted, using "..." or '...' quotation marks (not “smart quotes”).
Here’s an example — to see the HTML code, use the browser’s view source command (Firefox: right click, view page source, or Ctrl-U):
table element
<table border="1">; we will use style to control the table border later. W3C HTML allows border="1" and border="", but WHATWG HTML considers the border attribute obsolete.1tr is a table row enclosing one or more th (table header) or td (table data) elementsExample:
<table>
<tr>
<th>Name</th>
<th>Color</th>
</tr>
<tr>
<td>Jack</td>
<td>Blue</td>
</tr>
<tr>
<td>Jill</td>
<td>Green</td>
</tr>
</table>Appears as:
| Name | Color |
|---|---|
| Jack | Blue |
| Jill | Green |
Let’s begin with an example.
Download and study the HTML file (web page) css-example.html.
The <link ...> element refers to a style sheet which is a separate file. This is much like an <a href> element, but instead of letting you click to go to another page, it includes the style rules from the other file into this page.
Some elements have an id attribute. This can be used to identify elements in the style rules. No two elements are allowed to have the same id value within a page!
Some elements have a class attribute. This also can be used to refer to elements in the style rules. But in this case, it is okay to have many elements with the same class value.
View the downloaded HTML file in your browser. It looks pretty plain. That’s because the style file referred to in the link element doesn’t exist.
Let’s create the style file, one rule at a time. Eventually we should have something like this: css-example.css.
See also CSS Notes.
The style language is called Cascading Style Sheets (CSS). The style describes the presentation desired for the document; usually this means appearance, but it could be sound, if the computer reads the document to a visually impaired person.
There are three ways to incorporate CSS into a web page:
Any element (at least any element which is going to be presented) may have a style attribute, which specifies one or more style properties and their values.
Example:
<p style="color: red; font-weight: bold;">He said WHAT?!</p>The head of the web page may contain one or more style elements containing embedded style sheets.
Example:
<head>
...
<style type="text/css">
INSERT STYLE RULES HERE
</style>
...
</head>These style elements contain style rules, which we will describe shortly.
The head of the web page may contain one or more link elements that hook up the web page to external style sheets.
Example:
<head>
...
<link rel="stylesheet" type="text/css" href="site-style.css"/>
...
</head>These external style sheets contain style rules, like those in style elements.
I usually prefer the third way, an external style sheet, because it is very easy this way for all the pages on a site to share a common style: just link them all to the same style sheet. And if you want to fix up the style, you only have to fix it up in one place.
Now, what are these style rules like? Here’s an example:
p { color: red;
background-color: black;
font-weight: bold;
}This rule begins with a selector, p, which says what kind of elements it will apply to.
There are many kinds of selectors, but the simplest kind is just the name of an element (here, p for paragraph).
Following the selector is a list of properties (name: value;), enclosed in curly braces ({, }).
Properties of elements of any sort.
color of the text or the background-color
red, navy, olive, etc.#ff0000rgb(255, 0, 0)hsl(120, 50%, 75%)border is described by its color, style, and width (thickness)
thin solid black, medium dashed blue, inset 5px hsl(30, 50%, 50%)thin, medium, thick, or a numerical length with units: in (inches), cm (centimeters), mm (millimeters), pt (points, 1/72 inch), pc (picas, 12 pt), px (pixels)Block display elements are those that get, generally, a rectangular display area. They have a “box.” Most of the elements we have covered are of the block display type. Exceptions are inline display elements (em, strong), and list-item display elements (li, etc.).
Here are some properties you can set for elements that are block displayed.
padding is extra space within the bounding box (think of a “padded cell”: the padding is within the cell to protect the patient)border lines at the bounding boxmargin is extra space outside the bounding box (separating the box from its container)Properties of text content or attributes.
text-align: left | right | center | justifytext-indent: length (can be negative or positive, absolute or percent)font-family: family1, family2, …
"Century Schoolbook L"font-style: normal | italic | obliquefont-size: absolute measure | relative measure | xx-small | x-small | small | medium | large | x-large | xx-large | larger | smallerfont-weight: normal | bold | ...text-decoration: underline | overline | ...text-transform: capitalize | uppercase | lowercase | nonefont-variant: small-caps | noneBesides the element-name selectors, there are many other kinds. We will consider just two more in this course: selecting elements by the attributes class and id (identity).
An HTML element can be given a unique ID attribute:
<p id="par1"> ...</p>
<p id="par2"> ...</p>Style rules can match the ID attribute with selectors such as:
p [id = par1] { ... }
p#par1 { ... }
[id = par2] { ... }
#par2 { ...}HTML element can be given a class attribute; multiple elements can share the same class value:
<p class="humor">A funny thing happened on the way to the forum ...</p>
<p class="humor">And the next day he said, ...</p>Style rules can match the class attribute with selectors such as
p [class = humor] { ... }
p.humor { ... }
[class = humor] { ... }
.humor { ...}Example: TO BE ADDED
This is a little tricky. In older versions of HTML, you could just write <img align="center" ...>, but in HTML5, the align attribute is no longer allowed.
The CSS text-align property only applies to text, so that won’t help.
It turns out that a CSS rule specifying display: block; margin: auto; does the trick. You can also specify a width, either as pixels (no units) or as a percent:
img {
display: block;
margin: auto;
}or
img {
display: block;
margin: auto;
width: 200;
}or
img {
display: block;
margin: auto;
width: 50%;
}<img src="URL" alt="TEXT" width="W" height="h"/><table>, <tr>, <th>, <td><style type="text/css>, <link rel="stylesheet" type="text/css" href="URL"/>Simon Pieters, ed., Differences from HTML4, dated 28 May 2013, retrieved 1 Feb 2014.↩