CSS 의사 요소는 요소뿐만 아니라 CSS 클래스에도 적용할 수 있습니다.
예시
CSS 클래스에서 CSS Pseudo 요소를 사용하는 예를 살펴보겠습니다. −
<!DOCTYPE html> <html> <head> <title>CSS Pseudo Elements and CSS Classes</title> <style> form { width:70%; margin: 0 auto; text-align: center; } * { padding: 2px; margin:5px; box-sizing: border-box; } input[type="button"] { border-radius: 10px; } .child{ line-height: .1em; border: 4px solid black; } ::selection{ color:white; } .child:nth-child(1), h1::selection{ background-color: #FF8A00; } .child:nth-child(2), div::selection{ background-color: #F44336; } .child:nth-child(3), p::selection{ background-color: #C303C3; } .child:nth-child(4), article::selection{ background-color: #4CAF50; } .child:nth-child(5), code::selection{ background-color: #FEDC11; } </style> </head> <body> <form> <fieldset> <legend>CSS Pseudo Elements and CSS Classes</legend> <div id="container"> <h1>A Heading</h1> <div>Welcome to DIV World</div> <p>Every one is welcome in this town. Make yourself comfertable</p> <article>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</article> <code>function helloWorld(){ document.write("Hello World");}</code> <div><span class="child">h1</span><span class="child">div</span><span class="child">p</span><span class="child">article</span><span class="child">code</span></div> </div> </body></html>
출력
이것은 다음과 같은 출력을 생성합니다 -
초기 레이아웃 -