Monday, August 13, 2012

HTML Images


Example

Norwegian Mountain Trip

Pulpit Rock

Try it yourself »

Try it Yourself - Examples

Insert images
How to insert images into an HTML document.

Insert images from different locations
How to insert an image from another folder or another server.
(You can find more examples at the bottom of this page).

HTML Images - The <img> 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 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.

Syntax for defining an image:
<img src="url" alt="some_text"/>

The URL points to the location where the image is stored. An image named "boat.gif", located in the "images" directory on "www.w3schools.com" has the URL: http://www.w3schools.com/images/boat.gif.
The browser displays the image where the <img> 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.

HTML Images - The Alt Attribute

The required alt attribute specifies an alternate text for an image, if the image cannot be displayed.
The value of the alt attribute is an author-defined text:

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

The alt attribute provides alternative information for an image if a user for some reason cannot view it (because of slow connection, an error in the src attribute, or if the user uses a screen reader).

HTML Images - Set Height and Width of an Image

The height and width attributes are used to specify the height and width of an image.
The attribute values are specified in pixels by default:

<img src="pulpit.jpg" alt="Pulpit rock" width="304" height="228" />

Tip: It is a good practice to specify both the height and width attributes for an image. If these attributes are set, the space required for the image is reserved when the page is loaded. However, without these attributes, the browser does not know the size of the image. The effect will be that the page layout will change during loading (while the images load).

Basic Notes - Useful Tips

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

Note: When a web page is loaded, it is the browser, at that moment, that actually gets the image from a web server and inserts it into the page. Therefore, make sure that the images actually stay in the same spot in relation to the web page, otherwise your visitors will get a broken link icon. The broken link icon is shown if the browser cannot find the image.

More Examples

Aligning images
How to align an image within the text.

Let the image float
How to let an image float to the left or right of a paragraph.

Make a hyperlink of an image
How to use an image as a link.

Create an image map
How to create an image map, with clickable regions. Each of the regions is a hyperlink.

HTML Image Tags

TagDescription
<img />Defines an image
<map>Defines an image-map
<area />Defines a clickable area inside an image-map

Saturday, August 11, 2012

HTML Links


Links are found in nearly all Web pages. Links allow users to click their way from page to page.


Try it Yourself - Examples

How to create links in an HTML document.
(You can find more examples at the bottom of this page)

HTML Hyperlinks (Links)
A hyperlink (or link) is a word, group of words, or image that you can click on to jump to a new document or a new section within the current document.
When you move the cursor over a link in a Web page, the arrow will turn into a little hand.
Links are specified in HTML using the <a> tag.

The <a> tag can be used in two ways:

1.    To create a link to another document, by using the href attribute
2.    To create a bookmark inside a document, by using the name attribute


HTML Link Syntax
The HTML code for a link is simple. It looks like this:
<a href="http://www.nabeeljamil.blogspot.com/">Link text</a>
The href attribute specifies the destination of a link.
Example
<a href="http://www.nabeeljamil.blogspot.com/">Nabeel Jamil’s Blog</a>

which will display like this: Visit W3Schools
Clicking on this hyperlink will send the user to W3Schools' homepage.

Tip: The "Link text" doesn't have to be text. It can be an image or any other HTML element.


HTML Links - The target Attribute
The target attribute specifies where to open the linked document.
The example below will open the linked document in a new browser window or a new tab:
Example
<a href=" http://www.nabeeljamil.blogspot.com/" target="_blank">Visit My Blog</a>


HTML Links - The name Attribute
The name attribute specifies the name of an anchor.
The name attribute is used to create a bookmark inside an HTML document.
Note: The upcoming HTML5 standard suggests using the id attribute instead of the name attribute for specifying the name of an anchor. Using the id attribute actually works also for HTML4 in all modern browsers.
Bookmarks are not displayed in any special way. They are invisible to the reader.
Example
A named anchor inside an HTML document:
<a name="tips">Useful Tips Section</a>

Create a link to the "Useful Tips Section" inside the same document:
<a href="#tips">Visit the Useful Tips Section</a>

Or, create a link to the "Useful Tips Section" from another page:
<a href="http://www.w3schools.com/html_links.htm#tips">Visit the Useful Tips Section</a>


Basic Notes - Useful Tips
Note: Always add a trailing slash to subfolder references. If you link like this: href="http://www.w3schools.com/html", you will generate two requests to the server, the server will first add a slash to the address, and then create a new request like this: href="http://www.w3schools.com/html/".
Tip: 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.


