CSS 中的外部样式表导入
在另一个 CSS 声明中导入额外的 CSS 文件。为此,使用 @import 规则,因为它将样式表链接到文档中。通常,当一个样式表依赖于另一个样式表时,就会使用此规则。它在文档顶部指定,位于 <head> 内的 @charset 声明之后。除此之外,也可以使用 <link> 元素。
语法
@import 规则的语法如下所示: -
@import /*url or list-of-media-queries*/
媒体查询可以是复合语句,这些语句可以指定文档在不同媒体中的行为。
使用 @import 规则导入外部样式表
以下示例实现了 @import 规则。使用 @import,我们已包含 style.css 文件 -
@import url(style.css);
body {
background-color: honeydew;
}
@import 规则用于此目的,因为它将样式表链接到文档中。样式文件包括此 HTML 文档的样式。让我们来看示例 -
示例
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
@import url(style.css);
body {
background-color: honeydew;
}
</style>
</head>
<body>
<p>This is demo paragraph one.</p>
<p class="two">This is demo paragraph two.</p>
<p>This is demo paragraph three</p>
</body>
</html>
style.css
p { color: navy; font-style: italic; }
.two { color: darkgreen; font-size: 24px; }
使用元素包含外部样式表
让我们看另一个示例,使用 <link> 元素导入外部样式表 -
HTML 文档
<!DOCTYPE html> <html> <head> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <div></div> </body> </html>
style.css
div {
height: 50px;
width: 100px;
border-radius: 20%;
border: 2px solid blueviolet;
box-shadow: 22px 12px 3px 3px lightblue;
position: absolute;
left: 30%;
top: 20px;
}
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言
C++
C#
MongoDB
MySQL
Javascript
PHP