CSS之偽類,css

來源:互聯網
上載者:User

CSS之偽類,css

1.

:link                     向未被訪問的連結添加樣式

:visited                向已被訪問的連結添加樣式

:hover                  當滑鼠懸浮在元素上方時,向元素添加樣式

:active                 向被啟用的元素添加樣式(被點擊)

以上四種偽類應用於超連結時,a:hover必須在a:link和a:visited之後,a:active必須在a:hover之後,a:link和a:visited兩者的順序沒有規定

1.1 超連結:

<style>a:link{    color:red;    }a:visited{    color:green;    }a:hover{    color:blue;    }a:active{    color:yellow;    }</style><a href="#">pmx-cnblogs</a>

 1.2 div

<style>div{    width:100px;    height:100px;    background-color:red;    border:3px solid blue;    overflow:hidden;        transition:width 1s,height 2s,background-color 1s,line-height 2s,font-size 2s;        line-height:100px;    text-align:center;}div:hover{    background-color:green;    width:300px;        height:300px;    line-height:300px;        font-size:2em}div:active{    color:yellow;}</style><div>This is div</div>

 正常狀態:

滑鼠移到div上時的樣子:

滑鼠左鍵按下後得樣子:

 2. :first-child

 p:first-child選擇作為某個元素的第一個子項目的p元素,而不是選擇p元素的第一個子項目

<style>#myDiv > p:first-child{   background-color:#3C6;    }</style><body>    <p>p8</p>    <div id='myDiv' style='width:300px;height:300px;overflow:auto'>        <p>p1</p>        <p>p2</p>        <p>p3</p>        <div>            <p>p4</p>            <p>p5</p>        </div>    </div>    <p>p6</p>    <p>p7</p></body>

P1作為myDiv的第一個子項目被選中

 3. :focus

向獲得焦點的元素添加特殊樣式,比如按鈕被點擊後,文字框輸入文本

<style>input:focus{    background-color:yellow;    }</style><input type='text' value=""/>

 <style>:lang(zh){ color:red; }</style><p lang="zh">p7</p>

5. :target

:target偽類和超連結有關,當我們希望開啟頁面或者單擊按鈕定位到頁面中某個元素時,常常是這樣的

<a href="#jump>jump</a> <!--必須是ID號--><p id="#jump">This is paragraph</p>

:target就是捕獲點擊超連結後,定位到的元素,在:target裡面的CSS代碼都是控制這個捕獲到的元素的。

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Document</title>    <style>         :target{            background-color:red;        }    </style></head><body>    <a href="#jump1">jump To 1</a>    <a href="#jump2">jump To 2</a>    <p id="jump1">This is paragraph1</p>    <p id="jump2">This is paragraph2</p></body></html>

點擊jump to 1時,paragraph1變紅。

點擊jump to 2時,paragraph2變紅

:target表示所有被定位到的元素,背景色改變。如果我們想讓paragraph1單獨變紅,可以這樣:

<style>         #jump1:target{            background-color:red;        }    </style>

 案例:

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Document</title>    <style>         .tab_list{            position: relative;            margin:50px;            min-height: 200px;        }        .tab_content{            position: absolute;            width:600px;            height:170px;            padding:15px;            border:1px solid #91a7b4;            border-radius:3px;            box-shadow:0 2px 3px rgba(0,0,0,0.1);            font-size:1.2em;            line-height:1.5em;            color:#666;            background-color: #fff;            overflow: hidden;        }        .tab_menu{            position: absolute;            top:100%;            list-style-type: none;            margin:0;            padding:0;        }        .tab_menu li{            display:inline-block;        }        .tab_menu a{            text-decoration: none;            display:block;            padding:5px;            font-size:12px;            font-family:"楷體";            color:#333;            border:1px solid #91a7b4;            border-radius: 0 0 5px 5px;            background-color:#e3f1f8;            margin-right:10px;        }        :target{            z-index:10;                    }            </style></head><body>    <div class="tab_list">        <ul class="tab_menu">            <li><a href="#tab1">標籤一</a></li>            <li><a href="#tab2">標籤二</a></li>            <li><a href="#tab3">標籤三</a></li>        </ul>        <div id="tab1" class="tab_content">歡迎加設計達人Q群:50063010<br/>            設計達人以原創和分享一些高品質的設計文章為主,希望喜歡!        </div>        <div id="tab2" class="tab_content">            <p>本Tab轉場效果純CSS3製作,無任何JavaScript</p>            最新Q群:50063010<br/>            愛設計,愛分享——設計達人(<a href="http://www.shejidaren.com">http://www.shejidaren.com</a>)        </div>        <div id="tab3" class="tab_content">            <p>高品質設計文章分享平台</p>            <p>歡迎加設計達人Q群:50063010<br /><a href="http://www.shejidaren.com/">設計達人</a>以原創和分享一些高品質的設計文章為主,希望喜歡</p>        </div>    </div></body></html>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.