【從0到1學Web前端】CSS偽類和虛擬元素,css偽

來源:互聯網
上載者:User

【從0到1學Web前端】CSS偽類和虛擬元素,css偽
1.CSS中的偽類

CSS 偽類用於向某些選取器添加特殊的效果。

文法:
selector : pseudo-class {property: value}

CSS 類也可與偽類搭配使用

selector.class : pseudo-class {property: value}

如下面的一段代碼:

a.red : visited {color: #FF0000}<a class="red" href="css_syntax.asp">CSS Syntax</a>
1.1錨偽類

在支援 CSS 的瀏覽器中,連結的不同狀態都可以不同的方式顯示,這些狀態包括:活動狀態,已被訪問狀態,未被訪問狀態,和滑鼠移至上方狀態。

a:link {color: #FF0000} /* 未訪問的連結 */a:visited {color: #00FF00}  /* 已訪問的連結 */a:hover {color: #FF00FF}    /* 滑鼠移動到連結上 */a:active {color: #0000FF}   /* 選定的連結 */

注意:

1.在 CSS 定義中,a:hover 必須被置於 a:link 和 a:visited之後,才是有效。
2.在 CSS 定義中,a:active 必須被置於a:hover 之後,才是有效。
3.偽類名稱對大小寫不敏感。

1.2 CSS2 - :first-child 偽類

定義:

first-child 偽類來選擇元素的第一個子項目。

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Document</title>    <style type="text/css">        p:first-child {font-weight: bold;}        li:first-child {text-transform:uppercase;}    </style></head><body>    <div>        <p>These are the necessary steps:</p>        <ul>            <li>Intert Key</li>            <li>                Turn key <strong>clockwise</strong>            </li>            <li>Push accelerator</li>        </ul>        <p>            Do <em>not</em>            push the brake at the same time as the accelerator.        </p>    </div></body></html>

效果如:

第一個規則將作為某元素第一個子項目的所有 p 元素設定為粗體。第二個規則將作為某個元素(在 HTML 中,這肯定是 ol 或 ul 元素)第一個子項目的所有 li 元素變成大寫。

必須聲明<!DOCTYPE>,這樣 :first-child 才能在 IE 中生效。

<style type="text/css">p:first-child {  color: red;  } </style>

選取器匹配作為任何元素的第一個子項目的 p 元素

<style type="text/css">p > i:first-child {  font-weight:bold;  } </style>

選取器匹配所有 <p>元素中的第一個<i>元素

<style type="text/css">p:first-child i {  color:blue;  } </style>

選取器匹配所有作為元素的第一個子項目的 <p>元素中的所有 <i>元素

1.3 CSS2 - :lang 偽類

:lang 偽類使你有能力為不同的語言定義特殊的規則

<style type="text/css">        q:lang(no) {                quotes: "~" "~"        }    </style>    <body>        <p>            文字            <q lang="no">段落中的引用的文字</q>            文字        </p></body>

2.CSS中的虛擬元素

CSS 虛擬元素用於將特殊的效果添加到某些選取器。

文法:

虛擬元素的文法:

選取器 : 虛擬元素 { 屬性: 值 }

CSS 類也可以與虛擬元素配合使用:

選取器 . 類: 虛擬元素 { 屬性: 值 }
p.article:first-letter {color: #FF0000}<p class="article">文章中的一個段落。</p>

:first-line的使用方法:

p {font-size: 12pt}p:first-line {color: #0000FF; font-variant: small-caps}

first-line 虛擬元素僅能被用於區塊層級元素。
下面的屬性可以被應用到 first-line 虛擬元素:
font 屬性
color 屬性
background 屬性
word-spacing
letter-spacing
text-decoration
vertical-align
text-transform
line-height
clear

2.1多重虛擬元素
p {font-size: 12pt;}p:first-letter {color: #FF0000; font-size: 24pt;}p:first-line {color: #0000FF;}<p>The first words of an article</p>
2.2 CSS2 - :before 虛擬元素

before(>=IE8) 虛擬元素可用於在某個元素之前插入某些內容。

h1:before{    content: url(beep.wav)}
2.3 CSS2 - :after 虛擬元素

after(>=IE8) 偽類可用於在某個元素之後插入某些內容。

h1:after{    content: url(beep.wav)}

content 屬性與 :before 及 :after 虛擬元素配合使用,來插入產生內容。

a:after  {  content: " (" attr(href) ")";  }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.