Monday, 24 December 2012

Images.


Images are a staple of any web designer, so it is very important that you understand how to use them properly. In order to place an image onto a website, one needs to know where the image file is located within the file tree of the web server -- the URL (Unified Resource Locator).


Use the <img /> tag to place an image on your webpage. Like the <br /> tag, <img /> tag does not require a formal ending tag. Instead, all we need to do to close this tag out with a slash (/) placed just inside the ending bracket (/>).

HTML Image Code:
<img src="http://www.tizag.com/pics/htmlT/sunset.gif" />
HTML Image:

So that you can follow along with us, we've provided a global URL of an image we have stored on Tizag's web server. If you have an active connection to the internet, you should be able to use the URL in the example to render this image on your own web page. If not, you will have to download (right click the image and Save As...on a PC, control-click and Save on a Mac). Once saved to your local computer, point the image src attribute to the recently downloaded image file. It might help to save the image file in the same folder as your web page.

html - image src attribute
The source attribute (src) is what makes an image tag display an image. An image source is a URL value and should point to the directory location of an image file. Let's take one more look at the code from our first HTML image example.

HTML Image Code:
<img src="http://www.tizag.com/pics/htmlT/sunset.gif" />
An image source value is essentially the URL of a picture file and tells the web browser where the image is located so that it can then display the image correctly.

html - source urls
Image source URLs can be either local or global, meaning that the picture files you wish to display on your website can be either hosted locally on your machine (local) or hosted elsewhere on some other web site domain (global).

Global: http://www.tizag.com/pics/htmlT/sunset.gif
Local: pics/sunset.gif
Local URLs are relative to the file path of the web page itself. For example, if the picture file is placed inside the same directory as the web page, then the local URL for the image would simply be the name of the image, since it is residing in the same directory as the HTML page.

Local URLs Explained:
Local Src Location Description
src="sunset.gif" picture file resides in same directory as .html file
src="pics/sunset.gif" picture file resides in the pics directory
src="../sunset.gif" picture resides one folder "up" from the .html file
src="../pics/sunset.gif" picture file resides in the pics directory, one folder "up" from the .html file.
Pictures must reside on the same web host as your .html file in order for you to use local URLs. A URL cannot contain drive letters such as C:\, since a src URL is a relational interpretation based on the location of the.html file and the location of the picture file. Something like src="C:\www\web\pics\" will not work.

Each URL format has its pros and cons. Using the URL of pictures on other sites poses a problem if the other site happens to change the physical location of the picture file. Copying the file directly to your web server solves this problem. However, as you continue to upload picture files to your system, you may eventually run short on hard drive space. Use your best judgment based upon your situation.

html - image height and width attributes
Height and width are HTML attributes that define an element's height and width properties. These values can either be percentage-based (%) or rely on pixel sizes.

HTML Height and Width Attributes:
<img src="sunset.gif" height="50" width="100" />
HTML Height and Width (Pixels):

Above, we've used hard-coded pixel values for the height and width of the sunset image to ensure that this image will always render 50 pixels high by 100 pixels wide. By hard-coding these values, we are ensuring that the image will only display 50 pixels high by 100 pixels wide, even if the picture file itself happens to be much larger. If the dimensions of the picture are much larger, then we risk some severe skewing as the browser tries to shrink our image into our small box.

Height and width values can also be a percentage. Percentage values are relative to the parent HTML element (usually the body), which means if you have a parent element like a <body> element that is the whole screen (1024x768), then an image with a height and width of 100% will take up that entire body element (1024x768). In our example below, we have placed the image in a table element that is about 400 pixels wide by 200 pixels tall.

HTML Height and Width Code:
<table height='200' width='400'>
  <tr>
    <td>
      <img src="sunset.gif" height="100%" width="100%">
    </td>
  </tr>
</table>
HTML Height and Width (Percentage):

This image is now pixelated, having been rendered to fill the 400x200 table element. The reason we are seeing a low-quality, pixelated rendering of the image is because the actual image file is much smaller than 400x200 and has been stretched by the web browser because we coded it to do so.

Here's a few things to remember when trying to place images on your web page:

