Professional Documents
Culture Documents
2 - HTML
2 - HTML
2 - HTML
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Creating an HTML Document
• The Outer Structure
• The Metadata
• The Content
The Outer Structure
• There are two elements that provide the outer structure of an HTML
document—the DOCTYPE and html elements.
<!DOCTYPE HTML>
<html>
<!-- elements go here -->
</html>
• A text header, denoted using the <h1>, <h2>, <h3>, <h4>, <h5>, <h6> tags.
• A paragraph, denoted using the <p> tag.
• A horizontal ruler, denoted using the <hr> tag.
• A link, denoted using the <a> (anchor) tag.
• A list, denoted using the <ul> (unordered list), <ol> (ordered list) and <li> (list
element) tags.
• An image, denoted using the <img> tag
• A divider, denoted using the <div> tag
• A text span, denoted using the <span> tag
Types of Attributes
• Local
The href attribute is local to the a element.
• Global
There are a set of global attributes that can be applied to any
HTML element.
Local Attributes
• href use a tag is a local attribute.
Global Attributes
1. The accesskey Attribute
2. The class Attribute
3. The contenteditable Attribute
4. The contextmenu Attribute
5. The dir Attribute
6. The draggable Attribute
7. The dropzone Attribute
8. The hidden Attribute
9. The id Attribute
10. The lang Attribute
11. The spellcheck Attribute
12. The style Attribute
13. The tabindex Attribute
14. The title Attribute
The accesskey Attribute
• The accesskey attribute lets you specify one or more keyboard shortcuts that will select the element on
the page.
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
</head>
<body>
<form>
Name: <input type="text" name="name" accesskey="n"/>
<p/>
Password: <input type="password" name="password" accesskey="p"/>
<p/>
<input type="submit" value="Log In" accesskey="s"/>
</form>
</body>
</html>
The accesskey Attribute Example
• In this example, I have added the accesskey attribute to three input
elements.
• The idea is to enable users who are regular users of a page or site to
use keyboard shortcuts to move between commonly used elements.
• The key combination required to trigger the accesskey setting differs
between platforms—for Windows, it is the Alt key and the accesskey
value pressed together.
The accesskey Attribute Example
• You can see the effect of the accesskey attribute.
• I press Alt+n to focus on the first input element and enter my name.
• I then press Alt+p to focus on the second input element and enter my
password.
• Alt+s presses the Log In button, which submits the form.
The class Attribute
• The class attribute is used to classify or categorize elements. You usually do this so that
you can locate elements in the document that belong to a given class or to apply a CSS
style.
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
</head>
<body>
<a class="class1 class2" href="http:// WebName.com">web site</a>
<p/>
<a class="class2 otherclass" href="http://w3c.org">W3C web site</a>
</body>
</html>
The contenteditable Attribute
• The contenteditable attribute is new in HTML5 and allows the user to change the
content in the page.
• Setting the attribute value to true allows the user to edit the element contents, and
setting it to false disables this feature.
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
</head>
<body>
<p contenteditable="true">It is raining right now</p>
</body>
</html>
The contextmenu Attribute
• The contextmenu attribute allows you to define context menus for
elements. These menus pop up when the user triggers them (for
example, when a Windows PC user right-clicks). At the time of this
writing, no browser supports the contextmenu attribute.
The dir Attribute
• The dir attribute specifies the direction of an element’s text. The two
supported values are ltr (for leftto- right text) and rtl (for right-to-left text).
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
</head>
<body>
<p dir="rtl">This is right-to-left</p>
<p dir="ltr">This is left-to-right</p>
</body>
</html>
The draggable and dropzone Attribute
• The draggable Attribute
The draggable attribute is part of the HTML5 support for drag
and drop, and it is used to indicate whether an element can be
dragged.
• The dropzone Attribute
The dropzone attribute is part of the HTML5 support for drag and
drop. It is the counterpart to the draggable attribute I just described.
The hidden Attribute
• The hidden attribute is a Boolean attribute that indicates an element is not
presently relevant. Browsers interpret this attribute by hiding the element from
view. When the hidden attribute is applied to an element, the browser doesn’t
render it at all. It is as though it were not contained in the HTML.
<body>
<button onclick="toggleHidden()">Toggle</button>
<table>
<tr><th>Name</th><th>City</th></tr>
<tr><td>Adam Freeman</td><td>London</td></tr>
<tr id="toggle" hidden><td>Joe Smith</td><td>New York</td></tr>
<tr><td>Anne Jones</td><td>Paris</td></tr>
</table>
</body>
The id Attribute
• The id attribute is used to assign a unique identifier to an element. These identifiers
are commonly used to apply styles to an element or to select an element with
JavaScript.
<body>
<a href="http:// WebName.com">web site</a>
<p/>
<a id="w3clink" href="http://w3c.org">W3C web site</a>
</body>
• Tip The id attribute can also be used to navigate to a particular section in a document.
If you imagine a document called example.html that contains an element with an id
attribute value of myelement, you can navigate directly to the element by requesting
example.html#myelement. This last part of the URL (the # plus the element id) is
known as the URL fragment identifier.
The lang Attribute
• The lang attribute is used to specify the language of an element’s
contents.
• The value for the lang attribute must be a valid ISO language code.
<body>
<p lang="en">Hello - how are you?</p>
<p lang="fr">Bonjour - comment êtes-vous?</>
<p lang="es">Hola - ¿cómo estás?</p>
</body>
The spellcheck Attribute
• The spellcheck attribute is used to specify if the browser should check
the spelling of an element’s content. Using this attribute makes sense
only when it is applied to an element the user can edit.
• The permitted values for the spellcheck attribute are true (spellchecking
is enabled) and false (spellchecking is disabled). The way that
spellchecking is implemented differs between browsers. Other browsers
require the user to explicitly perform a spellcheck.
<body>
<textarea spellcheck="true">This is some mispelled text</textarea>
</body>
The style Attribute
• The style attribute allows you to define a CSS style directly on an
element
<body>
<a href="http://WebName.com" style="background: grey; color:white;
padding:10px;“>
Visit the site
</a>
</body>
The tabindex Attribute
• The tabindex attribute allows you to control the order in which the Tab key moves the focus
through the HTML page, overriding the default order.
• A tabindex value of -1 ensures that an element will not be selected when the user presses
the Tab key.
<body>
<form>
<label>Name: <input type="text" name="name" tabindex="1"/></label>
<p/>
<label>City: <input type="text" name="city" tabindex="-1"/></label>
</p>
<label>Country: <input type="text" name="country" tabindex="2"/></label>
</p>
<input type="submit" tabindex="3"/>
</form>
</body>
The title Attribute
• The title attribute provides additional information about an element,
which is commonly used by the browser to display tool tip
information.
<body>
<a title=”Book Publishing" href="http://WEBName.com">Visit the
WebSite</a>
</body>
Using HTML Entities
• As you can see from the examples in this chapter, there are some
characters that have special meaning in HTML document—the
obvious ones being the < and > characters. You will sometimes need
to use these characters in your content without wanting them to be
interpreted as HTML. To do this, you use HTML entities. An entity is a
code the browser substitutes for the special character.
HTML Entities
Character Entity Name Entity Number
< < <
€ € €
£ £ £
§ § §
© © ©
® ® ®
™ ™ ™