CSS3选择器

2017-09-15  本文已影响0人  秋枫残红

基础

属性选择器(CSS2)

<style>
        [id="test1"]{
            background-color: yellow;
            height: 200px;
            width:400px;
        }
    </style>

属性选择器的扩展(CSS3)

<style>
        [id^="te"]{
            background-color: yellow;
            height: 200px;
            width:400px;
        }
    </style>
<div id="test"> 
</div>
<style>
        [id$="st"]{
            background-color: yellow;
            height: 200px;
            width:400px;
        }
    </style>
<div id="test"> 
</div>
<style>
        [id*="es"]{
            background-color: yellow;
            height: 200px;
            width:400px;
        }
    </style>
<div id="test"> 
</div>

伪类选择器

  <style>
        a:link{
            color: #FF0000;
            text-decoration: none;
        }
        /*鼠标点击之前*/
        a:hover{
            color: #00FF00;
            text-decoration: none;
        }
        /*鼠标挪动到链接上*/
        a:visited{
            color: #FF00FF;
            text-decoration: underline;
        }
        /*鼠标点击之后*/
        a:active{
            color:#0000ff;
            text-decoration: underline;
        }
        /*鼠标点击之时*/
    </style>

伪元素选择器

<style>
        p:first-line{
            color: #00ccff;
        }
    </style>
<p>云泥岂知鲲鹏志,<br>扶摇直上九万里</p>
<style>
        p:first-letter{
            color: #00ccff;
        }
    </style>
<p>云泥岂知鲲鹏志,<br>扶摇直上九万里</p>
p:before{
            content: '待到秋来九月八,';
            color: #00ccff;
        }
/*插入文字*/
p:before{
            content: url(1.png);
            color: #00ccff;
        }
/*插入图片*/

结构性伪类选择器

root,not,empty,target

:root{
    background-color: yellow;
}
body{
    background-color: limegreen;
}
<style>
        #test *:not(h1){
            background-color: yellow;
        }
    </style>
div:empty{
            height: 100px;
            width: 100px;
            background-color: yellow;
        }
<style type="text/css">
:target{
    background-color: yellow;
}
</style>
</head>
<body>
<p id="menu">
<a href="#text1">示例文字1</a> | 
<a href="#text2">示例文字2</a> | 
<a href="#text3">示例文字3</a> | 
<a href="#text4">示例文字4</a> | 
<a href="#text5">示例文字5</a> 
</p>
<div id="text1">
<h2>示例文字1</h2>
<p>...此处略去</p>
</div>
<div id="text2">
<h2>示例文字2</h2>
<p>...此处略去</p>
</div>
<div id="text3">
<h2>示例文字3</h2>
<p>...此处略去</p>
</div>
<div id="text4">
<h2>示例文字4</h2>
<p>...此处略去</p>
</div>
<div id="text5">
<h2>示例文字5</h2>
<p>...此处略去</p>
</body>

first-child、last-child、nth-child、nth-last-child

li:first-child{
            background-color: yellow;
        }
        li:last-child{
            background-color: black;
        }
<style>
        li:nth-child(3){
            background-color: yellow;
        }
    </style>
<style>
        li:nth-last-child(2){
            background-color: yellow;
        }
    </style>
<style>
        ul li:nth-child(even){
            background-color: yellow;
        }
    </style>
所有偶数序列的元素
<style>
        ul li:nth-child(odd){
            background-color: yellow;
        }
    </style>
所有奇数序列的元素

nth-of-type与nth-last-of-type

<style type="text/css">
        h2:nth-of-type(odd){
        background-color:yellow;
}
</style>

循环使用样式

    <style>
        li:nth-child(3n){
            background-color: yellow;
        }
        li:nth-child(3n+1){
            background-color: red;
        }
        li:nth-child(3n+2){
            background-color: green;
        }
    </style>

UI元素状态伪类选择器

E:hover、E:active、E:focus

E:enabled、E:disabled

<script>
function radio_onchange()
{    
    var radio=document.getElementById("radio1");
    var text=document.getElementById("text1");
    if(radio.checked)
        text.disabled="";
    else
    {
        text.value="";
        text.disabled="disabled";
    }
}
</script>
<style>
input[type="text"]:enabled{
    background-color:yellow; 
}
input[type="text"]:disabled{
    background-color:purple;
}
</style>
<form>
<input type="radio" id="radio1" name="radio" 
 onchange="radio_onchange();">可用</radio>
<input type="radio" id="radio2" name="radio" 
onchange="radio_onchange();">不可用</radio><br/>
<input type=text id="text1" disabled />
</form>

E:read-only、E:read-write

<style type="text/css">
input[type="text"]:read-only{
        background-color: gray;
}
input[type="text"]:read-write{
        background-color: greenyellow;
}
//firefox
input[type="text"]:-moz-read-only{
        background-color: gray;
}
input[type="text"]:-moz-read-write{
        background-color: greenyellow;
}
</style>

E:checked、E:default、E:indeterminate、

input[type="radio"]:indeterminate{
        outline: solid 1px blue;
}

E::selection

<style type="text/css">
p::selection{
    background:red;
    color:#FFF;
}
p::-moz-selection{
    background:red;
    color:#FFF;
}
input[type="text"]::selection{
    background:gray;
    color:#FFF;
}
input[type="text"]::-moz-selection{
    background:gray;
    color:#FFF;
}
td::selection{
    background:green;
    color:#FFF;
}
td::-moz-selection{
    background:green;
    color:#FFF;
}
</style>

通用兄弟元素选择器

div ~ p {background-color:#00FF00;}
上一篇 下一篇

猜你喜欢

热点阅读