Maintain the same height to width ratio. The ratio is critical, and must be maintained to avoid skewing.
Always scale down. -- Larger images will always scale down nicely and continue to look sharp.
If no height or width attribute is specified inside the <img> tag, the browser will use the actual dimensions of the image file to render the image. This can cause problems with the page layout if the picture file is too large, as other HTML elements will be moved further down the page in the event of an over-sized image.

Another concept to keep in mind is that as a browser begins rendering HTML components, it handles them one after another in sequence. Before it can move from one element on to the next, the browser needs to know the size and shape of an element. If this information is provided in the tag, that's one less step required by the browser to render an image element and will result in the page loading faster for your users.

html - alternative attribute
The alt attribute specifies alternate text to be displayed if for some reason the browser cannot find the image, or if a user has image files disabled in their web browser settings. Text-only browsers greatly depend on the alt attribute since they are not capable of displaying pictures.

HTML Alternative Attribute (alt):
<img src="http://example.com/brokenlink/sunset.gif" alt="Beautiful Sunset" />
HTML Alternative Text Attribute:

The alt attribute is also an attribute that search engines may look for when displaying images. The text value contained within this attribute must reflect the substance of the image in order to receive "credit" from a search engine.

html - horizontally align images
Images can be aligned horizontally to the right or to the left of other elements using the align attribute. Image elements are aligned to the left by default.

align
right
left
HTML Align Attribute Code:
<img src="sunset.gif" align="right">

HTML Image Align: Right:

As you can see, the image's right edge has now been aligned with the right edge of the display box. Since the display box is the parent element, this is the desired behavior for the align attribute. If we take this example a step further, you can achieve some really great designs by embedding aligned images inside of paragraph <p> elements.

HTML Image Align Code:
<p>This is paragraph 1, yes it is...</p>
<p>
<img src="sunset.gif" align="right">
The image will appear along the...isn't it?
</p>
<p>This is the third paragraph that appears...</p>
Image Wrap Around:
This is paragraph 1, yes it is. I think this paragraph serves as a nice example to show how this image alignment works.

The image will appear along the right-hand side of the paragraph. As you can see this is very nice for adding a little eye candy that relates to the specified paragraph. If we were talking about beautiful tropical sunsets, this picture would be perfect. But we aren't talking about that, so it's rather a waste, isn't it?

This is the third paragraph that appears below the paragraph with the image!

html gifs vs. jpegs vs. pngs
Gifs are best used for banners, clip art, and buttons. The main reason for this is that .gif files can have transparent backgrounds -- a priceless attribute when it comes to web design. On the down side, .gif files are limited to only 256 colors and any .gif image containing more than a few colors tends to have a larger file size than their .jpeg or .png counterparts. Large picture files are a plague of web design!

Jpegs have an unlimited color wheel and a high compression rate, which downsizes your load times and saves on hard drive space. Although .jpeg (or .jpg) files don't allow for transparent backgrounds, their size/quality ratio is outstanding. It's best to use .jpeg files for photo galleries or artwork. Avoid using .jpeg files for graphical designs, though; stick to using them for thumbnails, backgrounds, and photo galleries.

PNG image files are the best of both worlds. They have a large color wheel, low file size, and allow for transparencies like .gif images do. With a high-compression rate and transparent coloring, they might just be the best format for any web graphics.

When in doubt, try saving an image in multiple formats and decide which is better, keeping file size and quality in mind.

Head


HTML Head Tutorial

Browser Support
   
The head section of a web page is a container element. It can contain a variety of items, including but not limited to:
the page title
meta data
links to external resources (style sheets, scripts, etc.)
embedded styles
JavaScripts
The title tag is the only required element. Here's an example:

<html>
<head>
<title>Your Page Title</title>
</head>
<body>
Your web page content...
</body>
</html>

Links


html - text links (anchors)
The World Wide Web got its spidery name from the plentiful connections (links) that link websites together with the click of a button. What most people don't know is that HTML links are actually HTML anchors constructed using anchor tags (<a>).
HTML Text Link:
<a>I am a text link!</a>
HTML Text Link:
I am a text link!
While the example above appears and feels like a text link when viewed through a web browser, the element is incomplete as it is missing a vital attribute that references another web page called a Hypertext Reference (href).

