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