HTML
HTML
HTML
The Nile River is the longest river in the world, measuring over
6,850 kilometers long (approximately 4,260 miles).
Line Breaks
The line break element is unique because it is only composed
of a starting tag. You can use it anywhere within your HTML code
and a line break will be shown in the browser.
The code in the example above will result in an output that looks like
the following:
Unordered Lists
In HTML, you can use an unordered list tag (<ul>) to create a list of
items in no particular order. An unordered list outlines individual list
items with a bullet point.
The <ul> element should not hold raw text and won’t automatically
format raw text into an unordered list of items. Individual list items
must be added to the unordered list using the <li> tag. The <li> or
list item tag is used to describe an item in a list.
<ul>
<li>Limes</li>
<li>Tortillas</li>
<li>Chicken</li>
</ul>
In the example above, the list was created using the <ul> tag and all
individual list items were added using <li> tags.
Limes
Tortillas
Chicken
Ordered Lists
Ordered lists (<ol>) are like unordered lists, except that each list item
is numbered. They are useful when you need to list different steps in
a process or rank items for first to last.
You can create the ordered list with the <ol> tag and then add
individual list items to the list using <li> tags.
<ol>
<li>Preheat the oven to 350 degrees.</li>
<li>Mix whole wheat flour, baking soda, and salt.</li>
<li>Cream the butter, sugar in separate bowl.</li>
<li>Add eggs and vanilla extract to bowl.</li>
</ol>
Image Alts
The alt attribute, which means alternative text, brings
meaning to the images on our sites. The alt attribute can be
added to the image tag just like the src attribute. The value
of alt should be a description of the image.
If the image on the web page is not one that conveys any meaningful
information to a user (visually impaired or otherwise),
the alt attribute should be left empty.
Videos
In addition to images, HTML also supports displaying videos. Like
the <img> tag, the <video> tag requires a src attribute with a link to the
video source. Unlike the <img> tag however, the <video> element
requires an opening and a closing tag.
In this example, the video source (src) is myVideo.mp4 The source can be
a video file that is hosted alongside your webpage, or a URL that
points to a video file hosted on another webpage.
After the src attribute, the width and height attributes are used
to set the size of the video displayed in the browser.
The controls attribute instructs the browser to include basic
video controls: pause, play and skip.
<!DOCTYPE html>
<!DOCTYPE html>
<html>
</html>
Anything between the opening <html> and closing </html> tags will
be interpreted as HTML code. Without these tags, it’s possible that
browsers could incorrectly interpret your HTML code.
The Head
The <head> element contains the metadata for a web page. Metadata
is information about the page that isn’t displayed directly on the web
page. Unlike the information inside of the <body> tag, the metadata in
the head is information about the page itself.
<head>
</head>
Page Titles
If you navigate to the Codecademy catalog and look at the top of your
browser, you’ll notice the words All Courses & Tutorials |
Codecademy, which is the title of the web page.
<!DOCTYPE html>
<html>
<head>
<title>My Coding Journal</title>
</head>
</html>
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 target attribute is useful!
<a href="https://en.wikipedia.org/wiki/Brown_bear"
target="_blank">The Brown Bear</a>
Many sites also link to internal web pages like Home, About, and
Contact.
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, Contact will appear as a link.
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.
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="https://www.Prickly_Pear_Closeup.jpg" 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:
<ol>
<li><a href="#top">Top</a></li>
<li><a href="#bottom">Bottom</a></li>
</ol>
In the example above, the links to <p id="top"> and <h1 id="bottom"> are
embedded in an ordered list. These links appear in the browser as a
numbered list of links. An id is especially helpful for organizing
content belonging to a div!
Whitespace
<body>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
</body>
This example is easier to read, because each element is on its own
line. While the first example required you to read the entire line of
code to identify the elements, this example makes it easy to identify
the body tag and two paragraphs.Par
Comments
Comments begin with <!-- and end with -->. Any characters in
between will be ignored by your browser.
<!-- This is a comment that the browser will not display.
-->
HTML TABLES
<table>
</table>
The <table> element will contain all of the tabular data we
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
data element: <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
To add titles to rows and columns, you can use the table
heading element: <th>.
The table heading element is used just like a table data element,
except with a relevant title. Just like table data, a table heading
must be placed within a table row.
<table>
<tr>
<th></th>
<th scope="col">Saturday</th>
<th scope="col">Sunday</th>
</tr>
<tr>
<th scope="row">Temperature</th>
<td>73</td>
<td>81</td>
</tr>
</table>
What happened in the code above?
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.
When rendered, this code will appear similar to the image below:
Note, also, the use of the scope attribute, which can take one
of two values:
Table Borders
You can achieve the same table border effect using CSS.
table, td {
border: 1px solid black;
}
The code in the example above uses CSS instead of HTML to show
table borders.
<table>
<tr>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
</tr>
<tr>
<td colspan="2">Out of Town</td>
<td>Back in Town</td>
</tr>
</table>
In the example above, the data Out of Town spans
the Monday and Tuesday table headings using the value 2 (two
columns). The data Back in Town appear only under
the Wednesday heading.
Spanning Rows
<table>
<tr> <!-- Row 1 -->
<th></th>
<th>Saturday</th>
<th>Sunday</th>
</tr>
<tr> <!-- Row 2 -->
<th>Morning</th>
<td rowspan="2">Work</td>
<td rowspan="3">Relax</td>
</tr>
<tr> <!-- Row 3 -->
<th>Afternoon</th>
</tr>
<tr> <!-- Row 4 -->
<th>Evening</th>
<td>Dinner</td>
</tr>
</table>
In the example above, there are four rows:
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 Saturday column. 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.
Table Body
<table>
<tbody>
<tr>
<th></th>
<th>Saturday</th>
<th>Sunday</th>
</tr>
<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, all of the table data is contained within a table
body element.
Table Head
When a table’s body is sectioned off, however, it also makes sense to
section off the table’s column headings using
the <thead> element.
<table>
<thead>
<tr>
<th></th>
<th scope="col">Saturday</th>
<th scope="col">Sunday</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Morning</th>
<td rowspan="2">Work</td>
<td rowspan="3">Relax</td>
</tr>
<tr>
<th scope="row">Afternoon</th>
</tr>
<tr>
<th scope="row">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>
HTML FORMS
How a Form Works
Text Input
If we want to create an input field in our <form>, we’ll need the help of
the <input> element.
The first value for the type attribute we’re going to explore is "text".
When we create an <input> element with type="text", it renders a text
field that users can type into. Note that the default value
of type is "text". It’s also important that we include
a name attribute for the <input> — without the name attribute,
information in the <input> won’t be sent when the <form> is submitted.
We could also assign a default value for the value attribute so that
users have a pre-filled text field when they first see the rendered form
like so:
Adding a Label
The <label> element has an opening and closing tag and displays text
that is written between the opening and closing tags. To associate
a <label> and an <input>, the <input> needs an id attribute. We then
assign the for attribute of the <label> element with the value of
the id attribute of <input>, like so:
Look, now users know what the <input> element is for! Another
benefit for using the <label> element is when this element is
clicked, the corresponding <input> is highlighted/selected.
Add Placeholder Text to a Text Field
Placeholder text is what is displayed in your input element before your
user has inputted anything.
Password Input
Think about all those times we have to put sensitive information, like
a password or PIN, into a <form>. Luckily, we have
the type="password" attribute for <input>!
An <input type ="password"> element will replace input text with another
character like an asterisk (*) or a dot (•). The code below provides an
example of how to create a password field:
<form>
<label for="user-password">Password: </label>
<input type="password" id="user-password" name="user-
password">
</form>
After a user types into the field, it would look like:
Even though the password field obscures the text of the password,
when the form is submitted, the value of the text is sent. In other
words, if “hunter2” is typed into the password field, “user-
password=hunter2” is sent along with the other information on the
form.
Number Input
We’ve now gone over two type attributes for <input> related to text.
But, we might want our users to type in a number — in which case we
can set the type attribute to… (you guessed it)… "number"!
By setting type="number" for an <input> we can restrict what users type
into the input field to just numbers (and a few special characters
like -, +, and .). We can also provide a step attribute which creates
arrows inside the input field to increase or decrease by the value of
the step attribute. Below is the code needed to render an input field
for numbers:
<form>
<label for="years"> Years of experience: </label>
<input id="years" name="years" type="number" step="1">
</form>
Which renders:
Range Input
<form>
<label for="volume"> Volume Control</label>
<input id="volume" name="volume" type="range" min="0"
max="100" step="1">
</form>
The code above renders:
In the example above, every time the slider moves by one, the value
of the <input>‘s value attribute changes.
This practice has two new elements and an attribute that you may not
be familiar with.
Checkbox Input
<form>
<p>Choose your pizza toppings:</p>
<label for="cheese">Extra cheese</label>
<input id="cheese" name="topping" type="checkbox"
value="cheese">
<br>
<label for="pepperoni">Pepperoni</label>
<input id="pepperoni" name="topping" type="checkbox"
value="pepperoni">
<br>
<label for="anchovy">Anchovy</label>
<input id="anchovy" name="topping" type="checkbox"
value="anchovy">
</form>
To do this, just add the word checked to the inside of an input element. For
example:
<form>
<p>What is sum of 1 + 1?</p>
<input type="radio" id="two" name="answer" value="2">
<label for="two">2</label>
<br>
<input type="radio" id="eleven" name="answer"
value="11">
<label for="eleven">11</label>
</form>
Which renders:
Notice from the code snippet, radio buttons (like checkboxes) do not
display their value. We have an associated <label> to represent the
value of the radio button. To group radio buttons together, we
assign them the same name and only one radio button from that
group can be selected.
Here's an example of a radio button:
<label>
<input type="radio" name="indoor-outdoor">Indoor
</label>
We can also nest the input element within the label tags:
<label for="indoor">
<input id="indoor" type="radio" name="indoor-
outdoor">Indoor
</label>
The fieldset wrapper and legend tag are not necessary when the choices
are self-explanatory, like a gender selection. Using a label with
the for attribute for each radio button is sufficient.
Here's an example:
<form>
<fieldset>
<legend>Choose one of these three
items:</legend>
<input id="one" type="radio" name="items"
value="one">
<label for="one">Choice One</label><br>
<input id="two" type="radio" name="items"
value="two">
<label for="two">Choice Two</label><br>
<input id="three" type="radio" name="items"
value="three">
<label for="three">Choice Three</label>
</fieldset>
</form>
Cat Form
Indoor Outdoor
Cat Form
Is your cat an indoor or outdoor cat? Indoor Outdoor
(diferença ao meter a tag legend)
Type=”file”
What if you wanted to allow a user to upload a profile picture?
Well, the input type file allows just that. Add a label with the
text Upload a profile picture: , and add an input accepting a
file upload.
<label>Upload a profile picture: <input type="file"
/></label>
Dropdown list
Radio buttons are great if we want our users to pick one option out of
a few visible options, but imagine if we have a whole list of options!
<form>
<label for="lunch">What's for lunch?</label>
<select id="lunch" name="lunch">
<option value="pizza">Pizza</option>
<option value="curry">Curry</option>
<option value="salad">Salad</option>
<option value="ramen">Ramen</option>
<option value="tacos">Tacos</option>
</select>
</form>
Which renders:
And if we click on the field containing the first option, the list is
revealed:
Notice in the code that we’re using the element <select> to create
the dropdown list. To populate the dropdown list, we add
multiple <option> elements, each with a value attribute. By default,
only one of these options can be selected.
The text rendered is the text included between the opening and
closing <option> tags. However, it is the value of the value attribute
that is used in <form> submission (notice the difference in the text
and value capitalization). When the <form> is submitted, the
information from this input field will be sent using the name of
the <select> and the value of the chosen <option>. For instance,
if a user selected Pizza from the dropdown list, the
information would be sent as "lunch=pizza".
Datalist Input
Even if we have an organized dropdown list, if the list has a lot of
options, it could be tedious for users to scroll through the entire list to
locate one option. That’s where using the <datalist> element comes in
handy.
<form>
<label for="city">Ideal city to visit?</label>
<input type="text" list="cities" id="city" name="city">
<datalist id="cities">
<option value="New York City"></option>
<option value="Tokyo"></option>
<option value="Barcelona"></option>
<option value="Mexico City"></option>
<option value="Melbourne"></option>
<option value="Other"></option>
</datalist>
</form>
Notice, in the code above, we have an <input> that has
a list attribute. The <input> is associated to the <datalist> via
the <input>‘s list attribute and the id of the <datalist>.
While <select> and <datalist> share some similarities, there are some
major differences. In the associated <input> element, users can
type in the input field to search for a particular option. If none
of the <option>s match, the user can still use what they typed
in. When the form is submitted, the value of
the <input>‘s name and the value of the option selected, or what
the user typed in, is sent as a pair.
Textarea element
An <input> element with type="text" creates a single row input field for
users to type in information. However, there are cases where
users need to write in more information, like a blog post. In
such cases, instead of using an <input>, we could use <textarea>.
The <textarea> element is used to create a bigger text field for users to
write more text. We can add the attributes rows and cols to determine
the amount of rows and columns for the <textarea>. Take a look:
<form>
<label for="blog">New Blog Post: </label>
<br>
<textarea id="blog" name="blog" rows="5" cols="30">
</textarea>
</form>
In the code above, an empty <textarea> that is 5 rows by 30 columns is
rendered to the page like so:
<form>
<input type="submit" value="Send">
</form>
Which renders:
Notice in the code snippet that the value assigned to the <input> shows
up as text on the submit button. If there isn’t a value attribute, the
default text, Submit shows up on the button.
EX:
<button type="submit">Submit</button>
FORM VALIDATION
Requiring an Input
Sometimes we have fields in our <form>s which are not optional, i.e.
there must be information provided before we can submit it. To
enforce this rule, we can add the required attribute to
an <input> element.
appear:
There are certainly cases where we wouldn’t want our users typing
more than a certain number of characters (think about the character
cap for messages on Twitter). We might even want to set a minimum
number of characters. Conveniently, there are built-in HTML5
validations for these situations.
Matching a Pattern
In addition to checking the length of a text, we could also add a
validation to check how the text was provided. For cases when we
want user input to follow specific guidelines, we use
the pattern attribute and assign it a regular expression, or regex.
SEMANTIC HTML
Header and Nav
A <header> is a container usually for either navigational links or
introductory content containing <h1> to <h6> headings.
<header>
<nav>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#about">About</a></li>
</ul>
</nav>
</header>
<main>
<header>
<h1>Types of Sports</h1>
</header>
<article>
<h3>Baseball</h3>
<p>
The first game of baseball was played in
Cooperstown, New York in the summer of 1839.
</p>
</article>
</main>
As we see above, <main> contains an <article> and <header> tag with
child elements that hold the most important information related to
the page.
Contact information
Copyright information
Terms of use
Site Map
Reference to top of page links
For example:
<footer>
<p>Email me at Codey@Codecademy.com</p>
</footer>
<section>
<h2>Fun Facts About Cricket</h2>
<article>
<p>A single match of cricket can last up to
5 days.</p>
</article>
</section>
In the code above, the <article> element containing a fact about
cricket was placed inside of the <section> element. It is important to
note that a <section> element could also be placed in
an <article> element depending on the context.
Bibliographies
Endnotes
Comments
Pull quotes
Editorial sidebars
Additional information
<article>
<p>The first World Series was played between Pittsburgh
and Boston in 1903 and was a nine-game series.</p>
</article>
<aside>
<p>
Babe Ruth once stated, “Heroes get remembered, but
legends never die.”
</p>
</aside>
As shown above, the information within the <article> is the important
content. Meanwhile the information within the <aside> enhances the
information in <article> but is not required in order to understand it.
<figure>
<img src="overwatch.jpg">
</figure>
In this code, we created a <figure> element so that we can
encapsulate our <img> tag. In <figure> we used the <img> tag to insert
an image onto the webpage. We used the src attribute within
the <img> tag so that we can link the source of the image.
<figure>
<img src="overwatch.jpg">
<figcaption>This picture shows characters from
Overwatch.</figcaption>
</figure>
In the example above, we added a <figcaption> into
the <figure> element to describe the image from the previous
example. This helps group the <figure> content with
the <figcaption> content.
While the content in <figure> is related to the main flow of the
document, its position is independent. This means that you can
remove it or move it somewhere else without affecting the flow of the
document.
<audio>
<source src="iAmAnAudioFile.mp3" type="audio/mp3">
</audio>
In this example, we created an <audio> element. Then we created
a <source> element to encapsulate our audio link. In this
case, iAmAnAudioFile.mp3 is our audio file. Then we specified the type by
using type and named what kind of audio it is
<embed src="download.gif"/>
In the example above, <embed> is being used to add in a gif from a local
file known as download.gif. Embed can be used to add local files as well
as media content straight from some other websites.
For older browsers, the type will default to text, so it helps to show users the
expected date format in the label or placeholder text just in case.
Here's an example:
Here's an example:
keyboard-only users.
HTML5 allows this attribute to be used on any element, but it's particularly
useful when it's used with interactive ones. This includes links, buttons, and
form controls.
Here's an example:
--------------//////////////////////////////////////////////////------------------------
Note: One final point, each page should always have one (and only
one) h1 element, which is the main subject of your content. This and the other
headings are used in part by search engines to understand the topic of the
page.
FREECODECAMP
So the styling of the page looks similar on mobile as it does on a
desktop or laptop, you need to add a meta element with a
special content attribute.
NOTA: The div element is used mainly for design layout purposes
unlike the other content elements you have used so far.
NOTA: Next, you want to center the div horizontally. You can do this by
setting its margin-left and margin-right properties to auto. Think
of the margin as invisible space around an element. Using these two
margin properties, center the div element within the body element.
.item p { }
Using the above selector, add a display property with value inline-
block so the p elements behave more like inline elements.
NOTA: That's closer, but the price didn't stay over on the right. This is
because inline-block elements only take up the width of their
content. To spread them out, add a width property to
the flavor and price class selectors that have a value of 50% each.
NOTA: The current width of the menu will always take up 80% of
the body element's width. On a very wide screen, the coffee and dessert
appear far apart from their prices.