{ ๊น€๐Ÿ‘พ์˜ CSS๋Š” ์žฌ๋ฐŒ๋‹ค } #2 - Selectors

26536 ๋‹จ์–ด ๊น€๋ฒ„๊ทธCSSCSS

๐ŸŽˆ์š”์†Œ, ํด๋ž˜์Šค, ID ์„ ํƒ์ž (Type, Class & ID Selector)

Type Selector

p {
	color: #212529;
}

blockquote {
	background-color: #e0e0e0;
}

HTML์˜ ํƒœ๊ทธ ์ž์ฒด๋ฅผ ์ง์ ‘์ ์œผ๋กœ ์ง€์นญํ•˜๋Š” ๊ฑฐ๋‹ค. ์š”์†Œ ์„ ํƒ์ž๋ผ๊ณ  ํ•œ๋‹ค.

Class Selector

<div class="box">
	<!-- ... -->
</div>
<div class="box">
	<!-- ... -->
</div>
<div class="box">
	<!-- ... -->
</div>

๋‹จ ํ•œ๋ฒˆ์„ ์Šคํƒ€์ผ๋ง ์„ ์–ธ์œผ๋กœ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์š”์†Œ๋ฅผ ๋™์‹œ์— ์Šคํƒ€์ผ๋ง ํ•  ์ˆ˜ ์žˆ์–ด ๊ต‰์žฅํžˆ ํšจ์œจ์ ์œผ๋กœ ํ•  ์ˆ˜ ์žˆ๋‹ค.

<div class="box-0 box-1 box-2">
	<!-- ... -->
</div>

.box-0.box-1
.box-0.box-2
.box-1.box-2
.box-0.box-1.box-2

ํ•œ ์š”์†Œ์— ์—ฌ๋Ÿฌ ๊ฐœ๋ฅผ ๋™์‹œ์— ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ณต๋ฐฑ์—†์ด ๋ถ™์–ด ์žˆ์œผ๋ฉด ํ•˜๋‚˜์˜ ์„ ํƒ์ž๊ฐ€ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊ณต๋ฐฑ์ด ํ•„์š”ํ•˜๋‹ค.

ID Seletor

<div id="leesaerom">
   <h1> Hello </h1>
</div>
#leesaerom {
	background: #0066ff;
}

๋‹จ ํ•˜๋‚˜๋งŒ ์„ ํƒํ•ด์„œ ์Šคํƒ€์ผ๋ง ํ•  ์ˆ˜ ์žˆ๋‹ค.

๐ŸŽˆ์ž์‹, ์ž์†, ํ˜•์ œ ์„ ํƒ์ž (Child, Descendant & Sibling Combinators)

Child Combinator

<section>
        <h1>Selectors</h1>
        <ul>
            <li>
                <h1>Child Combinator</h1>
                <p>
                    Lorem, ipsum dolor.
                </p>
            </li>
            <li>
                <h1>Child Combinator</h1>
                <p>
                    Lorem, ipsum dolor.
                </p>
            </li>
        </ul>
</section>
section > h1 {
    color: #ff4949;
}

  • parent > child : ๋ถ€๋ชจ ํƒœ๊ทธ ์•ˆ์— ์žˆ๋Š” ์ž์‹์„ ์„ ํƒํ•œ๋‹ค.

<section>์•ˆ์— <h1>๊ณผ <ul>๋กœ ๊ตฌ์„ฑ ๋˜์–ด ์žˆ์ง€๋งŒ CSS ํŒŒ์ผ์—์„œ <section>์•ˆ์— <h1>์ง€์ •๋˜์–ด ์žˆ์–ด์„œ Selectors์— ๋นจ๊ฐ•์ƒ‰์œผ๋กœ ํ‘œํ˜„๋œ๋‹ค.

Descendant Combinator

<section>
        <h1>Selectors</h1>
        <ul>
            <li>
                <h1>Child Combinator</h1>
                <p>
                    Lorem, ipsum dolor.
                </p>
            </li>
            <li>
                <h1>Child Combinator</h1>
                <p>
                    Lorem, ipsum dolor.
                </p>
            </li>
        </ul>
</section>
section  h1 {
    color: #ff4949;
}

  • parent descendant : ๋ถ€๋ชจ ํƒœ๊ทธ ์•ˆ์— ์žˆ๋Š” ๋ชจ๋“  ์ž์†์„ ์„ ํƒํ•œ๋‹ค.

