天天看點

在html中引入css的幾種方法

在HTML中引入CC有好幾種方法,按優先級排列分别是:1、行内式,2、嵌入式,3、導入式和連結式

在HTML中引入CSS的方法有以下幾種:

1)行内式

比如:

<body>
   <h1 style="color:white;background-color:blue">
       This is a line of Text.
   </h1>
 </body>
           

2)嵌入式

比如:

<head>
    <style type="text/css">
        h1 {
            color: white;
            background-color: blue
        }
    </style>
</head>
<body>
<h1>This is a line of Text.</h1>
<h1>This is another line of Text.</h1>
</body>
           

3)導入式和連結式

先要把CSS的内容寫到一個.CSS的檔案中

比如:

mycss.css

h1{
       color:white;background-color:blue
     }
           

導入式寫法(注意CSS檔案的路徑要正确):

<head>
    <style type="text/css">
        @import"mycss.css";
    </style>
</head>
<body>
<h1>This is a line of Text.</h1>
<h1>This is another line of Text.</h1>
</body>
           

連結式寫法(注意CSS檔案的路徑要正确):

<head>
    <link href="mycss.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<h1>This is a line of Text.</h1>
<h1>This is another line of Text.</h1>
</body>