MWD Lesson 2 Handout
MWD Lesson 2 Handout
Lesson 2 Handout
An Introduction to HTML
ABOUT THIS HANDOUT
This handout includes the following:
CORE CONCEPTS
1. When we talk about HTML, we often use the term markup. Markup is tagged content. HTML elements
are made up of tags which typically (although not always) include an opening tag such as <p> and a
closing tag such as </p>.
2. HTML elements sometimes have attributes. Attributes contain information about the particular element.
Here is an example of the image element with a src attribute: <img src=”gouda.jpg”>. Attributes are only
applied to the opening tag. You will never see an attribute applied to the closing tag of an element.
3. In HTML, elements are nested within one another, and this create relationships between those
elements. We often use family metaphors in order to discuss how elements are related to one another,
such as “parent” and “child.” The relationship between nested elements is known as the “Document
Object Model” or (DOM).
4. Semantic HTML is the term used to describe documents that are constructed with meaningful and
appropriate tags, the goal of which is to provide more context to people who are reading them.
Additionally, HTML has a limited number of elements available for use, and sometimes we need to
extend the meaning of a particular element; in order to do this, we can define a “class” attribute—for
example <p class=”introduction”>.
5. URL stands for “Uniform Resource Locator”, commonly referred to as the “hyperlink.” Different types of
URLs include relative URLs, absolute URLs, and fragment identifiers.
6. The most common way to embed images in HTML is the use of the <img> tag. In recent years we have
also seen the addition of the “src” and “srcset” attributes for the <img> tag, and these give us more
options for providing the best image for the job when it comes to mobile devices and other contexts.
7. Adding media to your web page begins with the relatively new <video> and <audio> elements. These
elements allow you to embed video files such as the MP4 format and audio files such as the MP3
format. However, you will almost certainly need to provide additional fallback content to account for
different browsers and/or older browsers.
• Create a blank text document with an HTML extension, paste the text from the Wikipedia article you
chose into that document, and open it in a browser, and see how it looks.
• Return to the document and begin adding different elements like <html>, <head>, <body>, and
<title>. Save it, view that page in the browser and see how it’s changed.
• Go back and add some text-level semantics—for example <emphasis> or <i> or <b> or some links.
Continue refining the document and keep looking at the browser and see how it’s changed.
• When you’re happy with your file, go ahead and push that up to Github and post a link in the forum.
Your Github repository should look a little bit something like this, where you have an index.html
file in it.
• The HTML file will probably end up looking something along the lines of this. I look forward to seeing
you in the next lesson, where we’ll dive a little bit deeper into HTML5 block level elements and some
of the other options available to us.
RESOURCES
• File Format plugin that opens and saves 24-bit WebP images
http://telegraphics.com.au/sw/product/WebPFormat
• A presentation by Jenn Lukas on the various implications of dealing with images,
http://slideshare.net/JennLukas/lets-talk-about-responsive-images-and-performance
• Browser compatibility for media formats
https://developer.mozilla.org/en-US/docs/Web/HTML/Supported_media_formats#Browser_compatibility
• HTML elements belonging to the flow content category
https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Content_categories#Flow_content
• Hyperlinks
https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Hyperlink
• The HTML <img> element
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img
• The HTML <video> element
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video
• The HTML <audio> element
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio
Now, the first one we have here is the opening h1 tag. The
second one is the closing h1 tag. And what happens is that
the first tag tells the browser to start the element. And the
second tag tells it to finish the element.
Elements that only have one tag and have no content are
considered null elements. You might sometimes, when you’re
browsing other people’s websites and viewing the source of
their HTML, you may see some of those null elements with
a trailing slash inside. This is a carryover from the days of
XHTML, when we wanted to explicitly close every element.
So we would say that this element is self-closing because it
both starts and closes the element,
And don’t worry about fully understanding what this markup means right now, but here we have an image
element which contains two attributes. An src attribute with a value inside the quotation marks after the equal
sign. And then an alt attribute for the alternative text, which is inside of quotes as well.
Attributes are only applied to the opening tag. You will never
see an attribute applied in the closing tag of an element.
It has a type attribute saying that this is an email type field. It’s
got a name, which is the variable email_address, which will
be passed to the back end, and then we have the required
attribute, which indicates that this particular field is required in
order for the form that it’s inside of to be submitted.
In the first one we have our first two attributes with quotes
around them, and then the required attribute shorted. The
second one is the one we just saw were required=” “. And
then in the third example, all three attributes have no quotes
around the value of the attribute.
Now, in the second thing, the jump to the menu (an element),
you see the A is capitalized. The ID is capitalized. There’s
no quotes around nav-jump. And then there is single quotes
around value #menu.
In terms of organizational best practice, it’s just a good idea to pick an indentation style and stick with it. Different
organizations have different indentation styles. Some use spaces. Some use tabs.
There is no right way to indent except to indent. And to make sure that your team or the people that you’re
working with are consistent from document to document. Because again, coming in and being able to see a
consistent indent style makes it easier to edit files later on.
So if you like using two spaces to indent, great. If you like using a tab to indent, perfect. Pick a style and go
with it.
So here you can see the header element is the parent. And
it has two children, the h1 and the anchor element. Taken
together, these form the Document Object Model.
Now, when choosing elements within HTML to mark up our documents, we should choose elements that make
our content more meaningful. HTML is intended to make our documents more expressive. Here we have just a
normal passage of content from my cuttlefish page. And if we were to do this in the browser, this is what it would
look like. Straight, normal Times New Roman.
Now, if I wanted to start marking up the document I might decide to link a brown pigment to the Wikipedia article
on Sepia. And when I do that, now all of a sudden that becomes a link within the document. That’s what the
anchor tag does. And we’ll talk a little bit more about the anchor tag later on.
Taking things a step further, this entire paragraph is a paragraph. Therefore, I can wrap it in a p element. And
then that will insert some margins above and below this particular block of text.
All in all, HTML is intended to allow our documents to be more meaningful, to provide more context to people
who are reading them. This is what we refer to as semantic HTML.
With HTML, you can comment your code and leave little
notes for yourself, or for other people that might be editing
your document. In this markup example, we see a paragraph
followed by some other code that’s in green. Now, the code
that’s in green starts off with a less than symbol or an opening angle bracket, an exclamation point, and two
Now, as we’ve been looking at all of this markup, we have seen the opening closing and angle brackets or the
less than and greater than symbol frequently used with our tags and other markup-related bits. Since they’re
used so frequently in markup, these are characters that must be encoded, because if we just put them nakedly
in our document, the browser might think we’re trying to open an element.
So in this very brief chapter, we talked a little bit about commenting your HTML and character encoding. In the
next chapter, we’re actually going to dive into a lot more detail and talk about HTML document structure, the title
element, and meta tags. I’ll see you in a few minutes.
Title, for instance, is used for the document name, and this
is what is displayed in the title bar. So here we see in the
browser, it says “My Page Title.”
Now, the head element is also where we would put that meta
tag for defining the character set that we’re using for the
document. Typically, you want to put that before the title because that way you can use UTF-8 characters within
the title as well.
There are a wide variety of other meta elements available to you as well, and these meta elements are used
to provide supplementary information—or meta information—about the document. One example would be the
page author or perhaps some instructions for search engine spiders. Don’t worry about not understanding these.
I’m just giving you a couple of examples.
Another example of this is using the link element to indicate alternate versions or resources for the document. In
this example, I’m using a link element to point to the canonical reference for my website on cuttlefish. Since the
page content comes from the Wikipedia article on cuttlefish, I am referencing it within a link element and saying
that the relationship of that link to this page is that is the canonical reference for this page.
TEXT-LEVEL SYMANTICS
Welcome back. In this chapter, we’re going to start actually
looking at markup and how to markup text. We’ll be talking
specifically about text-level or phrasing semantics.
If you think about this being read aloud, you might think of
something like “despite their name, cuttlefish are not fish but
mollusks.” Now, the default display of this in a browser is to
make it bold. Using CSS we could make it look any other way
we want to, but that’s the default appearance in a browser.
Now in terms of the display in the browser, here you see that the emphasis element gets displayed in italics by
default. Now the I element, as we’ve discussed earlier, is used for text that’s in an alternate voice or mood, or
even an alternate language.
We can use these classifications for styling purposes, for scripting purposes, or in order to expose information
from the page to other technologies. In terms of the actual display, they still look like italics, but we’ve added a
little bit more semantic meaning to the document by indicating how these particular elements are operating.
Now in terms of best practices, it’s important to keep your class names meaningful. You don’t want to classify
something as big red text because down the road, maybe the design changes and now all of a sudden that is
small blue text. So if we use class names that are meaningful and actually have some semantic value to them,
then they will be able to persist, even if the actual display, the style of that particular element changes over time.
So let’s look at a new passage here. Here we have another passage from the cuttlefish website, and we see
a familiar friend in there, an I element with a class of taxonomy, so it shares that with the other two instances
of I that we saw earlier. But in this case, we’re dealing with a binomial, which is the genus and the species,
Sepia apama.
Now hopping over into the browser, what you see is that
this title information actually gets exposed as tool tips.
That’s pretty awesome. Now unfortunately, from an assisted
technology standpoint, that title information is not read out to
users, at least not reliably. Most screen readers ignore title
attributes unless specifically instructed to read those out.
We can wrap the cite element around the Journal of Experimental Biology, and then we have indicated that this
is a citation. We could take it a step further and perhaps classify this particular citation as being a journal name.
Or you could say a magazine, or if it’s a book title or a movie title, maybe you give it a classification based on
that, in case you want to style each one differently.
In terms of visible display, cite automatically is given italics in pretty much every browser. Now sometimes, when
you’re creating an HTML document, you might be discussing something like, well, HTML, as I’m doing here.
Now here’s an example of that. I’m a big fan of the title attribute, but I wish it was better supported by screen
readers. So here I’m discussing code, and this is where I could actually use the code element to indicate that
this is in fact code.
Again, I might want to take it a step further and classify this code that I’m talking about as being HTML. This is
just another example of how classification can further extend the semantics that are built into HTML, to indicate
the sort of content that’s being contained inside the element. The code element will be displayed in a monotype
font, usually a slab serif, so that it looks like text from a typewriter.
Here we have “An Introduction to Cuttlefish.” If I wanted to select a portion of this, I could use a span element.
This is used for arbitrary spans of text. So if I wanted to only show the word “introduction,” as opposed to “An
Introduction to Cuttlefish.” I could wrap the stuff that I don’t want to show in a span element. So here we see a
span element around “an” and the space, and a second span element around the space and “to cuttlefish.”
So I could just as easily say B class equals hidden, instead of span class equals hidden. Now I’ve used this
approach on the fascinating cuttlefish site in the navigation. Let’s look at that example. So here you see, if I
inspect the introduction navigation item and expand that link, you can see it actually says “An Introduction to
Cuttlefish,” even though visibly, all we see is the word “introduction.” that’s pretty cool.
In the next section, I’m going to talk a little bit about URLs, links, and file references, before we dive into more
organizational semantics.
Now, here’s another example of a relative URL. We have images/Sepia-latimanus.jpg. Now in this case, this is
an address for a document named Sepia-latimanus.jpg inside of a directory named images that’s in the same
directory as the current document. So again, looking at the file system, if we’re on the index page, we’re moving
into the images folder, and then to Sepia-latimanus. And notice that the case of the link, the capital S, actually
matches the capital S in the URL.
Now, another kind of relative URL that we have used is the dot forward slash. You don’t see this all that often,
but dot forward slash simply means start where this document is and then move. So in this case, ./physiology.
html is the same as simply saying physiology.html. ./images/Sepia-latimanus.jpg is the same as simply saying
images/Sepia-latimanus.jpg.
Where things get a little bit different is when you’ve got dot dot slash, as you see here. What dot dot slash
means is, start where you currently are, move up one level in the directory structure, and then look for the
document. So in this case, the file physiology.html should
exist one directory level above the document that’s
referencing it.
So in terms of the file system that we’ve got here, you might
have a file inside of videos, and it’s referencing physiology.
So in this case, it needs to move up a level because it’s in a
subfolder. Taking it a step further, here we have ../../images/
Sepia-latimanus.jpg.
Now, if we were to reference /images/Sepia-latimanus.jpg, this means that the image file that we’re referencing
would exist within an images folder, which is in the root of the document. So it doesn’t matter if we’re two levels,
five levels, 18 levels deep in the directory structure of the website; the image file is always within the images
folder in the directory root, just like that.
So absolute URLs are a little bit better because they’re more predictable. And it means that files are more
portable within the overall directory structure of the site. And you don’t need to keep track of how many dot dots,
and how many slashes, and all that sort of stuff. You just know that that first slash is always for the root, and
then you do the rest of the URL based on the directory structure of your site.
Now, another form of absolute URL actually includes the domain and the protocol that are being used to
reference the file. So if I say http://aaron-gustafson.com/i-heart-cuttlefish/physiology.html, that is going to point to
physiology.html within the i-heart-cuttlefish subfolder within the root of aaron-gustafson.com. And it’s referencing
it via http as the protocol.
Similarly, I can reference google.com with https, which is secure http. And that would point to whatever the index
file that’s defined by the server. That might be index.html. It might be index.php, default.asp. All web servers give
us a way of defining what that index file is. And whatever that file is named, that’s what will be served if you just
went to the domain by itself. By default, that’s typically index.html.
It’s worth noting that in some cases, you may see a URL that starts with two slashes. This is a particularly odd
one, and it’s something that’s only just starting to become more prevalent. This URL points to the physiology.
html file within the i-heart-cuttlefish subfolder of aaron-gustafson.com, but it doesn’t define the protocol for
accessing that URL. The protocol is left off and is simply implied by the protocol of the existing page.
So if the existing page that is linking to this particular URL is under https, so it’s a secure server connection, then
it would request this file over https. If it’s a standard http request, then this URL would be requested over http.
Again, you don’t see this all that often, but it is starting to crop up, which is why I wanted to point it out.
Now, there are two other kinds of links that I want to bring up
really quickly, and that’s mailto and tel links. These can be
used for pointing out email addresses or telephone numbers,
and browsers automatically will trigger your email client to
open if you click on a mailto link. If your browser is on a
device that supports making a phone call, clicking the tel link
will actually prompt the user to dial that number.
The reason to do this, apart from being concerned about having a valid HTML document, is that a screen reader,
when it comes to an image, will actually read out the word “image” if there is no alt text. If the alt text is empty, it
will simply skip over it. If there is alt text, it will read the alternative text instead of reading the word “image.”
Now, if you’re using Photoshop as your photo editor, you can actually choose how you export the photos and
look at the various ways that it compresses them down. You do that by choosing Save for Web. And then
it’ll give you a four-up grid of different images. And you can choose the different image formats and see how
big they are in terms of file size, as well as how long they would take to download over particular speeds of
connection. This can be really useful when trying to figure out the best way to save the image.
If you look in the lower left-hand corner, you can see that the JPEG compresses a bit smaller. And I can even
adjust the quality to be a little bit better in order to make the image look sharper. Now, in the lower right-hand
corner, I can turn it into a GIF. And that GIF is going to be a little bit more pixely because it only has a limited
number of colors available to it. And because it’s a photo, it’s going to be a little bit bigger than a JPEG is. But
both of them are a lot smaller than the 24-bit PNG is.
There are a lot of settings that you can adjust within Photoshop in order to figure out the best possible solution.
After all the tweaking, it looks like the JPEG is the best choice at 21.84K. And it’ll only take 9 seconds to
download, even over a 28.8 kilobits per second connection.
Now, if you want to be able to export the WebP format, there is an option for that. You can download an exporter
from telegraphics.com.au. And you’ll be able to export directly from Photoshop into the WebP format. But just,
again, be aware that WebP is not universally supported at this time, so it may not be the optimum image format
to use right now.
Web designer Jenn Lukas has put together an amazing talk available at this URL that you should take a look at.
I highly recommend that you check it out.
Now, earlier on in this lesson, I mentioned that HTML as a language has continued to evolve, and that it has
mechanisms that allow it to continue to evolve. Now, each of the different specs evolves over a different time
period. So HTML is sometimes slower, sometimes faster. CSS runs on its own track, JavaScript on its own track.
Everything doesn’t move in lockstep. It kind of moves at its own pace. But each of these languages continues to
evolve.
And so as part of that evolution, we’ve had the creation of adaptive images, which are for use within a
responsive context. So here we have a standard way of embedding an image. Well, as part of the picture spec
for responsive images within HTML5, we can define more options that apply in different contexts.
Now, I can further define how these images should be chosen using the sizes attribute. With sizes, I can actually
use a media query. And we’ll talk a bit more about media queries in the CSS lessons. But I can use a media
query and say that when there is a minimum width of 800 pixels, the image will be displayed at 400 pixels wide.
Otherwise, it will be at 50%. So that’s basically what that little bit inside of the sizes means.
And what that does is it provides a set of instructions to the browser to inform it how the image is going to
be used so that the browser can make the most appropriate decision as to which image should be loaded in
which context. So I’m going to hop over to the browser really quick just to show you a video of how this ends
up working. Here we see we’ve got the image with the cuttlefish small JPEG in it. And then I’m going to slowly
increase the width of the browser, and you’ll see that it will swap out which image is being used.
So now you see it has switched from cuttlefish small to simply cuttlefish.jpg. And if you mouse over it, you can
see that the natural size is 800 by 625, but it’s only being displayed at 120 by 94. Now, the reason that it’s using
this higher resolution image is that I’m actually capturing this on a Retina MacBook Pro, so the higher resolution
screen is recognized by the browser, and it swaps in the most appropriate image given the context.
Now, I mentioned that srcset and sizes are part of the picture
spec. The picture spec actually came out of the development
of the picture element, which is used similarly to srcset and
sizes, but specifically for art direction of images. Here we
see a sample picture element. We have the actual containing
element, the opening and closing picture tags. And then within
that, we have multiple sources that are available to us.
And each of those contain their own source sets, so you could
have multiple different resolutions in here, and then a media
query that’s associated with them. So you could even define
an image specifically for print if you wanted to. And the way that picture works is that the browser will choose the
first image that matches the given context.
And then last but not least, you’ll notice we’ve got an image
element in there. This is actually a requirement. And this is the
fallback, because as I’ve said multiple times, browsers ignore
what they don’t understand. So that image element becomes
a fallback. If the browser understands the picture element, the
image gets ignored. But if the browser does not understand
the picture element, then that image element actually gets
exposed and the picture element’s ignored.
And the HTML file will probably end up looking something along the lines of this. I look forward to seeing you
in the next lesson, where we’ll dive a little bit deeper into HTML5 block level elements and some of the other
options available to us.