HTML5

HTML5 new tags

Posted on

website-code-html5-optimized

HTML 5 is the new major version of HTML. HTML5 brings a host of new elements and attributes to allow developers to make their documents more easily understood by other systems (especially search engines). In addition, HTML 5 will also include fancy APIs for drawing graphics on screen, storing data offline, dragging and dropping, and a lot more. Here are new HTML5 tags that will make it easier for you to write your Web sites.

 

Doctype

In HTML 5, there is only one doctype. It is declared in the beginning of the page by <!DOCTYPE HTML>. It simply tells the browser that it’s dealing with an HTML-document.

 

<video> and <audio>

One of the biggest uses for Flash, Silverlight, and similar technologies is to get a multimedia item to play. With HTML5 supporting the new video and audio controls, those technologies are now relegated to being used for fallback status. The browser can now natively display the controls, and the content can be manipulated through JavaScript. Don’t let the codec confusion scare you away. You can specify multiple sources for content, so you can make sure that your multimedia will play regardless of what codecs the user’s browser supports.

 

<input> type attributes

The venerable <input> element now has a number of new values for the type attribute, and browsers do some pretty slick things depending on its value. For example, set type to “datetime” and browsers can show calendar/clock controls to pick the right time, a trick that used to require JavaScript. There is a wide variety of type attributes, and learning them (and the additional attributes that go with some of them) will eliminate the need for a lot of JavaScript work.

 

<canvas>

The <canvas> tag gives HTML a bitmapped surface to work with, much like what you would use with GDI+ or the .NET Image object. While <canvas> isn’t perfect (layers need to be replicated by using multiple canvas objects stacked on top of each other, for example), it is a great way to build charts and graphs, which have been a traditional weak spot in HTML, as well as custom graphics. And that is just a start!

 

<header> and <footer>

The <header> and <footer> tags are two of the new semantic tags available. These two tags do not get you anything above and beyond <div> for the actual display. But they will reap long-term rewards for your search engine efforts, since the search engines will be able to tell the difference between “content” and things that are important to the user but that aren’t the actual content.

 

<nav>

The nav-tag is used to contain navigational elements, such as the main navigation on a site or more specialized navigation like next/previous-links.

 

<article> and <section>

The <article> and <section> tags are two more semantic tags that will boost your search engine visibility. Articles can be composed of multiple sections, and a section can have multiple articles. Confusing? Not really. An article represents a full block of content, and a section is a piece of a bigger whole. For example, if you are looking at a blog, the front page might have a section for the listing of all the posts, and each post would be an article with a section for the actual post and another for comments.

<aside>

The aside tag is used to wrap around content related to the main content of the page that could still stand on it’s own and make sense.

 

 <output>

The new <output> tag is unique, in that it expects its content to be generated dynamically with JavaScript. It has a value attribute, which can be manipulated through the DOM with JavaScript to change what is displayed on the screen. This is much more convenient than the current ways of doing things.

 

<details>

It seems like every Web site needs to have an expanding/collapsing block of text. While this is easy enough to do with JavaScript or server-side code, the <details> tag makes it even easier. It does exactly what we’ve all been doing for years now: makes a simple block that expands and collapses the content when the header is clicked. The <details> tag does not have widespread support yet, but it will soon.

 

 <figure> and <figcaption>

<figure> is a container for content (typically images, but it can be anything), and <figcaption> (which gets put inside the <figure> tag) provides a caption or subtitle for the contents of the <figure> tag. For example, you could have four images representing charts of sales growth within a <figure> tag, and a <figcaption> with text like “Year-to-year sales growth, 1989 – 1993.” The images would be shown next to each other with the text running below all four.

 <progress>and <meter>

<progress> and <meter> are similar. You use <progress> for a task or a “measure how complete something is” scenario. It also has an indeterminate mode for something that has an unknown duration (like searching a database). The <meter> tag is for gauges and measurements of value (thermometers, quantity used, etc.). While they may look alike on the screen in many cases, they do have different semantic meanings.

 

<datalist>

The <datalist> tag acts like a combo box, where the system provides a pre-made list of suggestions, but users are free to type in their own input as well. There are tons of possible uses for this, such as a search box pre-populated with items based on the user’s history. This is another one of those things that currently requires a bunch of JavaScript (or JavaScript libraries) to handle but that can be done natively with HTML5.

 

Other tags?

What other new tags have you found especially useful? Share your HTML5 experiences with us.

Advertisements

HTML Attributes

