Home Web Front-end HTML Tutorial Summary of the value of the class attribute of the tag in HTML and how to use it

Summary of the value of the class attribute of the tag in HTML and how to use it

Aug 27, 2018 pm 02:19 PM
class html input

This article mainly introduces you to the specific role of the class attribute of the input tag in HTML. There are examples to prove it. There is also an explanation of how to use the class attribute and attribute value of the input tag. The last point is about the class attribute. An example of other usage.

First of all, let’s talk about the role of the class attribute of the input tag in HTML:

The class attribute of the input tag is used to reference the class style of your page.

That is, you first define a class style in the tag, and then reference it.

Like this:

<style type="text/css">
.btn{
color:red;
}
</style>
<input type="button" class="btn" />
Copy after login

The effect of the page will not be displayed. Anyway, the color of the text on this button is red. If you are interested, you can try it yourself.

The following is an explanation of how to use the class attribute and attribute value of the specific input tag:

classname attribute. The class attribute in the a tag is used to add styles. The related concepts include class and mapping classname and classlist.

Generally speaking, all attributes can be accessed by object, object[""] and object.getAttribute(""), but because class is a JavaScript reserved word, it is mapped to classname for access by the above programs. Therefore, standard browsers support access in two ways: a.classname and a["classname"].

As for a.getattribute("class") or a.getattribute("classname"), it depends on whether the browser is compatible with this method of access. (To be tested: The former can run correctly in mozilla (firefox) and opera, but cannot be used in IE and safari. The latter can run correctly in IE and opera, but cannot be used in mozilla (firefox) and safari.)

But the DOM2-level method object.getattribute("") is no problem when used to obtain custom attributes in tags, so a conservative approach is

classList attribute. Because the value of the class attribute can contain several style names, such as class="top1 left", this string value often requires us to operate. Classname completes the mapping of class, but it seems very rigid when operating the value of the class attribute. . What to do? The new API classlist in HTML5 can solve this problem.

classlist attribute is another improvement on classname. Just like the arguments attribute and childnode attribute we have seen, it can be understood as an array-like object. (Still to be tested)

classlist brings some operation methods and attributes: add(), remove(), toggle(), contains(), etc. and length.

Can be accessed using a.classList or a["classList"], a.getAttribute("classList") still has browser compatibility issues.

In addition to being used in input tags, the class attribute also has other directions, such as the following:

Using the class attribute in HTML documentsExample

<html>
<head>
<style type="text/css">
h1.intro {color:blue;}
p.important {color:green;}
</style>
</head>
<body>
<h1 class="intro">Header 1</h1>
<p>A paragraph.</p>
<p class="important">Note that this is an important paragraph.</p>
</body>
</html>
Copy after login

Here is the comment:

The class attribute cannot be used in the following HTML elements: base, head, html , meta, param, script, style and title.

You can assign multiple classes to HTML elements, for example: . This allows you to combine several CSS classes into one HTML element.

Class names cannot start with numbers! Only Internet Explorer supports this practice.

【Related recommendations】

What does the head tag in HTML mean? An article teaches you how to use the head tag correctly

#How to center the th header content in an html table? A detailed introduction to the align attribute of th header tag

The above is the detailed content of Summary of the value of the class attribute of the tag in HTML and how to use it. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles