HTML Basics

Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

Heading 1

Heading 2
Heading 3
Heading 4
Heading 5
Heading 6

Headings are defined with the <h1> to <h6> tags.

<h1> defines the most important heading. <h6> defines the least important
heading.

<h1>Heading 1</h1>
<h2>Heading 2</h2>
<h3>Heading 3</h3>
<h4>Heading 4</h4>
<h5>Heading 5</h5>
<h6>Heading 6</h6>

Note: Use HTML headings for headings only. Don't use headings to make
text BIG or bold.

Bigger Headings
Each HTML heading has a default size. However, you can specify the size for
any heading with the style attribute, using the CSS font-size property:

<h1 style="font-size:60px;">Heading 1</h1>


HTML Horizontal Rules
The <hr> tag defines a thematic break in an HTML page, and is most often
displayed as a horizontal rule.

The <hr> element is used to separate content (or define a change) in an


HTML page:

<h1>This is heading 1</h1>


<p>This is some text.</p>
<hr>
<h2>This is heading 2</h2>
<p>This is some other text.</p>
<hr>

This is heading 1
This is some text.

This is heading 2
This is some other text.

This is heading 2
This is some other text.

The HTML <head> Element


The HTML <head> element has nothing to do with HTML headings.

The <head> element is a container for metadata. HTML metadata is data


about the HTML document. Metadata is not displayed.

The <head> element is placed between the <html> tag and the <body> tag:
<head>
<title>My First HTML</title>
<meta charset="UTF-8">
</head>

Note: Metadata typically define the document title, character set, styles,
links, scripts, and other meta information.

HTML Tag Reference


Tag reference contains additional information about these tags and their
attributes.

Tag Description

<html> Defines the root of an HTML document

<body> Defines the document's body

<head> A container for all the head elements (title, scripts,


styles, meta information, and more)

<h1> to <h6> Defines HTML headings

<hr> Defines a thematic change in the content


HTML Paragraphs
The HTML <p> element defines a paragraph:

<p>This is a paragraph.</p>
<p>This is another paragraph.</p>

Note: Browsers automatically add some white space (a margin) before and
after a paragraph.

HTML Display
You cannot be sure how HTML will be displayed.

Large or small screens, and resized windows will create different results.

With HTML, you cannot change the output by adding extra spaces or extra
lines in your HTML code.

The browser will remove any extra spaces and extra lines when the page is
displayed:

<p>
This paragraph
contains a lot of lines
in the source code,
but the browser
ignores it.
</p>

<p>
This paragraph
contains a lot of spaces
in the source code,
but the browser
ignores it.
</p>
HTML Line Breaks
The HTML <br> element defines a line break.

Use <br> if you want a line break (a new line) without starting a new
paragraph:

<p>This is<br>a paragraph<br>with line breaks.</p>

This is

A paragraph

With line breaks

The <br> tag is an empty tag, which means that it has no end tag.

The HTML <pre> Element


The HTML <pre> element defines preformatted text.

The text inside a <pre> element is displayed in a fixed-width font (usually


Courier), and it preserves both spaces and line breaks:

<pre>
My Bonnie lies over the ocean.

My Bonnie lies over the sea.

My Bonnie lies over the ocean.

Oh, bring back my Bonnie to me.


</pre>

The pre tag preserves both spaces and line breaks:


My Bonnie lies over the ocean.

My Bonnie lies over the sea.

My Bonnie lies over the ocean.

Oh, bring back my Bonnie to me.


The HTML Style Attribute
Setting the style of an HTML element, can be done with the style attribute.

The HTML style attribute has the following syntax:

<tagname style="property:value;">

The property is a CSS property. The value is a CSS value.

HTML Background Color


The background-color property defines the background color for an HTML
element.

This example sets the background color for a page to powderblue:

Example
<body style="background-color:powderblue; #RRGGBB- #AA00A0">

<h1>This is a heading</h1>
<p>This is a paragraph.</p>

</body>

HTML Text Color


The color property defines the text color for an HTML element:

Example
<h1 style="color:blue;">This is a heading</h1>
<p style="color:red;">This is a paragraph.</p>
HTML Fonts
The font-family property defines the font to be used for an HTML element:

Example
<h1 style="font-family:verdana;">This is a heading</h1>
<p style="font-family:courier;">This is a paragraph.</p>

HTML Text Size


The font-size property defines the text size for an HTML element:

Example
<h1 style="font-size:300%;">This is a heading</h1>
<p style="font-size:160%;">This is a paragraph.</p>

HTML Text Alignment