Posted on Updated on

Attributes provide additional information about HTML elements.


HTML Attributes

  • HTML elements can have attributes
  • Attributes provide additional information about an element
  • Attributes are always specified in the start tag
  • Attributes come in name/value pairs like: name=”value”

The lang Attribute

The document language can be declared in the tag.

The language is declared in the lang attribute.

Declaring a language is important for accessibility applications (screen readers) and search engines:

Example

<!DOCTYPE html>
<html lang=“en-US”>
<body><h1>My First Heading</h1><p>My first paragraph.</p>

</body>
</html>

The first two letters specify the language (en). If there is a dialect, use two more letters (US).


The title Attribute

HTML paragraphs are defined with the

tag.

In this example, the

element has a title attribute. The value of the attribute is “About Learnforweb“:

Example

<p title=“About Learnforweb”>
Learnforweb is a web developer’s site.
It provides tutorials and useful tricks covering
many aspects of Front-end Development,
including HTML, CSS, Bootstrap, JavaScript etc.
</p>
Note When you move the mouse over the element, the title will be displayed as a tooltip.

The href Attribute

HTML links are defined with the tag. The link address is specified in the href attribute:

Example

<a href=learnforweb.wordpress.com>This is a link</a>

You will learn more about links and the tag later in this tutorial.


Size Attributes

HTML images are defined with the tag.

The filename of the source (src), and the size of the image (width and height) are all provided as attributes:

Example

<img src=“code.jpg” width=“104” height=“142”>

The image size is specified in pixels: width=”104″ means 104 screen pixels wide.

You will learn more about images and the tag later in this tutorial.


The alt Attribute

The alt attribute specifies an alternative text to be used, when an HTML element cannot be displayed.

The value of the attribute can be read by “screen readers”. This way, someone “listening” to the webpage, i.e. a blind person, can “hear” the element.

Example

<img src=“Code.jpg” alt=“Coding” width=“104” height=“142”>

We Suggest: Always Use Lowercase Attributes

The HTML5 standard does not require lower case attribute names.

The title attribute can be written with upper or lower case like Title and/or TITLE.

W3C recommends lowercase in HTML4, and demands lowercase for stricter document types like XHTML.

Note Lower case is the most common. Lower case is easier to type.
At Learnforweb we always use lower case attribute names.

We Suggest: Always Quote Attribute Values

The HTML5 standard does not require quotes around attribute values.

The href attribute, demonstrated above, can be written as:

W3C recommends quotes in HTML4, and demands quotes for stricter document types like XHTML.

Sometimes it is necessary to use quotes. This will not display correctly, because it contains a space:

Example

<p title=About W3Schools>
Note Using quotes are the most common. Omitting quotes can produce errors.

Single or Double Quotes?

Double style quotes are the most common in HTML, but single style can also be used.

In some situations, when the attribute value itself contains double quotes, it is necessary to use single quotes:

Example

<p title=‘John “ShotGun” Nelson’>

Or vice versa:

Example

<p title=“John ‘ShotGun’ Nelson”>

Chapter Summary

  • All HTML elements can have attributes
  • The HTML title attribute provides additional “tool-tip” information
  • The HTML href attribute provides address information for links
  • The HTML width and height attributes provide size information for images
  • The HTML alt attribute provides text for screen readers
  • At W3Schools we always use lowercase HTML attribute names
  • At W3Schools we always quote attributes with double quotes

HTML Attributes

Below is an alphabetical list of some attributes often used in HTML:

Attribute Description
alt Specifies an alternative text for an image
disabled Specifies that an input element should be disabled
href Specifies the URL (web address) for a link
id Specifies a unique id for an element
src Specifies the URL (web address) for an image
style Specifies an inline CSS style for an element
title Specifies extra information about an element (displayed as a tool tip)
value Specifies the value (text content) for an input element.

HTML Elements

Posted on

HTML documents are made up by HTML elements.


HTML Elements

HTML elements are written with a start tag, with an end tag, with the content in between:

<tagname>content</tagname>

The HTML element is everything from the start tag to the end tag:

<p>My first HTML paragraph.</p>
Start tag Element content End tag
<h1> My First Heading </h1>
<p> My first paragraph. </p>
<br>
Note Some HTML elements do not have an end tag.

Nested HTML Elements

HTML elements can be nested (elements can contain elements).

All HTML documents consist of nested HTML elements.

This example contains 4 HTML elements:

Example

<!DOCTYPE html>
<html>
<body>

