Client/CSS

[CSS] display ์†์„ฑ

meeeeejin 2020. 6. 11. 09:09

display ์†์„ฑ

CSS์˜ display๋Š” ์š”์†Œ๊ฐ€ ์–ด๋–ป๊ฒŒ ๋ณด์—ฌ์งˆ ์ง€๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ์†์„ฑ์œผ๋กœ, ๊ตฌ๋ฌธ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. 

 

display: value;

 

value์— ์ฃผ๋กœ ์“ฐ์ด๋Š” ์†์„ฑ๊ฐ’์€ block, inline, inline-block, none์ด ์žˆ์Šต๋‹ˆ๋‹ค. 

 

block ์š”์†Œ

block ์š”์†Œ์—๋Š” <p>, <h1>~<h6>, <div>, <ol>, <ul>, <li>, <form>๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. 

 

์ด ์š”์†Œ๋“ค์€ width์™€ height๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๊ณ , ๋งŒ์•ฝ ์ง€์ •ํ•˜์ง€ ์•Š์•˜์„๊ฒฝ์šฐ ์ „์ฒด ๊ฐ€๋กœ ์‚ฌ์ด์ฆˆ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. 

๋˜ํ•œ block์š”์†Œ ๋‹ค์Œ์— ์˜ค๋Š” ์š”์†Œ๋Š” ์ƒˆ๋กœ์šด line์—์„œ ์‹œ์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์š”์†Œ๋“ค์ด ์•„๋ž˜๋กœ ์Œ“์ด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 

 

<block ์š”์†Œ ์˜ˆ์‹œ>

1
2
3
4
<h1 style="border: 1px solid black;">Hello</h1>
<div style="width: 100px; height: 100px; background-color: red;"></div>
<div style="width: 100px; height: 100px; background-color: blue;"></div>
<div style="width: 100px; height: 100px; background-color: green;"></div>
cs

<h1>์€ width๋ฅผ ์ •ํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ํ™”๋ฉด ์ „์ฒด๊ฐ€ ๊ฐ€๋กœ์‚ฌ์ด์ฆˆ๊ฐ€ ๋˜๊ณ ,  3๊ฐœ์˜ <div>๋Š” width: 100px; height: 100px๋กœ ์„ค์ •๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•  ์ ์€ <div> ์š”์†Œ ๋‚ด์— ๋‚ด์šฉ์„ ์ž‘์„ฑํ•˜์ง€ ์•Š์€ ์ƒํƒœ๊ธฐ ๋•Œ๋ฌธ์—, ๋งŒ์•ฝ width๋‚˜ height๋ฅผ ์ง€์ •ํ•ด์ฃผ์ง€ ์•Š๋Š”๋‹ค๋ฉด <div>์˜ ์‚ฌ์ด์ฆˆ๋Š” 0์ด ๋˜์–ด ํ™”๋ฉด์— ๋ณด์ด์ง€ ์•Š์Šต๋‹ˆ๋‹ค. 3๊ฐœ์˜ <div>๊ฐ€ ์˜†์œผ๋กœ ์Œ“์ผ ๊ณต๊ฐ„์ด ์žˆ์Œ์—๋„ ๊ฐ๊ฐ ์ƒˆ๋กœ์šด ์ค„์—์„œ ์‹œ์ž‘ํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ๋„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

 

 

inline ์š”์†Œ

inline ์š”์†Œ์—๋Š” <span>, <img>, <a> ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. 

 

์ด ์š”์†Œ๋“ค์€ ํƒœ๊ทธ๋‚ด์˜ content์— ์˜ํ•ด width๊ฐ€ ์ •ํ•ด์ง€๋ฉฐ, width๋ฅผ ๋”ฐ๋กœ ์ง€์ •ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. 

๋˜ํ•œ block์š”์†Œ์™€๋Š” ๋‹ค๋ฅด๊ฒŒ ์š”์†Œ๊ฐ€ ํ•ญ์ƒ ์ƒˆ๋กœ์šด ์ค„์—์„œ ์‹œ์ž‘ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ inline ์š”์†Œ๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ์˜†์œผ๋กœ ์Œ“์ด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 

 

<inline ์š”์†Œ ์˜ˆ์‹œ>

1
2
3
<span style="padding: 10px; background-color: red;">inline1</span>
<span style="padding: 10px; background-color: blue;">inline2</span>
<span style="padding: 10px; background-color: green;">inline3</span>
cs

<span>์€ inline ์š”์†Œ๊ธฐ ๋•Œ๋ฌธ์— ์ƒˆ๋กœ์šด ์ค„์—์„œ ์‹œ์ž‘ํ•˜์ง€ ์•Š๊ณ , ์˜†์œผ๋กœ ์Œ“์ด๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

 

 

