HTMLCourse Basic/Notes/Week1

From MozillaWiki
Jump to: navigation, search

Welcome to HTML

What is it?

HTML is a markup language. It's not a programming language because it doesn't understand logic; it simply contains information about the structure and display of a document. HTML is a type of XML, or Extensible Markup Language, which is just a more general markup language.

Intro to Tags

HTML is made up of tags. Tags give information about the content and display of text. Tags are simply little pieces of text starting with the less-than sign (<) and ending with the greater-than sign (>).

<p>This is the tag for a paragraph</p>

Of course, the greater than and less than signs are just text too, which means that an HTML page is essentially a text document. Look at an HTML page in a web browser, though, and it will interpret all those tags as instructions and display everything else. On the other hand, if you open a web page in a text editor like Notepad or BBEdit, you'll see the tags themselves.

With a few exceptions, all HTML tags must have an opening tag and a closing tag. If you want to make text bold for instance, you'll use the "strong" tag, <strong>, then type the text you want to make bold. When you are ready to go back to non-bold text, you'll close the strong tag like this: </strong> and continue with your text.

So, in the text editor view, you'd see:

<p>This paragraph has some <strong>bold information</strong> in it.</p> 

But in the browser, you'll see:

This paragraph has some bold information in it.

The Skeletal System

There are a few main tags that every web page must have. They are <html>,<head>,<title>, and <body>. So the very simplest web page looks like this in text view:

<html>
<head>
<title>The name of my page</title>
</head>

<body><
<p>Some information here.</p>
</body></html>

Those few lines are the basics you'll need to begin any web page. In fact, you can copy and paste them into a text editor to get started making a page. Once we're copying and pasting, though, there's one more line you should add. The doctype is a special line at the beginning of an HTML document that tells the browser which version of HTML to use. In versions before HTML5, the doctype included a long URL that linked to the specification. You'll still see this type, for example:

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

However, with HTML5, things get a lot easier. Here's the doctype we'll be using:

<!DOCTYPE HTML>

Our HTML skeleton now looks like this:

<!DOCTYPE HTML>
 <html>
 <head>
  <title>The name of my page</title>
 </head>
 <body>
  <p>Some information here.</p>
 </body>
</html>

Exercise

Create a basic html skeleton page in Text Wrangler, and open it in a browser.

Once it works in one browser, try opening it in a different browser to see if it renders differently.

The DOM

As you can see, HTML tags are nested: everything is inside html tag, the title is inside the head, and most everything else inside body.

<img src="nestingdolls_lg.jpg" width="345" height="292">

The hierarchical, nested structure of HTML is sometimes referred to as the Document Object Model, or DOM.The document is represented as a tree structure, with parents and children -- think of a family tree. Being able to access elements of the tree comes into play when we want to manipulate the HTML with CSS and especially with Javascript.

Some useful tags

<title>

<p>The title tag goes inside the head tag and is used to name the window in the browser. You don't see the title in the page itself (that's why it's in <head> and not <body>).


<h1>,<h2>,<h3> etc

These are header tags. h1 is the largest, then h2, etc. If you think in terms of an outline, h1 is for the top level item/s, then the next level down would use h2 tags, and so on, down to h6.

<p> and <br/>

The <p> tag wraps paragraphs. In general, most of the text of your page will be within p tags. The <p> tag makes a visible space between one paragraph and the next. Sometimes you may not want to start a whole new paragraph, and just insert a line break. The tag for that is <br/> , or break. You'll notice that <br/> has a slash at the end of it. That's because it doesn't wrap anything within it; it's just a break in the text. So instead of having an opening and closing tag like most tags do, it's all squished into one.

<p>Here is my paragraph. I'd like to end this line<br/>and move on to the next line.</p>

Notice that in the html code, this is all one line. But in the browser, you'll see the line break where you put the <br/> tag. Similarly, if you have line breaks in your code, they won't show up unless you use the break tag.

<p>Here is my paragraph. 
I'd like to end 
this line<br/>and move on to 
the next line.</p>

The piece of code above will display in the browser exactly the same way the code before it does. Line breaks in your text don't matter.

A little theory

Ideally, HTML tags should tell you something about what the different parts of the document are for. If some text is in an H1 tag, it should not be because it just needs to be a certain size, but that it is the main subject -- the first header-- of the page. To control the look of text, the best practice is to use CSS (Cascading Style Sheets) instead of relying on the HTML for the display. The idea is to separate the content and the visual display, so that an HTML page tells you about the structure of the document but can be displayed in a variety of ways. This concept is sometimes referred to as the Semantic Web.

Exercise

Create an outline in your HTML using header and paragraph tags.

Some examples: a table of contents, a proposal, notes for a presentation.


More tags

<a> : the link tag. The most important tag.

It wouldn't really be the web if pages and sites weren't linked together. So the link tag, the <a> tag is some ways the most important tag to know. This is what it looks like:

