Style Sheet/CSS

๐ŸŒŸ CSS ์„ ํƒ์ž๋ฅผ ๋ชจ๋˜ํ•˜๊ฒŒ :where() / :is() / :has() / :not()

์ธํŒŒ_ 2023. 3. 9. 09:28

css-where-is-has-not

:where() ๊ฐ€์ƒ ์„ ํƒ์ž

:where ์˜์‚ฌ ํด๋ž˜์Šค ์„ ํƒ์ž๋Š” css ์ฝ”๋”ฉํ• ๋•Œ ์„ ํƒ์ž์˜ ์ค‘๋ณต์„ ์ค„์ด๋Š” ๋ฐ ๋„์›€์ด ๋˜๋Š” ๋…€์„์ด๋‹ค.

์˜ˆ๋ฅผ๋“ค์–ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์—ฌ๋Ÿฌ ์—˜๋ฆฌ๋จผํŠธ ์•ˆ์— ์žˆ๋Š” anchor ํƒœ๊ทธ์— hover ํšจ๊ณผ๋ฅผ ์ฃผ๊ธฐ์œ„ํ•ด์„  ๊ฐ ์„ ํƒ์ž๋“ค์„ ์ฝค๋งˆ๋ฅผ ์ด์–ด ๋‚˜์—ดํ•˜์—ฌ ํ‘œํ˜„ํ•˜์—ฌ์•ผ ํ–ˆ์—ˆ๋‹ค.

<nav>
  <ul>
    <a href="#">element 1</a>
  </ul>
</nav>

<footer>
  <ol>
    <a href="#">element 2</a>
  </ol>
</footer>

<aside>
  <p>
    <a href="#">element 3</a>
  </p>
</aside>
nav > ul a:hover,
footer > ol a:hover,
aside > p a:hover {
  color: purple;
  text-decoration: underline wavy deeppink;
}

๊ทธ๋Ÿฐ๋ฐ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด a:hover ๋ถ€๋ถ„์ด ๊ฐ ์„ ํƒ์ž๋งˆ๋‹ค ์ค‘๋ณต๋˜๋Š” ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ๊ฑฐ๊ธฐ๋‹ค ์ ์šฉํ•ด์•ผํ•  ์š”์†Œ๊ฐ€ ๋งŽ์œผ๋ฉด ๋งŽ์„ ์ˆ˜๋ก ์„ ํƒ์ž์˜ ์–‘๋„ ๋Š˜์–ด๋‚˜ ๋‚˜์ค‘์— ๊ฐ€๋…์„ฑ ๋ฐ ์œ ์ง€๋ณด์ˆ˜๋„ ํž˜๋“ค์–ด์ง€๊ฒŒ ๋˜๋ฉฐ, ๋งŒ์ผ ์‰ผํ‘œ๋กœ ์ด์–ด์ง„ ์ด๋“ค ์„ ํƒ์ž์ค‘ ํ•˜๋‚˜๊ฐ€ ์ž˜๋ชป๋˜๋ฉด ์ „์ฒด๊ฐ€ ์ ์šฉ๋˜์–ด์ง€์ง€ ์•Š๋Š” ๋ฌธ์ œ์ ์ด ์ƒ๊ธฐ๊ฒŒ ๋œ๋‹ค.

 

:where() ์‚ฌ์šฉ๋ฒ•

์ด๋•Œ :where() ์„ ํƒ์ž๋ฅผ ํ†ตํ•ด ๋ณด๋‹ค ๋ชจ๋˜์Šค๋Ÿฝ๊ฒŒ css ์ฝ”๋“œ๋ฅผ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ถˆํ•„์š”ํ•œ ์ค‘๋ณต์„ ์—†์•  ์ฝ”๋“œ ์ž์ฒด๋„ ์งง์•„์ง€๊ฒŒ ๋˜๊ณ , ๊ฐ ์„ ํƒ์ž๋ฅผ ๊ฐœ๋ณ„์ ์œผ๋กœ ๋ถ„์„ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋งŒ์ผ ์ž˜๋ชป๋œ ์„ ํƒ์ž๊ฐ€ ์žˆ์–ด๋„ ์ดํ•ด๊ฐ€ ์•ˆ๋˜๋ฉด ๊ทธ๋Œ€๋กœ ๋ฒ„๋ ค๋ฒ„๋ ค ํ™•์žฅ์„ฑ๋„ ์ข‹๋‹ค.

