在HTML文档中嵌入CSS的几种方式_html/css_WEB-ITnose

WBOY
Release: 2016-06-24 12:03:38
Original
1604 people have browsed it

在HTML中常用以下3种方式定义CSS:Embedding(嵌入式)、Linking(引用式)、Inline(内联式)


一、嵌入式

    使用HTML的style元素,在文档中定义CSS样式。

<style type="text/css">h1{color:red}p{color:blue}</style>
Copy after login

二、内联式

    每一个HTML元素都包含一个style属性,可以直接定义样式。该样式仅能用于该元素的内容,对于另一个同名的元素则不起作用。

<p style="color:#FFF;font-weight:bold;">内联样式</p>
Copy after login

三、外部引用式

    外部引用指HTML文档本身不含有CSS样式,而是动态引用外部的CSS文件定义文档的表现形式。


    1、使用样式表的处理指令语句

        在HTML文档的开头部分写一个关于样式表的指令处理语句

<?xml-stylesheet type="text/css" href="mystyle.css" ?>指令语句
Copy after login
不过只有使用xml语法格式编写的html文档才支持使用该指令,大多数浏览器仅当被保存为xhtml或xml格式才有效,且JS不能处理这种CSS,所以不建议使用。


    2、使用@import命令

在style元素之间使用@import命令导入外部的css文件

<style type="text/css"><!--下面两行代码效果一样@import "mystyle.css";@import url("mystyle.css");--></style>
Copy after login
任何@import规则必须出现在所有规则之前。参数是一个css文件的URL地址。在一个css文件中也可以用@import指令将另一个css文件导入。


    3、使用link元素

<link rel="stylesheet" href="css%E7%9A%84url" type="text/css">
Copy after login

这也是最常用的方式。


    4、使用HTTP消息报头链接到样式表

可以使用HTTP消息报头的link字段链接一个外部样式表。

link:;rel=stylesheet;//等同于<link rel="stylesheet" href="css%E7%9A%84url" type="text/css">
Copy after login
HTTP 报头中可以使用多个link,从而链接多个样式表,且 HTTP报头中的link比HTML文档中的link(head元素中)具有优先级。


Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!