찾다
웹 프론트엔드CSS 튜토리얼CSS의 의사 클래스 선택기와 의사 요소 선택기의 코드 분석

CSS의 의사 클래스 선택기와 의사 요소 선택기의 코드 분석

Sep 05, 2018 pm 06:00 PM
의사 요소 선택기의사 클래스 선택기

이 글은 CSS의 의사 클래스 선택기와 의사 요소 선택기에 대한 소개를 제공합니다. 이는 특정 참조 가치가 있으므로 도움이 될 수 있습니다.

1. 의사 클래스 연결

1. 의사 클래스 연결

        /*链接伪类*/        注意:link,:visited,:target是作用于链接元素的!        
        :link       表示作为超链接,并指向一个未访问的地址的所有锚        
        :visited    表示作为超链接,并指向一个已访问的地址的所有锚        
        :target     代表一个特殊的元素,它的id是URI的片段标识符

2. 코드 예:
01_anchor pseudo-class.html

<head>
        <meta charset="UTF-8">
        <title></title>
        <!--:link:表示作为超链接,并指向一个未访问的地址的所有锚-->
        <style type="text/css">
            a{                text-decoration: none;            }
            a:link{                color: deeppink;            }
            #test:link{                background: pink;            }
        </style>
    </head>
    <body>
        <a href="#">点我点我点我</a>
        <p id="test">我是p啦</p>
    </body>

02_anchor pseudo-class.html

<head>
        <meta charset="UTF-8">
        <title></title>
        <!--:visited:表示作为超链接,并指向一个已访问的地址的所有锚-->
        <style type="text/css">
            a{                text-decoration: none;            }
            a:link{                color: black;            }
            a:visited{                color: pink;            }
        </style>
    </head>
    <body>
        <a href="#">点我点我点我</a>
    </body>

03_target .html

<head>
        <meta charset="UTF-8">
        <title></title>
        <!--:target 代表一个特殊的元素,这个元素的id是URI的片段标识符。--> 
        <style type="text/css">
            *{                margin: 0;                padding: 0;            }
            p{                width: 300px;                height: 200px;                line-height: 200px;                background: black;                color: pink;                text-align: center;                display: none;            }
            :target{                display: block;            }
        </style>
    </head>
    <body>
        <a href="#p1">p1</a>
        <a href="#p2">p2</a>
        <a href="#p3">p3</a>
        <p id="p1">
            p1        </p>
        <p id="p2">
            p2        </p>
        <p id="p3">
            p3        </p>
    </body>

2. 동적 의사 클래스

        /*动态伪类*/        注意:hover,:active基本可以作用于所有的元素!        
        :hover      表示悬浮到元素上        
        :active     表示匹配被用户激活的元素(点击按住时)
注意:
由于a标签的:link和:visited可以覆盖了所有a标签的状态,所以当:link,:visited,:hover,:active同时出现在a标签身上时 :link和:visited不能放在最后!!!
2. 코드 예:

    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            #test:hover{                color: pink;            }
            #test:active{                color: red;            }
        </style>
    </head>
    <body>
        <p id="test">
            我是test        </p>
    </body>
3. 개인 정보 보호 및: 방문 선택기
/*隐私与:visited选择器*/只有下列的属性才能被应用到已访问链接
:    color  background-color  border-color

4. 폼 관련 의사 클래스

1. 폼 관련 의사 클래스

    /*表单相关伪类*/
    :enabled    匹配可编辑的表单    
    :disable    匹配被禁用的表单    
    :checked    匹配被选中的表单    
    :focus      匹配获焦的表单

2. 코드 예: 01_form status.html

