HTML
HTML
Element Content
The content of an HTML element is the information between the opening and closing tags of
an element. <h1>Codecademy is awesome! 🙂</h1>
<li> List Item Element
The <li> list item element create list items inside:
<ol>
● Ordered lists <ol> <li>Head east on Prince St</li>
● Unordered lists <ul> <li>Turn left on Elizabeth</li>
</ol>
<ul>
<li>Cookies</li>
<li>Milk</li>
</ul>
Tables
<tr> Table Row Element
The table row element, <tr> , is used to add rows to a table before adding table data and
table headings. <table>
<tr>
...
</tr>
</table>
Forms
<input>: Checkbox Type
When using an HTML input element, the type="checkbox" attribute will render
a single checkbox item. To create a group of checkboxes related to the same topic, they <input type="checkbox" name="breakfast" value="bacon">Bacon
should all use the same name attribute. Since it’s a checkbox, multiple checkboxes can be 🥓 <br>
selected for the same topic. <input type="checkbox" name="breakfast" value="eggs">Eggs 🍳
<br>
<input type="checkbox" name="breakfast"
value="pancakes">Pancakes 🥞 <br>
<textarea> Element
The textarea element is used when creating a text-box for multi-line input (e.g. a
comment section). The element supports the rows and cols attributes which
<textarea rows="10" cols="30" name="comment"></textarea>
determine the height and width, respectively, of the element.
When rendered by the browser, textarea elds can be stretched/shrunk in size by the
user, but the rows and cols attributes determine the initial size.
Unlike the input element, the <textarea> element has both opening and closing
tags. The value of the element is the content in between these tags (much like a <p>
element). The code block shows a <textarea> of size 10x30 and with a name of
"comment" .
<form> Element
The HTML <form> element is used to collect and send information to an external source.
<form> can contain various input elements. When a user submits the form, information <form method="post" action="http://server1">
Enter your name:
in these input elements is passed to the source which is named in the action attribute
of the form.
<input type="text" name="fname">
<br/>
Enter your age:
<input type="text" name="age">
<br/>
<input type="submit" value="Submit">
</form>
<input>: Number Type
HTML input elements can be of type number . These input elds allow the user to enter
only numbers and a few special characters inside the eld. <input type="number" name="balance" />
The example code block shows an input with a type of number and a name of
balance . When the input eld is a part of a form, the form will receive a key-value pair
with the format: name: value after form submission.
<input> Element
The HTML <input> element is used to render a variety of input elds on a webpage
including text elds, checkboxes, buttons, etc. <input> element have a type
<label for="fname">First name:</label>
attribute that determines how it gets rendered to a page. <input type="text" name="fname" id="fname"><br>
The example code block will create a text input eld and a checkbox input eld on a
webpage. <input type="checkbox" name="vehicle" value="Bike"> I own a
bike
<select> Element
The HTML <select> element can be used to create a dropdown list. A list of choices
for the dropdown list can be created using one or more <option> elements. By default,
<select name="rental-option">
<option value="small">Small</option>
only one <option> can be selected at a time.
<option value="family">Family Sedan</option>
The value of the selected <select> ’s name and the <option> ’ s value
<option value="lux">Luxury</option>
attribute are sent as a key-value pair when the form is submitted.
</select>
Submitting a Form
Once we have collected information in a form we can send that information somewhere else
by using the action and method attribute. The action attribute tells the form <form action="/index3.html" method="PUT"></form>
to send the information. A URL is assigned that determines the recipient of the information.
The method attribute tells the form what to do with that information once it’s sent. An
HTTP verb is assigned to the method attribute that determines the action to be
performed.
The value of the <input> ‘s name and value attribute of the element are sent as
a key-value pair when the form is submitted.
<datalist> Element
When using an HTML input, a basic search/autocomplete functionality can be achieved by
pairing an <input> with a <datalist> . To pair a <input> with a <input list="ide">
<datalist> the <input> ’s list value must match the value of the id of
the <datalist> . The datalist element is used to store a list of <option> s.
<datalist id="ide">
<option value="Visual Studio Code" />
The list of data is shown as a dropdown on an input eld when a user clicks on the input
eld. As the user starts typing, the list will be updated to show elements that best match what
<option value="Atom" />
<option value="Sublime Text" />
has been typed into the input eld. The actual list items are speci ed as multiple option
</datalist>
elements nested inside the datalist .
datalist s are ideal when providing users a list of pre-de ned options, but to also allow
them to write alternative inputs as well.
<label> Element
The HTML <label> element provides identi cation for a speci c <input> based on
matching values of the <input> ‘s id attribute and the <label> ‘s for
<label for="password ">Password:</label>
<input type="text" id="password" name="password">
attribute. By default, clicking on the <label> will focus the eld of the related
<input> .
The example code will create a text input eld with the label text “Password: “ next to it.
Clicking on “Password: “ on the page will focus the eld for the related <input> .
max Attribute
HTML <input> s of type number have an attribute called max that speci es the
maximum value for the input eld. <input type="number" max="20">
The code block shows an input number eld that is set to have a maximum value of
20 . Any value larger than 20 will mark the input eld as having an error.
maxlength Attribute
In HTML, input elds with type text have an attribute called maxlength that
speci es the maximum number of characters that can be entered into the eld. The code <input type="text" name="tweet" maxlength="140">
block shows an input text eld that accepts text that has a maximum length of 140
characters.
pattern Attribute
In a text input element, the pattern attribute uses a regular expression to match
against (or validate) the value of the <input> , when the form is submitted.
<form action="/action_page.php">
Country code:
<input type="text" name="country_code"
pattern="[A-Za-z]{3}"
title="Three letter country code">
<input type="submit">
</form>
minlength Attribute
In HTML, an input eld of type text has an attribute that supports minimum length
validation. To check that the input text has a minimum length, add the minlength
<input type="text" name="username" minlength="6" />
attribute with the character count.
The example code block shows an example of a text eld that has a minimum length of 6 .
HTML Form Validators
HTML forms allow you to specify di erent kinds of validation for your input elds to make
sure that data is entered correctly before being submitted. HTML supports a number of
di erent validators, including things like minimum value, minimum/maximum length, etc. The
validators are speci ed as attributes on the input eld.
min Attribute
In HTML, input elds with type number have an attribute called min that speci es the
minimum value that can be entered into the eld. The code block provided shows an input <input type="number" name="rating" min="1" max="10">
number eld that accepts a number with minimum value 1.
Cheatsheets / Learn HTML
Semantic HTML
Semantic HTML
Semantic HTML introduces meaning to the code we write. Before Semantic HTML the
elements didn’t have any meaning as to what it does or what content goes in it. An element <!--Non Semantic HTML-->
such as <div> was used as a general-purpose element to create things from headers to <div id="footer">
footers to articles. With Semantic HTML we were introduced to elements that tell developers <p>this is a footer</p>
and browsers exactly what it does and what content should go in it. </div>
<!--Semantic HTML-->
<footer>
<p>this is a footer</p>
</footer>
Element Placement
Semantic HTML introduces elements that can tell developers exactly what the element does
or where it’s placed based on the name of that element. Some of these elements are
<header> , <nav> , <main> , and <footer> . <header> describes the
content at the top of the page <body> . It may include a logo, navigational links or a
search bar. <nav> encapsulates the page’s navigational links. It is often placed inside the
<header> or <footer> . <main> encapsulates the main content of a page
between the header/navigation and the footer areas. <footer> includes the page’s
footer content at the bottom of the <body> .
Embedding media
Semantic HTML introduces us to <video> , <audio> and <embed> .
<video> allows us to add videos to our website. <audio> allows us to implement <!--Video Tag-->
<video src="4kvideo.mp4">video not supported</video>
audio into our website. <embed> can be used to implement any type of media. These
elements are universal in that they all use the src attribute to link the source of the
<!--Audio Tag-->
content. <video> and <audio> requires a closing tag while <embed> is a self-
<audio src="koreanhiphop.mp3"></audio>
closing tag.
<!--Embed tag-->
<embed src="babyyoda.gif"/>
HTML Structure
HTML is organized into a family tree structure. HTML elements can have parents,
grandparents, siblings, children, grandchildren, etc. <body>
<div>
<h1>It's div's child and body's grandchild</h1>
<h2>It's h1's sibling</h2>
</div>
</body>
Closing Tag
An HTML closing tag is used to denote the end of an HTML element. The syntax for a closing
tag is a left angle bracket < followed by a forward slash / then the element name and a <body>
right angle bracket to close > . ...
</body>
Attribute Name and Values
HTML attributes consist of a name and a value using the following syntax:
name="value" and can be added to the opening tag of an HTML element to con gure <elementName name="value"></elementName>
or change the behavior of the element.
Unique ID Attributes
In HTML, speci c and unique id attributes can be assigned to di erent elements in order
to di erentiate between them. <h1 id="A1">Hello World</h1>
When needed, the id value can be called upon by CSS and JavaScript to manipulate,
format, and perform speci c instructions on that element and that element only. Valid id
attributes should begin with a letter and should only contain letters ( a-Z ), digits ( 0-9 ),
hyphens ( - ), underscores ( _ ), and periods ( . ).
HTML Attributes
HTML attributes are values added to the opening tag of an element to con gure the element
or change the element’s default behavior. In the provided example, we are giving the <p> <p id="my-paragraph" style="color: green;">Here’s some text
(paragraph) element a unique identi er using the id attribute and changing the color of for a paragraph that is being altered by HTML attributes</p>
the default text using the style attribute.
alt Attribute
An <img> element can have alternative text via the alt attribute. The alternative text
will be displayed if an image fails to render due to an incorrect URL, if the image format is not <img src="path/to/image" alt="text describing image" />
supported by the browser, if the image is blocked from being displayed, or if the image has
not been received from the URL.
The text will be read aloud if screen reading software is used and helps support visually
impaired users by providing a text descriptor for the image content on a webpage.
HTML Tag
The syntax for a single HTML tag is an opening angle bracket < followed by the element
name and a closing angle bracket > . Here is an example of an opening <div> tag.
<div>
Indentation
HTML code should be formatted such that the indentation level of text increases once for
each level of nesting. <div>
It is a common convention to use two or four space per level of nesting. <h1>Heading</h1>
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
</div>
Whitespace
Whitespace, such as line breaks, added to an HTML document between block-level elements
will generally be ignored by the browser and are not added to increase spacing on the <p>Test paragraph</p>
rendered HTML page. Rather, whitespace is added for organization and easier reading of the
HTML document itself. <!-- The whitespace created by this line, and above/below
this line is ignored by the browser-->
File Path
URL paths in HTML can be absolute paths, like a full URL, for example:
https://developer.mozilla.org/en-US/docs/Learn or a relative <a href="https://developer.mozilla.org/en-US/docs/Web">The
le path that links to a local le in the same folder or on the same server, for example: URL for this anchor element is an absolute file path.</a>
./style.css . Relative le paths begin with ./ followed by a path to the local le.
./ tells the browser to look for the le path from the current folder. <a href="./about.html">The URL for this anchor element is a
relative file path.</a>
Document Type Declaration
The document type declaration <!DOCTYPE html> is required as the rst line of an
HTML document. The doctype declaration is an instruction to the browser about what type <!DOCTYPE html>
of document to expect and which version of HTML is being used, in this case it’s HTML5.