Home  >  Article  >  Web Front-end  >  How to write divcss

How to write divcss

PHPz
PHPzOriginal
2023-05-21 11:38:08551browse

Div (Division) and CSS (Cascading Style Sheets) are two very important elements in web design. They allow us to better layout and style web pages. In this article, we will discuss how to write div layout using CSS.

In CSS, we can use div tags to define different areas on the page. These areas can contain elements such as text, images, tables, videos, etc. Using div tags, we can define the layout and style of the page, making it look more beautiful and professional.

The following is the code for a simple HTML page, which contains three div tags:

<!DOCTYPE html>
<html>
<head>
    <title>我的网页</title>
    <style>
        .container {
            width: 90%;
            margin: 0 auto;
            background-color: #fff;
            border: 1px solid #ccc;
            padding: 20px;
        }

        .header {
            background-color: #eee;
            text-align: center;
            padding: 20px;
        }

        .footer {
            background-color: #eee;
            text-align: center;
            padding: 20px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>欢迎来到我的网页!</h1>
        </div>
        <div class="content">
            <p>这是我的第一个网页,希望大家喜欢!</p>
        </div>
        <div class="footer">
            <p>版权所有 &copy; 2021</p>
        </div>
    </div>
</body>
</html>

In this example, we have three div tags:

  1. container: This tag contains the entire page and sets some basic styles, such as page width, background color, and border.
  2. header: This tag contains the title of the page and sets some styles, such as background color, text centering and padding.
  3. footer: This tag contains copyright information and sets some styles, such as background color, text centering and padding.

We use CSS to define the style of these div tags. In the above example, we use a class selector to select each div and define the styles individually.

In the .container class, we set the width of the web page to 90% and center it. We also set the background color, border color, and padding. This makes the entire page look cleaner and easier to read.

In the .header class, we set the background color, text centering and padding of the title. This makes the title easier to read, and clicking on the title takes you back to the top of the page.

In the .footer class, we also set the background color, text centering and padding. This makes the copyright information easier to read and can be found at the bottom of the page.

In addition to the above examples, we can also use different CSS selectors such as position selectors and id selectors to select and define the style of div tags. For example, we can use the following code to position two div tags on the left and right sides of the page:

<style>
    .left {
        float: left;
        width: 50%;
        background-color: #eee;
        padding: 20px;
    }

    .right {
        float: right;
        width: 50%;
        background-color: #eee;
        padding: 20px;
    }
</style>

<div class="left">
    <p>这个div位于页面的左侧。</p>
</div>
<div class="right">
    <p>这个div位于页面的右侧。</p>
</div>

In this example, we use the position selector (float) and the width attribute to position the left The side div and right div are placed on both sides of the page. We also set the background color and padding for each div tag.

Finally, we need to pay attention to some best practices when using divs and CSS:

  1. Use div tags as little as possible. Using too many div tags will make the code cluttered and difficult to maintain.
  2. Use semantic tags instead of div tags. Semantic tags include header, nav, section, article, aside, footer, etc. Their purpose is to clearly define the structure and content of the page.
  3. Always specify a class or id for div tags in order to style them.
  4. Don’t use too many !important tags in CSS, as this will clutter the code and make it difficult to maintain.
  5. Always follow CSS best practices, such as grouping related styles together, using nested selectors, etc. This makes the code cleaner and easier to read and understand.

In this article, we discussed how to write div layout using CSS. We looked at how to select and style div tags using CSS selectors such as class selectors, position selectors, and id selectors, as well as some best practices. With this knowledge, you can use div tags and CSS to build better, more beautiful, and more professional web pages.

The above is the detailed content of How to write divcss. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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