<section>์•ˆ์— ๋ชจ๋“  ์ž์†์˜ <h1>์„ ์„ ํƒํ•ด์„œ ๋นจ๊ฐ•์ƒ‰์œผ๋กœ ํ‘œํ˜„๋œ๋‹ค.

Sibling Combinator

<section>
        <h1>Sibling combinators</h1>
        <ol>
            <li>Do something</li>
            <li class="active">Do something else</li>
            <li>Create something</li>
            <li>Create somthing else</li>
            <li>Make something</li>
            <li>Make something else</li>
        </ol>
</section>
// parent + sibling
.active {
    font-weight: 700;
}

.active + li {
    color: #ff4949;
}

- parent + sibling : ๋ถ€๋ชจ ํƒœ๊ทธ ๋‹ค์Œ ํƒœ๊ทธ๋ฅผ ์„ ํƒํ•œ๋‹ค.

active ๊ธฐ์ค€์œผ๋กœ ๋‹ค์Œ์ธ <li> ์„ ํƒํ•ด์„œ ๋นจ๊ฐ•์ƒ‰์œผ๋กœ ํ‘œํ˜„ํ•œ๋‹ค.

.active {
    font-weight: 700;
}

// parent ~ sibling
.active ~ li {
    color: #0066ff;
}

  • parent ~ sibling : ๋ถ€๋ชจ ํƒœ๊ทธ ๋‹ค์Œ ๋ชจ๋“  ํƒœ๊ทธ๋ฅผ ์„ ํƒํ•œ๋‹ค.

active ๊ธฐ์ค€์œผ๋กœ ๋‹ค์Œ์— ์˜ค๋Š” ๋ชจ๋“  <li> ์„ ํƒํ•ด์„œ ํŒŒ๋ž‘์ƒ‰์œผ๋กœ ํ‘œํ˜„ํ•œ๋‹ค.

๐ŸŽˆ๊ตฌ์กฐ์  ๊ฐ€์ƒ ํด๋ž˜์Šค ์„ ํƒ์ž (Structual Pseudo-classes)

<section>
        <h1>Structual Pseudo Class</h1>
        <ol>
            <li>Do something</li>
            <li>Do something else</li>
            <li>Create something</li>
            <li>Create somthing else</li>
            <li>Make something</li>
            <li>Make something else</li>
        </ol>
</section>
li:first-child {
    color: #0066ff;
}

li:last-child {
    color: #ffc82c;
}

li:nth-child(3) {
    color: #ff4949;
}

๊ฐ€์ƒ ํด๋ž˜์Šค๋Š” ์–ด๋–ค ์ƒํƒœ๋ž‘ ์–ด๋–ค ์กฐ๊ฑด์„ ๋งŒ์กฑํ•  ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํด๋ž˜์Šค์ด๋‹ค.

  • element:first-child : ํƒœ๊ทธ ์ค‘์— ์ฒซ๋ฒˆ์งธ ํƒœ๊ทธ๋ฅผ ์„ ํƒํ•œ๋‹ค.
  • element:last-child : ํƒœ๊ทธ ์ค‘์— ๋งˆ์ง€๋ง‰ ํƒœ๊ทธ๋ฅผ ์„ ํƒํ•œ๋‹ค.
  • element:nth-child(n) : ์ฑ„๊ทธ ์ค‘์— n๋ฒˆ์งธ ํƒœ๊ทธ๋ฅผ ์„ ํƒํ•œ๋‹ค.

๐ŸŽˆ๋™์  ๊ฐ€์ƒ ํด๋ž˜์Šค ์„ ํƒ์ž (User Action Pseudo-classes)

<section>
        <h1>User Action Pseudo Class</h1>
        <a href="#">Youtube Channel</a>
        <input type="email" placeholder="Enter your email"/>
</section>
a:hover {
    background-color: #7e5bef;
}

a:active {
    background-color: #592dea;
}

input {
    outline: none;
    box-shadow: none;
}

input:focus {
    border-color: #1fb6ff;
}

input:active {
    border-color: #009eeb;
}

์œ ์ € ์˜ ์•ก์…˜์— ๋”ฐ๋ผ์„œ ๋ฐ˜์‘ํ•˜๋Š” ์„ ํƒ์ž์ด๋‹ค.

  • element:hover : ์–ด๋–ค ์š”์†Œ๋ฅผ ๋งˆ์šฐ์Šค๋ฅผ ๊ฐ–๋‹ค ๋ƒˆ์„ ๋•Œ ์ƒํƒœ๋ฅผ ํ‘œํ˜„ํ•œ๋‹ค.
  • element:active : ์–ด๋–ค ์š”์†Œ๋ฅผ ๋งˆ์šฐ์Šค ํด๋ฆญ ํ•˜์˜€์„๋•Œ ๋ณ€ํ•œ๋‹ค.
  • element:focus : input์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ๊ฐ€ ๋˜์—ˆ์Œ์„ ์•Œ๋ ค์ค„๋•Œ ๋ณ€ํ•œ๋‹ค.