html - hypertext reference (href)
A Hypertext Reference (href) is an HTML attribute of an anchor (link) tag that requires a valid URL in order to properly direct a user to a different location. In other words, this Hypertext Reference is where users will navigate to if they do click on this link. Use the demonstration below as a reference.

HTML Text Link Code:
<a href="http://www.tizag.com/" target="_blank">Tizag Home</a>
<br />
<a href="http://www.espn.com/" target="_blank">ESPN Home</a>
<br />
<a href="http://www.yahoo.com/" target="_blank">Yahoo Home</a>
HTML Text Links:
Tizag Home
ESPN Home
Yahoo Home
The address of a website is called a Uniform Resource Locator (a URL), and it acts like a street address for a website as a user is directed from one site to another. There are different types of URLs, and each has a slightly different look. The examples above link to what are known as Global URLs, since they are external web addresses that do not reside on the Tizag.com domain. Here's a look at the different types of URLs.

HTML Text Link Code:
Global - href="http://www.cnn.com/" Links to other domains outside your website domain.
Local - href="../internal/mypage2.html"   Links to other pages within your website domain.
Internal - href="#anchorname" Links to anchors embedded in the current web page.
html - link targets
The target attribute defines how each link will open when clicked. Will each one open in a new window, or will each one open in the current browser window? As the web designer, you call the shots as to how a user navigates from page to page, so long as you know how to handle the target attribute.

Link Targets:
Target= Description
_blank Opens new page in a new browser window
_self Loads the new page in the current window
_parent Loads new page into a parent frame
_top Loads new page into the current browser window, cancelling all frames
The two most important values are the top two, (target="_blank" and target="_self"). The _self value is generally the default. It loads each new page in the current browser window, while _blank opens up a new web browser window with each click of the text link.

The code below shows how you would link to ESPN.com, a popular sports website. The target attribute is added to allow the browser to open ESPN in a new window, so that the viewer can have a window that remains opened to our website. Here's the example.

HTML Link Target Code:
<a href="http://www.ESPN.com" target="_blank">ESPN.COM</a>
_blank Target:
ESPN.COM
Links are a big part of the user experience for any website. Always try to keep that in mind when working on a site's navigation. A web page that opens a new web browser window each time a user clicks a link is not the greatest way to entice users to stick around.

html - email links
Creating an email link is simple. If you want people to mail you about your site, a good way to do it is place an email link with a subject line already filled out for them.

HTML Email Link Code:
<a href="mailto:email@tizag.com?subject=Feedback" >Email@tizag.com</a>
Email Links:
Email@tizag.com
In some circumstances, it may be necessary to fill in the body of the email for the user as well.

HTML Email Link Code:
<a href="mailto:email@tizag.com?subject=Feedback&body=Sweet site!">
Email@tizag.com</a>
Complete Email:
Email@tizag.com
html - download links
Placing files available for download is done in exactly the same fashion as placing text links. However, things become complicated if we want to place image links for download. The best solution for images is to use a thumbnail links, which we will discuss in the next lesson.

HTML Download Link Code:
<a href="http://www.tizag.com/pics/htmlT/blanktext.zip">Text Document</a>
Download a Text Document:
Text Document
html - default links; base
Use the <base> tag in the head element to set a default URL for all links on a page to go to. It's always a good idea to set a base tag just in case your links become bugged somewhere down the line. Usually, you should set your base to your home page.

HTML Base Link Code:
<head>
  <base href="http://www.tizag.com/" />
</head>

HTML Text formatting


HTML text formatting

If you want people to read what you have written, then structuring your text well is even more important on the Web than when writing for print. People have trouble reading wide, long, paragraphs of text on Web sites unless they are broken up well.

This section will teach you basic text formatting elements .

Whitespace and Flow:
Before you start to mark up your text, it is best to understand what HTML does when it comes across spaces and how browsers treat long sentences and paragraphs of text.

