Home > Web Front-end > Front-end Q&A > div set html

div set html

PHPz
Release: 2023-05-21 17:47:07
Original
792 people have browsed it

In front-end development, we often need to use HTML to build the structure of web pages, while CSS is used to beautify and style web pages. In CSS, using div tags is a very common way to divide elements in a web page into different blocks to facilitate style setting and layout. This article will introduce how to use div tags to set the structure and style of web pages in HTML.

1. What is a div tag?

The div tag is a container tag in HTML that can be used to divide other HTML elements into different blocks. These blocks can have different styles and layouts, such as the header of the web page, navigation bar, main content area, sidebar, etc. Compared with other HTML tags, the div tag has no specific semantics. It is a general container tag.

2. Use div tags to set up web page structure

It is very simple to use div tags in HTML. You only need to add a

tag outside the elements that need to be divided into blocks. . For example, in a web page, we may need to separate the page header area and content area. It can be expressed like this:

<!DOCTYPE html>
<html>
<head>
    <title>使用div标签设置网页结构</title>
    <style>
        header {
            background-color: #333;
            color: #fff;
            padding: 20px;
            text-align: center;
        }

        .content {
            margin: 0 auto;
            width: 80%;
            padding: 20px;
        }
    </style>
</head>
<body>
    <div>
        <header>
            <h1>这是网页的头部</h1>
        </header>
        <div class="content">
            <p>这是网页的内容区域。</p>
        </div>
    </div>
</body>
</html>
Copy after login

In the above code, we use an outer layer

to wrap the
and .content elements, thus dividing them into two different blocks . The
tag represents the header area, and .content represents the content area. In CSS styles, we implement style settings for different blocks by targeting different class names.

3. Use div tags for web page layout

In addition to structural division, div tags can also be used to implement web page layout. For example, we can achieve the overall layout of the web page by setting the styles of different divs. Specifically, there are two common layout methods:

(1) Box model layout

The box model layout regards different areas of the web page as different boxes. By setting the box Properties such as width, height, and margins implement the overall layout. For example, we set a header area and a content area in the web page:

<!DOCTYPE html>
<html>
<head>
    <title>使用div标签进行网页布局</title>
    <style>
        .header {
            height: 100px;
            background-color: #333;
            color: #fff;
            text-align: center;
            line-height: 100px;
        }

        .content {
            margin: 0 auto;
            width: 80%;
            padding: 20px;
        }
    </style>
</head>
<body>
    <div class="header">这是网页的头部</div>
    <div class="content">
        <h2>这是网页的内容区域。</h2>
        <p>在这里填写网页内容……</p>
    </div>
</body>
</html>
Copy after login

In the above code, we set the styles of two classes: .header and .content, .header corresponds to the header area, . content corresponds to the content area. Among them, the height of .header is 100 pixels, and attributes such as background color, centering, and text style are set. The width of .content is 80%, center-aligned, and padding is set. With this setting, we can implement a simple web page layout.

(2) Grid layout

In addition to the box model layout, you can also use CSS grid layout to implement web page layout. Grid layout divides the entire area of ​​the web page into multiple grids. We can place different elements in different grids to achieve an overall layout.

For example, we can set up a web page layout with a header, navigation bar and content area like this:

<!DOCTYPE html>
<html>
<head>
    <title>使用div标签进行网页布局</title>
    <style>
        .container {
            display: grid;
            grid-template-columns: 200px auto;
            grid-template-rows: 100px auto;
            grid-template-areas:
                "header header"
                "nav content";
            grid-gap: 10px;
            height: 100%;
            padding: 20px;
            box-sizing: border-box;
            background-color: #f6f6f6;
        }

        .header {
            grid-area: header;
            background-color: #333;
            color: #fff;
            text-align: center;
            line-height: 100px;
        }

        .nav {
            grid-area: nav;
            background-color: #777;
            color: #fff;
            text-align: center;
            line-height: 50px;
        }

        .content {
            grid-area: content;
            padding: 20px;
            background-color: #fff;
            height: 100%;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">这是头部</div>
        <div class="nav">这是导航栏</div>
        <div class="content">这是内容区域</div>
    </div>
</body>
</html>
Copy after login

In the above code, we use grid layout to achieve the overall layout of the web page . The .container class sets a grid layout for the entire web page, which is divided into two columns and two rows. The size of each grid is automatically calculated. At the same time, we also set a specific grid position for each block, which is set through the grid-area attribute. Through such a detailed layout, we can get a web page with a header, navigation bar and content area.

Summary

The div tag is a general container tag used to divide blocks in HTML. By using div tags, we can quickly and easily implement the structure setting and layout of web pages. In CSS styles, we can set the styles of different blocks through different class names, and achieve the overall layout through box model layout or grid layout. Mastering the skills of using div tags for web development can help us achieve web development work more efficiently.

The above is the detailed content of div set html. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template