CSS-Pseudo-elements (偽元素)與 Pseudo-classes (偽類)

 

兩個冒號(::)是偽元素Pseudo-element,一個冒號是(:)偽類Pseudo-class

Pseudo-class顧名思義其特性就像是selector在操作class或是element一般的使用,

也可跟jQuery的filter配合,

例如:$( "li" ).filter( ":even" ).css( "background-color", "red" );

 

一、Pseudo-class

1、:link、:visited、:hover、:active

JS Bin

此範例為針對超連結被點選前(紅)、訪問過(綠)、滑鼠移到超連結上(紫)、滑鼠點選時(藍)

:link、:visited、:hover、:active須依先後順序放才能有正確效果

 

2、:first-child的應用

2-1、:first-child基本型

JS Bin on jsbin.com

選取該元素在父節點底下集合中排列第一位的指定元素,

注意如果有非指定元素排列在第一位,則指定元素將不會被選到。

 

2-2、p i:first-child將每個<p>tag底下的第一個<i>tag的字體變成藍色

on jsbin.com

 

2-3、p:first-child i將第一個<p>tag的所有的<i>tag字體變成藍色

JS Bin

 

3、更多其他用法的有

Selector Example Example description
:active a:active Selects the active link
:checked input:checked Selects every checked <input> element
:disabled input:disabled Selects every disabled <input> element
:empty p:empty Selects every <p> element that has no children
:enabled input:enabled Selects every enabled <input> element
:first-child p:first-child Selects every <p> elements that is the first child of its parent
:first-of-type p:first-of-type Selects every <p> element that is the first <p> element of its parent
:focus input:focus Selects the <input> element that has focus
:hover a:hover Selects links on mouse over
:in-range input:in-range Selects <input> elements with a value within a specified range
:invalid input:invalid Selects all <input> elements with an invalid value
:lang(language) p:lang(it) Selects every <p> element with a lang attribute value starting with "it"
:last-child p:last-child Selects every <p> elements that is the last child of its parent
:last-of-type p:last-of-type Selects every <p> element that is the last <p> element of its parent
:link a:link Selects all unvisited links
:not(selector) :not(p) Selects every element that is not a <p> element
:nth-child(n) p:nth-child(2) Selects every <p> element that is the second child of its parent
:nth-last-child(n) p:nth-last-child(2) Selects every <p> element that is the second child of its parent, counting from the last child
:nth-last-of-type(n) p:nth-last-of-type(2) Selects every <p> element that is the second <p> element of its parent, counting from the last child
:nth-of-type(n) p:nth-of-type(2) Selects every <p> element that is the second <p> element of its parent
:only-of-type p:only-of-type Selects every <p> element that is the only <p> element of its parent
:only-child p:only-child Selects every <p> element that is the only child of its parent
:optional input:optional Selects <input> elements with no "required" attribute
:out-of-range input:out-of-range Selects <input> elements with a value outside a specified range
:read-only input:read-only Selects <input> elements with a "readonly" attribute specified
:read-write input:read-write Selects <input> elements with no "readonly" attribute
:required input:required Selects <input> elements with a "required" attribute specified
:root root Selects the document's root element
:target #news:target Selects the current active #news element (clicked on a URL containing that anchor name)
:valid input:valid Selects all <input> elements with a valid value
:visited a:visited Selects all visited links

Tip1:

另外一點須注意的是CSS元素選取的方法雖然跟jQuery差不多,

但還是有不一樣的地方,例如jQuery有p:first的語法,

但pseudo-class卻是沒有這種語法喔。

Tip2:

在撰寫CSS樣式時須注意Pseudo-class跟Selector中間要留空白,在某瀏覽器可能無作用。

<!DOCTYPE html>
<html>
<head>
    <title>hover</title>
    <meta charset="utf-8" />
    <style>
        body {
            background: #000000;
            margin: 0;
        }

        ul {
            list-style-type: none;
            margin: 0;
            padding: 0;
        }

        li {
            display: inline-block;
        }

        .cover {
            position: absolute;
            top: 0;
              z-index:-1;
        }

        .color {
            opacity: 0.1;
            z-index:1;
        }

            .color :hover {
                opacity: 1;
            }

        .menu3 :hover {
            opacity: 1;
        }
    </style>
</head>
<body>
    <ul>
        <li class="menu3">
            <div class="color">
                <img src="http://i.imgur.com/LBJxiKM.png" />
            </div>
            <div class="cover">
                <img src="http://i.imgur.com/vyJOnbJ.png" />
            </div>
        </li>
    </ul>
</body>
</html>

 

二、Pseudo-element

1、p::first-line表示對p tag 的第一行do something

JS Bin

 

2、::first-letter表示對某tag的第一的字母do something

JS Bin

 

3、::before表示對某tag「裡面的前面」do something

<!DOCTYPE html>
<html>
<head>
    <style>
        h1 {
            border: 1px solid red;
        }
            h1::before {
                content: url(http://www.w3schools.com/css/smiley.gif);
            }
    </style>
</head>
<body>
    <h1>This is a heading</h1>
    <p>The ::before pseudo-element inserts content before an element.</p>
    <h1>This is a heading</h1>
    <p><b>Note:</b> IE8 supports the content property only if a !DOCTYPE is specified.</p>

</body>
</html>

雖然現在的瀏覽器對偽元素就算寫一個冒號也可以正常運作,不過為了方便區分,用兩個冒號還是比較好的。

 

4、更多其他用法的有

Selector Example Example description
::after p::after Insert content after every <p> element
::before p::before Insert content before every <p> element
::first-letter p::first-letter Selects the first letter of every <p> element
::first-line p::first-line Selects the first line of every <p> element
::selection p::selection Selects the portion of an element that is selected by a user

 

參考網址:

那些 CSS 偽元素可以幫你做的 10 個效果

CSS Pseudo-classes

CSS Pseudo-elements

CSS :nth-child() Selector