<h1>My First Heading</h1>
<p>My first paragraph.</p>

</body>
</html>

HTML Example Explained

The <html> element defines the whole document.

It has a start tag <html> and an end tag </html>.

The element content is another HTML element (the <body> element).

<html>
<body>

<h1>My First Heading</h1>
<p>My first paragraph.</p>

</body>
</html>

The <body> element defines the document body.

It has a start tag <body> and an end tag </body>.

The element content is two other HTML elements (<h1> and <p>).

<body>

<h1>My First Heading</h1>
<p>My first paragraph.</p>

</body>

The <h1> element defines a heading.

It has a start tag <h1> and an end tag </h1>.

The element content is: My First Heading.

<h1>My First Heading</h1>

The <p> element defines a paragraph.

It has a start tag <p> and an end tag </p>.

The element content is: My first paragraph.

<p>My first paragraph.</p>

Don’t Forget the End Tag

Some HTML elements will display correctly, even if you forget the end tag:

Example

<html>
<body>

<p>This is a paragraph
<p>This is a paragraph

</body>
</html>

The example above works in all browsers, because the closing tag is considered optional.

Never rely on this. It might produce unexpected results and/or errors if you forget the end tag.


Empty HTML Elements

HTML elements with no content are called empty elements.

<br> is an empty element without a closing tag (the <br> tag defines a line break).

Empty elements can be “closed” in the opening tag like this: <br />.

HTML5 does not require empty elements to be closed. But if you want stricter validation, or you need to make your document readable by XML parsers, you should close all HTML elements.


HTML Tip: Use Lowercase Tags

HTML tags are not case sensitive: <P> means the same as <p>.

The HTML5 standard does not require lowercase tags, but W3C recommends lowercase in HTML4, and demandslowercase for stricter document types like XHTML.

Note At Learnforweb we always use lowercase tags.

HTML Basic Examples

Posted on Updated on

Don’t worry if these examples use tags you have not learned.

You will learn them in the next chapters.


HTML Documents

All HTML documents must start with a type declaration:.

The HTML document itself begins with and ends with .

The visible part of the HTML document is between and .

Example

<!DOCTYPE html>
<html>
<body><h1>My First Heading</h1><p>My first paragraph.</p></body>
</html>


HTML Headings

HTML headings are defined with the

to

tags:

Example

<h1>This is a heading</h1>
<h2>This is a heading</h2>
<h3>This is a heading</h3>

HTML Paragraphs

HTML paragraphs are defined with the

tag:

Example

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

HTML Links

HTML links are defined with the tag:

Example

<a href=http://www.learnforweb.wordpress.com>This is a link</a>

The link address is specified in the href attribute.

Attributes are used to provide additional information about HTML elements.


HTML Images

HTML images are defined with the tag.

The source file (src), alternative text (alt), and size (width and height) are provided as attributes:

Example

<img src=“code.jpg” alt=“Coding” width=“104” height=“142”>
Note You will learn more about attributes in a later chapter.

HTML Editors

Posted on

Write HTML Using Notepad or TextEdit

HTML can be edited by using a professional HTML editor like:

  • Adobe Dreamweaver
  • Microsoft Expression Web
  • CoffeeCup HTML Editor

However, for learning HTML we recommend a text editor like Notepad (PC) or TextEdit (Mac).

We believe using a simple text editor is a good way to learn HTML.

Follow the 4 steps below to create your first web page with Notepad.


Step 1: Open Notepad

To open Notepad in Windows 7 or earlier:

Click Start (bottom left on your screen). Click All Programs. Click Accessories. Click Notepad.

To open Notepad in Windows 8 or later:

Open the Start Screen (the window symbol at the bottom left on your screen). Type Notepad.


Step 2: Write Some HTML

Write or copy some HTML into Notepad.

<!DOCTYPE html>
<html>
<body>

<h1>My First Heading</h1>

<p>My first paragraph.</p>

</body>
</html>

Notepad


Step 3: Save the HTML Page

Save the file on your computer.

Select File > Save as in the Notepad menu.

Name the file “index.htm” or any other name ending with htm.

UTF-8 is the preferred encoding for HTML files.

ANSI encoding covers US and Western European characters only.

View in Browser

Note You can use either .htm or .html as file extension. There is no difference, it is up to you.

Step 4: View HTML Page in Your Browser

Open the saved HTML file in your favorite browser. The result will look much like this:

View in Browser

Note To open a file in a browser, double click on the file, or right-click, and choose open with.