๐Ÿ“—

ch2 - 2. id์™€ class

Index


์„ ํƒ์ž id์™€ class ํƒœ๊ทธ๋Š” HTML, CSS ๊ทธ๋ฆฌ๊ณ  Javascript๊นŒ์ง€ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. HTML์—์„œ๋Š” ์ ์–ด๋‘” ํ…์ŠคํŠธ์˜ ์†์„ฑ์„ ์ •ํ•ด์ฃผ๊ฑฐ๋‚˜ CSS์™€ Javascript์—์„œ๋Š” ํƒœ๊ทธ ๋ณ„๋กœ ํŠน์ง•์„ ์ •ํ•ด์ค„ ๋•Œ ์‚ฌ์šฉํ•˜๋‹ˆ ์ •๋ง ๊ด‘๋ฒ”์œ„ํ•˜๊ฒŒ ์“ฐ์ด์ฃ . id์™€ class์˜ ํŠน์ง•, css์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•, ๊ทธ๋ฆฌ๊ณ  ์šฐ์„ ์ˆœ์œ„๋ฅผ ํ•จ๊ป˜ ์‚ดํŽด๋ณผ๊นŒ์š”.

1. id์™€ class ํŠน์ง•

1.1 About id

id ์†์„ฑ์€ ํŽ˜์ด์ง€์— ์žˆ๋Š” ํ•ด๋‹น ์š”์†Œ๋ฅผ ์œ ์ผํ•˜๊ฒŒ ์‹๋ณ„ํ•  ๋•Œ ์”๋‹ˆ๋‹ค. ์ฃผ์˜ ์‚ฌํ•ญ์œผ๋กœ๋Š” ๊ฐ™์€ ํŽ˜์ด์ง€ ์•ˆ์— id๋Š” ๋‹จ ํ•œ ๊ฐœ๋งŒ ์กด์žฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋˜ id ์ด๋ฆ„์€ ์•ŒํŒŒ๋ฒณ ๋˜๋Š” '_'์œผ๋กœ ์‹œ์ž‘ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. id๋ฅผ ์ด์šฉํ•ด css์™€ javascript์—์„œ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋‹ˆ ์ค‘์š”ํ•œ ์†์„ฑ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. ๊ฐœ์ธ ํ”„๋กœ์ ํŠธ์ผ ๊ฒฝ์šฐ๋Š” ์ƒ๊ด€์—†์ง€๋งŒ ์—ฌ๋Ÿฌ ์‚ฌ๋žŒ๋“ค๊ณผ ๊ฐ™์ด ํ•˜๋Š” ํ”„๋กœ์ ํŠธ์—์„œ๋Š” id์ด๋ฆ„์„ ์ตœ๋Œ€ํ•œ ์ง๊ด€์ ์ด๊ณ  ๊ฐ„๋‹จ๋ช…๋ฃŒํ•˜๊ฒŒ ์ง€์–ด์ฃผ๋Š” ๊ฒƒ๋„ ์ค‘์š”ํ•˜๋‹ต๋‹ˆ๋‹ค.
  • CODE
<head> <meta charset="utf-8"> <title>id</title> <style type ="text/css"> #blue { color : blue;} </style> </head> <body> <p>์ด ๋ฌธ๋‹จ์€ ํ‰๋ฒ”ํ•œ pํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค.</p> <p id = "blue">์ด ๋ฌธ๋‹จ์€ blue id๋ฅผ ๋ถ€์—ฌํ•˜์˜€์Šต๋‹ˆ๋‹ค.</p> </body>
  • ์‹คํ–‰ํ™”๋ฉด
notion imagenotion image

1.2 About class

class ์†์„ฑ์€ ๋˜ํ•œ id ์†์„ฑ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ•ด๋‹น ์š”์†Œ๋ฅผ ์‹๋ณ„ํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ id ์†์„ฑ์€ ํ•œ ํŽ˜์ด์ง€์— ํ•˜๋‚˜๋งŒ ์กด์žฌํ•ด์•ผ ํ•˜๋Š” ๋ฐ˜๋ฉด class ์†์„ฑ์€ ํ•œ ํŽ˜์ด์ง€์— ์—ฌ๋Ÿฌ ๊ฐœ๊ฐ€ ์กด์žฌํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋™์ผํ•œ class ์†์„ฑ์ด ๋“ค์–ด๊ฐ„ ์š”์†Œ๋“ค์„ ๋™์‹œ์— ์‹๋ณ„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. class ์†์„ฑ๋„ id ์†์„ฑ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ css์™€ javascript์—์„œ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋‹ˆ ์•„์ฃผ ์ค‘์š”ํ•œ ์†์„ฑ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. class๋„ id์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ด๋ฆ„์„ ์ง€์„ ๋•Œ ์ง๊ด€์ ์ด๊ณ  ๊ฐ„๋‹จ๋ช…๋ฃŒํ•˜๊ฒŒ ์ ์–ด์ฃผ๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค.
  • CODE