display์˜ ์†์„ฑ๊ฐ’

display์˜ ๋Œ€ํ‘œ์ ์ธ ์†์„ฑ ๊ฐ’์€ block, inline, inline-block, none์ด ์žˆ์Šต๋‹ˆ๋‹ค. 

 

  • display: block;
    // ์š”์†Œ๋ฅผ block ์š”์†Œ์ฒ˜๋Ÿผ ๋ณด์ด๊ฒŒ ํ•จ

  • display: inline;
    // ์š”์†Œ๋ฅผ inline ์š”์†Œ์ฒ˜๋Ÿผ ๋ณด์ด๊ฒŒ ํ•จ

  • display: inline-block;
    // inline ์š”์†Œ์ฒ˜๋Ÿผ format ๋˜์ง€๋งŒ width, height๋Š” ์ ์šฉ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•จ

  • display: none;
    // ์š”์†Œ๋ฅผ ์›น ์‚ฌ์ดํŠธ์—์„œ ์‚ฌ๋ผ์ง€๊ฒŒ ํ•จ(removed)

none์— ๋Œ€ํ•œ ์„ค๋ช…์„ ๋ง๋ถ™์ด์ž๋ฉด, ์š”์†Œ๊ฐ€ ์•ˆ ๋ณด์ด๊ฒŒ ํ•  ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ํ™”๋ฉด์—์„œ ํ•ด๋‹น ์š”์†Œ์˜ ๊ณต๊ฐ„ ํ• ๋‹น์„ ์•„์˜ˆ ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค

๋งŒ์•ฝ ์š”์†Œ๊ฐ€ ๊ณต๊ฐ„์€ ์ฐจ์ง€ํ•˜์ง€๋งŒ ์•ˆ ๋ณด์ด๋„๋ก ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด visibility: hidden; ์„ ์จ์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

 

display ์‚ฌ์šฉ ์˜ˆ์ œ

1. display: block;

<html>

1
2
3
<span>inline</span>
<span>inline</span>
<span class="block">display: block</span>
cs

<css>

1
2
3
4
5
6
7
8
9
span {
  background-color: yellow;
  border: 1px solid black;
}
 
.block {
  display: block;
  width: 200px;
}
cs

<์‹คํ–‰๊ฒฐ๊ณผ>

 

 

2. display: inline;

<html>

1
2
3
<div>block</div>
<div>block</div>
<div class="inline">display: inline</div>
cs

<css>

1
2
3
4
5
6
7
8
div {
  background-color: yellow;
  border: 1px solid black;
}
 
.inline {
  display: inline;
}
cs

<์‹คํ–‰๊ฒฐ๊ณผ>

 

3. display: inline-block;

<html>

1
2
3
<span>inline</span>
<span>inline</span>
<span class="inline-block">display: inline-block</span>
cs

<css>

1
2
3
4
5
6
7
8
9
10
span {
  background-color: yellow;
  border: 1px solid black;
}
 
.inline-block {
  display: inline-block;
  width: 200px;
  height: 100px;
}
cs

<์‹คํ–‰๊ฒฐ๊ณผ>

 

4. display: none;

<html>

1
2
3
4
<div class="none">display - none</div>
<div>div</div>
<div class="hidden">visibility - hidden</div>
<div>div</div>
cs

<css>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
div {
  background-color: yellow;
  border: 1px solid black;
  width: 100px;
  height: 100px;
}
 
.none {
  display: none;
}
 
.hidden {
  visibility: hidden;
}
cs

<์‹คํ–‰๊ฒฐ๊ณผ>

์•ž์„œ ๋งํ–ˆ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ, display: none;์€ ์›นํŽ˜์ด์ง€์—์„œ ์š”์†Œ๋ฅผ ์•„์˜ˆ ์ง€์›Œ๋ฒ„๋ฆฌ์ง€๋งŒ(๊ณต๊ฐ„ ํ• ๋‹น X) visibility: hidden;์€ ์›นํŽ˜์ด์ง€์—์„œ ์š”์†Œ๊ฐ€ ๋ณด์ด์ง€ ์•Š๊ฒŒ(๊ณต๊ฐ„ ํ• ๋‹น O) ํ•ด์ค๋‹ˆ๋‹ค. 

 

 

 

 

๊ณต๋ถ€ํ•œ ๊ฒƒ์„ ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค. ์ˆ˜์ •ํ•  ๊ฒƒ์ด ์žˆ์œผ๋ฉด ์•Œ๋ ค์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค :)

 

 

728x90