:where(nav > ul, footer > ol, aside > p) a:hover {
  color: purple;
  text-decoration: underline wavy deeppink;
}

See the Pen :where ์„ ํƒ์ž by barzz12 (@inpaSkyrim) on CodePen.

 

์ด์ค‘ :where() ์ฒ˜๋ฆฌ

์ข€ ๋” ์ด ์„ ํƒ์ž์˜ ๊ฐ•๋ ฅํ•œ ํšจ๊ณผ๋ฅผ ํ™•์—ฐํžˆ ๋Š๋‚„์ˆ˜ ์žˆ๊ฒŒ ์ข€ ๊ธธ๋‹ค๋ž€ ์˜ˆ์‹œ ์ฝ”๋“œ๋ฅผ ๊ฐ€์ ธ์™€๋ดค๋‹ค.

๋‹ค์Œ ์„ ํƒ์ž๋“ค์„ ๋ณด๋ฉด section, article, aside, nav ์„ ํƒ์ž์™€ ๊ทธ์˜ ์ž์†์ธ h1 ~ h6 ์„ ํƒ์ž๊ฐ€ ๋ฐ˜๋ณต๋˜๋Š” ๊ฑธ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฅผ ์ด์ค‘ :where() ์„ ํ†ตํ•ด ํš๊ธฐ์ ์œผ๋กœ ์ค„์ผ ์ˆ˜๊ฐ€ ์žˆ๋‹ค.

section h1, section h2, section h3, section h4, section h5, section h6, 
article h1, article h2, article h3, article h4, article h5, article h6, 
aside h1, aside h2, aside h3, aside h4, aside h5, aside h6, 
nav h1, nav h2, nav h3, nav h4, nav h5, nav h6 {
  color: #BADA55;
}

/* ↓ */

:where(section, article, aside, nav) :where(h1, h2, h3, h4, h5, h6) {
  color: #BADA55;
}

 

:where() ํ•œ๊ณ„์ 

๋‹ค๋งŒ ::before ๋‚˜ ::after ์™€ ๊ฐ™์€ ์˜์‚ฌ ์š”์†Œ๋Š” DOM์— ์žˆ๋Š” ์š”์†Œ๊ฐ€ ์•„๋‹ˆ๋ฏ€๋กœ ์„ ํƒ์ด ๋ถˆ๊ฐ€๋Šฅํ•ด ๋ฌถ์„์ˆ˜ ์—†๋‹ค๋Š” ํ•œ๊ณ„๊ฐ€ ์žˆ๋‹ค.

/* ๋™์ž‘ํ•˜์ง€ ์•Š์Œ */
a:where(::before, ::after) {
  ...
}

/* ์–ด์ฉ”์ˆ˜ ์—†์ด ์‰ผํ‘œ๋กœ ์ด์–ด์•ผ ํ•œ๋‹ค  */
a::before,
a::after {
  ...
}

:is() ๊ฐ€์ƒ ์„ ํƒ์ž

์ด ์„ ํƒ์ž๋Š” ์œ„์—์„œ ๋ฐฐ์šด :where ์„ ํƒ์ž์™€ ๋™์ผํ•˜๊ฒŒ ๋™์ž‘๋œ๋‹ค.

๋‹จ, ์œ ์ผํ•œ ์ฐจ์ด์ ์€ ๋ช…์‹œ๋„ ์— ์žˆ๋‹ค. id ์„ ํƒ์ž๋Š” ๋‹ค๋ฅธ ์„ ํƒ์ž๋ณด๋‹ค ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๊ฐ€์žฅ ๋†’๊ณ , css ์ฝ”๋“œ๊ฐ€ ์•„๋ž˜์ผ ์ˆ˜๋ก ์šฐ์„ ์ˆœ์œ„๊ฐ€ ์ ์šฉ๋œ๋‹ค๊ณ  ๋“ค์–ด๋ณธ์ ์ด ์žˆ์„ ๊ฒƒ์ธ๋ฐ ์ด๊ฒƒ์„ ๋งํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ฆ‰, :where()๋Š” ๋ช…์‹œ๋„๊ฐ€ 0์ด์ง€๋งŒ :is()๋Š” ๋ช…์‹œ๋„๊ฐ€ ๊ฐ€์žฅ ๋†’๋‹ค.

 

:where()์™€ :is() ์ฐจ์ด์ 

์•„๋ž˜ ์˜ˆ์ œ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด <header> , <main> , <footer> ์•ˆ์˜ ๊ธ€์ž์˜ ์ƒ‰์„ ์Šคํƒ€์ผ๋ง ํ•˜๋ ค ํ•œ๋‹ค. ์ด๋•Œ ๊ฐ๊ฐ :where() ์™€ :is() ๋กœ ๊ธ€์ž์ƒ‰์„ ์ง€์ •ํ•˜๊ณ  css ๋งˆ์ง€๋ง‰์— ์ „์ฒด ์„ ํƒ์ž๋กœ ๊ธ€์ž์ƒ‰์„ ๋…น์ƒ‰์œผ๋กœ ๋ฎ์–ด ์”Œ์› ๋‹ค. ๊ฒฐ๊ณผ๋Š” ์–ด๋–ป๊ฒŒ ๋ ๊นŒ?

<article>
  <h2>:where()</h2>
  <header class="where">
    <p>Section</p>
  </header>
  <main class="where">
    <p>Aside</p>
  <main/>
  <footer class="where">
    <p>Footer</p>
  </footer>
</article>

<article>
  <h2>:is()</h2>
  <header class="is">
    <p>Section</p>
  </header>
  <main class="is">
    <p>Aside</p>
  </main>
  <footer class="is">
    <p>Footer</p>
  </footer>
</article>
/* ๋งจ ์•„๋ž˜ ์ •์˜๋œ ์„ ํƒ์ž์— ์˜ํ•ด ๋ฌด์‹œ๋œ๋‹ค */
:where(header.where, main.where, footer.where) p {
  color: red;
}

/* ๊ฐ€์žฅ ๋ช…์‹œ๋„๊ฐ€ ๋†’์•„ ๋ฐ‘์— ๋ญ๊ฐ€ ์žˆ๋˜ ๋ฌด์กฐ๊ฑด ์ ์šฉ๋œ๋‹ค */
:is(header.is, main.is, footer.is) p {
  color: orange;
}

/* ์ „์ฒด ์„ ํƒ์ž ์ฒ˜๋ฆฌ */
header p,
main p,
footer p {
  color: green;
}

See the Pen :is() ์„ ํƒ์ž by barzz12 (@inpaSkyrim) on CodePen.

:where() ์€ ๋ช…์‹œ๋„๊ฐ€ 0์ด๊ธฐ ๋•Œ๋ฌธ์— css ๋งˆ์ง€๋ง‰์— ์„ ์–ธ๋œ ์ „์ฒด ์„ ํƒ์ž์— ์˜ํ•ด ํšจ๊ณผ๊ฐ€ ๋ฎ์–ด์”Œ์›Œ์กŒ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ :is()๋Š” ๋ช…์‹œ๋„๊ฐ€ ๋†’์•„ ๊ฐ€์žฅ ๋†’์€ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๊ฐ€์ง€๊ธฐ ๋•Œ๋ฌธ์— ๋’ค์— ๋ญ๊ฐ€ ์˜ค๋“  ๋ฌด์กฐ๊ฑด ํ•ด๋‹น ์Šคํƒ€์ผ๋กœ ์ ์šฉ๋˜๊ฒŒ ๋œ๋‹ค.


:not() ๊ฐ€์ƒ ์„ ํƒ์ž

์ด ์„ ํƒ์ž๋Š” ์ด๋ฆ„ ๊ทธ๋Œ€๋กœ :not() ๊ด„ํ˜ธ ์•ˆ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ์š”์†Œ๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ์š”์†Œ์˜ ์Šคํƒ€์ผ์„ ์ง€์ •ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค.

:not() ์„ ์ด์šฉํ•˜๋ฉด ์†”๋ฃจ์…˜์— ๋Œ€ํ•ด์„œ ์ข€๋” ์‹ฌํ”Œํ•˜๊ฒŒ ํ•ด๊ฒฐ์ฑ…์„ ๊ตฌ์„ฑํ•  ์ˆ˜ ๊ฐ€ ์žˆ๋Š”๋ฐ, ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ ๋ฆฌ์ŠคํŠธ ๋ฐ•์Šค ๋งˆ๋‹ค ๊ฐ„๊ฒฉ์„ ์ฃผ๊ธฐ ์œ„ํ•ด margin-bottom์„ ์„ค์ •ํ•˜์˜€๋‹ค๊ณ  ํ•˜์ž.

<div class="card">
  <ul>
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
    <li>Item 4</li>
    <li>Item 5</li>
  </ul>
</div>
ul li {
  margin-bottom: 20px;
}

See the Pen :not ์„ ํƒ์ž by barzz12 (@inpaSkyrim) on CodePen.

ํ•˜์ง€๋งŒ ๋ชจ๋“  ๋ฆฌ์ŠคํŠธ ํ•˜๋‹จ์— margin์ด ๋“ค์–ด๊ฐ€ ๋ฐ•์Šค๊ฐ€ ๋Œ€์นญ์ด ์ด๋ฃจ์–ด์ง€์ง€ ์•Š๋Š” ๋ฌธ์ œ์ ์ด ์žˆ๋‹ค.

margin-top๋„ ์„ค์ •ํ•˜๋Š” ๋ฒ•์ด๋‚˜ ๊ฐ•์ œ๋กœ li ์š”์†Œ ๋งˆ์ง€๋ง‰์€ margin์„ ์„ค์ •ํ•˜์ง€ ์•Š๋„๋ก ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๊ฒ ์ง€๋งŒ ์ข€๋” ์‹ฌํ”Œํ•˜๊ณ  ์ฝ”๋“œ ํ•œ์ค„๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜๊ฐ€ ์žˆ๋‹ค.

/* li ์š”์†Œ ๋งˆ์ง€๋ง‰์€ ์ ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค */
ul li:not(:last-of-type) {
  margin-bottom: 20px;
}

See the Pen Untitled by barzz12 (@inpaSkyrim) on CodePen.

 

์ด๋ฐ–์—๋„ ์ด๋ฅผ ์‘์šฉํ•ด ์†์„ฑ ์„ ํƒ์ž์—์„œ ์ง€์›์•ˆํ•˜๋Š” != not equals ์—ฐ์‚ฐ์ž ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

 

[CSS] ๐Ÿš€ ์†์„ฑ ์„ ํƒ์ž not equal ๊ตฌํ˜„ํ•˜๊ธฐ [attr!=value]

Attribute Selector not equal ๊ตฌํ˜„ CSS ์†์„ฑ(Attribute) ์„ ํƒ์ž ๋ฌธ๋ฒ•์—๋Š” equal ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ๋‹ค์–‘ํ•œ ๊ธฐํ˜ธ ๋ฌธ๋ฒ•์„ ์ง€์›ํ•œ๋‹ค. [attr=value] : attr ์†์„ฑ์ด ์ •ํ™•ํžˆ value์ธ ์š”์†Œ๋ฅผ ์„ ํƒ [attr~=value] : attr ์†์„ฑ์ด ์ •ํ™•ํžˆ v

inpa.tistory.com


:has() ๊ฐ€์ƒ ์„ ํƒ์ž

:has() ๋Š” ์ตœ๊ทผ์— ์ถ”๊ฐ€๋œ ์„ ํƒ์ž์ด๋‹ค. ์ด ์„ ํƒ์ž๋Š” ํ•ด๋‹น ๋ถ€๋ชจ ์„ ํƒ์ž๊ฐ€ ํŠน์ • ์ž์‹ ์„ ํƒ์ž๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์„ ๊ฒฝ์šฐ ์ ์šฉํ•˜๋Š” ์ด๋ฅธ๋ฐ” css์˜ if๋ฌธ ๋ฒ„์ „์ด๋ผ ๋ณด๋ฉด ๋œ๋‹ค. ๊ธฐ์กด์—๋Š” ์ด๋ฅผ ๊ตฌํ˜„ํ•˜๋ ค๋ฉด css๋กœ๋งŒ์€ ๋ฌธ๋ฒ•์ ์ธ ํ•œ๊ณ„๊ฐ€ ์žˆ์–ด์„œ SCSS๋‚˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ํ†ตํ•ด์„œ๋งŒ ๊ฐ€๋Šฅํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ :has() ๊ฐœ๋…์€ ๊ต‰์žฅํžˆ ์œ ์šฉํ•˜๊ณ  ํŽธ๋ฆฌํ•œ ์„ ํƒ์ž ๊ฐœ๋…์ด๋ผ jQuery์—์„œ ์ œ์ด์ฟผ๋ฆฌ ์ „์šฉ ์„ ํƒ์ž์œผ๋กœ๋งŒ ์“ฐ์˜€์ง€๋งŒ, ์–ผ๋งˆ์ „ ๋“œ๋””์–ด ๊ณต์‹ํ™” ๋˜์—ˆ๋‹ค. 