You might think that if you put several consecutive spaces between two words, the spaces would appear between those words onscreen, but this is not the case; by default, only one space will be displayed. This is known as white space collapsing. So you need to use special HTML tags to create multiple spaces.

Similarly, if you start a new line in your source document, or you have consecutive empty lines, these will be ignored and simply treated as one space. So you need to use special HTML tags to create more number of empty lines.



Centring Content - The <center> Element:
You can use <center> tag to put any content in the center of the page or any table cell.

Example:

<p>This is not in the center.</p>
<center>
<p>This is in the center.</p>
</center>
This will produce following result:

This is not in the center.

This is in the center.

Nonbreaking Spaces:
Suppose you were to use the phrase "12 Angry Men." Here you would not want a browser to split the "12" and "Angry" across two lines:

A good example of this technique appears in the movie "12 Angry Men."
In cases where you do not want the client browser to break text, you should use a nonbreaking space entity (&nbsp;) instead of a normal space. For example, when coding the "12 Angry Men" paragraph, you would use something similar to the following code:

<p>A good example of this technique appears in the movie "12&nbsp;Angry&nbsp;Men."</p>
Soft Hyphens:
Occasionally, you will want to allow a browser to hyphenate long words to better justify a paragraph. For example, consider the following code and its resulting output.

<p style="text-align: justify;"> The morbid fear of the number 13, or triskaidekaphobia, has plagued some important historic figures like Mahamiya and Nanao.</p>
In cases where you want a client browser to be able to hyphenate a word if necessary, use the soft hyphen entity (&shy;) to specify where a word should be hyphenated. So above example should be written as follows:

<p style="text-align: justify;"> Example for soft hyphen - The morbid fear of the number 13, or tri&shy;skai&shy;deka&shy;phobia, has plagued some important historic figures like Mahamiya and Nanao.</p>
This will produce following result:

Example for soft hyphen - The morbid fear of the number 13, or tri­skai­deka­phobia, has plagued some important historic figures like Mahamiya and Nanao.

NOTE: This may notwork with some web browsers.

Preserve Formatting - The <pre> Element:
Sometimes you want your text to follow the exact format of how it is written in the HTML document. In those cases, you can use the preformatted tag (<pre>).

Any text between the opening <pre> tag and the closing </pre> tag will preserve the formatting of the source document.

<pre>
function testFunction( strText ){
   alert (strText)
}
</pre>
This will produce following result:

function testFunction( strText ){
   alert (strText)
}

Horizontal Rules - The <hr /> Element
Horizontal rules are used to visually break up sections of a document. The <hr> tag creates a line from the current position in the document to the right margin and breaks the line accordingly.

For example you may want to give a line between two paragraphs as follows:

<p>This is paragraph one and should be on top</p>
<hr />
<p>This is paragraph two and should be at bottom</p>
This will produce following result:

This is paragraph one and should be on top

This is paragraph two and should be at bottom

Again <hr /> tag is an example of an empty element, where you do not need opening and closing tags, as there is nothing to go in between them.

Note: The <hr /> element has a space between the characters br and the forward slash. If you omit this space, older browsers will have trouble rendering the line break, while if you miss the forward slash character and just use <hr> it is not valid XHTML


Presentational Tags:

If you use a word processor, you are familiar with the ability to make text bold, italicized, or underlined; these are just three of the ten options available to indicate how text can appear in HTML and XHTML.

Bold Text - The <b> Element:
Anything that appears in a <b>...</b> element is displayed in bold, like the word bold here:

<p>The following word uses a <b>bold</b> typeface.</p>
This will produce following result:

The following word uses a bold typeface.



Italic Text - The <i> Element:
Anything that appears in a <i>...</i> element is displayed in italicized, like the word italicized here:

<p>The following word uses a <i>italicized</i> typeface.</p>
This will produce following result:

The following word uses a italicized typeface.



Underlined Text - The <u> Element:
Anything that appears in a <u>...</u> element is displayed with underline, like the word underlined here:

<p>The following word uses a <u>underlined</u> typeface.</p>
This will produce following result:

The following word uses a underlined typeface.