<head> <meta charset="utf-8"> <title>id</title> <style type ="text/css"> .blue { color : blue; } .bic { font-size : 20px; } .upper { text-transform : uppercase; } </style> </head> <body> <p>์ด ๋ฌธ๋‹จ์€ ํ‰๋ฒ”ํ•œ pํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค.</p> <p class="blue">์ด ๋ฌธ๋‹จ์€ blue class๋ฅผ ๋ถ€์—ฌํ•˜์˜€์Šต๋‹ˆ๋‹ค.</p> <p class="blue bic">์ด ๋ฌธ๋‹จ์€ blue, bic class๋ฅผ ๋ถ€์—ฌํ•˜์˜€์Šต๋‹ˆ๋‹ค.</p> <p class="blue upper">์ด ๋ฌธ๋‹จ์€ blue, bic class๋ฅผ ๋ถ€์—ฌํ•˜์˜€์Šต๋‹ˆ๋‹ค. </p> </body>
  • ์‹คํ–‰ํ™”๋ฉด
ย 
notion imagenotion image

2. CSS์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

์œ„์— ๊ฐ„๋žตํ•œ ์ฝ”๋“œ์—์„œ๋„ ์œ ์ถ”๊ฐ€ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ์กฐ๊ธˆ ๋” ์„ธ๋ถ„ํ™”ํ•˜์—ฌ ์‚ดํŽด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
<h1>h1์˜ ์ฒซ๋ฒˆ์งธ</h1> <h1>h1์˜ ๋‘๋ฒˆ์งธ</h1> <h1>h1์˜ ์„ธ๋ฒˆ์งธ</h1> <h1>h1์˜ ๋„ค๋ฒˆ์งธ</h1>
notion imagenotion image
์œ„ h1์˜ 4๊ฐœ ์ „๋ถ€๋ฅผ ๋นจ๊ฐ„์ƒ‰์œผ๋กœ ๋ฐ”๊ฟ”๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. CSS๋ฅผ ์ ์šฉํ•˜๋Š” 3๊ฐ€์ง€ ๋ฐฉ๋ฒ• ์ค‘์— ์šฐ๋ฆฌ๋Š” HTML์— <style> ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•ด์„œ css ์†์„ฑ์„ ๋„ฃ์–ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
<head> <style> h1 {color: red;} </style> </head> <body> <h1>h1์˜ ์ฒซ๋ฒˆ์งธ</h1> <h1>h1์˜ ๋‘๋ฒˆ์งธ</h1> <h1>h1์˜ ์„ธ๋ฒˆ์งธ</h1> <h1>h1์˜ ๋„ค๋ฒˆ์งธ</h1> </body>
notion imagenotion image
h1 ๋ชจ๋‘๊ฐ€ ๋นจ๊ฐ„์ƒ‰์œผ๋กœ ๋ณ€ํ•œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋‘ h1 ํƒœ๊ทธ๋กœ ์ž‘์„ฑ์ด ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋‘ ๋‹ค๋ฅธ ์ƒ‰์„ ์ฃผ๊ณ  ์‹ถ์„ ๋•Œ์—๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ํŠน์ • ํƒœ๊ทธ๋ฅผ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿด ๊ฒฝ์šฐ ์‚ฌ์šฉํ•˜๋Š” ํƒœ๊ทธ๊ฐ€ id์ž…๋‹ˆ๋‹ค.
<head> <style> h1 {color: red} #two {color: yellow} #three {color: green} #four {color: blue} </style> </head> <body> <h1>h1์˜ ์ฒซ๋ฒˆ์งธ</h1> <h1 id='two'>h1์˜ ๋‘๋ฒˆ์งธ</h1> <h1 id='three'>h1์˜ ์„ธ๋ฒˆ์งธ</h1> <h1 id='four'>h1์˜ ๋„ค๋ฒˆ์งธ</h1> </body>
๋‘๋ฒˆ์งธ h1 id ์ด๋ฆ„์„ 'two', ์„ธ๋ฒˆ์งธ h1 id ์ด๋ฆ„์„ 'three', ๋„ค๋ฒˆ์งธ h1 id ์ด๋ฆ„์„ 'four'๋กœ ์ง€์ •ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  <style>์—์„œ ํ•ด๋‹น id๋ฅผ ๊พธ๋ฉฐ์ฃผ๊ธฐ ์œ„ํ•˜์—ฌ two๋Š” yellow, three๋Š” green, four๋Š” blue๋กœ ์ƒ‰์ƒ์„ ์ง€์ •ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ๋ถ€๋ถ„์€ CSS์—์„œ id๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ๋•Œ #์„ ์“ด๋‹ค๋Š” ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค.
notion imagenotion image
์ด๋ฒˆ์—๋Š” ์ฒซ๋ฒˆ์งธ / ๋‘๋ฒˆ์งธ๋ถ€ํ„ฐ ๋„ค๋ฒˆ์งธ๊นŒ์ง€ ์ƒ‰์„ ๋‹ฌ๋ฆฌํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
<head> <style> h1 {color: red} #two {color: green} #three {color: green} #four {color: green} </style> </head> <body> <h1>h1์˜ ์ฒซ๋ฒˆ์งธ</h1> <h1 id='two'>h1์˜ ๋‘๋ฒˆ์งธ</h1> <h1 id='three'>h1์˜ ์„ธ๋ฒˆ์งธ</h1> <h1 id='four'>h1์˜ ๋„ค๋ฒˆ์งธ</h1> </body>
notion imagenotion image
๋‘๋ฒˆ์งธ๋ถ€ํ„ฐ ๋„ค๋ฒˆ์งธ๊นŒ์ง€ ๋‹ค ๊ฐ™์€ ์ƒ‰์ž„์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ํ•˜๋‚˜ํ•˜๋‚˜ ์ง€์ •์„ ํ•ด์ฃผ๋Š” ๊ฒƒ์€ ๋น„ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค. ์ด๋Ÿด ๊ฒฝ์šฐ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด class์ž…๋‹ˆ๋‹ค. class๋Š” ๋ฌถ์–ด์„œ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
<head> <style> h1 {color: red} .yellowgreen {color: yellowgreen} </style> </head> <body> <h1>h1์˜ ์ฒซ๋ฒˆ์งธ</h1> <h1 id='two' class='yellowgreen'>h1์˜ ๋‘๋ฒˆ์งธ</h1> <h1 id='three' class='yellowgreen'>h1์˜ ์„ธ๋ฒˆ์งธ</h1> <h1 id='four' class='yellowgreen'>h1์˜ ๋„ค๋ฒˆ์งธ</h1> </body>
notion imagenotion image
๊ฐ ๋ผ์ธ๋งˆ๋‹ค class ๋ช…(yellowgreen)์„ ์ถ”๊ฐ€ํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  <style>ํƒœ๊ทธ์— .yellowgreen์„ ๋ถˆ๋Ÿฌ ์ƒ‰์ƒ์„ ์ง€์ •ํ•ด์ฃผ์—ˆ์ฃ . class๋Š” id์™€ ๋‹ค๋ฅด๊ฒŒ, ์ (.)์„ class ๋ช… ์•ž์— ๋‘์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