You can read more on <a href="mypage.html">my page</a> or just try <br><a href="http://google.com">googling</a> it.

The a tag requires an attribute of 'href', which is short for hyperlink reference -- this is where you put the URL of the link. You can link to a page on your own site by using the name of the page, as in the first line above. (Of course, if it's in a different directory than the page you're linking from, you'll need to put the name of the directory too: "articles/brown.html".) You can also use a full URL, starting with 'http', like the link to google in the the example above.

Exercise

Create a new HTML page and link to it from your first page.

<img>

The image tag is another tag that doesn't open and close. It's also a tag that requires an attribute.

<img src="opal.jpg" />

In this case, the attribute you need is 'src', which is short for source. In other words, the source of the image: tell the tag which image you want to display.

Exercise

Add an image to your page.

lists <ul>, <ol>, <li>

There are lots of reasons to use lists in HTML. They are often used to display navigation menus, for example:

<ul>
<li><a href="index.html">Home</a></li>
<li><a href="news.html">Breaking News</a></li>
<li><a href="about.html">About Us</a></li>
<li><a href="contact.html">Contact Us</a></li>
</ul>

The <ul> tag, or unordered list tag, encloses each of the list items, which are wrapped in <li> tags. In the example above, each list item consists of a link, so you have <a> tags too.

When you need a numbered list, use the <ol>, or ordered list, tag:

<h1>Top 5 complaints</h1>
<ol>
<li>It's too loud.</li>
<li>I am hungry.</li>
<li>I am sleepy.</li>
<li>I hate the phone.</li>
<li>Facebook stole my identity</li>
</ol>

Exercise

Create a site navigation using the <ul> and <li> tags. (Next week, when we cover CSS, we'll style these to look like a navigation bar.) </div>

tables <table>,<tr>,<td>,<th>

Tables are used to diplay data in a grid. Think, spreadsheet. Whereas Excel and Word show you the grid literally, in HTML, you put tags around the different elements in the table.

The entire table begins and ends with the <table> tag.

Next come the rows, the <tr> tags. Within each row, there are table cells, denoted by <td>, or table data, tags. For your table to display properly, there should be the same number of cells in every row. (There's an exception to that: look up 'rowspan' and 'colspan' if you want to know.)

Because we write HTML tables one row at a time, there are no columns, per se, as there are in Excel. However, there is the table header, or, <th> tag, which you can use in the first row of your table to label the columns.

<table>
<tr><th>Name</th><th>Number</th></tr>
<tr><td>Jose</td><td>154</td></tr>
<tr><td>Gladiola</td><td>231</td></tr>
<tr><td>Francois</td><td>98</td></tr>
</table>

In the browser, you'll see this:

Name Number
Jose 154
Gladiola 231
Francois 98

Back in the day, tables were used a lot to layout visual elements on a page. If you're working on a website that was coded several years ago, you may encounter a page of tables within tables. Now that CSS styles are available, we don't have to build sites that way, but it's important to recognize table-based layouts when you see them so you know what's going on.

Exercise

Code a table in HTML.

Forms

Forms are used to get information from users to a web application. In order to process the data, you need a programming language like PHP or Javascript. The form itself, though, is all HTML.

Here's an example of a simple form:

<form method="post" action="myscript.php">
  <p>First Name  <input type="text" name="firstname" /></p>
  <p>Last Name  <input type="text" name="lastname" /></p>
  <p><input type="submit" name="Submit" value="Go" /></p>
</form>

The wrapping tag here is <form>. Inside that tag, you'll have <input> tags, which are the fields the user will fill out. This form has two basic fill-in-the blank input tags, for first name and last name. The last input tag is a submit button. The type of input field is set in the type attribute inside the input tag. There are also input types for radio buttons, which let the user choose one option of many, and checkboxes, which let the user choose as many of the option as they like.

Here's an example of radio buttons and checkboxes:

    <p>Choose one</p>
    <input type="radio" name="dairy" value="Milk" /> Milk<br>
    <input type="radio" name="dairy" value="Butter" /> Butter<br>
    <input type="radio" name="dairy" value="Cheese" /> Cheese
    
    <p>Choose one or more</p>
    <input type="checkbox" name="option1" value="Beer" /> Beer<br>
    <input type="checkbox" name="option2" value="Wine" checked="checked" /> Wine<br>
    <input type="checkbox" name="option3" value="Coffee" /> Coffee<br>
    <input type="checkbox" name="option4" value="Juice" /> Juice<br>
    <input type="checkbox" name="option5" value="Water" /> Water<br>

Resources

HTML Tutorial at W3C schools: http://www.w3schools.com/html/default.asp

HTML Reference: http://www.w3schools.com/tags/default.asp

HTML Cheat Sheet: http://www.webmonkey.com/2010/02/html_cheatsheet/