HTML 샘플
<ul> <li><a href="#top">TOP</a></li> <li><a href="https://developer.mozilla.org">mozilla</a></li> <li><a href="https://example.com">example</a></li> <li><a href="https://www.yahoo.co.jp">yahoo</a></li></ul>기본 속성 선택자
/* 전방 일치: "#" 으로 시작 */a[href^="#"] { color: fuchsia;}
/* 후방 일치: ".org" 으로 끝남 */a[href$=".org"] { color: orange;}
/* 부분 일치: "example" 을 포함 */a[href*="example"] { color: red;}
/* 복합 조건: "https" 로 시작하고 ".jp" 로 끝남 */a[href^="https"][href$=".jp"] { color: green;}선택자 목록
| 선택자 | 의미 |
|---|---|
[attr] | 속성이 존재함 |
[attr="value"] | value 와 완전 일치 |
[attr^="value"] | value 로 시작 |
[attr$="value"] | value 로 끝남 |
[attr*="value"] | value 를 포함 |
대소문자 무시(i 플래그)
끝에 i 를 붙이면 대소문자를 구별하지 않는다.
/* ".pdf" 도 ".PDF" 도 매치 */a[href$=".pdf" i]::after { content: " (PDF)";}:not() 과의 조합
/* 외부 링크(http/https)만 대상 */a[href^="http"]::after { content: " ↗";}
/* 페이지 내 링크(#)는 제외 */a[href^="http"]:not([href*="example.com"])::after { content: " ↗";}실용 예: 파일 타입별 아이콘
/* PDF 링크에 라벨 추가 */a[href$=".pdf" i]::after { content: " [PDF]"; font-size: 0.75em; color: #c00;}
/* 외부 링크에 화살표 아이콘 */a[href^="https://"]:not([href^="https://example.com"])::after { content: " ↗";}
/* 앵커 링크는 스타일 없음 */a[href^="#"] { text-decoration: none;}명시도
속성 선택자의 명시도는 클래스 선택자와 동일한 (0, 1, 0).
a[href^="#"] { } /* (0, 1, 0) — 클래스와 동일 */a.internal { } /* (0, 1, 1) — 클래스 + 요소로 이쪽이 우선 */
hsb.horse