JIYIK CN >

Current Location:Home > Learning > WEB FRONT-END > HTML >

The role attribute in HTML

Author:JIYIK Last Updated:2025/04/19 Views:

In this article, we will introduce the HTML role attribute and its uses.


Introduction to the role attribute in HTML

The role attribute in HTML belongs to the WAI-ARIA (Web Accessibility Initiative - Accessible Rich Internet Applications) specification. The main purpose of WAI-ARIA is to improve the accessibility of web interfaces to support people with disabilities.

It lets screen readers read the content of the HTML document using the information we provide. The role attribute defines the role of an HTML element describing the semantics.

First, let's understand the difference between semantic and non-semantic HTML elements. Elements such as <p>, , <table>and <li>are semantic elements that convey meaning.

It focuses more on its functionality rather than its appearance. The specific meaning of the element is presented to the browser and the developer.

For example, <p>tags create paragraphs.

On the other hand, non-semantic HTML elements are those that have no clear explanation of their content. For example, tags like and <div>.<span>

Such tags do not contain any information about the content. In other words, they have no semantics.

The role attribute provides semantics to non-semantic elements in HTML. We can write roles for elements using the format role="role_type".

Here, role_type is the role specified in ARIA. In the following sections, we will see different use cases for the role attribute.


Using the role attribute in HTML

One of the main purposes of the role attribute is to provide accessibility, especially for visually impaired users. Using a screen reader, the role attribute reads out the exact purpose or behavior of a particular element.

Let's consider a scenario where a user of a web application is visually impaired. We need to create a button to log the user in.

However, frameworks do not allow us to use <button>the <div> element. Although frameworks cannot do anything about this, we can still <a>make it possible using the <div> tag.

However, unless we use the role attribute, we have no way of telling the visually impaired that there is a login button.

In this case, we can use the role attribute to define the link as a button. We can do this by writing the value of the role attribute as "button".

As a result, the screen reader reads out to the user that Login is a button. Hence, he will be able to log into the system.

We can add a screen reader extension for the Chrome browser from the Chrome Web Store. When the user navigates the tab via keyboard, the screen reader will read Login as a button.

Sample code:

<a href="#" role="button"> Login </a>

Previous: None

Next:Readonly in HTML Select Tag

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Creating a currency input in HTML

Publish Date:2025/04/19 Views:119 Category:HTML

In this article, users will learn to input convert a normal HTML field into a currency input field. We will use HTML and jQuery to achieve our goal. input Convert plain to currency input in HTML using JavaScript toFixed() method In HTML, th

HTML Hide Buttons and Show Them Using Onclick

Publish Date:2025/04/19 Views:178 Category:HTML

This article will show you several ways to hide HTML buttons and make them visible using the onclick event. Using the CSS display property to show hidden buttons in HTML We can first hide the HTML button by setting its display property to n

Right align button in HTML

Publish Date:2025/04/19 Views:136 Category:HTML

Alignment is the positioning of text or buttons in different places like right, left or center on an HTML web page. We can easily use the alignment properties to place our text, images or other elements anywhere on the web page. There may b

Readonly in HTML Select Tag

Publish Date:2025/04/19 Views:176 Category:HTML

TML, Hypertext Markup Language, is widely used to format websites. Also, CSS (Cascading Style Sheets) can be used to style websites. Below are some of the attributes used in HTML and CSS to control user and cursor behavior. select readonly

HTML 中的 role 属性

Publish Date:2023/05/06 Views:354 Category:HTML

本篇文章介绍 HTML role属性。HTML中 role 属性介绍,role 属性定义描述语义的 HTML 元素的角色。

在 HTML 中打印时分页

Publish Date:2023/05/06 Views:681 Category:HTML

本篇文章介绍如何在打印 HTML 页面或内容时强制分页。将 page-break-after 属性设置为 always Inside @media print to Page Break in HTML

在 HTML 中显示基于 Cookie 的图像

Publish Date:2023/05/06 Views:187 Category:HTML

本文介绍如何根据 HTML 中的 cookies 显示图像。根据 HTML 中设置的 Cookies 显示图像。问题陈述是我们需要根据网页传递的cookie来显示特定的图像。

在 HTML 中创建下载链接

Publish Date:2023/05/06 Views:382 Category:HTML

本文介绍如何在 HTML 中创建下载链接。使用 download 属性在 HTML 中创建下载链接.。我们可以使用 HTML 锚元素内的下载属性来创建下载链接。

HTML 中的 ::before 选择器

Publish Date:2023/05/06 Views:647 Category:HTML

本教程介绍 CSS ::before 伪元素。CSS ::before 伪元素。 ::before 选择器是一个 CSS 伪元素,我们可以使用它在一个或多个选定元素之前插入内容。 它默认是内联的。

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial