<p>HTML is a programming language used to create web pages, which can be styled through CSS (Cascading Style Sheets). HTML can use inline styles, internal styles, and external style sheets to set properties such as text, color, layout, font size, and more. Below we will explain how to set styles in HTML.
- Inline styles
<p>Use style attributes within HTML tags to set inline styles. For example:
<p style="font-size: 20px; color: red;">这是一个段落</p>
Copy after login
<p>In this example, the style is applied directly within the
<p>
tag, which contains a
style
attribute to define the style. Inline styles can specify different style attributes for each tag, but if the style attributes need to be changed, the HTML code must be changed.
- Internal styles
<p>Internal styles can be used within the
<head>
tag of an HTML document
<style>
mark to define. For example:
<head>
<style>
p {
font-size: 20px;
color: red;
}
</style>
</head>
<body>
<p>这是一个段落</p>
</body>
Copy after login
<p>In this example, we define the style of the
<p>
element, and all page elements containing
<p>
will inherit this style. If you need to change the style attributes, you only need to change the content of the
<style>
tag once.
- External styles
<p>External styles can be created in separate files and referenced through links on the page. For example:
<head>
<link rel="stylesheet" href="styles.css">
</head>
Copy after login
<p>In this example, we specify the path to the CSS file through the
<link>
tag. A CSS file can contain multiple style definitions, as shown below:
p {
font-size: 20px;
color: red;
}
h1 {
font-size: 36px;
color: blue;
}
Copy after login
<p> In this example, we have defined two styles, one for all paragraphs and one for all Heading 1 elements. Using an external style sheet can help us avoid defining the same style multiple times in HTML and make it easier to maintain and update the style sheet.
<p>Summary
<p> Styling in HTML is a key aspect of web design. Whether you use inline styles, internal styles, or external style sheets, the main purpose of setting styles is to make your web pages more beautiful and easier to read. In practice, we should follow some best practices, such as using simple and clear selector and style names, avoiding using too many styles in HTML and using comments in external style sheets to help developers leverage and maintain the code.
The above is the detailed content of How to set style in html. For more information, please follow other related articles on the PHP Chinese website!