The text-align property defines the horizontal text alignment for an HTML
element:

Example
<h1 style="text-align:center;">Centered Heading</h1>
<p style="text-align:center;">Centered paragraph.</p>

Summary
 Use the style attribute for styling HTML elements
 Use background-color for background color
 Use color for text colors
 Use font-family for text fonts
 Use font-size for text sizes
 Use text-align for text alignment

HTML Formatting Elements


In the previous chapter, you learned about the HTML style attribute.

HTML also defines special elements for defining text with a


special meaning.

HTML uses elements like <b> and <i> for formatting output,
like bold or italic text.

Formatting elements were designed to display special types of text:

 <b> - Bold text


 <strong> - Important text
 <i> - Italic text
 <em> - Emphasized text
 <mark> - Marked text
 <small> - Small text
 <del> - Deleted text
 <ins> - Inserted text
 <sub> - Subscript text
 <sup> - Superscript text

HTML <b> and <strong> Elements


The HTML <b> element defines bold text, without any extra importance.

Example
<b>This text is bold</b>

The HTML <strong> element defines strong text, with added semantic
"strong" importance.

Example
<strong>This text is strong</strong>
HTML <i> and <em> Elements
The HTML <i> element defines italic text, without any extra importance.

Example
<i>This text is italic</i>

The HTML <em> element defines emphasized text, with added semantic
importance.

Example
<em>This text is emphasized</em>

HTML <small> Element


The HTML <small> element defines smaller text:

Example
<h2>HTML <small>Small</small> Formatting</h2>

HTML <mark> Element


The HTML <mark> element defines marked or highlighted text:

Example
<h2>HTML <mark>Marked</mark> Formatting</h2>
HTML <del> Element
The HTML <del> element defines deleted (removed) text.

Example
<p>My favorite color is <del>blue</del> red.</p>

HTML <ins> Element


The HTML <ins> element defines inserted (added) text.

Example
<p>My favorite <ins>color</ins> is red.</p>

HTML <sub> Element


The HTML <sub> element defines subscripted text.

Example
<p>This is <sub>subscripted</sub> text.</p>

HTML <sup> Element


The HTML <sup> element defines superscripted
text.

Example
<p>This is <sup>superscripted</sup> text.</p>
HTML Comment Tags
You can add comments to your HTML source by using the following syntax:

<!-- Write your comments here -->

Notice that there is an exclamation point (!) in the opening tag, but not in
the closing tag.

Note: Comments are not displayed by the browser, but they can help
document your HTML source code.

With comments you can place notifications and reminders in your HTML:

Example
<!-- This is a comment -->

<p>This is a paragraph.</p>

<!-- Remember to add more information here -->

Background Color
You can set the background color for HTML elements:

Hello World

Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy
nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi
enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit
lobortis nisl ut aliquip ex ea commodo consequat.

Example
<h1 style="background-color:DodgerBlue;">Hello World</h1>
<p style="background-color:Tomato;">Lorem ipsum...</p>
Text Color
You can set the color of text:

Hello World
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy
nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.

Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper


suscipit lobortis nisl ut aliquip ex ea commodo consequat.

Example
<h1 style="color:Tomato;">Hello World</h1>
<p style="color:DodgerBlue;">Lorem ipsum...</p>
<p style="color:MediumSeaGreen;">Ut wisi enim...</p>

Border Color
You can set the color of borders:

Hello World
Hello World
Hello World
Example
<h1 style="border-width:2px;
border-style:solid;
border-color: Tomato;">Hello World</h1>

<h1 style="border:2px solid DodgerBlue;">Hello World</h1>


<h1 style="border:2px solid Violet;">Hello World</h1>
Styling HTML with CSS
CSS stands for Cascading Style Sheets.

CSS describes how HTML elements are to be displayed on screen,


paper, or in other media.

CSS saves a lot of work. It can control the layout of multiple web pages all
at once.

CSS can be added to HTML elements in 3 ways:

 Inline - by using the style attribute in HTML elements


 Internal - by using a <style> element in the <head> section
 External - by using an external CSS file

Inline CSS
An inline CSS is used to apply a unique style to a single HTML element.

An inline CSS uses the style attribute of an HTML element.

This example sets the text color of the <h1> element to blue:

Example
<h1 style="color:blue;">This is a Blue Heading</h1>

Internal CSS
An internal CSS is used to define a style for a single HTML page.

An internal CSS is defined in the <head> section of an HTML page, within


a <style> element:

