CSS ์์ฑ Part 02.
๋ฐฐ์น
position
โถ ์์์ ์์น ์ง์
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
.container {
width: 300px;
background-color: royalblue;
position: relative;
}
.container .item {
border: 4px dashed red;
background-color: orange;
}
.container .item:nth-child(1) {
width: 100px;
height: 100px;
}
.container .item:nth-child(2) {
width: 140px;
height: 70px;
powition: absolute;
right: 10px;
bottom: 10px;
}
.container .item:nth-child(3) {
width: 70px;
height: 120px;
}
- static: ๊ธฐ๋ณธ๊ฐ(์์์ ์ด๋ฏธ ๋ค์ด์๋ ์์ฑ์ ๊ฐ). ๊ธฐ์ค ์์
- relative: ์์ ์์ ์ ๊ธฐ์ค
- absolute: ์์น ์ ๋ถ๋ชจ ์์๋ฅผ ๊ธฐ์ค. ๋ถ๋ชจ ์์๋ฅผ ๊ผญ ํ์ธํด์ผ ํจ. (relative)
- fixed: ๋ทฐ ํฌํธ(๋ธ๋ผ์ฐ์ ๋ฅผ ๊ธฐ์ค)
- sticky: ์คํฌ๋กค ์์ญ ๊ธฐ์ค
position๊ณผ ๊ฐ์ด ์ฌ์ฉํ๋ CSS์์ฑ. ๋ชจ๋ ์์๋ฅผ ์ฌ์ฉํ ์ ์๋ค. ์์์ ๊ฐ ๋ฐฉํฅ๋ณ ๊ฑฐ๋ฆฌ ์ง์
- top
- bottom
- left
- right
- z-index
์์์ display๊ฐ ๋ณ๊ฒฝ๋จ
- position ์์ฑ ๊ฐ์ผ๋ก absolute, fixed ๊ฐ ์ง์ ๋ ์์๋, display์์ฑ์ด block์ผ๋ก ๋ณ๊ฒฝ๋จ. (relative X)
<span>123</span>
/* ์ธ๋ผ์ธ ์์ฑ์ ๋๋น๊ฐ์ ๊ฐ์ง ์ ์์ผ๋, position ๊ฐ์ ๋ถ์ฌํ๋ฉด ๋๋น๊ฐ์ ๊ฐ์ง ์ ์๋ค. */
span {
width: 100px;
height: 100px;
background: orange;
font-size: 40px;
position: absolute;
}
์์ ์์ ์์(Stack order)
โถ ์ด๋ค ์์๊ฐ ์ฌ์ฉ์์ ๋ ๊ฐ๊น๊ฒ ์๋์ง(์์ ์์ด๋์ง) ๊ฒฐ์
- ์์์ position ์์ฑ์ ๊ฐ์ด ์๋ ๊ฒฝ์ฐ(relative, absolute, fixed, sticky) ์์ ์์. (๊ธฐ๋ณธ๊ฐ static ์ ์ธ)
- 1๋ฒ ์กฐ๊ฑด์ด ๊ฐ์ ๊ฒฝ์ฐ, z-index ์์ฑ์ ์ซ์ ๊ฐ์ด ๋์ ์๋ก ์์ ์์.
- 1๋ฒ๊ณผ 2๋ฒ ์กฐ๊ฑด๊น์ง ๊ฐ์ ๊ฒฝ์ฐ, HTML์ ๋ง์ง๋ง ๊ตฌ์กฐ์ผ ์๋ก ์์ ์์.
์ฆ, HTML ๋ง์ง๋ง ๊ตฌ์กฐ > z-index > position ์์ผ๋ก ์์ ์์. z-index๋ก ์กฐ์ ๊ฐ๋ฅ
z-index
โถ ์์์ ์์ ์ ๋๋ฅผ ์ง์
- auto: ๊ธฐ๋ณธ๊ฐ(์์์ ์ด๋ฏธ ๋ค์ด์๋ ์์ฑ์ ๊ฐ). ๋ถ๋ชจ ์์์ ๋์ผํ ์์ ์ ๋
- ์ซ์: ์ซ์๊ฐ ๋์์๋ก ์์ ์์(์ซ์๊ฐ ๋๋ฌด ์ปค์ง์ง ์๋๋ก ์ฃผ์ !! ex) z-index: 999;)
๋ฐฐ์น
์ถ๋ ฅ ํน์ฑ (display)
โถ ์์์ ํ๋ฉด ์ถ๋ ฅ(๋ณด์ฌ์ง) ํน์ฑ
<span>Hello world!</span>
body {
margin: 20px;
}
span {
/* spanํ๊ทธ๋ ๊ฐ๋ก, ์ธ๋ก ๊ฐ์ ๊ฐ์ง์ ์๊ธฐ ๋๋ฌธ์ display: block;์ ์ค๋ค. */
display: block;
width: 120px;
height: 30px;
background-color: royalblue;
color: #fff;
}
flex(์ ๋ ฌ)
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
.container {
display: flex; /* Flex container */
flex-direction: row; /* ์ผ์ชฝ๋ถํฐ ์ค๋ฅธ์ชฝ์ผ๋ก ์ํ์ผ๋ก ์ ๋ ฌ๋๋ค. */
flex-direction: row-reverse; /* ์ค๋ฅธ์ชฝ๋ถํฐ ์ผ์ชฝ์ผ๋ก ์ํ์ผ๋ก ์ ๋ ฌ๋๋ค. */
}
.container {
display: flex;
flex-direction: column; /* ์์์ ์๋๋ก ์์ง ์ ๋ ฌ */
flex-direction: column-reverse; /* ์๋์์ ์๋ก ์์ง ์ ๋ ฌ */
}
flex-wrap
โถ Flex Items ๋ฌถ์(์ค ๋ฐ๊ฟ) ์ฌ๋ถ
/* ํ๋ฉด์ด ์์์ ธ์ ์์์์์ธ .item์ด ๋์น๋ฉด ์๋์ผ๋ก ์๋๋ก ๋ด๋ ค๊ฐ๋ค. */
.container {
display: flex;
flex-wrap: wrap;
}
.container .item {
width: 100px;
height: 100px;
}
- nowrap: ๊ธฐ๋ณธ๊ฐ(์์์ ์ด๋ฏธ ๋ค์ด์๋ ์์ฑ์ ๊ฐ). ๋ฌถ์(์ค ๋ฐ๊ฟ)์์
- wrap: ์ฌ๋ฌ ์ค๋ก ๋ฌถ์
- wrap-reverse: wrap์ ๋ฐ๋ ๋ฐฉํฅ์ผ๋ก ๋ฌถ์
justify-content
โถ ์ฃผ ์ถ์ ์ ๋ ฌ ๋ฐฉ๋ฒ. ์ผ๋ฐ์ ์ธ ๊ฐ๋ก ์ ๋ ฌ
- flex-start: ๊ธฐ๋ณธ๊ฐ(์์์ ์ด๋ฏธ ๋ค์ด์๋ ์์ฑ์ ๊ฐ). Flex Items๋ฅผ ์์์ ์ผ๋ก ์ ๋ ฌ
- flex-end: Flex Items๋ฅผ ๋์ ์ผ๋ก ์ ๋ ฌ
- center: Flex Items๋ฅผ ๊ฐ์ด๋ฐ ์ ๋ ฌ
- space-between: ๊ฐ Flex Item ์ฌ์ด๋ฅผ ๊ท ๋ฑํ๊ฒ ์ ๋ ฌ
- space-arount: ๊ฐ Flex Item์ ์ธ๋ถ ์ฌ๋ฐฑ์ ๊ท ๋ฑํ๊ฒ ์ ๋ ฌ
align-content
โถ ๊ต์ฐจ ์ถ์ ์ฌ๋ฌ ์ค ์ ๋ ฌ ๋ฐฉ๋ฒ, ๋ฐ์ค๊ฐ ๋ ์ค ์ด์์ด์ด์ผ ํ๋ค.
- stretch: ๊ธฐ๋ณธ๊ฐ(์์์ ์ด๋ฏธ ๋ค์ด์๋ ์์ฑ์ ๊ฐ). Flex Items๋ฅผ ์์์ ์ผ๋ก ์ ๋ ฌ
- flex-start: Flex Items๋ฅผ ์์์ ์ผ๋ก ์ ๋ ฌ (์ธ๋ก์ ๋ ฌ)
- flex-end: Flex Items๋ฅผ ๋์ ์ผ๋ก ์ ๋ ฌ (์ธ๋ก์ ๋ ฌ)
- center: Flex Items๋ฅผ ๊ฐ์ด๋ฐ ์ ๋ ฌ (์ธ๋ก์ ๋ ฌ)
- space-between: ๊ฐ Flex Item ์ฌ์ด๋ฅผ ๊ท ๋ฑํ๊ฒ ์ ๋ ฌ
- space-around: ๊ฐ Flex Item์ ์ธ๋ถ ์ฌ๋ฐฑ์ ๊ท ๋ฑํ๊ฒ ์ ๋ ฌ
align-items
โถ ๊ต์ฐจ ์ถ์ ํ ์ค ์ ๋ ฌ ๋ฐฉ๋ฒ. ์ผ๋ฐ์ ์ธ ์์ง ์ ๋ ฌ
.container {
width: 500px;
height: 300px;
background-color: royalblue;
display: flex;
justify-content: center;
align-items: center;
}
.container .item {
width: 100px;
height: 100px;
border: 3px dashed red;
background-color: orange;
flex-grow: 1;
flex-basis: 0;
}
.container .item(3) {
flex-grow: 2;
}
- stretch: ๊ธฐ๋ณธ๊ฐ(์์์ ์ด๋ฏธ ๋ค์ด์๋ ์์ฑ์ ๊ฐ). Flex Items๋ฅผ ๊ต์ฐจ ์ถ์ผ๋ก ๋๋ฆผ
- flex-start: Flex Items๋ฅผ ๊ฐ ์ค์ ์์์ ์ผ๋ก ์ ๋ ฌ
- flex-end: Flex Items๋ฅผ ๊ฐ ์ค์ ๋์ ์ผ๋ก ์ ๋ ฌ
- center: Flex Items๋ฅผ ๊ฐ ์ค์ ๊ฐ์ด๋ฐ ์ ๋ ฌ
- baseline: Flex Items๋ฅผ ๊ฐ ์ค์ ๋ฌธ์ ๊ธฐ์ค์ ์ ์ ๋ ฌ
order
โถ Flex Item์ ์์
- 0: ๊ธฐ๋ณธ๊ฐ(์์์ ์ด๋ฏธ ๋ค์ด์๋ ์์ฑ์ ๊ฐ). ์์ ์์
- ์ซ์: ์ซ์๊ฐ ์์ ์๋ก ๋จผ์
flex-grow
โถ Flex Item์ ์ฆ๊ฐ ๋๋น ๋น์จ
- 0: ๊ธฐ๋ณธ๊ฐ(์์์ ์ด๋ฏธ ๋ค์ด์๋ ์์ฑ์ ๊ฐ). ์ฆ๊ธฐ ๋น์จ ์์
- ์ซ์: ์ฆ๊ฐ ๋น์จ
flex-basis์ ํจ๊ป ์ด๋ค.
flex-shrink
โถ Flex Item์ ๊ฐ์ ๋๋น ๋น์จ
- 1: ๊ธฐ๋ณธ๊ฐ(์์์ ์ด๋ฏธ ๋ค์ด์๋ ์์ฑ์ ๊ฐ). Flex Container ๋๋น์ ๋ฐ๋ผ ๊ฐ์ ๋น์จ ์ ์ฉ
- ์ซ์: ๊ฐ์ ๋น์จ
- ์ด ๊ธฐ๋ฅ์ ํด์ ํ๊ณ ์ถ์ผ๋ฉด flex-shirink:0;์ ํ๋ฉด ๋๋ค.
์๋ฅผ ๋ค์ด ๋ธ๋ผ์ฐ์ ๊ฐ ์ค์ด๋ค๋ฉด์ ์ด๋ฏธ์ง๋ ํ ์คํธ๊ฐ ์ค์ด๋ค๋ฉฐ ๋ ์ด์์์ด ๋ง๊ฐ์ง๋ ๊ฒฝ์ฐ๊ฐ ์๋๋ฐ ๋ง๊ฐ์ง์ง ์์์ผ๋ฉด ํ๋ display: flex;์ ์์์์์ flex-shrink: 0; ๋ฅผ ์ฃผ์ด ๋ธ๋ผ์ฐ์ ๊ฐ ์ค์ด๋ค์ด๋ ๋ ์ด์์์ด ๋ฌด๋์ง์ง ์๋๋ก ๊ณ ์ ํ๋ ๋ฐฉ๋ฒ์ผ๋ก ์์ฃผ ์ฌ์ฉ๋๋ค.
flex-basis
โถ Flex Item์ ๊ณต๊ฐ ๋ฐฐ๋ถ ์ ๊ธฐ๋ณธ ๋๋น
- auto: ์์์ Content ๋๋น ์ฐธ๊ณ ํ์ฌ flex-grow๋น์จ์ ์ ํจ
- ๋จ์: px, em, rem ๋ฑ ๋จ์๋ก ์ง์
- flex-basis: 0; ์ผ๋ก ์ง์ ํ๋ฉด content์ ๊ธฐ๋ณธ ๋๋น๋ฅผ ์๊ฐํ์ง ์๊ธฐ ๋๋ฌธ์ ๊ท ์ผํ ์ ๋ ฌ์ด ๊ฐ๋ฅํ๋ค.
flex-grow์ ํจ๊ป ์ด๋ค.