Flex (Flexbox)
- ๋จ์ผ ๋ฐฉํฅ(ํ ๋๋ ์ด)์ผ๋ก ์์ดํ ์ ๋ฐฐ์น.
- ์์ดํ ์ ํฌ๊ธฐ๊ฐ ๋์ ์ผ๋ก ๋ณํ ์ ์์ด ๋ฐ์ํ ๋์์ธ์ ์ ๋ฆฌ.
- ๊ฐ๋จํ ์ ๋ ฌ๊ณผ ๊ณต๊ฐ ๋ถ๋ฐฐ์ ๊ฐ๋ ฅ.
<!DOCTYPE html> <html> <head> <style> .flex-container { display: flex; justify-content: space-between; /* ์์ดํ ๊ฐ ๊ฐ๊ฒฉ์ ๊ท ๋ฑํ๊ฒ ๋ถ๋ฐฐ */ background-color: #f0f0f0; } .flex-item { background-color: #4CAF50; color: white; padding: 20px; margin: 10px; text-align: center; } </style> </head> <body> <div class="flex-container"> <div class="flex-item">1</div> <div class="flex-item">2</div> <div class="flex-item">3</div> </div> </body> </html>
123
์ ์์ ์์ .flex-container๋ display: flex๋ฅผ ํตํด ๊ฐ๋ก ๋ฐฉํฅ์ผ๋ก ์์ดํ
์ ๋ฐฐ์นํฉ๋๋ค. justify-content: space-between ์์ฑ์ ์์ดํ
์ฌ์ด์ ๊ณต๊ฐ์ ๊ท ๋ฑํ๊ฒ ๋๋์ด ๋ฐฐ์นํฉ๋๋ค. ๊ฒฐ๊ณผ์ ์ผ๋ก ์ธ ๊ฐ์ ๋ฐ์ค๊ฐ ์ํ์ผ๋ก ๋๋ํ ์ ๋ ฌ๋ฉ๋๋ค.
Grid
grid๋ 2์ฐจ์ ๊ทธ๋ฆฌ๋ ์์คํ
์ ๊ตฌ์ฑํ๋ ๋ฐ ์ฌ์ฉ๋๋ฉฐ, ์ํ(๊ฐ๋ก)๊ณผ ์์ง(์ธ๋ก) ๋ฐฉํฅ ๋ชจ๋๋ฅผ ๊ณ ๋ คํ ๋ ์ด์์์ ์ค๊ณํ ์ ์์ต๋๋ค. ๊ฐ ์
์ ํฌ๊ธฐ๋ฅผ ๋ฏธ๋ฆฌ ์ ์ํ์ฌ ์ผ๊ด๋ ๊ตฌ์กฐ๋ฅผ ์ ์งํ๋ฉฐ, ๋ณต์กํ ๋ ์ด์์์ ์ ํฉํฉ๋๋ค.
- ํ(row)๊ณผ ์ด(column)์ ๋ชจ๋ ์ ์ ๊ฐ๋ฅ.
- ์ ํฌ๊ธฐ๋ฅผ ๊ณ ์ ํ๊ฑฐ๋ ๋น์จ๋ก ์ค์ ํ์ฌ ์ผ์ ํ ๊ตฌ์กฐ ์ ์ง.
- ๋ ์ด์์ ๋ณํ๊ฐ ์ ์ ์ ์ ์ธ ๋์์ธ์ ์ ํฉ.
<!DOCTYPE html> <html> <head> <style> .grid-container { display: grid; grid-template-columns: 100px 100px 100px; /* 3์ด, ๊ฐ 100px */ grid-template-rows: 80px 80px; /* 2ํ, ๊ฐ 80px */ gap: 10px; /* ์ ๊ฐ ๊ฐ๊ฒฉ */ background-color: #f0f0f0; } .grid-item { background-color: #2196F3; color: white; padding: 20px; text-align: center; } </style> </head> <body> <div class="grid-container"> <div class="grid-item">1</div> <div class="grid-item">2</div> <div class="grid-item">3</div> <div class="grid-item">4</div> <div class="grid-item">5</div> <div class="grid-item">6</div> </div> </body> </html>
123456
์ ์์ ์์๋ grid-container๊ฐ 2x2 ๊ทธ๋ฆฌ๋ ๊ตฌ์กฐ๋ฅผ ํ์ฑํฉ๋๋ค. grid-template-columns: 1fr 1fr๋ก ๋ ๊ฐ์ ์ด์ ๋์ผํ ๋น์จ๋ก ๋๋๊ณ , grid-template-rows๋ก ํ ๋์ด๋ฅผ 100px๋ก ๊ณ ์ ํ์ต๋๋ค. gap ์์ฑ์ผ๋ก ์
์ฌ์ด์ 10px ๊ฐ๊ฒฉ์ ์ถ๊ฐํ์ต๋๋ค.
Grid๋ ๋ฐ์ํ์ ์ ํฉํ๊ฐ?
grid๋ ๋ฐ์ํ ๋์์ธ์ ๋งค์ฐ ๊ฐ๋ ฅํ ๋๊ตฌ์
๋๋ค. ๋ฏธ๋ฆฌ ์ ์๋ ๊ตฌ์กฐ๋ฅผ ์ ์งํ๋ค๋ ์ ์ ๊ณ ์ ๋ ๋ ์ด์์์ ์ ๋ฆฌํ๋ค๋ ์๋ฏธ์ผ ๋ฟ, ๋ฐ์ํ์ผ๋ก ๋์ ์ผ๋ก ์กฐ์ ํ๋ ๋ฐ๋ ํ์ํฉ๋๋ค. CSS Grid๋ ๋ค์๊ณผ ๊ฐ์ ๊ธฐ๋ฅ์ ํตํด ๋ฐ์ํ์ ์ง์ํฉ๋๋ค
- fr ๋จ์: ์ ์ฐํ ๋น์จ๋ก ์ด/ํ ํฌ๊ธฐ๋ฅผ ์ ์.
- minmax() ํจ์: ์ต์/์ต๋ ํฌ๊ธฐ๋ฅผ ์ค์ ํด ์ฝํ ์ธ ์ ๋ฐ๋ผ ์ ์.
- auto-fit / auto-fill: ์ด ๊ฐ์๋ฅผ ํ๋ฉด ํฌ๊ธฐ์ ๋ฐ๋ผ ์๋์ผ๋ก ์กฐ์ .
- ๋ฏธ๋์ด ์ฟผ๋ฆฌ: ํ๋ฉด ํฌ๊ธฐ์ ๋ฐ๋ผ ๊ทธ๋ฆฌ๋ ๊ตฌ์กฐ๋ฅผ ๋ณ๊ฒฝ.
<!DOCTYPE html> <html> <head> <style> .grid-container2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; } .item { background-color: lightgreen; padding: 20px; text-align: center; } @media (max-width: 600px) { .grid-container { grid-template-columns: 1fr; /* ์์ ํ๋ฉด์์๋ 1์ด๋ก */ } } </style> </head> <body> <div class="grid-container2"> <div class="item">์์ดํ 1</div> <div class="item">์์ดํ 2</div> <div class="item">์์ดํ 3</div> <div class="item">์์ดํ 4</div> <div class="item">์์ดํ 5</div> <div class="item">์์ดํ 6</div> </div> </body> </html>
์์ดํ 1์์ดํ 2์์ดํ 3์์ดํ 4์์ดํ 5์์ดํ 6
- auto-fit: ์ฌ์ฉ ๊ฐ๋ฅํ ๊ณต๊ฐ์ ๋ฐ๋ผ ์ด์ ์๋์ผ๋ก ์ฑ์.
- minmax(150px, 1fr): ๊ฐ ์ด์ ์ต์ ๋๋น๋ 150px, ์ต๋๋ ๋จ์ ๊ณต๊ฐ์ ๋น์จ๋ก ๋๋.
- ๊ฒฐ๊ณผ: ํ๋ฉด์ด ๋์ผ๋ฉด ์ฌ๋ฌ ์ด๋ก ๋ฐฐ์น๋๊ณ , ์ข์์ง๋ฉด ์ด์ด ์ค์ด๋ฆ.
- ํ๋ฉด ๋๋น๊ฐ 600px ์ดํ์ผ ๋ 1์ด๋ก ๊ฐ์ ์ ํ.
ํฐ ํ๋ฉด์์๋ ์์ดํ
์ด ์ฌ๋ฌ ์ด๋ก ๋์ด๋๊ณ , ์์ ํ๋ฉด์์๋ ์๋์ผ๋ก ํ ์ด๋ก ์ ๋ ฌ๋์ด ๋ฐ์ํ ๋ ์ด์์์ด ๊ตฌํ๋จ.