CSS patterns, selectors, pseudo-classes

To understand how CSS Selectors work, let's use the following web page as an example:

<html>

  <head>

    <title>CSS</title>

    <style type='text/css'>

      body {background: $FFFFEE; font-family:Calibri, Corbel, Arial; font-size: 12px;}

      table {border: 1px solid #CCCCCC;}

      h4 {margin-bottom: -1px; font-variant: small-caps;}

      .row1 {background: #EEE;}

      .row2 {background: #DDD;}

      .row3 {background: #CCC;}

      .row4 {background: #BBB;}

      td {padding:5px;}

      .btn_blue {width: 75px; color: blue;}

      .btn_green {width: 75px; color: green;}

    </style>

  </head>

  <body>

    <h4>John Smith <input type=checkbox name="select" /></h4>

    <table id='table1'>

      <tr class='row1' id=BPT>

        <td>View ID</td>

        <td><input type='button' value='Button 1' class='btn_blue' id='btnfirst'></td>

      </tr>

      <tr class='row2' id=QC>

        <td>View Address</td>

        <td><input type='button' value='Button 2' class='btn_blue'></td>

      </tr>

      <tr class='row3' id=QTP>

        <td>View Phone Number</td>

        <td><input type='button' value='Button 3' class='btn_green'></td>

      </tr>

    </table>

    <h4>Anne Anderson <input type="checkbox" name="select" checked=true /></h4>

    <table id='table2'>

     <tr class='row1' id=BPT>

        <td>View ID</td>

        <td><input type='button' value='Button 4' class='btn_blue'></td>

      </tr>

      <tr class='row2' id=QC>

        <td>View Address</td>

        <td><input type='button' value='Button 5' class='btn_blue'></td>

      </tr>

      <tr class='row3' id=QTP>

        <td>View Phone Number</td>

        <td><input type='button' value='Button 6' class='btn_green'></td>

      </tr>

      <tr class='row4' id=QTP>

        <td>View FAX Number</td>

        <td><input type='button' value='Button 7' class='btn_green'></td>

      </tr>

    </table>

  </body>

</html>

 

 Some of the important CSS patterns are described below:

PatternDescription
*Matches any node.
pMatches any P(paragraph) node.
table trMatches any TR node that is the descendant of TABLE node.
table > trMatches any TR node that is the child of TABLE node.
h4 + tableMatches any TABLE node that is preceded by H4.

 

Note: The CSS expressions in the examples below come from the HTML source at the beginning of this chapter.

SelectorDescriptionExampleDescription
.classSelects all elements with the supplied CLASS(CSS1)..row1Selects all elements where CLASS = row1.
#idSelects the element with the supplied ID(CSS1).#table1Selects the element with ID = table1.
*Selects all elements(CSS2).*Selects all elements.
elementSelects the supplied elements(CSS1).InputSelects all <input> elements.
[attribute]Selects all elements with the supplied attribute(CSS2).[id]Selects all elements with the ID attribute.
[attribute=value]Selects all elements with the supplied attribute where ATTRIBUTE equals VALUE(CSS2).[class=btn_blue]where the CLASS attribute equals btn_blue.
[attribute~=value]Selects the element whose ATTRIBUTE is a list of whitespace-separated values,one of which is exactly equal to VALUE(CSS2).[innertext~=John]Selects the element where the INNERTEXT is a list of whitespace-seperated values,one of which is exactly equal to JOHN.
[attribute^=value]Selects all elements where ATTRIBUTE value begins with VALUE(CSS3).[class^=btn]Selects all elements where the CLASS attribute begins with btn.
[attribute$=value]Selects all elements where ATTRIBUTE value ends with VALUE(CSS3).[class$=green]Selects all elements where the CLASS attribute ends with green.
[attribute*=value]Selects all elements where ATTRIBUTE value contains the substring VALUE(CSS3).[class*=w4]Selects all elements where the CLASS attribute contains the substring w4.
:first-of-typeSelects every element that is the first child of its parent(CSS3).input:first-of-typeSelects every INPUT element that is the first INPUT element of its parent.
:last-of-typeSelects every element that is the last child of its parent(CSS3).input:last-of-typeSelects every INPUT element that is the last INPUT element of its parent.
:only-of-typeSelects every element that is the only child of its parent(CSS3).input:only-of-typeSelects every INPUT element that is the only INPUT element of its parent.
:nth-child(n)Selects every element that is the nth child of its parent(CSS3).input:nth-child(1)Selects every INPUT element that is the first child of its parent.
:nth-of-type(n)Selects every specified element that is the nth element of its parent(CSS3).input:nth-of-type(1)Selects every INPUT element that is the first INPUT element of its parent.
:nth-last-of-type(n)Selects every specified element that is the nth element of its parent, counting from the last child(CSS3).:nth-last-of-type(1)Selects every INPUT element that is the first INPUT element of its parent,starting from the last child.

 

The next table details the CSS pseudo-classes.

Pseudo-ClassDescriptionExampleDescription
:last-childSelects every element that is the child of its parent(CSS3).td:last-childSelects every TD element that is the last child of its parent.
:rootSelects the document root element(CSS3).:rootSelects the document root element.
:enabledSelects every specified element that is enabled(CSS3).input:enabledSelects every enabled INPUT element.
:disabledSelects every specified element that is disabled(CSS3).input:disabledSelects every disabled INPUT element.
:checkedSelects every specified element that is checked(CSS3).input:checkedSelects every checked INPUT element.

转载于:https://www.cnblogs.com/dushuai/articles/3436253.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值