html 中css位置
时间: 2023-09-19 13:08:10 浏览: 51
在 HTML 中,可以将 CSS 样式表放置在以下位置:
1. 在 head 标签中的 style 标签中
可以在 HTML 文件的 head 标签中使用 style 标签来定义 CSS 样式。例如:
```html
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
<style>
body {
background-color: #f0f0f0;
}
h1 {
color: #333;
font-size: 32px;
}
</style>
</head>
<body>
<h1>Welcome to my website</h1>
<p>This is some text.</p>
</body>
</html>
```
2. 在 HTML 文件中引用外部样式表
可以在 HTML 文件的 head 标签中使用 link 标签来引用外部 CSS 文件。例如:
```html
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>Welcome to my website</h1>
<p>This is some text.</p>
</body>
</html>
```
在此示例中,HTML 文件中没有任何样式,而是使用 link 标签引用了名为 style.css 的外部样式表文件。
3. 在 HTML 标签中使用 style 属性
还可以在 HTML 标签中使用 style 属性来定义 CSS 样式。例如:
```html
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
</head>
<body>
<h1 style="color: #333; font-size: 32px;">Welcome to my website</h1>
<p style="background-color: #f0f0f0;">This is some text.</p>
</body>
</html>
```
在此示例中,H1 和 P 标签中都使用了 style 属性来定义 CSS 样式。