HTML and CSS Tutorial Chapter 1: Getting Started
Total Page:16
File Type:pdf, Size:1020Kb
HTML and CSS Tutorial This work is licensed under a Creative Commons License: Attribution 3.0 Unported. You are free: • to Share — to copy, distribute, and transmit the work • to Remix — to adapt the work Under the following conditions: • Attribution. You must attribute the work in the manner specified by the author or licensor. For attribution purposes, the URL to be associated with the work is http://www.goer.org/HTML, and the Title and Author of this work are as follows: “The Pocket HTML Tutorial” Copyright 2012 by Evan Goer. For any reuse or distribution, you must make clear to others the license terms of this work. Any of these conditions can be waived if you get permission from the copyright holder. Your fair use and other rights are in no way affected by the above. This is a human-readable summary of the Legal Code (the full license). Chapter 1: Getting Started Welcome to the world of HTML! If you have never written HTML code before, you should start with this section. If you’ve written HTML before, you should feel free to skip ahead to Chapter 2, Markup Basics. When you’re done with this chapter, you should be able to: • create and alter a simple web page • explain what HTML elements and attributes are • use your browser to help you view and reuse code from other websites Section Summaries The Getting Started chapter contains these sections: 1. Diving In — Demonstrates basic HTML concepts by diving in and creating a simple web page. 2. Structure — Examines document structure: the html element, the head, the title, and the body. 3. Tinkering — Makes alterations to the “Simple Web Page”. We’ll add text, change the background color, and explore how HTML handles whitespace. 4. Elements — Explores elements, which are the basic building blocks of a web page. What are the components of a element? What happens if you misspell a element? And is there a good use for the blink element? The answer might surprise you! 5. Attributes — Explores attributes, which modify the behavior of a element. We’ll look at the components of an attribute and talk about attribute whitespace, attribute misspellings, and other issues. 6. Browser Tools — Explains how to use key browser features, such as viewing source, viewing page properties, and saving images. Diving In The first thing we’re going to do is dive in and create a simple webpage. We’ll explore what’s going on soon, but for now, just follow the directions as best you can. To create your web page, you need: • A browser (you’re using one right now) • A plain text editor (you already have one on your machine) Step 1: Open a Text Editor A text editor is a program that edits plain text files. When authoring HTML, do not use Word, Wordpad, Pages, or any other program that does not save files as plain text. • Windows users: open Notepad ( Start | Programs | Accessories | Notepad ) • Mac OS X users: open vi or TextEdit ( Applications | TextEdit ) • Linux and other UNIX users: open vi or emacs Unfortunately, Notepad and TextEdit are not very good text editors. By default, Notepad always tries to save and open files with a .txt extension. You must constantly fight with it to make it save and open webpages (files with a .htm or .html extension). Be strong, and ye shall overcome. Likewise, TextEdit does not use plain text by default. Before using TextEdit to edit HTML, go into Preferences and set the Format for new documents to be Plain Text. For good measure, you might have to convert your existing document to plain text ( Format | Make Plain Text ). Using Notepad to write HTML is kind of like using a butter knife to cut a birthday cake — it’s the wrong tool for the job. A better choice would be a more advanced text editor, such as: • Crimson Editor (Windows, free) • Smultron (Mac OS X, free) • TextMate (Mac OS X, not free, but well worth the money) • jEdit (Cross-platform, free) All of these editors avoid the problems with file extensions and plain text described above, and have numerous extra features for advanced users. You don’t have to download one of these editors right now, but if you continue with web development, you’ll need to upgrade your tools. Step 2: Create and Save the Web Page Copy-and-paste the text in Example 1.1, “A Simple Webpage” into your text editor, starting with the text, "<html>". Example 1.1. A Simple Webpage view html view plain print ? 1. <html> 2. <head> 3. <title>A Simple Webpage</title> 4. </head> 5. <body> 6. This is a simple webpage. 7. </body> 8. </html> To see what this HTML code sample looks like as a web page, click the view html link above the code sample. The words that are surrounded with angle brackets (< >) are called elements. We will talk about what a element is soon, but first let’s finish creating and displaying your webpage. Once you have copied the text over to your text editor, save the file in your home directory or Desktop as the file simple.html. Note Notepad will try to append a .txt extension to your file name. Don’t let this happen. In the Save As dialog box, set File name to simple.html and change Save as type from Text Documents to All Files (*.*). Step 3: Display the Webpage in Your Browser Open the file simple.html by typing Ctrl-O (Cmd-O for Mac users) and selecting the file. Internet Explorer users should select Browse to find the file. After selecting simple.html, click Open. Your webpage should appear in the browser window. Compare it to Example 1.1. Does it match? (To compare results, click the view html link above Example 1.1.) If it does, congratulations! Let’s move on to the next section, where we’ll try to answer the question, what the heck is going on? Structure The previous section demonstrates how to create a simple web page. If you haven’t saved this example on your computer as the file simple.html, do so now. Example 1.2. A Simple Webpage view html view plain print ? 1. <html> 2. <head> 3. <title>A Simple Webpage</title> 4. </head> 5. <body> 6. This is a simple webpage. 7. </body> 8. </html> If you view simple.html in your browser, you will see the words “This is a simple webpage” on a white or grey background. Where did everything else go? And what are those words with the angle brackets, anyway? A Brief Introduction to Elements The web page simple.html uses these elements: html, head, title, and body. • Elements are delineated by angle brackets (< >). • Elements are “invisible”; they don’t directly appear in the web page. Instead, they serve as instructions to your browser. They can change your text’s appearance, create a link, insert an image, and much more. • An element starts with an opening tag (<element>) and ends with a closing tag (</element>). Some elements do not require closing tags. We’ll discuss the general properties of elements in some detail in Elements. For now, let’s focus on the particular elements in the “Simple Webpage” example. Structure of the Simple Webpage Although the “Simple Webpage” doesn’t look like much, its elements (html, head, title, and body) are fundamental to the structure of all HTML documents. Here’s what these elements mean: • <html>: “Here begins an HTML document.” The html element helps identify a file as an HTML document. • <head>: “Here begins the header.” The header contains elements that apply to the overall document. For example, it might contain elements that are designed for search engines to process or elements that change the overall appearance of the webpage. However, elements in the header do not display directly as normal webpage content. The reasons you would be concerned about the header are a bit abstract at this stage, so we won’t worry about it much until later. • <title>: “Here begins the document title.” (Must be in the header) If you view the file simple.html in your browser, along the top of your browser window you should see the words, “A Simple Webpage”. These words appear because of the title element. As an exercise, change the title of the simple.html webpage to, “My First Webpage”. Save your changes and view them by clicking the browser’s Refresh or Reload button. Titles might not seem important at first glance, but they’re actually quite useful. For example, if a search engine displays your page in a list of search results, it will probably display the title as your page’s title. If you omit the title element, the search engine will make up one for you. This is Not a Good Thing. Note You might have noticed that the title element is contained within the head element. Is this kosher? Absolutely! In fact, many elements are designed to contain other elements, and you will be nesting elements within other elements quite frequently as you continue. • <body>: “Here begins the body.” The body is where you put text and elements to be displayed in the main browser window. The reason that the words “This is a simple webpage” appear when you display simple.html is becaused you placed them in the body. So why do we only see “This is a simple webpage” when we display simple.html in a browser? The answer is, after you remove all the elements that are not designed to display in the browser window, the sentence “This is a simple webpage” is the only thing left.