1. HTML
Table of Contents
Markup Languages
The most basic kind of a file is a .txt file which simply stores a string of characters. A .txt file is not considered "code" because its characters are interpreted literally.
A markup language on the other hand contains symbols that will not be interpreted literally and instead influence how the text inside is viewed.
Markdown (written in .md files) is considered a markup language. For example, when we view ("render") the double-asterisk symbols ** surrounding a word, the text inside will become bold:
Here is some **bold** text.Similarly, HTML (Hypertext Markup Language) is a markup language and our browsers are the applications we use to view them.
<h1>Hello World!</h1>
<p>This is some <strong>bold</strong> text</p>To illustrate the difference between markup languages and regular .txt files, do the following:
In your
unit-2folder, create a new folder calledintro-to-htmlCreate two files called
index.txtandindex.htmlPut the code above inside both of them:
Right click on the
index.htmlfile and select Reveal in Finder/ExplorerOpen Chrome. Then drag and drop the
index.htmlinto the tab bar (there should be a + icon)Then do the same for your
index.txtfile.
What is the difference between how they are presented by Chrome?

At the end of the day, your browser is essentially built to render text and images.
The browser can interpret
.htmlfiles and render content with style. It can also execute JavaScript!The first HTML file you should create for a new website should always be
index.htmlindex.htmlis a magic name that servers will automatically look for if a user enters a domain without a file extension:test.comis the same astest.com/index.html.
index.htmlis commonly known as the entry point
The file:// protocol vs the http:// protocol
file:// protocol vs the http:// protocolWhen you open an HTML file directly to view in your browser, your browser uses the file:// protocol to retrieve the .html file from your file system.

Most of the time, your browser will be using the
https://protocol to get the.htmlfile from a server.A server is just another computer, somewhere else, that provides a resource to another computer (over the internet in this case)
HTTPS (Hypertext Transfer Protocol Secure) is how your browser communicates with a server.

