Skip to main content

HTML

  • Describes the structure of a Web page
  • HTML elements tell the browser how to display the content
  • Version (2023): HTML5 - HTML5.2

Basic

Page Structure

<!DOCTYPE html>
<html>
<head>
<!-- contains metadata tag -->
<title>Page title</title>
<style>
* {
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<!-- main content -->
<h1 id="demo">Header</h1>
<p>Some paragraph.</p>
</body>
<script>
<!-- This is comment in HTML -->
<!-- This tag use to add JavaScript - making dynamic web pages -->
document.getElementById("demo").innerHTML = "This is Heading";
</script>
<noscript>Sorry, your browser does not support JavaScript!</noscript>
</html>

Documents + Element

note
  • HTML is NOT Case Sensitive
  • <tagname>Content or nest HTML element here!</tagname>
Tag nameDescriptionExample
<!DOCTYPE html>Declaration represents the document type. Helps browsers to display web pages correctly.
htmlThe root element and it defines the whole HTML document.<html lang="en"><head></head><body></body></html>
headAll configs for web page here!<head></head>
linkDefines a link between a document and an external resource<link rel="stylesheet" href="./styles.css">
bodyDefines the document's body.<body></body>
h1, h2, h3, h4, h5, h6Heading from most important to less important<h1>First</h1>``<h2>Second</h2>...<h6>Last</h6>
pParagraph - automatically remove any extra spaces<p>Some text</p>
preDefines preformatted text - Good for multiple lines show
aUse for link - redirect to link put in href when user click<a href="https://trustmebro.ccl" title="Click here!">My Facebook</a>
imgImage showsrc for image linkalt for descriptionheight and widthcan control[[#Image]]<img src="image-src.jpg" alt="Image desc" height="200" width="400" />
brA line break<br/>
hrA thematic break - display as a horizontal rule<hr/>
b, strongBold text, Important Text<strong>Important</strong>
i, emItalic text, Emphasized text<em>Emphasized</em>
markhighlight text<mark>Mark text</mark>
smalldecrease text's font size<small>Smaller text</small>
delmark text with middle line<del>Delete text</del>
instext with underline to mark<ins>Insert text</ins>
subsmall text lower than others<sub>2</sub>
supsmall text higher then others<sup>x</sup>
divDefines a section in a document (block-level)Block & Inline Elements
spanDefines a section in a document (inline)Block & Inline Elements
iframeUsed to display a web page within a web page.<iframe src="https://www.w3schools.com/html/html_iframe.asp" style="height:200px;width:100%" title="Iframe Example"></iframe>
info
  • All HTML elements can have Attributes
    • It provides additional information about elements
    • Always specified in the start tag
    • Usually come in pairs like: name="value"
  • id -> specify a unique id for HTML element.
  • class -> specify a class for HTML element, multiple HTML elements can share the same class.
Not famous tagDescriptionExample
blockquoteDefines a section that is quoted from another source.
qDefines a short quotationThis is an <q>Asian</q> boi.
abbrMarking abbreviations can give useful information to browsers, translation systems and search-engines<abbr title="Vo Nhat Phi">VNP</abbr>
addressDefines the contact information for the author/owner of a document or an article.
bdo<bdo dir="rtl">Used to override the current text direction</bdo>
codeA piece of computer code<code>var x = 1;</code>
sampSample output from a computer program
varA variable in programming or in a mathematical expression

The target attribute specifies where to open the linked document. It can have one of the following values:

  • _self - Default. Opens the document in the same window/tab as it was clicked
  • _blank - Opens the document in a new window or tab
  • _parent - Opens the document in the parent frame
  • _top - Opens the document in the full body of the window
Special
<!-- Link to an Email Address  -->
<a href="mailto:someone@example.com">Send email</a>
<!-- Button as a Link -->
<button onclick="document.location='index.html'">HTML Tutorial</button>
Making bookmarks
  1. Use id to create bookmark: <h2 id="C4">Chapter 4</h2>
  2. Put to href with #: <a href="#C4">Jump to Chapter 4</a>

Image

AbbreviationFile FormatFile Extension
APNGAnimated Portable Network Graphics.apng
GIFGraphics Interchange Format.gif
ICOMicrosoft Icon.ico, .cur
JPEGJoint Photographic Expert Group image.jpg, .jpeg, .jfif, .pjpeg, .pjp
PNGPortable Network Graphics.png
SVGScalable Vector Graphics.svg
<img src="workplace.jpg" alt="Workplace" usemap="#workmap" />
<map name="workmap">
<area
shape="rect"
coords="34,44,270,350"
alt="Computer"
href="computer.htm"
/>
<area shape="rect" coords="290,172,333,250" alt="Phone" href="phone.htm" />
<area shape="circle" coords="337,300,44" alt="Coffee" href="coffee.htm" />
</map>

picture - gives web developers more flexibility in specifying image resources.

<picture>
<source media="(min-width: 650px)" srcset="img_food.jpg" />

<source media="(min-width: 465px)" srcset="img_car.jpg" />
<img src="img_girl.jpg" />
</picture>

Favicon + Title

<head>
<title>My Page Title</title>

<link rel="icon" type="image/x-icon" href="/images/favicon.ico" />
</head>

Table

head 1head 2head 3
body 1 1body 2 1body 3 1
body 1 2body 2 2body 3 2
body 1 ...body 2 ...body 3 ...
<table>
<caption>
Example table
</caption>
<thead>
<tr>
<th>head 1</th>
<th>head 2</th>
<th>head 3</th>
</tr>
<!-- other headers -->
</thead>
<tbody>
<tr>
<td>body 1 1</td>
<td>body 2 1</td>
<td>body 3 1</td>
</tr>
<!-- other contents -->
</tbody>
<tfoot>
<!-- use tr td like body -->
</tfoot>
</table>
Table's tag
  • thead, tbody and tfoot just use for group content. It can remove for special case (like vertical table, ...)
  • caption use to name + description table (optional)
  • th & td can use colspan to group columns and rowspan to group rows
  • (2024) colgroup and col currently not famous, so avoid using them

Lists

  • Unordered: ul > li * n
  • Ordered: ol > li * n
    • Attribute
      • type="1" (default)
      • start="0"
  • Description: dl > dt ~ dd
Ordered Type
  • 1 -> 1 2 3
  • A -> A B C
  • a -> a b c
  • I -> I II III IV
  • i -> i ii iii iv

Block & Inline Elements

  • Block-level element always starts on a new line & takes up the full width available.

    <address>
    <article>
    <aside>
    <blockquote>
    <canvas>
    <dd>
    <div>
    <dl>
    <dt>
    <fieldset>
    <figcaption>
    <figure>
    <footer>
    <form>
    <h1>-<h6>
    <header>
    <hr>
    <li>
    <main>
    <nav>
    <noscript>
    <ol>
    <p>
    <pre>
    <section>
    <table>
    <tfoot>
    <ul>
    <video>
  • Inline-level element does not start on a new line & only takes up as much width as necessary.

    <a>
    <abbr>
    <b>
    <bdo>
    <br>
    <button>
    <cite>
    <code>
    <dfn>
    <em>
    <i>
    <img>
    <input>
    <kbd>
    <label>
    <map>
    <object>
    <output>
    <q>
    <samp>
    <script></script>
    <select>
    <small>
    <span>
    <strong>
    <sub>
    <sup>
    <textarea>
    <time>
    <var>

The Head element

The HTML <head> element is a container for the following elements

  • title is required and it defines the title of the document.

  • style is used to define style information for a single document.

  • meta is typically used to specify the character set, page description, keywords, author of the document, and viewport settings.

    <meta charset="UTF-8" />
    <!-- good for search engines -->
    <meta name="keywords" content="HTML, CSS, JavaScript" />
    <meta name="description" content="Free Web tutorials" />
    <meta name="author" content="John Doe" />
    <!-- auto refresh -->
    <meta http-equiv="refresh" content="30" />
    <!-- Setting the viewport (fit size on multiple devices). -->
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  • link tag is most often used to link to external style sheets.

  • script is used to define client-side [[JavaScript]].

  • base specifies the base URL and/or target for all relative URLs in a page.

HTML Semantic Elements

TagDescription
articleDefines independent, self-contained content
asideDefines content aside from the page content
figcaptionDefines a caption for a <figure> element
figureSpecifies self-contained content, like illustrations, diagrams, photos, code listings, etc.
footerDefines a footer for a document or section
headerSpecifies a header for a document or section
mainSpecifies the main content of a document
navDefines navigation links
sectionDefines a section in a document
detailsDefines additional details that the user can view or hide
summaryDefines a visible heading for a details element
timeDefines a date/time

Form

Collect user input and sent to a server for processing.

<form>
<!-- Other form element here! -->
</form>

Input tag

<!-- type is required -->
<input
type="text"
id="input-1"
class="form-control"
placeholder="default display"
value="default text"
name="form-props-or-url-params"
hidden="false"
disabled="false"
required="true"
form="a-part-of-specifies-form"
autofocus="true"
autocomplete="false"
readonly="false"
size="200px"
maxLength="100"
pattern="/[abc]+/"
onclick="action()"
onchange="action()"
onblur="action()"
onfocus="action()"
aria-*="custom-aria-props"
/>

TypeSpecial Atrributes
buttonvalue - text inside
submit
reset
color
checkboxchecked
radiochecked
range
filemultiple
numbermin, max, step
email
password
search
tel
text
url
monthmin, max
weekmin, max
timemin, max
datemin, max
datetime-localmin, max
hidden
imagesrc, height, width
Input Form special attribute
  • formaction - run override current action
  • formenctype - submit with encode form data
  • formmethod - submit override current method
  • formtarget - override target

Label tag

  • Useful for screen-reader users
  • Helps users who have difficulty clicking on very small regions (such as radio buttons or checkboxes)
  • Focus on for and id
<label for="fname">First name:</label><br />
<input type="text" id="fname" name="fname" value="John" />

Form attributes

AttributesDescriptionExample
actionDefines the action to be performed when the form is submittedaction="/login.asp"
target_blank - new window / tab _self - current window _parent - parent frame _top - full body of windowtarget="_blank"
methodGET (default), POST, PUT, PATCH, DELETEmethod="get"
autocompleteAllow browser auto fill - default is onautocomplete="off"
novalidateSpecifies that the form-data (input) should not be validated when submitted
relSpecifies the relationship between a linked resource and the current documentSee here
danger

URL limit: 2048 characters

Form elements

ElementsUsingAttributes
buttontype
labelLabel tag
inputInput taglist - use with datalist
textareaA multi-line inputrows, cols
selectDrop-down list, contain collection of optionname, size (visible item), multiple
optionselected
optgroupGroup of related options
datalistSpecifies a list of pre-defined options for an input
outputDefines the result of a calculation
fieldsetWrap group elements
legendTitle for group elements wrap by fieldset
<form>
<fieldset>
<legend>Personalia:</legend>

<label for="fname">First name:</label><br />
<input type="text" id="fname" name="fname" value="John" /><br />
<label for="lname">Last name:</label><br />
<input type="text" id="lname" name="lname" value="Doe" /><br /><br />

<select id="cars" name="cars">
<optgroup label="Swedish Cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
</optgroup>
<optgroup label="German Cars">
<option value="mercedes" selected>Mercedes</option>
<option value="audi">Audi</option>
</optgroup>
</select>

<input list="browsers" />
<datalist id="browsers">
<option value="Edge"></option>
<option value="Firefox"></option>
<option value="Chrome"></option>
<option value="Opera"></option>
<option value="Safari"></option>
</datalist>

<hr />
<input type="submit" value="Submit" />
</fieldset>
</form>

Graphics

CanvasScalable Vector Graphics
Draws 2D graphics, on the fly (with JavaScript)A language for describing 2D graphics in XML
Resolution dependentResolution independent
No support for event handlersSupport for event handlers
Poor text rendering capabilitiesBest suited for applications with large rendering areas (Google Maps)
You can save the resulting image as .png or .jpgSlow rendering if complex (anything that uses the DOM a lot will be slow)
Well suited for graphic-intensive gamesNot suited for game applications

Media

Format has bold is supported by all browser!

Video FormatFileDevelopedDescription
MPEG.mpg .mpegMoving Pictures Expert GroupThe first popular video format on the web. Not supported anymore in HTML.
AVI Audio Video Interleave.aviMicrosoftCommonly used in video cameras and TV hardware. Plays well on Windows computers, but not in web browsers.
WMVWindows Media Video.wmvMicrosoftSame above
QuickTime.movAppleCommonly used in video cameras and TV hardware. Plays well on Apple computers, but not in web browsers.
RealVideo.rm .ramReal MediaAllow video streaming with low bandwidths. Does not play in web browsers.
Flash.swf .flvMacromediaOften requires an extra component (plug-in) to play in web browsers.
Theora Ogg.oggXiph. Org Foundation.Supported by HTML.
WebM.webmMozilla, Opera, Adobe, and GoogleSupported by HTML.
MPEG-4/MP4.mp4Moving Pictures Expert GroupCommonly used in video cameras and TV hardware. Supported by all browsers and  recommended by YouTube.
Audio FormatFileDevelopedDescription
MIDIMusical Instrument Digital Interface.mid .midiMain format for all electronic music devices like synthesizers and PC sound cards. MIDI files do not contain sound, but digital notes that can be played by electronics. Plays well on all computers and music hardware, but not in web browsers.
RealAudio.rm .ramReal MediaAllow streaming of audio with low bandwidths. Does not play in web browsers.
WMAWindows Media Audio.wmaMicrosoftPlays well on Windows computers, but not in web browsers.
AACAdvanced Audio Coding.aacAppleThe default format for iTunes. Plays well on Apple computers, but not in web browsers.
WAV.wavIBM and MicrosoftPlays well on Windows, Macintosh, and Linux operating systems. Supported by HTML.
Ogg.oggXiph. Org Foundation.Supported by HTML.
MP3.mp3The sound part of MPEG files. MP3 is the most popular format for music players. Combines good compression (small files) with high quality.
MP4.mp4MP4 is a video format, but can also be used for audio.

Video tag

<video width="320" height="240" controls>
<source src="movie.mp4" type="video/mp4" />
<source src="movie.ogg" type="video/ogg" />
<track
src="fgsubtitles_en.vtt"
kind="subtitles"
srclang="en"
label="English"
/>
<track
src="fgsubtitles_no.vtt"
kind="subtitles"
srclang="no"
label="Norwegian"
/>
Your browser does not support the video tag.
</video>

Audio tag

<audio controls autoplay muted>
<source src="horse.ogg" type="audio/ogg" />
<source src="horse.mp3" type="audio/mpeg" />
Your browser does not support the audio element.
</audio>

YouTube videos

<iframe
width="420"
height="315"
src="https://www.youtube.com/embed/tgbNymZ7vqY?autoplay=1&mute=1"
>
</iframe>
Youtube API Params

Use after ? and combine with &

  • autoplay=1
  • mute=1
  • loop=1
  • playlist=
  • control=0 (not display) | 1 (default)

Resources