Professional Documents
Culture Documents
Preparing For HTML
Preparing For HTML
Now that you've learned about some of the most common HTML
elements, it's time to learn how to set up an HTML file.
HTML files require certain elements to set up the document properly. You
can let web browsers know that you are using HTML by starting your
document with a document type declaration.
In the future, however, a new standard will override HTML5. To make sure
your document is forever interpreted correctly, always include <!DOCTYPE
html>at the very beginning of your HTML documents.
The Head
So far you've done two things to set up the file properly:
We have added these elements to the Brown Bears page you previously
created. Now, let's also give the browser some information about the
page itself. We can do this by adding a <head> element.
The opening and closing head tags typically appear as the first item after
your first HTML tag:
<head> </head>
Page Titles
What kind of metadata about the web page can the <head>element
contain?
If you navigate to the Codecademy catalog and look at the top of your
browser, you'll notice the words Catalog | Codecademy, which is the title of
the web page.
1.
After the first paragraph, right above the heading that says Species, add a
link that says: Learn More. Don't add the URL just yet.
It's possible that one or more links on your web page link to an entirely
different website. In that case, you may want users to read the linked
website, but hope that they return to your web page. This is exactly when
the targetattribute is useful!
In this exercise, we've used the terminology "open in a new window." It's
likely that you are using a modern browser that opens up websites in
new tabs, rather than new windows. Before the advent of browsers with
tabs, additional browser windows had to be opened to view more
websites. The target="_blank"attribute, when used in modern browsers, will
open new websites in a new tab.
Before we learn how to link between internal pages, let's establish where
our files are stored. When making multi-page static websites, web
developers often store HTML files in the root directory, or a main folder
where all the files for the project are stored. As the size of the projects you
create grows, you may use additional folders within the main project
folder to organize your code.
project-folder/ |—— about.html |—— contact.html |—— index.html
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.
<a href="./contact.html">Contact</a>
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, Contactwill appear as a link.
A relative path is a filename that shows the path to a local file (a file on
the same website, such as ./index.html) versus an absolute path (a full URL,
like https://www.codecademy.com/learn/learn-html which is stored in a different
folder). The ./ in ./index.html tells the browser to look for the file in the
current folder.
Linking At Will
You've probably visited websites where not all links were made up of text.
Maybe the links you clicked on were images or some other form of
content.
So far, we've added links that were made up of only text, like the
following:
<a href="https://en.wikipedia.org/wiki/Opuntia" target="_blank">Prickly Pear</a>
Text-only links, however, would significantly decrease your flexibility as a
web developer!
Thankfully, HTML allows you to turn nearly any element into a link by
wrapping that element with an anchor element. With this technique, it's
possible to turn images into links by simply wrapping the <img> element
with an <a> element.
<a href="https://en.wikipedia.org/wiki/Opuntia" target="_blank"><img src="#"
alt="A red prickly pear fruit"/></a>
In the example above, an image of a prickly pear has been turned into a
link by wrapping the outside of the <img> element with an <a> element.
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:
<p id="top">This is the top of the page!</p> <h1 id="bottom">This is the bottom!
</h1>
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.
Whitespace
The rest of this lesson will focus on some tools developers use to make
code easier to interpret.
Both tools take advantage of the fact that the position of elements in a
browser is independent of the amount of whitespace or indentation in
the index.html file.
For example, if you wanted to increase the space between two paragraphs
on your web page, you would not be able to accomplish this by adding
space between the paragraph elements in the index.html file. The
browser ignores whitespace in HTML files when it renders a web page, so
it can be used as a tool to make code easier to read and follow.
Indentation
The second tool web developers use to make the structure of code easier
to read is indentation. The spaces are inserted using the space and tab bars
on your keyboard.
Comments
HTML files also allow you to add comments to your code.
1. They help you (and others) understand your code if you decide to
come back and review it at a much later date.
2. They allow you to experiment with new code, without having to
delete old code.
<!-- Favorite Films Section --> <p>The following is a list of my favorite
films:</p>
In this example, the comment is used to denote that the following text
makes up a particular section of the page.
<!-- <p> Test Code </p> -->
In the example above, a valid HTML element (a paragraph element) has
been "commented out." This practice is useful when there is code you
want to experiment with, or return to, in the future.
HTML Tags
You now know all of the basic elements and set-up you need to structure
an HTML page and add different types of content. With the help of CSS,
very soon you’ll be creating beautiful websites!
While some tags have a very specific purpose, such as image and video
tags, most tags are used to describe the content that they surround,
which helps us modify and style our content later. There are seemingly
infinite numbers of tags to use (many more than we've taught). Knowing
when to use each one is based on how you want to describe the content
of your HTML. Descriptive, well-chosen tags are one key to high-quality
web development. A full list of available HTML tags can be found in
Mozilla documentation.
Take some time to edit the workspace you created and observe how it
changes!
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<img src=https://s3.amazonaws.com/codecademy-
content/courses/learn-html/elements-and-structure/image-one.jpeg
><img/>
<p>NYFW can be both amazingly fun & incredibly overwhelming,
especially if you've never been. Luckily, I'm here to give you an insider's
guide and make your first show a pleasurable experience. By taking my
tips and tricks, and following your gut, you'll have an unforgettable
experience!
</p>
<img src=https://s3.amazonaws.com/codecademy-
content/courses/learn-html/elements-and-structure/image-two.jpeg>
<img/>
<p>If you're lucky or connected you can get an invite, sans the price
tag. But I wasn't so lucky or connected my first 2 years so I'm here to help
you out. First, plan out which shows are most important to you and make
a schedule and this is a biggie: SET A BUDGET. If you're worrying about
blowing your cash the whole time you won't have fun. Then check out
prices, days, and times and prioritize the designers you want to see most.
Lastly, purchase your tickets and get excited!</p>
<img src=https://s3.amazonaws.com/codecademy-
content/courses/learn-html/elements-and-structure/image-
three.jpeg><img/>
<ol>
</ol>
<p><strong>email</strong>: isa@fashionblog.com |
<strong>phone</strong>: 917-555-1098 | <strong>address</strong>:
371 284th St, New York, NY, 10001 </p>
</div>
</body>
</html>
SECOND PART.
TABLES
Create a Table
Before displaying data, you must first create the table that will contain the
data by using the <table> element.
<table> </table>
The <table> element will contain all of the tabular data you plan on
displaying.
Table Rows
In many programs that use tables, the table is already predefined for you,
meaning that it contains the rows, columns, and cells that will hold data.
In HTML, all of these components must be created.
The first step in entering data into the table is to add rows using the table
row element: <tr>.
<table> <tr> </tr> <tr> </tr> </table>
In the example above, two rows have been added to the table.
Table Data
Rows aren't sufficient to add data to a table. Each cell element must also
be defined. In HTML, you can add data using the table dataelement: <td>.
<table> <tr> <td>73</td> <td>81</td> </tr> </table>
In the example above, two data points (73 and 81) were entered in the one
row that exists. By adding two data points, we created two cells of data.
If the table were displayed in the browser, it would show a table with one
row and two columns.
Table Headings
Table data doesn't make much sense without titles to describe what the
data represents.
First, a new row was added to hold the three headings: a blank heading,
a Saturday heading, and a Sunday heading. The blank heading creates the
extra table cell necessary to align the table headings correctly over the
data they correspond to.
In the second row, one table heading was added as a row title: Temperature.
Note, also, the use of the scopeattribute, which can take one of two values:
1. row - this value makes it clear that the heading is for a row.
2. col - this value makes it clear that the heading is for a column.
HTML code for tables may look a little strange at first, but analyzing it
piece by piece helps make the code more understandable.
Table Borders
So far, the tables you've created have been a little difficult to read
because they have no borders.
Spanning Columns
What if the table contains data that spans multiple columns?
For example, a personal calendar could have events that span across
multiple hours, or even multiple days.
Spanning Rows
Data can also span multiple rows using the rowspan attribute.
1. The first row contains an empty cell and the two column headings.
2. The second row contains the Morning row heading, along with Work,
which spans two rows under the Saturdaycolumn. The "Relax" entry
spans three rows under the Sunday column.
3. The third row only contains the Afternoon row heading.
4. The fourth row only contains the Dinner entry, since "Relax" spans
into the cell next to it.
If you'd like to see how the browser interprets the code above, feel free to
copy and paste it into the code editor to understand it a little better.
Table Body
Over time, a table can grow to contain a lot of data and become very
long. When this happens, the table can be sectioned off so that it is easier
to manage.
Table Head
In the last exercise, the table's headings were kept inside of the table's
body. When a table's body is sectioned off, however, it also makes sense
to section off the table's headings using the <thead> element.
<table> <thead> <tr> <th></th> <th>Saturday</th> <th>Sunday</th> </tr> </thead>
<tbody> <tr> <th>Morning</th> <td rowspan="2">Work</td> <td
rowspan="3">Relax</td> </tr> <tr> <th>Afternoon</th> </tr> <tr> <th>Evening</th>
<td>Dinner</td> </tr> </tbody> </table>
In the example above, the only new element is <thead>. The table headings
are contained inside of this element. Note that the table's head still
requires a row in order to contain the table headings.
Table Footer
The bottom part of a long table can also be sectioned off using
the <tfoot> element.
<table> <thead> <tr> <th>Quarter</th> <th>Revenue</th> <th>Costs</th> </tr>
</thead> <tbody> <tr> <th>Q1</th> <td>$10M</td> <td>$7.5M</td> </tr> <tr>
<th>Q2</th> <td>$12M</td> <td>$5M</td> </tr> </tbody> <tfoot> <tr>
<th>Total</th> <td>$22M</td> <td>$12.5M</td> </tr> </tfoot> </table>
In the example above, the footer contains the totals of the data in the
table. Footers are often used to contain sums, differences, and other data
results.
You can use CSS to style tables just like you have done in the past.
Specifically, you can change style the various aspects mentioned above.
table, th, td { border: 1px solid black; font-family: Arial, sans-serif; text-
align: center; }
The code in the example above demonstrates just some of the various
table aspects you can style using the CSS properties you learned about
earlier.
HTML Tables
Great job! In this lesson, you learned how to create a table, add data to it,
and section the table into smaller parts that make it easier to read.
Intro to CSS
The basic structure of every web page, HTML, is very plain on its own. The
beautiful websites that you see across the internet are styled with a variety
of tools, including CSS.
Inline Styles
Although CSS is a different language than HTML, it's possible to write CSS
code directly within HTML code using inline styles.
If you'd like to add more than one style with inline styles, simply keep
adding to the styleattribute. Make sure to end the styles with a semicolon
(;).
<p style="color: red; font-size: 20px;">I'm learning to code!</p>
Fortunately, HTML allows you to write CSS code in its own dedicated
section with the <style> element. CSS can be written between opening and
closing <style> tags. To use the <style> element, it must be placed inside of
the <head>element.
<head> <style> </style> </head>
After adding a <style> tag in the head section, you can begin writing CSS
code.
<head> <style> p { color: red; font-size: 20px; } </style> </head>
The CSS code in the example above changes the color of all paragraph
text to red and also changes the size of the text to 20 pixels. Note how
the syntax of the CSS code matches (for the most part) the syntax you
used for inline styling. The main difference is that you can specify which
elements to apply the styling to.
Again, the details of the CSS syntax in the example above aren't important
at the moment. You will learn more about the details of CSS syntax in
later lessons.
You can create a CSS file by using the .css file name extension, like
so: style.css
With a CSS file, you can write all the CSS code needed to style a page
without sacrificing the readability and maintainability of your HTML file.
Linking the CSS File
Perfect! We successfully separated structure (HTML) from styling (CSS),
but the web page still looks bland. Why?
When HTML and CSS code are in separate files, the files must be linked.
Otherwise, the HTML file won't be able to locate the CSS code, and the
styling will not be applied.
You can use the <link> element to link HTML and CSS files together.
The <link> element must be placed within the head of the HTML file. It is a
self-closing tag and requires the following three attributes:
When linking an HTML file and a CSS file together, the <link>element will
look like the following:
<link href="https://www.codecademy.com/stylesheets/style.css" type="text/css"
rel="stylesheet">
Note that in the example above the path to the stylesheet is a URL:
https://www.codecademy.com/stylesheets/style.css
Specifying the path to the stylesheet using a URL is one way of linking a
stylesheet.
If the CSS file is stored in the same directory as your HTML file, then you
can specify a relative path instead of a URL, like so:
<link href="./style.css" type="text/css" rel="stylesheet">
Using a relative path is very common way of linking a stylesheet.
Tag Name
CSS can select HTML elements by using an element's tag name. A tag
name is the word (or character) between HTML angle brackets.
For example, in HTML, the tag for a paragraph element is <p>. The CSS
syntax for selecting <p>elements is:
p { }
In the example above, all paragraph elements will be selected using a
CSS selector. The selector in the example above is p. Note that the CSS
selector matches the HTML tag for that element, but without the angle
brackets.
In addition, two curly braces follow immediately after the selector (an
opening and closing brace, respectively). Any CSS properties will go inside
of the curly braces to style the selected elements.
Class Name
CSS is not limited to selecting elements by tag name. HTML elements can
have more than just a tag name; they can also have attributes. One
common attribute is the class attribute. It's also possible to select an
element by its class attribute.
So far, we've selected elements using only one class name per element. If
every HTML element had a single class, all the style information for each
element would require a new class.
Luckily, it's possible to add more than one class name to an HTML
element's class attribute.
ID Name
If an HTML element needs to be styled uniquely (no matter what classes
are applied to the element), we can add an ID to the element. To add an
ID to an element, the element needs an id attribute:
<h1 id="large-title"> ... </h1>
Then, CSS can select HTML elements by their id attribute. To select
an id element, CSS prepends the id name with a hashtag (#). For instance,
if we wanted to select the HTML element in the example above, it would
look like this:
#large-title { }
The id name is large-title, therefore the CSS selector for it is #large-title.
CSS classes are meant to be reused over many elements. By writing CSS
classes, you can style elements in a variety of ways by mixing classes on
HTML elements.
For instance, imagine a page with two headlines. One headline needs to
be bold and blue, and the other needs to be bold and green. Instead of
writing separate CSS rules for each headline that repeat each other's code,
it's better to write a .bold CSS rule, a .green CSS rule, and a .blueCSS rule.
Then you can give one headline the bold green classes, and the other
the bold blueclasses.