Rumah  >  Artikel  >  hujung hadapan web  >  HTML---CSS层叠样式表

HTML---CSS层叠样式表

不言
不言asal
2018-04-26 14:34:482277semak imbas

本篇文章给大家分享了关于HTML---CSS层叠样式表的内容,有需要的朋友可以参考一下


CSS层叠样式表


1.结构,样式,行为的分离

<!--样式-->
<style type="text/css">
    p{
        background-color:green;
        height:100px;
        width:400px;
        border:1px solid red;
    }
    h2{
        background-color:#aaa;
        height:100px;
        width:400px;
        border:1px solid red;
    }
    <!--选择器-->
    .yellow{
        background-color:yellow;
        height:300px;
        width:600px;
        border:1px solid red;
    }
</style>

<!--行为-->
<script type="text/javascript">
    <!--当页面加载完毕,我们就执行一个函数,来完成对h2的操作-->
    window.onload()=function(){
        <!--获取要操作的h2标签-->
        h2Node=document.getElementById("tt");
        <!--当鼠标经过,我们就改变h2的外观-->
        h2Node.onmouseover=function(){
            this.className="yellow";
        }
        <!--鼠标离开,就恢复h2的外观和大小-->
        h2Node.onmouseout=function(){
            this.className="";
        }
    }

</script>
<body>
    <h2 id="tt">静夜思</h2>
    <p>床前明月光</p>
</body>

2.css的分类
(1)id选择器
(2)标签选择器
(3)类选择器
(4)分组选择器
(5)通配符选择器
(6)伪类选择器(对超链接的操作)
(7)派生选择器,也称复合选择器
选择器的优先级:就近原则,范围越小,优先级越高
可以使用!important改变优先级

<style>
    /*id选择器*/
    #a01{
        color:red;
    }
    /*标签选择器*/
    p{
        color:blue;
    }
    /*类选择器*/
    .c01{
        background:green;
    }
    /*分组选择器*/
    h1,h2,h3{
        color:yellow
    }
    /*通配符选择器*/
    *{
        background:#aaa
    }
    /*派生选择器*/
    li strong{
        color:orange;
    }
</style>
<body>
    <ul>
        <li><strong>无序列表选项1</strong></li>
        <li>无序列表选项2</li>
        <li>无序列表选项3</li>
        <li>无序列表选项4</li>
    </ul>
    <h1 id="a01">静夜思</h1>
    <h2 class="c01">床前明月光</h2>
    <h3>疑是地上霜</h3>
    <p>举头望明月</p>
    <strong>低头思故乡</strong>
</body>

伪类选择器
支持css的浏览器中,链接的不同状态都可以不同的方式显示,这些状态包括:活动状态,已被访问状态,未被访问状态,和鼠标悬停状态
伪类的顺序:link,visited,hover,active

<style type="text/css">
    a:link{ /*未被访问状态*/
        color:#000000;
        text-decoration:none;
    }
    a:visited{ /*已访问过的*/
        color:#FF6633;
    }
    a:hover{ /*鼠标悬停*/
        color:#00FF66;
        rext-decoration:underline;
    }
    a:active{
        color:#CCFF6;
    }
</style>
<body>
    <a href="#">构造css规则</a>
</body>

focus伪类
在元素获得焦点时向元素添加特殊样式

c9ccee2e6ea535a969eb3f532ad9fe89
    input:focus{
        background-color:#FF0066
    }
531ac245ce3e4fe3d50054a55f265927
6c04bd5ca3fcae76e30b72ad730ca86d
    e388a4556c0f65e1904146cc1a846bee
        e7f5741c8aebd1cf4935f2b73c310eb7
    94b3e26ee717c64999d7867364b1b4a3
36cc49f0c466276486e50c850b7e4956

3.css的使用方式
(1)内嵌式

<style>
    li{
        color:red
    }
</style>
<body>
    <ul>
        <li><strong>无序列表选项1</strong></li>
        <li>无序列表选项2</li>
        <li>无序列表选项3</li>
        <li>无序列表选项4</li>
    </ul>

</body>

(2)行内式

6c04bd5ca3fcae76e30b72ad730ca86d
    e388a4556c0f65e1904146cc1a846beefeb9eb9529a5c82c05b1eb381b6d651d我45a2772a6b6107b401db3c9b82c049c2能抽象出整个世界94b3e26ee717c64999d7867364b1b4a3
36cc49f0c466276486e50c850b7e4956

(3)导入式

<style type="text/css">
    @import "文件路径";
</style>
<body>
    <ul>
        <li><strong>无序列表选项1</strong></li>
        <li>无序列表选项2</li>
        <li>无序列表选项3</li>
        <li>无序列表选项4</li>
    </ul>
    <h1 id="a01">静夜思</h1>
    <h2 class="c01">床前明月光</h2>
    <h3>疑是地上霜</h3>
    <p>举头望明月</p>
    <strong>低头思故乡</strong>
</body>

创建.css文件

    #a01{
        color:red;
    }
    p{
        color:blue;
    }

(4) 链接式

<link href="文件路径" rel="stylesheet" type="text/css">
<body>
    <ul>
        <li><strong>无序列表选项1</strong></li>
        <li>无序列表选项2</li>
        <li>无序列表选项3</li>
        <li>无序列表选项4</li>
    </ul>
    <h1 id="a01">静夜思</h1>
    <h2 class="c01">床前明月光</h2>
    <h3>疑是地上霜</h3>
    <p>举头望明月</p>
    <strong>低头思故乡</strong>
</body>

创建 .css文件

    #a01{
        color:red;
    }
    p{
        color:blue;
    }

               

相关推荐:

详细说明CSS层叠样式表

CSS层叠样式表_html/css_WEB-ITnose

Atas ialah kandungan terperinci HTML---CSS层叠样式表. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn