CSS 의사 요소
의사 요소 란 무엇입니까?
CSS 의사 요소는 요소의 지정된 부분에 스타일을 지정하는 데 사용됩니다.
예를 들어 다음과 같은 용도로 사용할 수 있습니다.
- 요소의 첫 글자 또는 줄 스타일 지정
- 요소의 내용 앞이나 뒤에 내용 삽입
통사론
의사 요소의 구문:
selector::pseudo-element {
property: value;
}
::첫 번째 줄 의사 요소
::first-line
의사 요소는 텍스트의 첫 번째 줄에 특별한 스타일을 추가하는 데 사용됩니다 .
다음 예제에서는 모든 <p> 요소에 있는 텍스트의 첫 번째 줄에 서식을 지정합니다.
예시
p::first-line
{
color: #ff0000;
font-variant: small-caps;
}
참고: 의사 ::first-line
요소는 블록 수준 요소에만 적용할 수 있습니다.
::first-line
의사 요소 에는 다음 속성이 적용됩니다 .
- 글꼴 속성
- 색상 속성
- 배경 속성
- 단어 간격
- 문자 간격
- 텍스트 장식
- 수직 정렬
- 텍스트 변환
- 줄 높이
- 분명한
이중 콜론 표기법에 주목하세요. 이중 콜론 ::first-line
이 CSS3
:first-line
의 의사 요소에 대한 단일 콜론 표기법을 대체했습니다. 이것은 의사 클래스
와 의사 요소 를 구별하기 위한 W3C의 시도였습니다 .
단일 콜론 구문은 CSS2 및 CSS1에서 의사 클래스와 의사 요소 모두에 사용되었습니다.
이전 버전과의 호환성을 위해 단일 콜론 구문은 CSS2 및 CSS1 의사 요소에 대해 허용됩니다.
::첫 글자 의사 요소
의사 요소는 텍스트의 첫 ::first-letter
글자에 특별한 스타일을 추가하는 데 사용됩니다.
다음 예제에서는 모든 <p> 요소에 있는 텍스트의 첫 글자에 서식을 지정합니다.
예시
p::first-letter
{
color: #ff0000;
font-size: xx-large;
}
참고: 의사 ::first-letter
요소는 블록 수준 요소에만 적용할 수 있습니다.
다음 속성이 ::first-letter 의사 요소에 적용됩니다.
- 글꼴 속성
- 색상 속성
- 배경 속성
- 여백 속성
- 패딩 속성
- 테두리 속성
- 텍스트 장식
- vertical-align("float"이 "none"인 경우에만)
- 텍스트 변환
- 줄 높이
- 뜨다
- 분명한
의사 요소 및 HTML 클래스
의사 요소는 HTML 클래스와 결합할 수 있습니다.
예시
p.intro::first-letter {
color: #ff0000;
font-size: 200%;
}
위의 예는 class="intro"인 단락의 첫 글자를 빨간색으로 더 큰 크기로 표시합니다.
다중 의사 요소
여러 유사 요소를 결합할 수도 있습니다.
다음 예에서 단락의 첫 글자는 xx-large 글꼴 크기에서 빨간색입니다. 첫 번째 줄의 나머지 부분은 파란색이며 작은 대문자로 표시됩니다. 단락의 나머지 부분은 기본 글꼴 크기와 색상이 됩니다.
예시
p::first-letter
{
color: #ff0000;
font-size: xx-large;
}
p::first-line
{
color: #0000ff;
font-variant: small-caps;
}
CSS - ::before Pseudo-element
의사 요소를 사용하여 요소 의 ::before
콘텐츠 앞에 일부 콘텐츠를 삽입할 수 있습니다.
다음 예에서는 각 <h1> 요소의 내용 앞에 이미지를 삽입합니다.
예시
h1::before
{
content: url(smiley.gif);
}
CSS - ::after 의사 요소
의사 요소는 요소 의 ::after
내용 뒤에 일부 내용을 삽입하는 데 사용할 수 있습니다.
다음 예에서는 각 <h1> 요소의 내용 뒤에 이미지를 삽입합니다.
예시
h1::after
{
content: url(smiley.gif);
}
CSS - ::marker 의사 요소
::marker
의사 요소는 목록 항목의 마커를 선택합니다 .
다음 예제에서는 목록 항목의 마커 스타일을 지정합니다.
예시
::marker {
color: red;
font-size: 23px;
}
CSS - ::selection 의사 요소
::selection
의사 요소는 사용자가 선택한 요소의 부분과 일치합니다 .
,
, 및 에 적용할 수 있는 CSS 속성은 다음과 ::selection
같습니다
.color
background
cursor
outline
다음 예에서는 선택한 텍스트를 노란색 배경에 빨간색으로 만듭니다.
예시
::selection {
color: red;
background: yellow;
}
모든 CSS 유사 요소
Selector | Example | Example description |
---|---|---|
::after | p::after | Insert something after the content of each <p> element |
::before | p::before | Insert something before the content of each <p> element |
::first-letter | p::first-letter | Selects the first letter of each <p> element |
::first-line | p::first-line | Selects the first line of each <p> element |
::marker | ::marker | Selects the markers of list items |
::selection | p::selection | Selects the portion of an element that is selected by a user |
모든 CSS 의사 클래스
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 |