HTML
- Standard markup language for creating Web pages
- Stands for Hyper Text Markup Language
- Describes the structure of a Web page
- Consists of a series of elements
- The elements tell the browser how to display the content
HTML Components
- html
- head
- Title: title
- body
- Headings: h1/h2/h3/h4/h5/h6
- Paragraph: p
- Link: a
- head
sample.html
1 |
|
<!DOCTYPE html> declaration defines this document is an HTML5 document, it helps the browser to display web pages correctly. All HTML documents must start with a document type declaration, it is not case sensitive.
<html> is the root element of an HTML page
<head> contains meta information about the HTML page
HTML Elements
HTML is not case sensitive.
1
<p> is equivliant as <P>
Syntax
1
<starttag> Content </endtag>
Some HTML elemnts does not have content, these elements are called empty elements, they do not have an end tag.
1
<br>
Some HTML elements will display correctly even if you forget the end tag.
1
<p>Hello World!
<br> defines a line break.
HTML Attributes
- Provide additional information about HTML elements
- All HTML elements can have attributes
- Attributes always specified in the start tag
- Attributes usually come in name-value pairs. Eg. name=”value”
Examples of Attributes as below
sample.html
1 |
|
Two ways to specify the URL in src
attribute.
Absolute URL
Link to an external image that is hosted on another website.
1
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/6/63/IMG_%28business%29.svg/1200px-IMG_%28business%29.svg.png">
Note:
External images might be under copyright.
Also, you cannot control external images, it can be removed or changed suddenly.
Relative URL
Link to an image that is hosted within the website. The URL does not include the domain name.
If the URL start with a slash, it will be relative to the domain.
1
<img src="/img/pic.png">
If the URL start without a slash, it will be relative to the current page.
1
<img src="pic.png">
It is always the best to use relative URLs. It will not break if you change domain.
HTML Heading
Heading are important, search engines uses headings to index the structure and content of the web page.
Use HTML heading for headings ONLY. Do not use it to make text big or bold.
Each heading has a default size. However, we can specify the size for heading with
style
attribute.Eg.
1
<h1 style="font-size:60px;">Heading 1</h1>
HTML Paragraph
With HTML, you can not change the display by adding extra spaces or extra lines in HTML code, the browser will automatically remove any extra spaces and lines when page displayed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15<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><hr> tag defines a thematic break in an HTML page, most often displayed as a horizontal rule.
Used to seperate content in an HTML page.
1
2
3
4<h1>This is a heading</h1>
<p>This is a paragraph</p>
<hr>
<p>This is another paragraph</p><br> defines a line break. Use it when you want a line break (new line) without starting a new paragraph.
1
<p>This is <br> a paragraph <br> with line breaks.</p>
<pre> defines pre-formatted text.
Text inside <pre> element is displayed in a fixed-width font (usually Courier) and it preserves both spaces and line breaks.
1
2
3
4
5
6
7
8
9<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>
HTML Styles
Syntax
1
<tagname style="property:value;">
Background Color -
background-color
1
2
3
4<body style="background-color:powderblue;">
<h1 style="background-color:tomato">This is a heading</h1>
<p style="background-color:yellow">This is a paragraph</p>
</body>Text Color -
text-color
1
2<h1 style="color:blue;">Heading 1</h1>
<p style="color:red;">Paragraph</p>Fonts -
font-family
1
2<h1 style="font-family:verdana;">Heading 1</h1>
<p style="font-family:courier;">Paragraph</p>Text Size -
font-size
1
2<h1 style="font-size:300%;">Heading 1</h1>
<p style="font-size:160%;">Paragraph 1</p>Text Alignment -
text-align
1
2<h1 style="text-align:center;">Heading 1</h1>
<p style="text-align:left;">Paragraph 1</p>
HTML Text Formatting
<strong> defines text with strong importance. Content inside is typically displayed in bold.
<i> defines a part of text in an alternate voice or mood. Content inside is typically displayed in italic.
Often used to indicate a technical term, a phrase from another language, a thought, a ship name etc.
<em> a screen reader will pronounce the words in it with an emphasis, using verbal stress.
<mark> defines text are marked or highlighted.
<ins> defines a text that has been inserted into a document.
<sub> defines subscript text. Appears half a character below the normal line, sometimes rendered in a smaller font. Can be used for chemical formulas like H2O.
<sup> defines superscript text. Appears half a character above the normal line, sometimes rendered in a smaller font. Can be used for footnotes like WWW[1] or math power.
1 | <p>This text is normal.</p> |
HTML Quotation and Citation Elements
<blockquote> defines a section that is quoted from another source. Browser usually indent this elements.
<q> defines a short quotation “ “. Browser usually insert quotation marks around the content.
<abbr> defines an abbreviation缩写 or an acronym首字母缩略词 such as “HTML”, “Mr.” , “ASAP”.
Tips:
Use the global title attribute to show the description for the abbreviation/acronym when you mouse over the element.
<address> defines the contact information for the owner/author of the document. Content inside it usually rendered in italic and browser will add a line break before and after the <address> element.
<cite> defines the title of a creative work. (Eg. a poem, book, song, movie etc.) Usually rendered in italic.
<bdo> stands for Bi-Directional Override双向覆盖. Used to override the current text direction.
1 | <p>Here is a quote from WWF's website:</p> |
HTML Comments
Hide Inline Content
1
<p>This <!-- great text --> is a paragraph.</p>
HTML Colors
Specified with predefined color names, or with RGB (Red, Green, Blue), HEX, HSL, RGBA (Red, Green, Blue, Alpha channel (opacity不透明度)), or HSLA values.
Eg.
Color name: Tomato/Orange/DodgerBlue/MediumSeaGreen/Gray/Violet/SlateBlue etc.
1
2
3
4
5<h1 style="background-color:rgb(255, 99, 71);">RGB rgb(red, green, blue) value range from 0-255</h1>
<h1 style="background-color:#ff6347;">HEX values</h1>
<h1 style="background-color:hsl(9, 100%, 64%);">HSL values</h1>
<h1 style="background-color:rgba(255, 99, 71, 0.5);">RGBA</h1>
<h1 style="background-color:hsla(9, 100%, 64%, 0.5);">HSLA values</h1>Shades of Gray often defined using equal values for all three parameters.
Eg.
rgb (60, 60, 60), #f8f8f8, hsl (0, 0%, 40%)
rgba (red, green, blue, alpha) - alpha range from 0.0 (fully transparent) and 1.0 (not transparent at all)
Hexadecimal color is specified with: #RRGGBB. RR(red), GG(green) and BB(blue). Hexadecimal integers specify the components of the color. Range from 00-ff (same as decimal 0-255).
HSL stands for hue色相, saturation饱和度 and lightness亮度. hsl (hue, saturation, lightness)
Hue is a degree on the color wheel from 0 - 360. 0 is red, 120 is green, 240 is blue.
Saturation is a percentage value. 0% means a shade of gray. 100% is the full color.
Lightness is also a percentage value. 0% is black, 100% is white.
HSLA is an extension of HSL with Alpha channel (opacity)
Border Color
1
<h1 style="border:2px solid Tomato;">Hello World</h1>
HTML CSS
Stands for Cascading Style Sheets级联样式表
The word cascading means that a style applied to a parent element will also apply to all children elements within the parent. So, if you set the color of the body text to “blue”, all headings, paragraphs, and other text elements within the body will also get the same color (unless you specify something else)!
Css can be added to HTML document in 3 ways:
Inline - using
style
attribute inside the HTML elements1
2<h1 style="color:blue;">A Blue Heading</h1>
<p style="color:red;">A red paragraph.</p>Internal - using <style> element in the <head> section
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<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 - using <link> element to link to an external CSS file
1
2
3
4
5
6
7
8
9
10
11
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>styles.css
1
2
3
4
5
6
7
8
9body {
background-color: powderblue;
}
h1 {
color: blue;
}
p {
color: red;
}With an external style sheet, you can change the look of an entire web site, by changing one file!
CSS Border - defines a border around an HTML element.
1
2
3p {
border: 2px solid powderblue;
}CSS Padding - defines a padding (space) between the text and the border.
1
2
3
4p {
border: 2px solid powderblue;
padding: 30px;
}CSS Margin - defines a margin (space) outside the border.
1
2
3
4p {
border: 2px solid powderblue;
margin: 50px;
}Link to External CSS - can be referenced with a full URL or with a path relative to the current web page.
1
2
3
4
5
6
7
8<!-- This example uses a full URL to link to a style sheet -->
<link rel="stylesheet" href="https://www.w3schools.com/html/styles.css">
<!-- This example links to a style sheet located in the html folder on the current web site -->
<link rel="stylesheet" href="/html/styles.css">
<!-- This example links to a style sheet located in the same folder as the current page -->
<link rel="stylesheet" href="styles.css">
HTML Links
1 | <a href="https://www.w3schools.com/">Visit W3Schools.com!</a> |
By default, links will appear as follows in all browsers:
- An unvisited link is underlined and blue
- A visited link is underlined and purple
- An active link is underlined and red
Links can be styled with CSS, to get another look!
By default, the linked page will be displayed in the current browser window.
The target
attribute specifies where to open the linked document.
_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
1 | <a href="https://www.w3schools.com/" target="_blank">Visit W3Schools!</a> |
Use an Image as a Link
put the
<img>
tag inside the<a>
tag1
2
3<a href="default.asp">
<img src="smiley.gif" alt="HTML tutorial" style="width:42px;height:42px;">
</a>Link to an Email Address
Use
mailto:
inside thehref
attribute to create a link that opens the user’s email program (to let them send a new email)1
<a href="mailto:[email protected]">Send email</a>
Button as a Link
add JavaScript code, JavaScript allows you to specify what happens at certain events such as a click of a button.
1
<button onclick="document.location='default.html'">Click Me</button>
Link Titles
The
title
attribute specifies extra information about an element. The information is most often shown as a tooltip text when the mouse moves over the element.1
<a href="https://www.w3schools.com/html/" title="Go to W3Schools HTML section">Visit our HTML Tutorial</a>
HTML Link Colors
- Use CSS to remove the underline from the link.
1
<a href="html_images.asp" style="text-decoration:none;">HTML Images</a>
Link decoration Example
An unvisited link will be green with no underline.
A visited link will be pink with no underline.
An active link will be yellow and underlined.
When mousing over a link (a:hover) it will become red and underlined
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25<style>
a:link {
color: green;
background-color: transparent;
text-decoration: none;
}
a:visited {
color: pink;
background-color: transparent;
text-decoration: none;
}
a:hover {
color: red;
background-color: transparent;
text-decoration: underline;
}
a:active {
color: yellow;
background-color: transparent;
text-decoration: underline;
}
</style>Link Buttons
1
2
3
4
5
6
7
8
9
10
11
12
13
14<style>
a:link, a:visited {
background-color: #f44336;
color: white;
padding: 15px 25px;
text-align: center;
text-decoration: none;
display: inline-block;
}
a:hover, a:active {
background-color: red;
}
</style>
Link Bookmarks
HTML links can be used to create bookmarks, so that readers can jump to specific parts of a web page.
To create a bookmark - first create the bookmark, then add a link to it.
When the link is clicked, the page will scroll down or up to the location with the bookmark.
Example
1
2
3
4
5
6
7
8<!-- 1. use the id attribute to create a bookmark -->
<h2 id="C4">Chapter 4</h2>
<!-- 2. Add a link to the bookmark ("Jump to Chapter 4"), from within the same page -->
<a href="#C4">Jump to Chapter 4</a>
<!-- You can also add a link to a bookmark on another page -->
<a href="html_demo.html#C4">Jump to Chapter 4</a>
HTML Images
Images are not technically inserted into a web page; images are linked to web pages. The <img>
tag creates a holding space for the referenced image.
Syntax
<img src="url" alt="alternatetext">
Example
1 | <img src="pic_trulli.jpg" alt="Italian Trulli"> |
Better using the style
attribute to set the pictures width and height. It prevents styles sheets from changing the size of images
1 |
|
Image Floating
Use the CSS float
property to let the image float to the right or to the left of a text
1 | <p> |
The HTML <map>
tag defines an image map. An image map is an image with clickable areas. The areas are defined with one or more <area>
tags.
1 |
|
HTML Classes
Syntax
.classname {
}
Multiple HTML elements can share the same class.
The
class
attribute can be used on any HTML element.
Example
1 |
|
HTML Id
Syntax
#idname {
}
Used to specify a unique id for an HTML element.
Cannot have more than one element with the same id in an HTML document.
The id name must contain at least one character, cannot start with a number, and must not contain whitespaces (spaces, tabs, etc.).
Example
1 |
|
HTML Head
<meta> Element
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17<!-- Define the character set used -->
<meta charset="UTF-8">
<!-- Define keywords for search engines -->
<meta name="keywords" content="HTML, CSS, JavaScript">
<!-- Define a description of your web page -->
<meta name="description" content="Free Web tutorials">
<!-- Define the author of a page -->
<meta name="author" content="John Doe">
<!-- Refresh document every 30 seconds -->
<meta http-equiv="refresh" content="30">
<!-- Setting the viewport to make your website look good on all devices -->
<meta name="viewport" content="width=device-width, initial-scale=1.0"><base> Element
Specifies the base URL and/or target for all relative URLs in a page.
Must have either an href or a target attribute present, or both.
There can only be one single
<base>
element in a document!1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<html>
<head>
<base href="https://www.w3schools.com/" target="_blank">
</head>
<body>
<h1>The base element</h1>
<p><img src="images/stickman.gif" width="24" height="39" alt="Stickman"> - Notice that we have only specified a relative address for the image. Since we have specified a base URL in the head section, the browser will look for the image at "https://www.w3schools.com/images/stickman.gif".</p>
<p><a href="tags/tag_base.asp">HTML base tag</a> - Notice that the link opens in a new window, even if it has no target="_blank" attribute. This is because the target attribute of the base element is set to "_blank".</p>
</body>
</html>
HTML Layout
About this Post
This post is written by Andy, licensed under CC BY-NC 4.0.