์‚ฌ์šฉ๋ฒ•์€ ์˜ˆ๋ฅผ๋“ค์–ด div.parent ๊ฐ€ ๋งŒ์ผ p ์š”์†Œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฒฝ์šฐ์—๋งŒ ์ž์‹ ์˜ ์š”์†Œ์— ์Šคํƒ€์ผ์„ ์ ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

<div class="parent">
  <p>Child</p>
</div>
/* div ์š”์†Œ๊ฐ€ ์ž์‹์œผ๋กœ p๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์„ ๊ฒฝ์šฐ ์Šคํƒ€์ผ ์ ์šฉ */
div:has(> p) {
  background: red;
}

css-has

 

:has() ์„ ํƒ์ž ์กฐํ•ฉ ์ข…๋ฅ˜

:has() ๊ด„ํ˜ธ ์•ˆ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์„ ํƒ์ž ๋ฌธ๋ฒ•์„ ๋„ฃ์Œ์œผ๋กœ์จ ์ž์‹, ํ˜•์ œ, ์ž์† ์„ ํƒ์ž๋ฅผ ๋‹ค์–‘ํ•œ ํญ์œผ๋กœ ์ง€์ •ํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

/* a ์š”์†Œ๊ฐ€ ์ž์†์œผ๋กœ img๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์„ ๊ฒฝ์šฐ */
a:has(img) {
}

/* a ์š”์†Œ๊ฐ€ ์ž์‹์œผ๋กœ img๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์„ ๊ฒฝ์šฐ */
a:has(> img) {
}

/* h3 ์š”์†Œ๊ฐ€ ํ˜•์ œ๋กœ p๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์„ ๊ฒฝ์šฐ */
h3:has(+ p) {
}

/* article ์š”์†Œ๊ฐ€ ์ž์†์œผ๋กœ h3 ๊ทธ๋ฆฌ๊ณ  p๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์„ ๊ฒฝ์šฐ (and ๋…ผ๋ฆฌ) */
article:has(h3, p) {
}

 

:has() ์™€ :not() ์กฐํ•ฉ

์ด๋ฒˆ์—” ์•ฝ๊ฐ„ ๋‚œ์ด๋„ ๋†’์€ ์กฐํ•ฉ์„ ๊ฐ€์ ธ์™€ ๋ณด์•˜๋‹ค. :has() ์™€ :not() ๊ฐ€์ƒ ์š”์†Œ๋ฅผ ์–ด๋–ค ์ˆœ์„œ๋Œ€๋กœ ์กฐํ•ฉํ•˜๋Š๋ƒ์— ๋”ฐ๋ผ ๋…ผ๋ฆฌ์ ์œผ๋กœ ์™„์ „ํžˆ ๋‹ฌ๋ผ์ง€๊ธฐ ๋•Œ๋ฌธ์—, ์–ด๋–ป๊ฒŒ ๋˜๋Š”์ง€ ์ฝ”๋“œ๋ฅผ ๋ณด๊ณ  ์ถ”๋ก ํ•ด๋ณด์ž.

/* div ์š”์†Œ๊ฐ€ ์ž์†์œผ๋กœ h3์„ ๊ฐ€์ง€๊ณ  ์žˆ์ง€ ์•Š์„ ๊ฒฝ์šฐ ์ „์ฒด ์นผ๋ผ ์ ์šฉ */
div.section1:not(:has(h3)) {
  color: red;
}

/* div ์š”์†Œ์˜ ์ž์†์ด h3์ด ์•„๋‹Œ ์š”์†Œ๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ์ „์ฒด ์นผ๋ผ ์ ์šฉ */
div.section2:has(:not(h3)) {
  color: blue;
}

See the Pen :has() ์™€ :not() ์กฐํ•ฉ by barzz12 (@inpaSkyrim) on CodePen.

 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์…€๋ ‰ํ„ฐ ์กฐํ•ฉ