How to write HTML
HTML (Hypertext Markup Language) is the language used to build webpages. It describes content and structure of a webpage. In other words, what to display and where/how to display it.
HTML elements are created using a pair of opening and closing tags:
<!-- This is a "heading" -->
<h1>My awesome website</h1>
<!-- This is a "paragraph" -->
<p>My hobbies are...</p>
<!-- This is an "unordered list", a.k.a. a bullet list -->
<ul>
<!-- "list items" are nested inside the ul -->
<li>coding</li>
<li>soccer</li>
<li>cooking</li>
</ul>We create a comment with the syntax
<!-- comment -->Notice the opening tag (
<tagName>) and the closing tag (</tagName>) for each HTML elementThe tag name determines how the content inside them is displayed (e.g. as a heading, a paragraph, or a bulleted list)
Only the content between the tags is displayed to the user. The angle brackets (
<>) and the tag name are not displayed.
Nesting Elements
As you may have noticed with the ul element, tags can nest other tags inside them.
An element that is commonly used to contain other elements is the <div> element (short for "division").
<div>
<p>This is nested inside the division</p>
<p>We can put as much stuff in here as we want!<p>
<div>
<p>And nest quite deeply!</p>
</div>
</div>Nested elements should always be indented one level for readability.
Other examples of this are the ordered list and unordered list tags and their list items:
<!-- An ordered list is rendered with numbers -->
<h2>Steps to make dinner</h2>
<ol>
<li>Get freezer meal</li>
<li>Put in microwave</li>
<li>Eat</li>
</ol>
<!-- An unordered list is rendered with bullets -->
<h2>Pets</h2>
<ul>
<li>Dogs</li>
<li>Cats</li>
<li>Fish</li>
</ul>We refer to the element acting as the "container" as the parent and the elements nested inside as the children. Tags that are in the same nesting level next to each other are sibling tags.
Links and Attributes
One of the most basic elements in a website is a link to another page. We create a link element (a.k.a. an "anchor") using the <a> tag:
<!-- The href attribute goes in the opening tag -->
<a href="https://google.com">Go to Google</a>Notice that this tag contains extra code inside of the opening <a> tag! href stands for "hyperlink reference" and is an "attribute". Attributes provide additional piece of information used to render an element and are included in the opening tag.
In this case, the content between the tags is what the user will see while the
hrefattribute tells the browser where to redirect the user after clicking on the link.
Links can take you to online sites, local or absolute pages of our own, or even reference elements within the page (they link to ids)
<a href="https://google.com">Go to Google</a>
<a href="./">Go Home</a>
<a href="./about">Our About Page</a>
<a href="#more-information">See more information</a>Be careful with relative links:
"/"is the root of the server which may not be where you mean.Local links
./,../will direct you from your current directory.Links can also be nested, but will still flow seamlessly in the final document.
<p> If you're looking for things <a href="https://google.com">Go to Google</a>, and you'll see ads</p>Link text should always be descriptive of where you're going, never just put "click here."
Images and Self-Closing Tags
Another common element type is an image. Images have one required attribute src and an optional attribute alt
<img
src="https://http.cat/images/415.jpg"
alt="cat eating a record"
/>Notice that the image element doesn't require a closing bracket. It is considered a self-closing tag.
srcis the source location of the imagealtdescribe the picture to screen readers.If the content is central to your site, you must include an
altattributeIf the picture is just a decoration (like a background image) you do not need an
altattribute.Be specific with your
alttext.
src can be a hyperlink (a link to another web page) or a local link:
<img src="https://http.cat/images/415.jpg" alt="A cat eating a record" />
<img src="./images/lecture-prep/images/cat-pizza.png" alt="cat eating a pizza" />A cooler version of an image with a caption is
figure, look it up!
<figure>
<img src="./images/lecture-prep/images/cat-pizza.png" alt="cat eating a pizza" >
<figcaption>Here we see a cat eating a pizza it does not deserve.</figcaption>
</figure>Ids and Classes
Two attributes that we can apply to ANY element are id and class. These allow us to label elements.
ids mark a tag as a single, unique, important item on your page.classes are for denoting a bunch of related tags, and can appear more than once per page.Both
ids andclasses are used to reference elements on pages so that they can be accessed, either by JS or CSS.They are a way to differentiate tags on your page from other identical tags.
<h1 id="main-heading">Hello world!</h1>
<p class="blue-text">lorem ipsum</p>
<p>lorem ipsum</p>
<p class="blue-text">lorem ipsum</p>
<p>lorem ipsum</p>idandclassnames are case sensitive, cannot start with numbers, and can't have spaces.Stylistically, in pure html, you'll see
kebab-casenames.You can have multiple classes by separating with spaces, but only one id.
You can include both an id and class on a single tag.
<p id="important-text" class="blue funky-font">
some text
</p>id= one name per page, one per tagclass= multiple per page, multiple space separated names per tag
Properly formatted document
The way that HTML works is it's a "document" filled with "tags".
Let's start simple with some boilerplate to set up our document
<!DOCTYPE html>
<html lang="en">
<!-- Everything else goes here -->
</html>These two tags make sure our browser knows what we're dealing with.
Just copy them in without worrying.
There are other doctypes and languages, but this is what we'll use
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>The
headtag is where we put meta information, scripts, links, and titles.The
titletag is the only one visible to our users as what appears in the browser tag.metadoes other things like tell the mobile browser how big to start the page and what character set we're using.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>The
bodytag is where we can put things for our users to see.From here on out, always include the boilerplate above
VS Code users: start with an empty document, type
htmland then from the popup, selecthtml:5and hit Enter
Create a new HTML file and type in html:5
Semantic vs Non Semantic Elements
So far, all the tags we've made were semantic — their name told us about their function:
htags are headings,aare links,pare paragraphs.However, there's another kind of tag that conveys no meaning: non-semantic.
Pretty much the only non-semantic tags you'll see are
divandspan. These are just for grouping other related tags together (div) or calling out a particular section of text (span).
<div id="main-user-info">
<p><span class="name-text">Name:</span> Zo</p>
<p>Age: 24</p>
<p>Hobbies:
<ul>
<li>Basketball</li>
<li>Coding</li>
</ul>
</div>Non-semantic tags should only be used for grouping or grabbing parts of text.
<div>
<div>Hobbies</div>
<div class="hobby">Basketball</div>
<div class="hobby">coding</div>
</div>
<!-- NO AWFUL. NEVER. -->Quiz
Q: What is different about how the browser shows the .txt and .html files?
Q: Browsers are specialized "viewer" applications for .html files. What are other examples of applications that are made to view specific kinds of files?
Q: How do you think these different tags affect what is displayed?
Q: What is wrong with the HTML below?
<h1>
Hello World!
</h3>Q: When should you use <ol> and when should you use <ul>?
Q: In the example above, which elements are parents, which elements are children, and which elements are siblings?
Q: What is wrong with this HTML element?
<img>cat-pizza.png</img>Q: How would you create a <p> element with the id "caption-1" and the classes "italic" and "centered"?
Q: What page does this anchor tag take us to?
<a href="./">Go Home</a>Q: Which semantic tags should we use in the code snippet above?
Last updated