Document Type Declaration
HTML files require certain elements to set up the document properly. We can let web browsers know
that we are using HTML by starting our document with a document type declaration.
The declaration looks like this:
This declaration is an instruction, and it must be the first line of code in your HTML document.
It tells the browser what type of document to expect, along with what version of HTML is being
used in the document.
The html tag
The DOCTYPE declaration provides the browser with two pieces of information (the type of document and
the HTML version to expect), but it doesn’t actually add any HTML structure or content.
To create HTML structure and content, we must add opening and closing
html tags after declaring
:
Anything between the opening and closing
html tags will be interpreted as HTML code.
Without these tags, it’s possible that browsers could incorrectly interpret your HTML code.
Linking to Other Web Pages
One of the powerful aspects of HTML (and the Internet), is the ability to link to other web pages.
You can add links to a web page by adding an anchor element
(a) and including the text of
the link in between the opening and closing tags.
In the example above, the
href (hyperlink reference) attribute has been set to the value
of the URL
https://www.wikipedia.org/
Opening Links in a New Window
The target attribute specifies how a link should open.
For a link to open in a new window, the target attribute requires a value of _blank. The target attribute
can be added directly to the opening tag of the anchor element, just like the href attribute.
In the example above, setting the target attribute to "_blank" instructs the browser to open the relevant
Wikipedia page in a new window.
Linking to Relative Page
Thus far you have learned how to link to external web pages. Many sites also link to internal web pages
like Home, About, and Contact.
Before we learn how to link between internal pages, let’s establish where our files are stored.
The example above shows three different files —
about.html, contact.html, and index.html in one folder.
HTML files are often stored in the same folder, as shown in the example above. If the browser is currently
displaying index.html, it also knows that about.html and contact.html are in the same folder. Because the
files are stored in the same folder, we can link web pages together using a relative path.
In this example, the
a tag is used with a relative path to link from the current HTML file to the
contact.html file in the same folder. On the web page, Contact will appear as a link.
The
./ in
./contact.html tells the browser to look for the file in the current folder.
Linking to Same Page
How do we make it easier for a user to jump to different portions of our page?
When users visit our site, we want them to be able to click a link and have
the page automatically scroll to a specific section.
In order to link to a target on the same page, we must give the target an
id, like this:
In this example, the
p element is assigned an id of “top” and the
h1 element is assigned “bottom.”
An id can be added to most elements on a webpage.
An id should be descriptive to make it easier to remember the purpose of a link. The target link is a
string containing the # character and the target element’s id.
In the example above, the links to
p id="top" and
h1 id="bottom" are embedded in an ordered list.
An id is especially helpful for organizing content belonging to a div!