Home >Web Front-end >Front-end Q&A >How to write css in html page

How to write css in html page

王林
王林Original
2023-05-09 10:59:372580browse

CSS (Cascading Style Sheets) is a technology used to design and beautify HTML pages. CSS styles can be created and referenced through the c9ccee2e6ea535a969eb3f532ad9fe89 tag within an HTML document or from an external CSS file.

The following is a detailed introduction to how to write CSS styles in HTML pages:

  1. Embedded
    Embedded refers to embedding CSS styles directly into HTML pages, using < The ;style> tag wraps CSS code. For example:
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>内嵌式CSS样式表</title>
        <style>
            body {
                background-color: #f0f0f0;
                font-family: Arial, sans-serif;
            }
            h1 {
                color: #333;
                text-align: center;
            }
        </style>
    </head>
    <body>
        <h1>欢迎来到我的网站</h1>
        <p>这是一段普通的文字</p>
    </body>
</html>

In the above example, we first use the e8e496c15ba93d81f6ea4fe5f55a2244 tag to set the character set of the document, and then use the c9ccee2e6ea535a969eb3f532ad9fe89 tag in the 93f0f5c25f18dab9d176bd4f6de5d30e tag to write the CSS style . Within the c9ccee2e6ea535a969eb3f532ad9fe89 tag, we set the document background color, font style and title text color.

Note that the c9ccee2e6ea535a969eb3f532ad9fe89 tag must be placed within the 93f0f5c25f18dab9d176bd4f6de5d30e tag, and all CSS styles must be placed within the c9ccee2e6ea535a969eb3f532ad9fe89 tag. Using CSS styles directly within the 6c04bd5ca3fcae76e30b72ad730ca86d tag is invalid.

  1. External style
    External style sheet means that we put the CSS style in a separate file and then reference it into the HTML page through the 2cdf5bf648cf2f33323966d7f58a7f3f tag. External style sheets have the advantages of being used multiple times and easy to maintain.

In this case, we need to use a file with a .css extension to store the CSS styles. The file can be placed on the server or on your local computer. For example:

/* css/style.css */

body {
    background-color: #f0f0f0;
    font-family: Arial, sans-serif;
}

h1 {
    color: #333;
    text-align: center;
}

In the HTML page, we need to use the 2cdf5bf648cf2f33323966d7f58a7f3f tag to associate the external CSS file with the HTML document. For example:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>外部式CSS样式表</title>
        <link rel="stylesheet" type="text/css" href="css/style.css">
    </head>
    <body>
        <h1>欢迎来到我的网站</h1>
        <p>这是一段普通的文字</p>
    </body>
</html>

In the above code, we use the 2cdf5bf648cf2f33323966d7f58a7f3f tag to associate the style.css file with the HTML document. Note that the href attribute must point to the URL or relative path of the CSS file. If the CSS file is located in the same directory as the HTML document, the href attribute can point directly to the CSS file name.

  1. Inline style
    Inline style refers to applying CSS styles directly to HTML elements. rather than defining them in the stylesheet. For example:
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>行内式CSS样式表</title>
    </head>
    <body>
        <h1 style="color: #333; text-align: center;">欢迎来到我的网站</h1>
        <p style="font-family: Arial, sans-serif;">这是一段普通的文字</p>
    </body>
</html>

Inline, we define CSS styles in the style attribute of the HTML element. Each attribute must be separated by a semicolon, and the attribute value must be enclosed in quotes. Please note that using inline can make your HTML code cluttered and difficult to maintain. Therefore, it is generally not recommended to use inline in real projects.

Summary
When writing CSS styles in HTML pages, you can use inline, external and inline methods to achieve it. Among them, external style is the most commonly used method, which can concentrate styles in one file for easy maintenance and multiple use. For inline and inline formats, it is recommended to use them only when necessary, otherwise it may lead to cluttered and difficult-to-maintain HTML code.

The above is the detailed content of How to write css in html page. 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
Previous article:ie cannot load cssNext article:ie cannot load css