<head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>无标题文档</title>
        <style>
            input {                width: 100px;                height: 30px;                color: #000;            }

            input:enabled {                color: red;            }

            input:disabled {                color: blue;            }
        </style>
    </head>

    <body>
        <input type="text" value="晓飞张" />
        <input type="text" value="晓飞张" disabled="disabled" />
    </body>

02_form status.html

<head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>无标题文档</title>
        <style>
            input:checked {                width: 100px;                height: 100px;            }
        </style>
    </head>

    <body>
        <input type="checkbox" />
    </body>
03_ 포커스를 받으세요. html
<head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            input:focus{                background: pink;            }
            p:focus{                background: pink;            }

        </style>
    </head>
    <body>
        <input type="text"  value="" />
        <p style="width: 200px;height: 200px;background: deeppink;" contenteditable="true" ></p>
    </body>

04_시뮬레이트 라디오 버튼.html

<head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>无标题文档</title>
        <style>
            label {                float: left;                margin: 0 5px;                overflow: hidden;                position: relative;            }

            label input {                position: absolute;                left: -50px;                top: -50px;            }

            span {                float: left;                width: 50px;                height: 50px;                border: 3px solid #000;            }

            input:checked~span {                background: red;            }
        </style>
    </head>

    <body>
        <label>
        <input type="radio" name="tab" />
        <span></span>
    </label>
        <label>
        <input type="radio" name="tab" />
        <span></span>
    </label>
        <label>
        <input type="radio" name="tab" />
        <span></span>
    </label>
    </body>

4. 구조적 의사 클래스

1. 구조적 의사 클래스

/*结构性伪类*/index的值从1开始计数!!!!
index可以为变量n(只能是n)
index可以为even odd    #wrap ele:nth-child(index)      表示匹配#wrap中第index的子元素 这个子元素必须是ele    #wrap ele:nth-of-type(index)    表示匹配#wrap中第index的ele子元素
    除此之外:nth-child和:nth-of-type有一个很重要的区别!!
            nth-of-type以元素为中心!!!

:nth-child(index)系列         
    :first-child
    :last-child
    :nth-last-child(index)
    :only-child (相当于:first-child:last-child 或者 :nth-child(1):nth-last-child(1))
:nth-of-type(index)系列
    :first-of-type
    :last-of-type
    :nth-last-type(index)
    :only-of-type   (相当于:first-of-type:last-of-type 或者 :nth-of-type(1):nth-last-of-type(1))

:not        :empty(内容必须是空的,有空格都不行,有attr没关系)

2. 코드 예:

<head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">

            /*子元素的标签应该要统一*/
            /*ul .item:nth-child(3){
                border: 1px solid;
            }*/


            ul .item:nth-of-type(3){                border: 1px solid;            }
            /*ul p:nth-of-type(3){
                border: 1px solid;
            }
            ul p:nth-of-type(3){
                border: 1px solid;
            }
            ul li:nth-of-type(3){
                border: 1px solid;
            }*/
        </style>
    </head>
    <body>
        <ul>
            <p class="item">p1</p>
            <p class="item">p2</p>
            <p class="item">p3</p>
            <li class="item">1</li>
            <li class="item">2</li>
            <li class="item">3</li>
            <li class="item">4</li>
            <li class="item">5</li>
            <p class="item">p1</p>
            <p class="item">p2</p>
            <p class="item">p3</p>
            <li class="item">6</li>
            <li class="item">7</li>
            <li class="item">8</li>
            <li class="item">9</li>
        </ul>
    </body>

04_not.html

<head>
        <meta charset="UTF-8">
        <title>not</title>
        <style type="text/css">
            * {                margin: 0;                padding: 0;                border: none;            }

            a {                text-decoration: none;                color: #333;                font-size: 14px;                display: block;                float: left;                width: 100px;                height: 30px;            }

            p {                width: 800px;                margin: 0 auto;            }

            p>a:not(:last-of-type) {                border-right: 1px solid red;            }
        </style>
    </head>

    <body>
        <p>
            <a href="#">first</a>
            <a href="#">second</a>
            <a href="#">third</a>
            <a href="#">fourth</a>
            <a href="#">fifth</a>
        </p>
    </body>
05_empty.html
<head>
        <meta charset="UTF-8">
        <title>empty</title>
        <style type="text/css">
            p {                height: 200px;                background: #abcdef;            }

            p:empty {                background: #f00;            }
        </style>
    </head>

    <body>
        <p></p>
        <p>Second</p>
        <p></p>
        <p>Third</p>
    </body>

5. 의사 요소

1. 의사 요소

/*伪元素*/
    ::after
    ::before
    ::firstLetter
    ::firstLine
    ::selection

2. 코드 예: after.html

<head>
        <meta charset="UTF-8">
        <title>after</title>
        <style type="text/css">
            p {                width: 300px;                height: 100px;                border: 1px solid #000;            }

            p::after {                content: "我在内容的后面";            }
        </style>
    </head>

    <body>
        <p>伪元素</p>
    </body>

before.html

<head>
        <meta charset="UTF-8">
        <title>before</title>
        <style type="text/css">
            p {                width: 300px;                height: 100px;                border: 1px solid #000;            }

            p::before {                content: "我在内容的前面";            }
        </style>
    </head>

    <body>
        <p>伪元素</p>
    </body>
firstLetter.html
<head>
        <meta charset="UTF-8">
        <title>First-Letter</title>
        <style type="text/css">
            p {                width: 500px;                margin: 0 auto;                font-size: 12px;            }

            p::first-letter {                color: #f00;                font-size: 24px;                font-weight: bold;            }
        </style>
    </head>

    <body>
        <p>sssss</p>
    </body>

firstLine. html

<head>
        <meta charset="UTF-8">
        <title>First-Line</title>
        <style type="text/css">
            p {                width: 500px;                margin: 0 auto;            }

            p::first-line {                color: #f00;                font-weight: bold;            }
        </style>
    </head>

    <body>
        <p>
            sssss<br> sssss            <br> sssss            <br>
        </p>
    </body>

Selection.html

<head>
        <meta charset="UTF-8">
        <title>Selection</title>
        <style type="text/css">
            p::selection {                background: red;                color: pink;            }
        </style>
    </head>

    <body>
        <p>SelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelectionSelection</p>
    </body>

관련 권장 사항:

CSS에서 의사 클래스, 의사 요소 및 인접 요소 선택기 사용에 대한 몇 가지 팁

CSS의 선택기 유형 요약 및 효율성 비교 예

위 내용은 CSS의 의사 클래스 선택기와 의사 요소 선택기의 코드 분석의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
의사 요소가 어린이라는 것을 조금 상기시켜줍니다.의사 요소가 어린이라는 것을 조금 상기시켜줍니다.Apr 19, 2025 am 11:39 AM

여기에는 어린이 요소가있는 컨테이너가 있습니다.

'다이나믹 히트 영역'이있는 메뉴'다이나믹 히트 영역'이있는 메뉴Apr 19, 2025 am 11:37 AM

플라이 아웃 메뉴! 두 번째는 호버 이벤트를 사용하여 더 많은 메뉴 항목을 표시하는 메뉴를 구현해야합니다. 우선, 그들은해야합니다

WebVTT의 비디오 접근성 향상WebVTT의 비디오 접근성 향상Apr 19, 2025 am 11:27 AM

"웹의 힘은 보편적입니다. 장애에 관계없이 모든 사람의 접근은 필수적인 측면입니다."- Tim Berners-Lee

주간 플랫폼 뉴스 : CSS :: 마커 의사 요소, 사전 렌더링 웹 구성 요소, 사이트에 웹 멘션 추가주간 플랫폼 뉴스 : CSS :: 마커 의사 요소, 사전 렌더링 웹 구성 요소, 사이트에 웹 멘션 추가Apr 19, 2025 am 11:25 AM

이번 주에 Roundup : DatePickers는 키보드 사용자에게 두통, Fouc와 싸우는 데 도움이되는 새로운 웹 구성 요소 컴파일러를 제공하고 있으며, 마침내 스타일링 목록 항목 마커에 손을 대고 사이트에서 웹 커넥션을 얻는 4 단계입니다.

너비와 유연한 아이템을 만드는 것은 함께 훌륭하게 재생됩니다너비와 유연한 아이템을 만드는 것은 함께 훌륭하게 재생됩니다Apr 19, 2025 am 11:23 AM

짧은 답변 : Flex-Shrink 및 Flex-Basis는 아마도 당신이 찾고있는 것일 것입니다.

끈적 끈적하고 테이블 헤더를 배치하십시오끈적 끈적하고 테이블 헤더를 배치하십시오Apr 19, 2025 am 11:21 AM

당신은 ' t 포지션 : 스티커; 에이

주간 플랫폼 뉴스 : 검색 콘솔에서의 HTML 검사, 글로벌 스크립트 범위, Babel Env 추가 기본값 쿼리 추가주간 플랫폼 뉴스 : 검색 콘솔에서의 HTML 검사, 글로벌 스크립트 범위, Babel Env 추가 기본값 쿼리 추가Apr 19, 2025 am 11:18 AM

이번 주에 Web Platform News의 세계를 둘러싼 Google 검색 콘솔은 크롤링 된 마크 업을보다 쉽게 ​​볼 수 있습니다.

Indieweb 및 웹 협상Indieweb 및 웹 협상Apr 19, 2025 am 11:16 AM

Indieweb는 일입니다! 그들은 회의와 모든 것을 얻었습니다. 뉴요커는 심지어 그것에 대해 글을 쓰고 있습니다.

See all articles

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

뜨거운 도구

SecList

SecList

SecLists는 최고의 보안 테스터의 동반자입니다. 보안 평가 시 자주 사용되는 다양한 유형의 목록을 한 곳에 모아 놓은 것입니다. SecLists는 보안 테스터에게 필요할 수 있는 모든 목록을 편리하게 제공하여 보안 테스트를 더욱 효율적이고 생산적으로 만드는 데 도움이 됩니다. 목록 유형에는 사용자 이름, 비밀번호, URL, 퍼징 페이로드, 민감한 데이터 패턴, 웹 셸 등이 포함됩니다. 테스터는 이 저장소를 새로운 테스트 시스템으로 간단히 가져올 수 있으며 필요한 모든 유형의 목록에 액세스할 수 있습니다.

에디트플러스 중국어 크랙 버전

에디트플러스 중국어 크랙 버전

작은 크기, 구문 강조, 코드 프롬프트 기능을 지원하지 않음

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

SublimeText3 영어 버전

SublimeText3 영어 버전

권장 사항: Win 버전, 코드 프롬프트 지원!

PhpStorm 맥 버전

PhpStorm 맥 버전

최신(2018.2.1) 전문 PHP 통합 개발 도구