Strike Text - The <strike> Element:
Anything that appears in a <strike>...</strike> element is displayed with strikethrough, which is a thin line through the text:

<p>The following word uses a <strike>strikethrough</strike> typeface.</p>
This will produce following result:

The following word uses a strikethrough typeface.


Monospaced font - The <tt> Element:
The content of a <tt> element is written in monospaced font. Most fonts are known as variable-width fonts because different letters are of different widths (for example, the letter m is wider than the letter i). In a monospaced font, however, each letter is the same width.

<p>The following word uses a <tt>monospaced</tt> typeface.</p>
This will produce following result:

The following word uses a monospaced typeface.

To Become more comfortable - Do Online Practice

Superscript Text - The <sup> Element:
The content of a <sup> element is written in superscript; the font size used is the same size as the characters surrounding it but is displayed half a character.s height above the other characters.

<p>The following word uses a <sup>superscript</sup> typeface.</p>
This will produce following result:

The following word uses a superscript typeface.



Subscript Text - The <sub> Element:
The content of a <sub> element is written in subscript; the font size used is the same as the characters surrounding it, but is displayed half a character.s height beneath the other characters.

<p>The following word uses a <sub>subscript</sub> typeface.</p>
This will produce following result:

The following word uses a subscript typeface.



Larger Text - The <big> Element:
The content of the <big> element is displayed one font size larger than the rest of the text surrounding it.

<p>The following word uses a <big>big</big> typeface.</p>
This will produce following result:

The following word uses a big typeface.


Smaller Text - The <small> Element:
The content of the <small> element is displayed one font size smaller than the rest of the text surrounding it.

<p>The following word uses a <small>small</small> typeface.</p>
This will produce following result:

The following word uses a small typeface.


Grouping - The <div> and <span> Elements :
The <div> and <span> elements allow you to group together several elements to create sections or subsections of a page.

For example, you might want to put all of the footnotes on a page within a <div> element to indicate that all of the elements within that <div> element relate to the footnotes. You might then attach a style to this <div> element so that they appear using a special set of style rules.

The <div> element is used to group block-level elements together:

<div id="menu" align="middle" >
<a href="/index.htm">HOME</a> |
<a href="/about/contact_us.htm">CONTACT</a> |
<a href="/about/index.htm">ABOUT</a>
</div>

<div id="content" align="left" bgcolor="white">
<h5>Content Articles</h5>
<p>Actual content goes here.....</p>
</div>
This will produce following result:

HOME | CONTACT | ABOUT
Content Articles

Actual content goes here.....

The <span> element, on the other hand, can be used to group inline elements only. So, if you had a part of a sentence or paragraph you wanted to group together you could use the <span> element.

<div><p>This is the example of <span style="color:green">span tag</span> and the <span style="color:purple">div tag</span> alongwith CSS</p></div>
This will produce following result:

This is the example of span tag and the div tag alongwith CSS

These tags are commonly used with CSS to allow you to attach a style to a section of a page.


For a complete list of HTML Tags and related attributes please check reference to HTML Tags Reference.

Paragraphs


Paragraphs
Now that you have the basic structure of an HTML document, you can mess about with the
 content a bit.

Go back to your text editor and add this lines to your page:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>

<head>
<title>My first web page</title>
</head>

<body>
This is my first web page
How exciting
</body>

</html>
Look at the document in your browser.

You might have expected your document to appear as you typed it, on two lines, but
 instead you should see something like:

This is my first web page How exciting.

This is because web browsers don't usually take any notice of what line your code is on.It also doesn't take any notice of spaces  (you would get the same result if you typed "This is my first web page      How exciting").

If you want text to appear on different lines, you need to explicitly state that.

Change your two lines of content so that they look like this:


<p>This is my first web page</p>
<p>How exciting</p>
The p tag is for paragraph.

Look at the results of this. The two lines will now appear on two lines.

Think of the HTML content as if it were a book - with paragraphs where appropriate.

Emphasis
You can emphasise text in a paragraph using em (emphasis) and strong (strong emphasis). These are two ways of doing pretty much the same thing, although traditionally, browsers display em in italics and strong in bold.


