HTML attributes are special words used inside the opening tag to control the element's behaviour. HTML attributes are a modifier of a HTML element type.
The <a>
tag defines a hyperlink. The href
attribute specifies the URL of the page the link goes to:
Example:
HTML
<!DOCTYPE html>
<html>
<body>
<h2>The href Attribute</h2>
<p>HTML links are defined with the a tag. The link address is specified in the href attribute:</p>
<a href="https://itupto.com">Visit Site</a>
</body>
</html>
The <img>
tag is used to embed an image in an HTML page. The src
attribute specifies the path to the image to be displayed:
src="https://itupto.com/avatar.png"
Example:
HTML
<!DOCTYPE html>
<html>
<body>
<h2>The src Attribute</h2>
<p>HTML images are defined with the img tag, and the filename of the image source is specified in the src attribute:</p>
<img src="https://itupto.com/avatar.png" width="100" height="100">
</body>
</html>
The <img>
tag should also contain the width
and height
attributes, which specify the width and height of the image (in pixels):
width="200" height="200"
Example:
HTML
<!DOCTYPE html>
<html>
<body>
<h2>Width and Height Attributes</h2>
<p>The width and height attributes of the img tag, defines the width and height of the image:</p>
<img src="https://itupto.com/avatar.png" width="200" height="200">
</body>
</html>
The required alt
attribute for the <img>
tag specifies an alternate text for an image, if the image for some reason cannot be displayed. This can be due to a slow connection, or an error in the src
attribute, or if the user uses a screen reader.
alt="Girl with a jacket"
Example:
HTML
<!DOCTYPE html>
<html>
<body>
<h2>The alt Attribute</h2>
<p>The alt attribute should reflect the image content, so users who cannot see the image get an understanding of what the image contains:</p>
<img src="https://itupto.com/avatar.png" alt="Girl with a jacket" width="200" height="200">
</body>
</html>
Example:
HTML
<!DOCTYPE html>
<html>
<body>
<img src="img_typo.jpg" alt="Girl with a jacket">
<p>If we try to display an image that does not exist, the value of the alt attribute will be displayed instead. </p>
</body>
</html>
The style
attribute is used to add styles to an element, such as color, font, size, and more.
style="color:red;"
Example:
HTML
<!DOCTYPE html>
<html>
<body>
<h2>The style Attribute</h2>
<p>The style attribute is used to add styles to an element, such as color:</p>
<p style="color:red;">This is a red paragraph.</p>
</body>
</html>
You should always include the lang
attribute inside the <html>
tag, to declare the language of the Web page. This is meant to assist search engines and browsers.
The following example specifies English as the language:
<html lang="en">
<!DOCTYPE html>
<html lang="en">
<body>
...
</body>
</html>
<!DOCTYPE html>
<html lang="en-US">
<body>
...
</body>
</html>
The title
attribute defines some extra information about an element.
The value of the title attribute will be displayed as a tooltip when you mouse over the element:
Example:
HTML
<!DOCTYPE html>
<html>
<body>
<h2 title="I'm a header">The title Attribute</h2>
<p title="I'm a tooltip">Mouse over this paragraph, to display the title attribute as a tooltip.</p>
</body>
</html>
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:
<p title='John "ShotGun" Nelson'>
<p title="John 'ShotGun' Nelson">
HTML headings are titles or subtitles that you want to display on a webpage.
The <h1> to <h6> HTML elements represent six levels of section headings. <h1> is the highest section level and <h6> is the lowest. By default, all heading elements create a block-level box in the layout, starting on a new line and taking up the full width available in their containing block.
<h1>Heading 1</h1>
<h2>Heading 2</h2>
<h3>Heading 3</h3>
<h4>Heading 4</h4>
<h5>Heading 5</h5>
<h6>Heading 6</h6>
Example:
HTML
<!DOCTYPE html>
<html>
<body>
<h1>Heading 1</h1>
<h2>Heading 2</h2>
<h3>Heading 3</h3>
<h4>Heading 4</h4>
<h5>Heading 5</h5>
<h6>Heading 6</h6>
</body>
</html>
HTML headings are defined with the <h1>
to <h6>
tags.
<h1>
defines the most important heading. <h6>
defines the least important heading.
<h1>Heading 1</h1> <h2>Heading 2</h2> <h3>Heading 3</h3> <h4>Heading 4</h4> <h5>Heading 5</h5> <h6>Heading 6</h6>
Example:
HTML
<!DOCTYPE html>
<html>
<body>
<h1>Heading 1</h1>
<h2>Heading 2</h2>
<h3>Heading 3</h3>
<h4>Heading 4</h4>
<h5>Heading 5</h5>
<h6>Heading 6</h6>
</body>
</html>
Search engines use the headings to index the structure and content of your web pages.
Users often skim a page by its headings. It is important to use headings to show the document structure.
<h1>
headings should be used for main headings, followed by <h2>
headings, then the less important <h3>
, and so on.
Note: Use HTML headings for headings only. Don't use headings to make text BIG or bold.
Each HTML heading has a default size. However, you can specify the size for any heading with the style
attribute, using the CSS font-size
property:
<h1 style="font-size:60px;">Heading 1</h1>
Example:
HTML
<!DOCTYPE html>
<html>
<body>
<h1 style="font-size:60px;">Heading 1</h1>
<p>You can change the size of a heading with the style attribute, using the font-size property.</p>
</body>
</html>