:has() ์„ ํƒ์ž๋Š” ์Šคํฌ๋ฆฝํŠธ๋กœ ๋™์ ์œผ๋กœ ์š”์†Œ๋ฅผ ์กฐ์ž‘ํ•ด์•ผ ํ• ๋•Œ ๋น›์„ ๋ฐœํ•œ๋‹ค.

์˜ˆ๋ฅผ๋“ค์–ด div.parent ๊ฐ€ ๋งŒ์ผ p ์š”์†Œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฒฝ์šฐ์—๋งŒ ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ  ์‹ถ๋‹ค๋ฉด, ๊ธฐ์กด์—๋Š” child ์š”์†Œ๋ฅผ ์ผ์ผํžˆ ์ˆœํšŒํ•ด์„œ ํƒ์ƒ‰ํ•˜๋ฉฐ ๋ถ„๊ฐ„ํ•ด์•ผ ๋ฌ์ง€๋งŒ, ์ด selector ํ•˜๋‚˜๋กœ ๊ณง๋ฐ”๋กœ ์ผ์น˜ํ•˜๋Š” ์š”์†Œ๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค.

<div class="parent">
  <input type="checkbox" />
  <p>Child</p>
</div>

<div class="parent">
  <input type="radio" />
  <p>Child</p>
</div>

<div class="parent">
  <p>Child</p>
</div>
document.querySelectorAll('.parent:has(p)')

/* ์ œ์ด์ฟผ๋ฆฌ๋„ ์ž์ฒด ์ง€์›ํ•œ๋‹ค. */
$('.parent:has(p)')

 

๋ธŒ๋ผ์šฐ์ €๊ฐ€ :has() ๋ฅผ ์ง€์›ํ•˜๋Š”์ง€ ํ…Œ์ŠคํŠธ

๋‹จ, ์ตœ์‹  ์ŠคํŽ™์ธ ๋งŒํผ ์•„์ง ํŒŒ์ด์–ดํญ์Šค๋‚˜ ์ธํ„ฐ๋„ท ์ต์Šคํ”Œ๋กœ๋Ÿฌ์—์„œ๋Š” ์•„์ง ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค.

css-has

๋งŒ์ผ ํ•ด๋‹น ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ด ์„ ํƒ์ž๋ฅผ ์ง€์›ํ•˜๋Š”์ง€ ์•Œ์•„๋ณด๊ณ  ์šฐํšŒ ๋ฐฉ๋ฒ•์„ ์ ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด @supports ๋ฅผ ํ†ตํ•ด ๊ตฌ์„ฑํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

@supports(selector(:has(p))) {
  /* Supported! */
}

@supports not (selector(:has(p))) {
  /* Not Supported! ์šฐํšŒ์ฝ”๋“œ ์ž‘์„ฑ */
}
 

[CSS] ๐Ÿ“š ์ตœ์‹  CSS ๊ธฐ๋Šฅ ์ง€์›์—ฌ๋ถ€ ํ™•์ธ ๐ŸŽจ @supports

@supports ์ตœ์‹  ์ŠคํŽ™์˜ css๊ฐ’์„ ์‚ฌ์šฉํ•  ๋•Œ, ํ•ด๋‹น css๋ฅผ ์ œ๊ณตํ•˜๋Š” ๋ธŒ๋ผ์šฐ์ €์— ๋”ฐ๋ผ ๋งž์ถคํ˜•์œผ๋กœ ์ ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ css๋ฅผ ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฐฉ๋ฒ•์€ ๋ฐ”๋กœ @supports ๋ฌธ๋ฒ•(๊ธฐ๋Šฅ ์ฟผ๋ฆฌ, feature query)์„ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด

inpa.tistory.com



# ์ฐธ๊ณ ์ž๋ฃŒ

https://junghan92.medium.com/%EB%B2%88%EC%97%AD-where-is-has-%EB%8B%B9%EC%8B%A0%EC%9D%98-%EC%82%B6%EC%9D%84-%EB%8D%94-%EC%89%BD%EA%B2%8C-%EB%A7%8C%EB%93%A4%EC%96%B4-%EC%A4%84-%EC%83%88%EB%A1%9C%EC%9A%B4-css-%EC%84%A0%ED%83%9D%EC%9E%90-ee44dd58aa3b 

https://css-tricks.com/complete-guide-to-css-functions/