More Examples
How to use an image as a link.

How to link to a bookmark.

Break out of a frame
How to break out of a frame (if your site is locked in a frame).

Create a mailto link
How to link to a mail message (will only work if you have mail installed).

Create a mailto link 2
Another mailto link.

HTML Link Tags
Tag
Description
Defines an anchor

HTML Styles


CSS (Cascading Style Sheets) is used to style HTML elements.

Look! Styles and colors
This text is in Verdana and red
This text is in Times and blue
This text is 30 pixels high



Try it Yourself - Examples
Using styles in HTML
How to add style information into the <head> section.

Link that is not underlined
How to make a link that is not underlined, with the style attribute.

Link to an external style sheet
How to use the <link> tag to link to an external style sheet.

Styling HTML with CSS
CSS was introduced together with HTML 4, to provide a better way to style HTML elements.
CSS can be added to HTML in the following ways:
  • Inline - using the style attribute in HTML elements
  • Internal - using the <style> element in the <head> section
  • External - using an external CSS file
The preferred way to add CSS to HTML, is to put CSS syntax in separate CSS files.
However, in this HTML tutorial we will introduce you to CSS using the style attribute. This is done to simplify the examples. It also makes it easier for you to edit the code and try it yourself.
You can learn everything about CSS in our CSS Tutorial.


Inline Styles
An inline style can be used if a unique style is to be applied to one single occurrence of an element.
To use inline styles, use the style attribute in the relevant tag. The style attribute can contain any CSS property. The example below shows how to change the text color and the left margin of a paragraph:
<p style="color:blue;margin-left:20px;">This is a paragraph.</p>
To learn more about style sheets, visit our CSS tutorial.


HTML Style Example - Background Color
The background-color property defines the background color for an element:
Example
<!DOCTYPE html>
<html>

<body style="background-color:yellow;">
<h2 style="background-color:red;">This is a heading</h2>
<p style="background-color:green;">This is a paragraph.</p>
</body>

</html>
The background-color property makes the "old" bgcolor attribute obsolete.


HTML Style Example - Font, Color and Size
The font-family, color, and font-size properties defines the font, color, and size of the text in an element:
Example
<!DOCTYPE html>
<html>

<body>
<h1 style="font-family:verdana;">A heading</h1>
<p style="font-family:arial;color:red;font-size:20px;">A paragraph.</p>
</body>

</html>
The font-family, color, and font-size properties make the old <font> tag obsolete.


HTML Style Example - Text Alignment
The text-align property specifies the horizontal alignment of text in an element:
Example
<!DOCTYPE html>
<html>

<body>
<h1 style="text-align:center;">Center-aligned heading</h1>
<p>This is a paragraph.</p>
</body>

</html>
The text-align property makes the old <center> tag obsolete.


Internal Style Sheet
An internal style sheet can be used if one single document has a unique style. Internal styles are defined in the <head> section of an HTML page, by using the <style> tag, like this:
<head>
<style type="text/css">
body {background-color:yellow;}
p {color:blue;}
</style>
</head>


External Style Sheet
An external style sheet is ideal when the style is applied to many pages. With an external style sheet, you can change the look of an entire Web site by changing one file. Each page must link to the style sheet using the <link> tag. The <link> tag goes inside the <head> section:
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css" />
</head>


HTML Style Tags
Tag
Description
Defines style information for a document
Defines the relationship between a document and an external resource


Deprecated Tags and Attributes
In HTML 4, several tags and attributes were used to style documents. These tags are not supported in newer versions of HTML.
Avoid using the elements <font>, <center>, and <strike> and the attributes color and bgcolor.

HTML Editors


Writing 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: Start Notepad
To start Notepad go to:
Start
    All Programs
        Accessories
            Notepad

Step 2: Edit Your HTML with Notepad
Type your HTML code into your Notepad:
Notepad

Step 3: Save Your HTML
Select Save as.. in Notepad's file menu.
When you save an HTML file, you can use either the .htm or the .html file extension. There is no difference, it is entirely up to you.
Save the file in a folder that is easy to remember, like w3schools.

Step 4: Run the HTML in Your Browser
Start your web browser and open your html file from the File, Open menu, or just browse the folder and double-click your HTML file.
The result should look much like this:
View in Browser