Lesson 3html Elements
Lesson 3html Elements
HTML ELEMENTS
An HTML element usually consists of a start tag and an end tag, with the content inserted in
between:
The HTML element is everything from the start tag to the end tag:
<br>
HTML elements with no content are called empty elements. Empty elements do not have an
end tag, such as the <br> element (which indicates a line break).
NESTED ELEMENTS
Example
<!DOCTYPE html>
<html>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>
Example Explained
<html>
<body>
</body>
</html>
Inside the <body> element is two other HTML elements: <h1> and <p>.
<body>
</body>
HTML ATTRIBUTES
href Attribute
HTML links are defined with the <a> tag. The link address is specified in the href attribute:
Example
<a href="https://www.w3schools.com">This is a link</a>
src Attribute
Example
<img src="img_girl.jpg">
HTML images also have width and height attributes, which specifies the width and height of the
image:
Example
<img src="img_girl.jpg" width="500" height="600">
The width and height are specified in pixels by default; so width="500" means 500 pixels wide.
alt Attribute
The alt attribute specifies an alternative text to be used, if an image cannot be displayed.
The value of the alt attribute can be read by screen readers. This way, someone "listening" to
the webpage, e.g. a vision impaired person, can "hear" the element.
Example
<img src="img_girl.jpg" alt="Girl with a jacket">
The alt attribute is also useful if the image cannot be displayed (e.g. if it does not exist):
style Attribute
The style attribute is used to specify the styling of an element, like color, font, size etc.
Example
<p style="color:red">This is a red paragraph.</p>
lang Attribute
Declaring a language is important for accessibility applications (screen readers) and search
engines:
<!DOCTYPE html>
<html lang="en-US">
<body>
...
</body>
</html>
The first two letters specify the language (en). If there is a dialect, add two more letters (US).
title Attribute
Here, a title attribute is added to the <p> element. The value of the title attribute will be
displayed as a tooltip when you mouse over the paragraph:
Example
<p title="I'm a tooltip">
This is a paragraph.
</p>
The title attribute can be written with uppercase or lowercase like title or TITLE.
The HTML5 standard does not require quotes around attribute values.
W3C recommends quotes in HTML, and demands quotes for stricter document types like
XHTML.
Using quotes are the most common. Omitting quotes can produce errors.
At W3Schools we always use quotes around attribute values.
Double quotes around attribute values are the most common in HTML, but single quotes can
also be used.
In some situations, when the attribute value itself contains double quotes, it is necessary to use
single quotes:
Or vice versa:
Attribute Description
Alt Specifies an alternative text for an image, when the image cannot be
displayed
<html>
<head>
<title>Page title</title>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Note: Only the content inside the <body> section (the white area above) is displayed in a
browser.
The <!DOCTYPE> declaration represents the document type, and helps browsers to display
web pages correctly.
It must only appear once, at the top of the page (before any HTML tags).
<!DOCTYPE html>