css基础--联合选择器

来源:互联网 发布:星星知我心第二部全集 编辑:程序博客网 时间:2024/05/22 09:43

1、css 层叠样式表(层叠:一层一层的;样式表:很多的属性和属性值),使页面显示效果更好,css将页面内容和显示样式进行分离提高了显示功能功能


2、css 和 html的结合方式:

    a、行内样式,在每个html标签上面都有一个style属性把css和html结合在一起;

        <div style="background-color:red;color:green;">


    b、内嵌样式,在每个html的<head></head>里面以标签<style></style>实现;

        <style type="text/css">    
            div {
                background-color:blue;
                color: red;
            }        
        </style>


    c、导入样式,在<style></style>标签里面使用语句@import url(css文件的路径);

        <style type="text/css">
            @import url(div.css);
        </style>


    d、链接样式,使用标签<link>引入外部css样式文件。

        <link rel="stylesheet" type="text/css" href="css文件的路径" />


    注:导入样式在某些浏览器下不起作用,一般使用链接样式。优先级:行内>内嵌>导入>链接


3、css 选择器:

    a、标签选择器,使用标签名作为选择器的名称;

        div{
            background - color: gray;
            color: white;
        }


    b、class选择器,每个html标签都有一个属性class;

        /*<div class="haha">aaaaaaa</div>*/
        
        .haha {
            background-color: orange;
        }


    c、id选择器,每个标签上面有一个id;

        /*<div id="hehe">bbbbb</div>*/
        
        #hehe {
            background-color: orange;
        }


    d、关联选择器,设置标签里面的嵌套标签的样式;

        <div><p>wwwwwwww</p></div>
        
        div p {
            background-color: orange;
        }


    e、组合选择器,把不同的标签设置成相同的样式;

        <div>1111</div>
        <p>22222</p>

        div,p {
            background-color: orange;
        }


    f、伪元素选择器,css里面提供了一个定义好的样式

    ** 超链接的状态
        原始状态   鼠标放上去状态  点击           点击之后
        :link         :hover        :active        :visited
    
    ** 记忆的方法
        lv  ha




选择器例子例子描述CSS.class.intro选择 class="intro" 的所有元素。1#id#firstname选择 id="firstname" 的所有元素。1**选择所有元素。2elementp选择所有 <p> 元素。1element,elementdiv,p选择所有 <div> 元素和所有 <p> 元素。1element elementdiv p选择 <div> 元素内部的所有 <p> 元素。1element>elementdiv>p选择父元素为 <div> 元素的所有 <p> 元素。2element+elementdiv+p选择紧接在 <div> 元素之后的所有 <p> 元素。2[attribute][target]选择带有 target 属性所有元素。2[attribute=value][target=_blank]选择 target="_blank" 的所有元素。2[attribute~=value][title~=flower]选择 title 属性包含单词 "flower" 的所有元素。2[attribute|=value][lang|=en]选择 lang 属性值以 "en" 开头的所有元素。2:linka:link选择所有未被访问的链接。1:visiteda:visited选择所有已被访问的链接。1:activea:active选择活动链接。1:hovera:hover选择鼠标指针位于其上的链接。1:focusinput:focus选择获得焦点的 input 元素。2:first-letterp:first-letter选择每个 <p> 元素的首字母。1:first-linep:first-line选择每个 <p> 元素的首行。1:first-childp:first-child选择属于父元素的第一个子元素的每个 <p> 元素。2:beforep:before在每个 <p> 元素的内容之前插入内容。2:afterp:after在每个 <p> 元素的内容之后插入内容。2:lang(language)p:lang(it)选择带有以 "it" 开头的 lang 属性值的每个 <p> 元素。2element1~element2p~ul选择前面有 <p> 元素的每个 <ul> 元素。3[attribute^=value]a[src^="https"]选择其 src 属性值以 "https" 开头的每个 <a> 元素。3[attribute$=value]a[src$=".pdf"]选择其 src 属性以 ".pdf" 结尾的所有 <a> 元素。3[attribute*=value]a[src*="abc"]选择其 src 属性中包含 "abc" 子串的每个 <a> 元素。3:first-of-typep:first-of-type选择属于其父元素的首个 <p> 元素的每个 <p> 元素。3:last-of-typep:last-of-type选择属于其父元素的最后 <p> 元素的每个 <p> 元素。3:only-of-typep:only-of-type选择属于其父元素唯一的 <p> 元素的每个 <p> 元素。3:only-childp:only-child选择属于其父元素的唯一子元素的每个 <p> 元素。3:nth-child(n)p:nth-child(2)选择属于其父元素的第二个子元素的每个 <p> 元素。3:nth-last-child(n)p:nth-last-child(2)同上,从最后一个子元素开始计数。3:nth-of-type(n)p:nth-of-type(2)选择属于其父元素第二个 <p> 元素的每个 <p> 元素。3:nth-last-of-type(n)p:nth-last-of-type(2)同上,但是从最后一个子元素开始计数。3:last-childp:last-child选择属于其父元素最后一个子元素每个 <p> 元素。3:root:root选择文档的根元素。3:emptyp:empty选择没有子元素的每个 <p> 元素(包括文本节点)。3:target#news:target选择当前活动的 #news 元素。3:enabledinput:enabled选择每个启用的 <input> 元素。3:disabledinput:disabled选择每个禁用的 <input> 元素3:checkedinput:checked选择每个被选中的 <input> 元素。3:not(selector):not(p)选择非 <p> 元素的每个元素。3::selection::selection选择被用户选取的元素部分。3