3. ์šฐ์„ ์ˆœ์œ„

์•„๋ž˜ ์ฝ”๋“œ์™€ ๊ฐ™์ด id์™€ class๊ฐ€ ๋™์‹œ์— ์žˆ์„ ๊ฒฝ์šฐ์—๋Š” id > class > tag ์ˆœ์œผ๋กœ style ์ ์šฉ์ด ๋ฉ๋‹ˆ๋‹ค.
<head> <style> h1 {color: red} .yellowgreen {color: yellowgreen} #four {color: skyblue} </style> </head> <body> <h1>h1์˜ ์ฒซ๋ฒˆ์งธ</h1> <h1 id='two' class='yellowgreen'>h1์˜ ๋‘๋ฒˆ์งธ</h1> <h1 id='three' class='yellowgreen'>h1์˜ ์„ธ๋ฒˆ์งธ</h1> <h1 id='four' class='yellowgreen'>h1์˜ ๋„ค๋ฒˆ์งธ</h1> </body>
notion imagenotion image
h1์˜ ๋‘๋ฒˆ์งธ, ์„ธ๋ฒˆ์งธ, ๋„ค๋ฒˆ์งธ๋Š” ๋‹ค ๊ฐ™์€ class(yellowgreen)์ด์—ˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋งˆ์ง€๋ง‰ h1์— id ์†์„ฑ์„ style์—์„œ ๋ณ€๊ฒฝํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด, h1์˜ ๋„ค๋ฒˆ์งธ๋Š” class์˜ style๋„, id์˜ style๋„ ์ ์šฉ๋˜์—ˆ์ง€๋งŒ ์šฐ์„ ์ˆœ์œ„์— ๋”ฐ๋ผ id์˜ ์†์„ฑ์ธ skyblue์ƒ‰์„ ๋‚˜ํƒ€๋‚ด๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
๋˜ํ•œ ๋™์ผํ•œ ์†์„ฑ์„ ์—ฌ๋Ÿฌ ํŒŒ์ผ์ด๋‚˜ class์—์„œ ๋‹ค์ค‘์œผ๋กœ ์ •์˜ํ•˜๊ณ  ์žˆ๋‹ค๋ฉด ๊ฐ€์žฅ ๋‚˜์ค‘์— ์ ์šฉ๋œ ํŒŒ์ผ์ด๋‚˜ class๊ฐ€ ํ•ด๋‹น ์†์„ฑ์„ ๋ฎ์–ด์“ฐ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฝ์šฐ one์ด๋ผ๋Š” ํด๋ž˜์Šค๊ฐ€ one.css, two.css, three.css ๋ชจ๋‘ ์ ์šฉ๋˜์–ด ์žˆ๋‹ค๊ณ  ํ•˜๋”๋ผ๋„ three.css๊ฐ€ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค.
<html> <head> <link rel="stylesheet" href="/css/one.css"> <link rel="stylesheet" href="/css/two.css"> <link rel="stylesheet" href="/css/three.css"> </head> <body> <h1 class="one">hello world</h1> </body> </html>
ย