Home > Web Front-end > Front-end Q&A > How to connect css and html

How to connect css and html

WBOY
Release: 2023-05-29 18:30:38
Original
5495 people have browsed it
<p>HTML and CSS are two basic skills for web development. HTML is used to build the structure of web pages, while CSS is used to beautify the appearance of web pages. Whether you're an experienced web developer or a beginner, you need to know how to combine these two skills to create truly great web pages.

<p>1. Use internal CSS

<p>In the head tag of an HTML page, you can embed CSS styles through the <style> tag. You can define all the CSS styles you need in the <style> tag, so that the HTML page will apply the CSS styles to the document when it loads. For example, the following example:

<!DOCTYPE html>
<html>
<head>
    <title>我的网页</title>
    <style>
        body {
            background-color: blue;
        }
        h1 {
            color: white;
            text-align: center;
        }
    </style>
</head>
<body>
    <h1>欢迎来到我的网页</h1>
    <p>这是我的第一个网页</p>
</body>
</html>
Copy after login
<p> In the above code, we set the page background color to blue and the title color by embedding CSS styles in the <style> tag Be white and center aligned.

<p>2. Use external CSS files

<p>If you want to use the same CSS style in multiple HTML pages, then using internal CSS will be very redundant. At this time we can use external CSS document. We create a new file with the .css suffix, such as style.css, and link the CSS style file to the page through the <link> tag in the head tag of the HTML page. For example:

<!DOCTYPE html>
<html>
<head>
    <title>我的网页</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <h1>欢迎来到我的网页</h1>
    <p>这是我的第一个网页</p>
</body>
</html>
Copy after login
<p>In the style file style.css, we can define all the CSS styles we need to use.

body {
    background-color: blue;
}
h1 {
    color: white;
    text-align: center;
}
Copy after login
<p>3. Use inline CSS

<p>In addition to internal CSS and external CSS files, we can also use inline CSS. Inline CSS refers to using the style attribute within HTML tags to define CSS styles. For example:

<!DOCTYPE html>
<html>
<head>
    <title>我的网页</title>
</head>
<body>
    <h1 style="color: white; text-align: center;">欢迎来到我的网页</h1>
    <p style="background-color: blue;">这是我的第一个网页</p>
</body>
</html>
Copy after login
<p>In the above code, we use the style attribute in the <h1> tag and the <p> tag to define the title respectively color and alignment, as well as the background color of the paragraph.

<p>Summary

<p>Whether it is internal CSS, external CSS files or inline CSS, their ultimate purpose is to beautify the appearance of the web page. In actual development, we can choose different ways to link CSS and HTML according to the actual situation. When there are more CSS styles to be defined, we can use external CSS files; when there are fewer styles to be defined, we can use internal CSS or inline CSS.

The above is the detailed content of How to connect css and 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