Example
<!DOCTYPE html>
<html>
<head>
<style>
body {background-color: powderblue;}
h1 {color: blue;}
p {color: red;}
</style>
</head>
<body>

<h1>This is a heading</h1>
<p>This is a paragraph.</p>

</body>
</html>

External CSS
An external style sheet is used to define the style for many HTML pages.

With an external style sheet, you can change the look of an entire
web site, by changing one file!

To use an external style sheet, add a link to it in the <head> section of the
HTML page:

Example
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>

<h1>This is a heading</h1>
<p>This is a paragraph.</p>

</body>
</html>
An external style sheet can be written in any text editor. The file must not
contain any HTML code, and must be saved with a .css extension.

Here is how the "styles.css" looks:

body {
background-color: powderblue;
}
h1 {
color: blue;
}
p {
color: red;
}

CSS Fonts
The CSS color property defines the text color to be used.

The CSS font-family property defines the font to be used.

The CSS font-size property defines the text size to be used.

Example
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
color: blue;
font-family: verdana;
font-size: 300%;
}
p {
color: red;
font-family: courier;
font-size: 160%;
}
</style>
</head>
<body>

<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>

CSS Border
The CSS border property defines a border around an HTML element:

Example
p {
border: 1px solid powderblue;
}

CSS Padding
The CSS padding property defines a padding (space) between the text and
the border:

Example
p {
border: 1px solid powderblue;
padding: 30px;
}

CSS Margin
The CSS margin property defines a margin (space) outside the border:

Example
p {
border: 1px solid powderblue;
margin: 50px;
}
The id Attribute
To define a specific style for one special element, add an id attribute to the
element:

<p id="p01">I am different</p>

then define a style for the element with the specific id:

Example
#p01 {
color: blue;
}
Note: The id of an element should be unique within a page, so the id selector
is used to select one unique element!

The class Attribute


To define a style for special types of elements, add a class attribute to the
element:

<p class="error">I am different</p>

then define a style for the elements with the specific class:

Example
p.error {
color: red;
}
External References
External style sheets can be referenced with a full URL or with a path relative
to the current web page.

This example uses a full URL to link to a style sheet:

Example
<link rel="stylesheet" href="https://www.w3schools.com/html/styles.c
ss">

This example links to a style sheet located in the html folder on the current
web site:

Example
<link rel="stylesheet" href="/html/styles.css">

This example links to a style sheet located in the same folder as the current
page:

Example
<link rel="stylesheet" href="styles.css">
HTML File Paths

Path Description

<img src="picture.jpg"> picture.jpg is located in the same folder as the


current page

<img picture.jpg is located in the images folder in the


src="images/picture.jpg"> current folder

<img picture.jpg is located in the images folder at the


src="/images/picture.jpg"> root of the current web

<img src="../picture.jpg"> picture.jpg is located in the folder one level up


from the current folder

HTML File Paths


A file path describes the location of a file in a web site's folder structure.

File paths are used when linking to external files like:

 Web pages
 Images
 Style sheets
 JavaScripts
Absolute File Paths
An absolute file path is the full URL to an internet file:

Example
<img src="https://www.w3schools.com/images/picture.jpg" alt="Mountain">

The <img> tag and the src and alt attributes are explained in the chapter
about HTML Images.

Relative File Paths


A relative file path points to a file relative to the current page.

In this example, the file path points to a file in the images folder located at
the root of the current web:

Example
<img src="/images/picture.jpg" alt="Mountain">

In this example, the file path points to a file in the images folder located in
the current folder:

Example
<img src="images/picture.jpg" alt="Mountain">

In this example, the file path points to a file in the images folder located in
the folder one level above the current folder:

Example
<img src="../images/picture.jpg" alt="Mountain">

Best Practice
It is best practice to use relative file paths (if possible).
When using relative file paths, your web pages will not be bound to your
current base URL. All links will work on your own computer (localhost) as
well as on your current public domain and your future public domains.

HTML Forms
HTML Form Example
First name:
Mickey

Last name:
Mouse

Submit

The <form> Element


The HTML <form> element defines a form that is used to collect user input:

<form>
.
form elements
.
</form>

An HTML form contains form elements.

Form elements are different types of input elements, like text fields,
checkboxes, radio buttons, submit buttons, and more.

The <input> Element


The <input> element is the most important form element.

The <input> element can be displayed in several ways, depending on


the type attribute.
Here are some examples:

Type Description

<input Defines a one-line text input field


type="text">

<input Defines a radio button (for selecting one of many choices)


type="radio">

<input Defines a submit button (for submitting the form)


type="submit">

