๋ฐ˜์‘ํ˜•

focus-within ์‚ดํŽด๋ณด๊ธฐ


:focus-within์€ ํฌ์ปค์Šค๋ฅผ ๋ฐ›์€ ์š”์†Œ ํ˜น์€ ํฌ์ปค์Šค๋ฅผ ๋ฐ›์€ ์š”์†Œ(์ฃผ๋กœ input ํƒœ๊ทธ)๋ฅผ ํฌํ•จํ•˜๋Š” ๋ถ€๋ชจ ์š”์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค. input ๊ฐ™์€ ํ•˜์œ„ ์š”์†Œ๊ฐ€ focus ๋์„ ๋•Œ ๋ถ€๋ชจ ์š”์†Œ์˜ ์Šคํƒ€์ผ์„ ๋ณ€๊ฒฝํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ์œ ์šฉํ•˜๋‹ค.

 

์•„๋ž˜ ์˜ˆ์ œ์—์„œ input ํƒœ๊ทธ๋ฅผ ์ž์‹์œผ๋กœ ํฌํ•จํ•˜๋Š” .wrapper ํด๋ž˜์Šค ์ด๋ฆ„์˜ ๋ถ€๋ชจ ์š”์†Œ๊ฐ€ ์žˆ๋‹ค.

<div class="wrapper">
  <p>
    Lorem Ipsum ist ein einfacher Demo-Text für die Print- und Schriftindustrie.
  </p>
  <label for="color">Choose text color</label>
  <input type="color" id="color" value="#1863b4" />
</div>

 

input ํƒœ๊ทธ๋ฅผ ์„ ํƒํ•ด์„œ ํ™œ์„ฑํ™”๋˜๋ฉด(ํฌ์ปค์Šค๋ฅผ ๋ฐ›์œผ๋ฉด), ํ•ด๋‹น input ํƒœ๊ทธ๋ฅผ ํฌํ•จํ•˜๋Š” ๋ถ€๋ชจ ์š”์†Œ์ธ .wrapper ํด๋ž˜์Šค์— focus-within ์˜์‚ฌํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ทธ๋ฆผ์ž ํšจ๊ณผ๋ฅผ ์ฃผ๋Š” ์ฝ”๋“œ.

.wrapper:focus-within {
  box-shadow: 0px 0px 5px 5px #ccc;
}

 

input ํƒœ๊ทธ์˜ id์™€ label ํƒœ๊ทธ์˜ for ์†์„ฑ์„ ๋™์ผํ•˜๊ฒŒ ์ž…๋ ฅํ•ด์„œ ์—ฐ๊ฒฐํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— label ํƒœ๊ทธ๋ฅผ ํด๋ฆญํ–ˆ์„ ๋•Œ๋„ ๊ทธ๋ฆผ์ž ํšจ๊ณผ๊ฐ€ ์ ์šฉ๋œ๋‹ค.

 

 

์ฝ”๋“œํŽœ ์˜ˆ์ œ ์ฝ”๋“œ


See the Pen [CSS] :focus-within by ColorFilter (@colorfilter) on CodePen.

 

 

๋ฒˆ์™ธ) focus๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ์š”์†Œ


์–ด๋–ค ์š”์†Œ๊ฐ€ focus๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š”์ง€ ์ •์˜ํ•œ ๊ณต์‹์ ์ธ ๋ฌธ์„œ๋Š” ์—†์ง€๋งŒ(๋น„๊ณต์‹์ ์œผ๋กœ ์ •๋ฆฌํ•œ ์‚ฌ์ดํŠธ๋Š” ์žˆ์Œ) ์ผ๋ฐ˜์ ์œผ๋กœ ์•„๋ž˜ ์—˜๋ฆฌ๋จผํŠธ๋“ค์ด ํฌ์ปค์Šค๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

 

  • HTMLInputElement
  • HTMLSelectElement
  • HTMLTextAreaElement
  • HTMLAnchorElement
  • HTMLButtonElement
  • HTMLAreaElement

 

document.activeElement๋Š” ํ˜„์žฌ ํ™œ์„ฑํ™”(active)๋˜์–ด ์žˆ๋Š” DOM ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ์ฝ๊ธฐ ์ „์šฉ ์†์„ฑ์ด๋‹ค. ์ด ์†์„ฑ์„ ์ด์šฉํ•ด ์•„๋ž˜ ๊ฐ™์€ ๊ฐ„๋‹จํ•œ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ํฌ์ปค์Šค๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ์—˜๋ฆฌ๋จผํŠธ์ธ์ง€ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋‹ค.

function focus(el) {
  el.focus(); // ์ธ์ž๋กœ ๋ฐ›์€ ์—˜๋ฆฌ๋จผํŠธ ํ™œ์„ฑํ™”(active)
  return el === document.activeElement; // true๋ฉด focus๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ์š”์†Œ
}

 

div ํƒœ๊ทธ์—์„œ๋„ focus๋ฅผ ๋ฐ›๊ณ  ์‹ถ๋‹ค๋ฉด tabindex๋ฅผ 0 ํ˜น์€ -1๋กœ ์„ค์ •ํ•˜๋ฉด ๋œ๋‹ค

<div tabindex="-1">ํ…์ŠคํŠธ</div>

 

์•„๋ž˜๋Š” tabindex="-1" tabindex="0" ์ฐจ์ด์  (์„ค๋ช… ์ถœ์ฒ˜). ์ฐธ๊ณ ๋กœ tabIndex="1" ๋กœ ์„ค์ •ํ•˜๋ฉด ํƒญ(Tab) ์šฐ์„  ์ˆœ์œ„๊ฐ€ ๊ฐ€์žฅ ๋†’์•„์ ธ์„œ ํƒญ ํ‚ค๋ฅผ ๋ˆ„๋ฅด๋ฉด ์ฒซ๋ฒˆ์งธ๋กœ ํฌ์ปค์Šค๋œ๋‹ค.

 

  • If given a value of -1, the element can't be tabbed to but focus can be given to the element programmatically (using element.focus()).
  • If given a value of 0, the element can be focused via the keyboard and falls into the tabbing flow of the document. Values greater than 0 create a priority level with 1 being the most important.

 

๋ ˆํผ๋Ÿฐ์Šค


 


๊ธ€ ์ˆ˜์ •์‚ฌํ•ญ์€ ๋…ธ์…˜ ํŽ˜์ด์ง€์— ๊ฐ€์žฅ ๋น ๋ฅด๊ฒŒ ๋ฐ˜์˜๋ฉ๋‹ˆ๋‹ค. ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•ด ์ฃผ์„ธ์š”
๋ฐ˜์‘ํ˜•