LERAN HTML COMPLETE

Learn one of the most powerful programming languages in the world and become a rockstar developer.

HTML Attributes


What are Attributes

Attributes define additional characteristics or properties of the element such as width and height of an image. Attributes are always specified in the start tag (or opening tag) and usually consists of name/value pairs like name="value". Attribute values should always be enclosed in quotation marks.

Also, some attributes are required for certain elements. For instance, an <img> tag must contain a src and alt attributes. Let's take a look at some examples of the attributes usages:

The href Attribute

HTML links are defined with the <a> tag. The link address is specified in the href attribute:

<a href="https://www.adzetech.com">This is a link</a>

You will learn more about links and the <a> tag later in this tutorial.


The src Attribute

HTML images are defined with the <img> tag.

The filename of the image source is specified in the src attribute:

<img src="img_girl.jpg">

The width and height Attributes

 HTML images also have width and height attributes, which specifies the width and height of the image:

The width and height are is specified in pixels by default; so width="500" means 500 pixels wide.

<img src="img_girl.jpg" width="500" height="600">

You will learn more about images in our HTML Images chapter.


The 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.

<img src="img_girl.jpg" alt="Girl with a jacket">

Note: The alt attribute is also useful if the image cannot be displayed (e.g. if it does not exist):

<img src="img_girl.jpg" alt="Girl with a jacket">

The style Attribute

The style attribute allows you to specify CSS styling rules such as color, font, border, etc. directly within the element. Let's check out an example to see how it works:

Example

<p style="color: blue;">This is a paragraph.</p>
<img src="images/sky.jpg" style="width: 300px;" alt="Cloudy Sky">
<div style="border: 1px solid red;">Some content</div>

You will learn more about styling HTML elements in HTML styles chapter.

The attributes we've discussed above are also called global attributes..

A complete list of attributes for each HTML element is listed inside HTML5 tag reference.


Click on the "Try it Yourself" button to see how it works.

Previous Next
x
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Privacy Policy That's Fine