码迷,mamicode.com
首页 > Web开发 > 详细

CSS基础知识总结之css样式引用的三种方式

时间:2018-04-02 00:08:36      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:图标   short   .com   oct   styles   href   doctype   css   选择   

在html中增加css样式有三种:

1.在标签中增加style属性:

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="UTF-8">
    <title>天蓝蓝</title>
    <!--tag标签的图标-->
    <link rel="shortcut icon" href="timg.jpg">
</head>
<body>
<div style="background-color: blue ;width: 80px;height: 80px "></div>
</body>
</html>

技术分享图片

2.

第二种可以在head中增加style标签 style中通过选择器定位标签增加style样式

css选择器:

  id选择器:

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="UTF-8">
    <title>天蓝蓝</title>
    <style>
        /*id选择器*/
        /*#代表id,#i1代表定位到class为i1的元素*/
       #i1{
           background-color: #000000;
           width: 80px;
           height: 80px;
       }
    </style>
</head>
<body>
<!--id在html只允许出现一个同名id-->
<div id="i1"></div>
</body>
</html>

   class选择器:最常用

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="UTF-8">
    <title>天蓝蓝</title>
    <style>
        /*class选择器*/
        /*.代表class,.c1代表定位到class为c1的元素*/
       .c1{
           background-color: #000000;
           width: 80px;
           height: 80px;
       }
    </style>
</head>
<body>
<div class="c1"></div>
</body>
</html>

  标签选择器:

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="UTF-8">
    <title>天蓝蓝</title>
    <style>
         /*标签选择器*/
        /*所有的div的标签都带有下面属性*/
       div{
           background-color: pink;
           width: 80px;
           height: 80px;
       }
    </style>
</head>
<body>
<div class="c1"></div>
<div></div>
</body>
</html>

技术分享图片

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="UTF-8">
    <title>天蓝蓝</title>
    <style>
       div{
           background-color: pink;
           width: 80px;
           height: 80px;
       }
        .c1{
            background-color: blue;
            width: 80px;
            height: 80px;
        }
    </style>
</head>
<body>
<div class="c1"></div>
<div></div>
</body>
</html>

技术分享图片

  层级选择器:

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="UTF-8">
    <title>天蓝蓝</title>
    <style>
       /*层级选择器*/
        div span{
            background-color: red;
            width: 80px;
            height: 80px;
        }
         #i1 span{
             background-color: blue;
             width: 80px;
             height: 80px;
         }
        .c1 span{
            background-color: pink;
            width: 80px;
            height: 80px;
        }
    </style>
</head>
<body>
<div class="c1">
    <span>这是span标签</span>
</div>
<div id="i1">
    <span>这是span标签</span>
</div>
<div>
    <span>层级选择器</span>
</div>

</body>
</html>

  多个标签引用同一个标签:

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="UTF-8">
    <title>天蓝蓝</title>
    <style>
        /*多个标签引用同一个标签*/
         #i1,#i2,#i3{
             background-color: blue;
             width: 80px;
             height: 80px;
         }
        .c1{
            background-color: pink;
            width: 80px;
            height: 80px;
        }
    </style>
</head>
<body>
<div id="i1"></div><br>
<div id="i2"></div><br>
<div id="i3"></div>
<div class="c1"></div>
<div class="c1"></div>
<div class="c1"></div>
</body>
</html>

3. 在head里面使用link引入css样式表:

css表:c1.css

.c1{
    background-color:red;
    width: 80px;
    height: 80px;
}
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="UTF-8">
    <title>天蓝蓝</title>
    <link rel="stylesheet" href="c1.css">
</head>
<body>
<div class="c1"></div>
<div class="c1"></div>
</body>
</html>

样式表的引用顺序联系:

css表:c2.css

.c1{
    background-color: pink;
    width: 100px;
    height: 100px;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!--<link rel="stylesheet" href="c2.css">-->
    <style>
        /*样式表的引用关系,以标签为起的位置,由下而上的开始应用*/
     .c1{
        background-color: red;
        width: 100px;
        height: 100px;
        }
    </style>
    <link rel="stylesheet" href="c2.css">
</head>
<body>
<div class=‘c1‘ style="background-color: #0000CC;width: 100px;height: 100px"></div>
</body>
</html>

 

CSS基础知识总结之css样式引用的三种方式

标签:图标   short   .com   oct   styles   href   doctype   css   选择   

原文地址:https://www.cnblogs.com/wxcx/p/8684809.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!