Text Input
<input type="text"> defines a one-line input field for text input:

Example
<form>
First name:<br>
<input type="text" name="firstname"><br>
Last name:<br>
<input type="text" name="lastname">
</form>

This is how it will look like in a browser:

First name:
Last name:

Note: The form itself is not visible. Also note that the default width of a text
field is 20 characters.

Radio Button Input


<input type="radio"> defines a radio button.

Radio buttons let a user select ONE of a limited number of choices:

Example
<form>
<input type="radio" name="gender" value="male" checked> Male<br>
<input type="radio" name="gender" value="female"> Female<br>
<input type="radio" name="gender" value="other"> Other
</form>

This is how the HTML code above will be displayed in a browser:

Male
Female
Other

The Submit Button


<input type="submit"> defines a button for submitting the form data to
a form-handler.

The form-handler is typically a server page with a script for processing input
data.

The form-handler is specified in the form's action attribute:

Example
<form action="/action_page.php">
First name:<br>
<input type="text" name="firstname" value="Mickey"><br>
Last name:<br>
<input type="text" name="lastname" value="Mouse"><br><br>
<input type="submit" value="Submit">
</form>

This is how the HTML code above will be displayed in a browser:

First name:
Mickey

Last name:
Mouse

Submit

The Action Attribute


The action attribute defines the action to be performed when the form is
submitted.

Normally, the form data is sent to a web page on the server when the user
clicks on the submit button.

In the example above, the form data is sent to a page on the server called
"/action_page.php". This page contains a server-side script that handles the
form data:

<form action="/action_page.php">

If the action attribute is omitted, the action is set to the current page.

The Target Attribute


The target attribute specifies if the submitted result will open in a new
browser tab, a frame, or in the current window.

The default value is "_self" which means the form will be submitted in the
current window.

To make the form result open in a new browser tab, use the value " _blank":
Example
<form action="/action_page.php" target="_blank">

Other legal values are "_parent", "_top", or a name representing the name
of an iframe.

The Method Attribute


The method attribute specifies the HTTP method (GET or POST) to be used
when submitting the form data:

Example
<form action="/action_page.php" method="get">

or:

Example
<form action="/action_page.php" method="post">

When to Use GET?


The default method when submitting form data is GET.

However, when GET is used, the submitted form data will be visible in the
page address field:

/action_page.php?firstname=Mickey&lastname=Mouse

Notes on GET:

 Appends form-data into the URL in name/value pairs


 The length of a URL is limited (about 3000 characters)
 Never use GET to send sensitive data! (will be visible in the URL)
 Useful for form submissions where a user wants to bookmark the
result
 GET is better for non-secure data, like query strings in Google
When to Use POST?
Always use POST if the form data contains sensitive or personal information.
The POST method does not display the submitted form data in the page
address field.

Notes on POST:

 POST has no size limitations, and can be used to send large amounts
of data.
 Form submissions with POST cannot be bookmarked

The Name Attribute


Each input field must have a name attribute to be submitted.

If the name attribute is omitted, the data of that input field will not be sent at
all.

This example will only submit the "Last name" input field:

Example
<form action="/action_page.php">
First name:<br>
<input type="text" value="Mickey"><br>
Last name:<br>
<input type="text" name="lastname" value="Mouse"><br><br>
<input type="submit" value="Submit">
</form>

Grouping Form Data with <fieldset>


The <fieldset> element is used to group related data in a form.

The <legend> element defines a caption for the <fieldset> element.

Example
<form action="/action_page.php">
<fieldset>
<legend>Personal information:</legend>
First name:<br>
<input type="text" name="firstname" value="Mickey"><br>
Last name:<br>
<input type="text" name="lastname" value="Mouse"><br><br>
<input type="submit" value="Submit">
</fieldset>
</form>

This is how the HTML code above will be displayed in a browser:

Personal information:First name:


Mickey

Last name:
Mouse

Submit

Test Yourself with Exercises!

Here is the list of all <form> attributes:

Attribute Description

accept-charset Specifies the charset used in the submitted form


(default: the page charset).

action Specifies an address (url) where to submit the form


(default: the submitting page).

autocomplete Specifies if the browser should autocomplete the


form (default: on).
enctype Specifies the encoding of the submitted data
(default: is url-encoded).

method Specifies the HTTP method used when submitting the


form (default: GET).

name Specifies a name used to identify the form (for DOM


usage: document.forms.name).

novalidate Specifies that the browser should not validate the


form.

target Specifies the target of the address in the action


attribute (default: _self).

You might also like