In this beginner HTML tutorial we’ll show you how to create a simple HTML Web page. If you’re just starting out with Web design, then you’ll find this tutorial to be a handy introduction to the world of HTML!
What is HTML?
HTML (HyperText Markup Language) is the language used to create Web pages. HTML is pretty easy to work with. It uses a system called tags to specify things such as bold or italic text, or images, or links. Any text that is not in a tag is displayed as it is on the page
Tags, Attributes, and Elements
Although the basics of HTML are plain text, we need a bit more to make it a valid HTML document.
Tags
The basic structure of an HTML document includes tags, which surround content and apply meaning to it.
Change your document so that it looks like this:
This is my first web page
Now save the document again, go back to the web browser and select “refresh” (which will reload the page).
The appearance of the page will not have changed at all, but the purpose of HTML is to apply meaning, not presentation, and this example has now defined some fundamental elements of a web page.
The first line on the top that starts
<!DOCTYPE
… is to let the browser know that you know what the hell you’re doing. You may think that you don’t actually know what you’re doing yet, but it’s important to stick this in. If you don’t, browsers will switch into “quirks mode” and act in a very peculiar way. Don’t worry about this just yet, you can learn more about “document types” in the HTML Advanced Tutorial if you really want to. For the moment, just remember to shove this line at the top of your web pages and you’re laughin’.
To get back to the point,
<html>
is the opening tag that kicks things off and tells the browser that everything between that and the </html>
closing tag is an HTML document. The stuff between <body>
and</body>
is the main content of the document that will appear in the browser window.CLOSING TAGS
The
</body>
and </html>
close their respective tags. ALL HTML tags should be closed. Although older versions of HTML lazily allowed some tags not to be closed, latest standards require all tags to be closed. This is a good habit to get into anyway.
Not all tags have closing tags like this (
<html></html>
) some tags, which do not wrap around content will close themselves. The line-break tag for example, looks like this : <br />
. We will come across these examples later. All you need to remember is that all tags must be closed and most (those with content between them) are in the format of opening tag → content → closing tag.Attributes
Tags can also have attributes, which are extra bits of information. Attributes appear inside the opening tag and their value is always inside quotation marks. They look something like <tag attribute=”value”>Margarine</tag>. We will come across tags with attributes later.
Elements
Tags tend not to do much more than mark the beginning and end of an element. Elements are the bits that make up web pages. You would say, for example, that everything that is in-between and includes the
<body>
and </body>
tags is the body element. As another example, whereas ‘<title>
‘ and ‘</title>
‘ aretags, ‘<title>Rumple Stiltskin</title>
‘ is a title element.Page Titles
All HTML pages should have a page title.
To add a title to your page, change your code so that it looks like this:
My first web page This is my first web page
We have added two new elements here, that start with the
head
tag and the title
tag (and see how both of these close).
The head element (that which starts with the
<head>
opening tag and ends with the </head>
tag) appears before the body element (starting with <body>
and ending with </body>
) and contains information about the page. The information in the head element does not appear in the browser window.
We will see later on that other elements can appear inside the head element, but the most important of them is the title element.
If you look at this document in the browser (save and refresh as before), you will see that “My first web page” will appear on the title bar of the window (not the actual canvas area). The text that you put in between the title tags has become the title of the document (surprise!). If you were to add this page to your ‘favourites’ (or ‘bookmarks’, depending on your browser), you would see that the title is also used there.
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 another line to your page:
My first web page This is my first web page How exciting
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:
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.Yes, that <em>is</em> what I said. How <strong>very</strong> exciting.
Line breaks
The line-break tag can also be used to separate lines like this:
This is my first web page How exciting
Headings
Thep
tag is just the start of text formatting. If you have documents with genuine headings, then there areHTML tags specifically designed just for them. They areh1
,h2
,h3
,h4
,h5
andh6
,h1
being the almighty emperor of headings andh6
being the lowest pleb. Change your code to the following:My first web page</pre> <h1>My first web page</h1> <h2>What this is</h2> <pre> A simple page put together using HTML</pre> <h2>Why this is</h2> <pre> To learn HTMLNote that theh1
tag is only used once – it is supposed to be the main heading of the page and shouldn’t be used multiple times.h2
toh6
however, can be used as often as you desire, but they should always be used in order, as they were intended. For example, anh4
should be a sub-heading of anh3
, which should be a sub-heading of anh2
.Lists
There are three types of list; unordered lists, ordered lists and definition lists. We will look at the first two here, and definition lists in the HTML Intermediate Tutorial.Unordered lists and ordered lists work the same way, except that the former is used for non-sequential lists with list items usually preceded by bullets and the latter is for sequential lists, which are normally represented by incremental numbers. Theul
tag is used to define unordered lists and theol
tag is used to define ordered lists. Inside the lists, theli
tag is used to define each list item. Change your code to the following:My first web page </pre> <h1>My first web page</h1> <h2>What this is</h2> <pre> A simple page put together using HTML</pre> <h2>Why this is</h2> <ul> <li>To learn HTML</li> <li>To show off</li> <li>Because I've fallen in love with my computer and want to give her some HTML loving.</li> </ul> <pre>If you look at this in your browser, you will see a bulleted list. Simply change theul
tags tool
and you will see that the list will become numbered. Lists can also be included in lists to form a structured hierarchy of items. Replace the above list code with the following:</pre> <ul> <li>To learn HTML</li> <li>To show off <ol> <li>To my boss</li> <li>To my friends</li> <li>To my cat</li> <li>To the little talking duck in my brain</li> </ol> </li> <li>Because I've fallen in love with my computer and want to give her some HTML loving.</li> </ul> <pre>A list within a list. And you could put another list within that. And another within that. And so on and so forth.Links
So far you’ve been making a stand-alone web page, which is all very well and nice, but what makes the internet so special is that it all links together. The ‘H’ and ‘T’ in ‘HTML‘ stand for ‘hypertext‘, which basically means a system of linked text. An anchor tag (a
) is used to define a link, but you also need to add something to the anchor tag – the destinationof the link. Add this to your document:My first web page </pre> <h1>My first web page</h1> <h2>What this is</h2> <pre> A simple page put together using HTML</pre> <h2>Why this is</h2> <pre> To learn HTML</pre> <h2>Where to find the tutorial</h2> <pre> <a href="http://www.w3programmers.com">HTML Dog</a>The destination of the link is defined in thehref
attribute of the tag. The link can be absolute, such as “http://www.w3programmers.com”, or it can be relative to the current page. So if, for example, you had another file called “flyingmoss.html” then the line of code would simply be<a href="flyingmoss.html">The miracle of moss in flight</a>or something like this. A link does not have to link to another HTML file, it can link to any file anywhere on the web. A link can also send a user to another part of the same page they are on. You can add anid
attribute to just about any tag, for example<h2 id="moss">Moss</h2>
, and then link to it by using something like this:<a href="#moss">Go to moss</a>Selecting this link will scroll the page straight to the element with that id. Thea
tag allows you to open the link in a newly spawned window, rather than replacing the web page the user is on, which at first thought may sound like a good idea as it doesn’t take the user away from your site. There are a number of reasons why you shouldn’t do this however. From a usability point of view, this method breaks navigation. The most commonly used navigation tool on a browser is the “back” button. Opening a new window disables this function. On a wider, more general usability point, users do not want new windows to be popping up all over the place. If they want to open a link in a new window then they can choose to do so themselves.Images
Things might seem a little bland and boring with all of this text formatting. Of course, the web is not just about text, it is multi-media and the most common form of media is the image. Theimg
tag is used to put an image in an HTMLdocument and it looks like this:</pre> <img src="http://www.w3programmers.com/images/logo.gif" alt="HTML Dog logo" width="157" height="70" /> <pre></pre> <img src="http://www.w3programmers.com/images/logo.gif" alt="HTML Dog logo" width="157" height="70" /> <pre>hesrc
attribute tells the browser where to find the image. Like thea
tag, this can be absolute, as the above example demonstrates, but is usually relative.For example, if you create your own image and save it as “alienpie.jpg” in a directory called “images” then the code would be<img src="images/alienpie.jpg"...
Thewidth
andheight
attributes are necessary because if they are excluded, the browser will tend to calculate the size as the image loads, instead of when the page loads, which means that the layout of the document may jump around while the page is loading.Thealt
attribute is the alternative description. This is used for people who cannot or choose not to view images. This is a requirement in the latest versions of HTML. Note that, like thebr
tag, because theimg
tag does not have a closing tag, it closes itself, ending with “/>
“Tables
Across the worldwide web, HTML tables are used and abused to layout pages. We will come across how to layout a page without tables, in the CSS Advanced Tutorial. The correct use for tables is to do exactly what you would expect a table to do – to structure tabular data.There are a number of tags used in tables, and to fully get to grips with how they work is probably the most difficult area of this HTML Beginners Tutorial. Copy the following code into the body of your document and then we will go through what each tag is doing:</pre> <table> <tbody> <tr> <td>Row 1, cell 1</td> <td>Row 1, cell 2</td> <td>Row 1, cell 3</td> </tr> <tr> <td>Row 2, cell 1</td> <td>Row 2, cell 2</td> <td>Row 2, cell 3</td> </tr> <tr> <td>Row 3, cell 1</td> <td>Row 3, cell 2</td> <td>Row 3, cell 3</td> </tr> <tr> <td>Row 4, cell 1</td> <td>Row 4, cell 2</td> <td>Row 4, cell 3</td> </tr> </tbody> </table> <pre>The table element defines the table. The tr element defines a table row. The td element defines a data cell. These must be enclosed in tr tags, as shown above. If you imagine a 3×4 table, which is 12 cells, there should be four tr elements to define the rows and three td elements within each of the rows, making a total of 12 td elements.Forms
Forms can be used to send data across the web and are often used as contact forms, registration forms and login formsOn their own, forms are useless. They need to be hooked up to a program that will process the data inputted by the user. These take all manner of guises and are outside of the remit of this website. If you use an internet service provider to host your HTML, they will be able to help you with this and will probably have clear and simple instructions on how, for example, to make a form-to-email form work. The basic tags used in the actual HTML of forms are form, input, textarea, select and option. form defines the form and within this tag, there is one required action attribute which tells the form where its contents will be sent to when it is submitted. The optional method attribute tells the form how the data in it is going to be sent and it can have the value get (which is default) or post. This is commonly used, and often set to post which hides the information (get latches the information onto the URL). So a form element will look something like this:</pre> <form action="processingscript.php" method="post"></form> <pre>Theinput
tag is the daddy of the form world. It can take ten forms, outlined below:
- <input type=”text” /> is a standard textbox. This can also have a
value
attribute, which sets the initial text in the textbox.- <input type=”password” /> is similar to the textbox, but the characters typed in by the user will be hidden.
- <input type=”checkbox” /> is a checkbox, which can be toggled on and off by the user. This can also have a checked attribute, which would be used in the format
<input type="checkbox" checked="checked" />
, and makes the initial state of the check box to be switched on, as it were.- <input type=”radio” /> is similar to a checkbox, but the user can only select one radio button in a group. This can also have a
checked
attribute, used in the same way as the checkbox.- <input type=”file” /> is an area that shows the files on your computer, like you see when you open or save a document in most programs, and is used to enable users to upload files.
- <input type=”submit” /> is a button that when selected will submit the form. You can control the text that appears on the submit button (as you can with
button
andreset
types – see below) with thevalue
attribute, for example<input type="submit" value="Ooo. Look. Text on a button. Wow" />
.- <input type=”image” /> is an image that will submit the coordinates of where the user clicked on it. This also requires a
src
attribute, like theimg
tag.- <input type=”button” /> is a button that will not do anything without extra code added.
- <input type=”reset” /> is a button that when selected will reset the form fields to their default values.
- <input type=”hidden” /> is a field that will not be displayed and is used to pass information such as the page name that the user is on or the email address that the form should be posted to.
Note that the input tag closes itself with a “/>
” at the end. A textarea is, basically, a large textbox. It requires arows
andcols
attribute and is used like this:<
textarea
rows
=
"5"
cols
=
"20"
>A big load of text here</
textarea
>
The
select
tag works with theoption
tag to make drop-down select boxes. They work like this:<select> <option value="first option">Option 1</option></select> <select> <option value="second option">Option 2</option></select> <select> <option value="third option">Option 3</option></select>When the form is submitted, the value of the selected option will be sent. Similar to thechecked
attribute of checkboxes and radio buttons, anoption
tag can also have aselected
attribute, which would be used in the format<option value="mouse" selected="selected">Rodent</option>All of the tags mentioned above will look very nice presented on the page, but if you hook up your form to a form-handling program, they will all be ignored. This is because the form fields need names. So to all of the fields, the attributename
needs to be added, for example
<
input
type
=
"text"
name
=
"talkingsponge"
/>
A form might look like the one below. (Note: this form will not work unless there is a “contactus.php” file, which is stated in theaction
attribute of theform
tag, to handle the submitted date)</pre> <form action="contactus.php" method="post"> Name: <input type="text" name="name" value="Your name" /> Comments: <textarea name="comments" rows="5" cols="20">Your comments</textarea> Are you: <input type="radio" name="areyou" value="male" /> Male <input type="radio" name="areyou" value="female" /> Female <input type="radio" name="areyou" value="hermaphrodite" /> An hermaphrodite <input type="radio" name="areyou" value="asexual" checked="checked" /> Asexual <input type="submit" /> <input type="reset" /></form> <pre>Putting It All Together
If you have gone through all of the pages in this HTML Beginner Tutorial then you should be a competent HTMLer.In fact, due to the fact that most people who use HTML use it rather badly, you should be better than most.The following code incorporates all of the methods that have been explained above:My first web page <!-- By the way, this is a comment --> </pre> <h1>My first web page</h1> <h2>What this is</h2> <pre> A simple page put together using HTML. <strong>A simple page put together using HTML.</strong> A simple page put together using HTML. A simple page put together using HTML. A simple page put together using HTML. A simple page put together using HTML. A simple page put together using HTML. A simple page put together using HTML. A simple page put together using HTML.</pre> <h2>Why this is</h2> <ul> <li>To learn HTML</li> <li>To show off <ol> <li>To my boss</li> <li>To my friends</li> <li>To my cat</li> <li>To the little talking duck in my brain</li> </ol> </li> <li>Because I've fallen in love with my computer and want to give her some HTML loving.</li> </ul> <h2>Where to find the tutorial</h2> <pre> <a href="http://www.w3programmers.com"><img src="http://www.w3programmers.com/images/logo.gif" alt="HTML Dog logo" width="157" height="70" /></a></pre> <h3>Some random table</h3> <table border="1"> <tbody> <tr> <td>Row 1, cell 1</td> <td>Row 1, cell 2</td> <td>Row 1, cell 3</td> </tr> <tr> <td>Row 2, cell 1</td> <td>Row 2, cell 2</td> <td>Row 2, cell 3</td> </tr> <tr> <td>Row 3, cell 1</td> <td>Row 3, cell 2</td> <td>Row 3, cell 3</td> </tr> <tr> <td>Row 4, cell 1</td> <td>Row 4, cell 2</td> <td>Row 4, cell 3</td> </tr> </tbody> </table> <h3>Some random form</h3> <pre> <strong>Note:</strong> It looks the part, but won't do a damned thing</pre> <form action="somescript.php" method="post"> Name: <input type="text" name="name" value="Your name" /> Comments: <textarea name="comments" rows="10" cols="20">Your comments</textarea> Are you: <input type="radio" name="areyou" value="male" /> Male <input type="radio" name="areyou" value="female" /> Female <input type="radio" name="areyou" value="hermaphrodite" /> An hermaphrodite <input type="radio" name="areyou" value="asexual" checked="checked" /> Asexual <input type="submit" /> <input type="reset" /></form> <pre>There you have it. Save the file and play around with it – this is the best way to understand how everything works.
0 comments:
Post a Comment