Home >Web Front-end >HTML Tutorial >What is a div tag? How to use div tag in HTML?
The div tag in HTML is an important foundation for realizing web pages and is essential for learning HTML knowledge. This article will introduce to you how to use div tags in HTML.
What is a div tag?
The div tag represents a set of necessary structures. The purpose is to break the characters sandwiched between div tags into blocks. Therefore, a separate div tag has no effect such as changing color or changing character style.
Also, if the div tag doesn't fit into other elements, don't use it yet.
However, in fact, when we need to specify which element, we often use div tags.
By adding the id and class to the div tag and using CSS to process the id and class, you can change the color in the div tag or change it to another style.
Comparison of div tag and Section tag
I think the section tag is more appropriate to display a single collection on the document
The section tag is used to indicate the group for documentation.
I think sections have items of sections
Just like this concept of a section clause, the section tag is a block and usually has no meaning if it is extracted.
In a newspaper article, even if you only clip the chapter items, I don't think the intention of the entire article can be conveyed.
By comparison, the section tag is used differently from the div tag, but is used as a sentence to represent a group.
Usage of div tag
Write the div tag as follows.
<div>内容</div>
As mentioned above, div tags are very useful by adding id and class. How to assign id and class is as follows.
<div id="id名">内容</div> <div id="class名">内容</div>
By the way, id and class
By the way, what are id and class? id and class are called attributes, id is something like I has a D number, something with the same id name can only be used once within the page.
Instead, we use the id attribute for things that we only use once.
On the other hand, the class attribute can be used multiple times on the same page. If you want to specify "this element and this element have the same color", use the class attribute.
Of course, you can use the class attribute even if you only use it once.
Let’s look at the actual example
Let’s first look at the results surrounded by div tags
For id and class, we specify The color name of each noun.
Create div.html using the following source code and open it in the browser.
div.html
<html> <head> <meta charset="utf-8" /> </head> <body> <div id="red">苹果</div> <div class="skyblue">天空</div> <div class="yellow">香蕉</div> <div class="blue">大海</div> <div class="lightyellow">蒲公英</div> </body> </html>
The display effect in the browser is as follows:
The above only displays text, now let us Color the background using CSS.
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <style type="text/css"> #red { background-color: #FF0000; } .skyblue { background-color: skyblue; } .yellow{ background-color: yellow; } .blue{ background-color: blue; } .beige{ background-color: beige; } </style> </head> <body> <div id="red">苹果</div> <div class="skyblue">天空</div> <div class="yellow">香蕉</div> <div class="blue">大海</div> <div class="beige">蒲公英</div> </body> </html>
The display effect in the browser is as follows:
You can see that the background is colored, and you can also confirm that multiple identical classes have been specified.
The above is the detailed content of What is a div tag? How to use div tag in HTML?. For more information, please follow other related articles on the PHP Chinese website!