focus์™€ active๋Š” ์—„๋ฐ€ํžˆ ๋งํ•˜๋ฉด ๋‹ค๋ฅธ ์ƒํƒœ์ด๊ธด ํ•˜์ง€๋งŒ focus๋Š” active๋ฅผ ํฌํ•จ์„ ํ•˜๋Š” ์•„์ฃผ ๋ฏธ๋ฌ˜ํ•œ ๊ด€๊ณ„์ด๋‹ค.

  • focus : ์š”์†Œ๊ฐ€ ํฌ์ปค์Šค ๋˜์—ˆ์„ ๋•Œ (active ์ƒํƒœ๋„ ํฌ์ปค์Šค ์ƒํƒœ์˜ ์ผ๋ถ€)
  • active : ์š”์†Œ๋ฅผ ๋งˆ์šฐ์Šค๋กœ ํด๋ฆญํ•˜๋Š” ์ฐฐ๋‚˜์˜ ์ˆœ๊ฐ„

focus์ƒํƒœ์ผ ๋•Œ์˜ ์Šคํƒ€์ผ์„ ๊ธฐ๋ณธ์œผ๋กœ ๊น”๊ณ  active์ƒํƒœ์ผ ๋•Œ ์ถ”๊ฐ€๋กœ ์Šคํƒ€์ผ์„ ์ถ”๊ฐ€ํ•ด์„œ ๋ฎ์–ด์ค€๋‹ค.

๐ŸŽˆCSS ์„ ํƒ์ž ์˜ฌ๋ฆผํ”ฝ

p {
    color: blue;
}

p {
    color: hotpink;
}

CSS๋Š” ๋‚˜์ค‘์— ์„ ์–ธ๋œ ์„ ํƒ์ž์˜ ์Šคํƒ€์ผ๋ง์ด ์ด์ „์˜ ์„ ์–ธ๋œ ์„ ํƒ์ž๋ฅผ ๋ฎ์–ด๋ฒ„๋ฆฐ๋‹ค. ๊ทธ ์ด์œ ๋Š” Cascading Style Sheet์˜ Cascading ๋•Œ๋ฌธ์ด๋‹ค.

CSS ์„ ํƒ์ž ์šฐ์„ ์ˆœ์œ„

  1. ID
  2. Class, Pseudo-class
  3. Type
<p id="text" class="a b c d e f g h i j k l m n o">
        Lorem ipsum dolor, sit amet consectetur adipisicing elit. Libero, ducimus?
</p>
#text {
    color: #0066ff;
}

.a.b.c.d.e.f.g.h.i.j.k.l.m.n.o {
    color: #ff4949;
}

Rule Breakers (๋น„์ถ”์ฒœ)

Inline Style

<p id="text" class="a b c d e f g h i j k l m n o" style="color: #ffc82c;">
        Lorem ipsum dolor, sit amet consectetur adipisicing elit. Libero, ducimus?
</p>
#text {
    color: #0066ff;
}

.a.b.c.d.e.f.g.h.i.j.k.l.m.n.o {
    color: #ff4949;
}

์—ด์‹ฌํžˆ ์Šคํƒ€์ผ์„ ํ•˜๋”๋ผ๋„ inline style์€ ๊ฐ•๋ ฅํ•ด์„œ inline style์ด ์šฐ์„ ์ˆœ์œ„๋‹ค.

!important

<p id="text" class="a b c d e f g h i j k l m n o" style="color: #ffc82c;">
        Lorem ipsum dolor, sit amet consectetur adipisicing elit. Libero, ducimus?
</p>
#text {
    color: #0066ff;
}

.a.b.c.d.e.f.g.h.i.j.k.l.m.n.o {
    color: #ff4949;
}

p {
    color: #ff4949 !important;
}

inline style๋ณด๋‹ค !important๊ฐ€ ๋” ๊ฐ•๋ ฅํ•˜๋‹ค.


์ตœ์ข…์ ์œผ๋กœ ์šฐ์„ ์ˆœ์œ„๋Š”

!important(๋น„์ถ”์ฒœ) > Inline Style(๋น„์ถ”์ฒœ) > ID > Class, Pseudo-class > Type

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