HTML Images

Example

Norwegian Mountain Trip

Pulpit Rock

Try it yourself


Examples

Try it Yourself – Examples

Insert images
This example demonstrates how to display images in your Web page.

Insert images from different locations
This example demonstrates how to display images from another folder or another server in your Web page.

(You can find more examples at the bottom of this page)


The Image Tag and the Src Attribute

In HTML, images are defined with the <img> tag.

The <img> tag is empty, which means that it contains attributes only and it has no closing tag.

To display an image on a page, you need to use the src attribute. Src stands for “source”. The value of the src attribute is the URL of the image you want to display on your page.

The syntax of defining an image:

<img src=”url” />

The URL points to the location where the image is stored. An image named “boat.gif” located in the directory “images” on “www.w3schools.com” has the URL: http://www.w3schools.com/images/boat.gif.

The browser puts the image where the image tag occurs in the document. If you put an image tag between two paragraphs, the browser shows the first paragraph, then the image, and then the second paragraph.


The Alt Attribute

The alt attribute is used to define an “alternate text” for an image. The value of the alt attribute is an author-defined text:

<img src=”boat.gif” alt=”Big Boat” />

The “alt” attribute tells the reader what he or she is missing on a page if the browser can’t load images. The browser will then display the alternate text instead of the image. It is a good practice to include the “alt” attribute for each image on a page, to improve the display and usefulness of your document for people who have text-only browsers.


Basic Notes – Useful Tips

If an HTML file contains ten images – eleven files are required to display the page right. Loading images take time, so my best advice is: Use images carefully.


Examples

More Examples

Background image
This example demonstrates how to add a background image to an HTML page.

Aligning images
This example demonstrates how to align an image within the text.

Let the image float
This example demonstrates how to let an image float to the left or right of a paragraph.

Adjust images to different sizes
This example demonstrates how to adjust images to different sizes.

Display an alternate text for an image
This example demonstrates how to display an alternate text for an image. The “alt” attribute tells the reader what he or she is missing on a page if the browser can’t load images. It is a good practice to include the “alt” attribute for each image on a page.

Make a hyperlink of an image
This example demonstrates how to use an image as a link.

Create an image map
This example demonstrates how to create an image map, with clickable regions. Each of the regions is a hyperlink.

Turn an image into an image map
This example demonstrates how to turn an image into an image map. You will see that if you move the mouse over the image, the coordinates will be displayed on the status bar.


Image Tags

Tag Description
<img> Defines an image
<map> Defines an image map
<area> Defines a clickable area inside an image map

HTML Links

A link is the “address” to a document (or a resource) on the web.


Examples

Try it Yourself – Examples

HTML links
This example demonstrates how to create links in an HTML document.

Open a link in a new browser window
This example demonstrates how to link to another page by opening a new window, so that the visitor does not have to leave your Web site.

(You can find more examples at the bottom of this page)


Hyperlinks, Anchors, and Links

In web terms, a hyperlink is a reference (an address) to a resource on the web.

Hyperlinks can point to any resource on the web: an HTML page, an image, a sound file, a movie, etc.

An anchor is a term used to define a hyperlink destination inside a document.

The HTML anchor element <a>, is used to define both hyperlinks and anchors.

We will use the term HTML link when the <a> element points to a resource, and the term HTML anchor when the <a> elements defines an address inside a document..


An HTML Link

Link syntax:

<a href=”url”>Link text</a>

The start tag contains attributes about the link.

The element content (Link text) defines the part to be displayed.

Note: The element content don’t have to be a text. You can link from an image or any other HTML element.


The href Attribute

The href attribute defines the link “address”.

This <a> element defines a link to W3Schools:

<a href=”http://www.w3schools.com/”>Visit W3Schools!</a>

The code above will display like this in a browser:

Visit W3Schools!


The target Attribute

The target attribute defines where the linked document will be opened.

The code below will open the document in a new browser window:

Example

<a href=”http://www.w3schools.com/&#8221;
target=”_blank”>Visit W3Schools!</a>

Try it yourself


The name Attribute

When the name attribute is used, the <a> element defines a named anchor inside a HTML document.

Named anchor are not displayed in any special way. They are invisible to the reader.

Named anchor syntax:

<a name=”label”>Any content</a>

The link syntax to a named anchor:

<a href=”#label”>Any content</a>

The # in the href attribute defines a link to a named anchor.

Example:

A named anchor inside an HTML document:

<a name=”tips”>Useful Tips Section</a>

A link to the Useful Tips Section from the same document:

<a href=”#tips”>
Jump to the Useful Tips Section</a>

A link to the Useful Tips Section from another document:

<a href=”http://www.w3schools.com/html_tutorial.htm#tips”&gt;
Jump to the Useful Tips Section</a>

Basic Notes – Useful Tips

Always add a trailing slash to subfolder references. If you link like this: href=”http://www.w3schools.com/html&#8221;, you will generate two HTTP requests to the server, because the server will add a slash to the address and create a new request like this: href=”http://www.w3schools.com/html/&#8221;

Named anchors are often used to create “table of contents” at the beginning of a large document. Each chapter within the document is given a named anchor, and links to each of these anchors are put at the top of the document.

If a browser cannot find a named anchor that  has been specified, it goes to the top of the document. No error occurs.


Examples

More Examples

An image as a link
This example demonstrates how to use an image as a link.

Link to a location on the same page
This example demonstrates how to use a link to jump to another part of a document.

Break out of a frame
This example demonstrates how to break out of a frame, if your site is locked in a frame.

Create a mailto link
This example demonstrates how to link to a mail message (will only work if you have mail installed).

Create a mailto link 2
This example demonstrates a more complicated mailto link.


Link Tags

Tag Description
<a> Defines an anchor