<p>Yes, that <em>is</em> what I said. How <strong>very</strong> exciting.</p>
Line breaks
The line-break tag can also be used to separate lines like this:


This is my first web page<br />
How exciting
However, this method is over-used and shouldn't be used if two blocks of text are intended to
 be separate from one another (because if that's what you want to do you probably want the p tag).

Note that because there's no content involved with the line-break tag, there is no closing
tag and it closes itself with a "/" after the "br".

Headings


HTML Headings

HTML Headings are just the same as you have expected - 'Heading' or 'Title' of the webpage.
Headings are defined with <h1> to <h6> tags, 'h1' being the most important and prominent. The size of the text between these tags reduces with 'h2', 'h3' and so on. By default, headings are displayed in bold text.
Now let's write some HTML codes to test the headings in browser. Open notepad or any other text-editor and type the following html codes. Alternatively as usual, you can use HTML Code Tester.

HTML Headings Example

<html>
<head>
    <title>HTML Headings</title>
</head>
<body>
    <h1>
        This is 'h1' heading</h1>
    <h2>
        This is 'h2' heading</h2>
    <h3>
        This is 'h3' heading</h3>
    <h4>
        This is 'h4' heading</h4>
    <h5>
        This is 'h5' heading</h5>
    <h6>
        This is 'h6' heading</h6>
</body>
</html>

Save this webpage as 'headings.html' in html folder. Open this file in your web browser to see the difference between various html headings. Did you notice the gap between the html headings? This is because browsers add some empty space just before and after headings to make these more prominent.

Attributes.


Attributes

You can add attributes to a number of elements.

What is an attribute?

As you probably remember, elements give structure to a HTML document and tells the browser how you want your website to be presented (for example, <br /> informs the browser to make a line break). In some elements you can add more information. Such additional information is called an attribute.

Example 1:


<h2 style="background-color:#ff0000;">My friendship with HTML</h2>


Attributes are always written within a start tag and are followed by an equals sign and the attribute details written between inverted commas. The semicolon after the attribute is for separating different style commands. We will get back to that later.

What is the catch?

There are many different attributes. The first one you will learn is style. With the style attribute you can add layout to your website. For instance a background colour:

Example 2:


<html>

 <head>
 </head>

 <body style="background-color:#ff0000;">
 </body>

</html>


will show a completely red page in the browser - go ahead and see for yourself. We will explain in greater detail how the colour system works in a few moments.

Note that some tags and attributes use US spelling i.e. color instead of colour. It is important that you are careful to use the same spelling as we use in the examples in this tutorial - otherwise, browsers will not be able to understand your codes. Also, don't forget to always close the inverted commas (quotation marks) after an attribute.

How did the page become red?

In the above example, we asked for the background colour with the code "#ff0000". This is the colour code for red using so called hexadecimal numbers (HEX). Each colour has its own hexadecimal number. Here are some examples:

White: #ffffff
Black: #000000 (zeros)
Red: #ff0000
Blue: #0000ff
Green: #00ff00
Yellow: #ffff00

A hexadecimal colour code consists of # and six digits or letters. There are more than 1000 HEX codes and it is not easy to figure out which HEX code is tied to a specific colour. To make it easier we have made a chart of the 216 most commonly used colours: 216 Web Safe Colour Chart.

You can also use the English name for the most common colours (white, black, red, blue, green and yellow).

Example 3:


<body style="background-color: red;">


Enough about colours. Let's get back to the attributes.

Which elements can use attributes?

Different attributes can be applied to most elements.

You will often use attributes in tags such as the body tag while you will rarely use attributes in, for example, a br tag since a line break normally is a line break without any parameters to adjust.

Just as there are many different elements, so there are many different attributes. Some attributes are tailor made for one particular element while others can be used for many different element. And vice versa: some elements can only contain one kind of attribute while others can contain many.

It may sound a bit confusing but once you get acquainted with the different attributes it is actually very logical and you will soon see how easy they are to use and how many possibilities they provide.

This tutorial will introduce you to the most important attributes.