๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป

[CSS] ๊ทธ๋ฆฌ๋“œ ๋ ˆ์ด์•„์›ƒ(Grid Layout) ๋ณธ๋ฌธ

WEB/HTML & CSS

[CSS] ๊ทธ๋ฆฌ๋“œ ๋ ˆ์ด์•„์›ƒ(Grid Layout)

tskyoi 2020. 5. 23. 17:11

https://caniuse.com/

caniuse๋ผ๋Š” HTML, CSS, Javascript์˜ ๊ธฐ์ˆ ๋“ค์„ ์›น๋ธŒ๋ผ์šฐ์ €๋“ค์ด ์–ผ๋งˆ๋‚˜ ์ฑ„ํƒํ•˜๊ณ  ์žˆ๋Š”์ง€์— ๋Œ€ํ•œ ํ†ต๊ณ„๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋Š” ์‚ฌ์ดํŠธ์— ๋”ฐ๋ฅด๋ฉด, CSS์˜ Grid Layout์€ ๊ฑฐ์˜ ๋ชจ๋“  ์›น๋ธŒ๋ผ์šฐ์ €์— ์ ์šฉ์‹œํ‚ฌ ์ˆ˜ ์žˆ๊ณ , ์ „ ์„ธ๊ณ„ 95.27%์˜ ์‚ฌ์šฉ์ž๋“ค์ด ์ด ๊ธฐ์ˆ ์„ ์ ์šฉํ•œ ์‚ฌ์ดํŠธ๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ฒ˜๋Ÿผ Grid Layout์€ ์›นํŽ˜์ด์ง€์— ๋ ˆ์ด์•„์›ƒ์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ์•„์ฃผ ์œ ์šฉํ•œ ๊ธฐ์ˆ ์ž…๋‹ˆ๋‹ค.

 

https://caniuse.com/

 

Can I use... Support tables for HTML5, CSS3, etc

About "Can I use" provides up-to-date browser support tables for support of front-end web technologies on desktop and mobile web browsers. The site was built and is maintained by Alexis Deveria, with occasional updates provided by the web development commu

caniuse.com


1. ๊ทธ๋ฆฌ๋“œ ๋ ˆ์ด์•„์›ƒ ์ ์šฉํ•˜๊ธฐ

 

๊ทธ๋ฆฌ๋“œ ๋ ˆ์ด์•„์›ƒ์„ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋จผ์ € div ํƒœ๊ทธ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ž์‹ ์š”์†Œ(item)๋“ค์„ ๋ถ€๋ชจ ์š”์†Œ(wrapper ๋˜๋Š” container)๋กœ ๊ฐ์‹ธ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

<div class="wrapper">
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
    <div>6</div>
</div>

1, 2, 3, 4, 5, 6 ์ด๋ผ๋Š” ์š”์†Œ๋“ค์— ๊ทธ๋ฆฌ๋“œ ๋ ˆ์ด์•„์›ƒ์„ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ๊ฐ์˜ ์š”์†Œ๋“ค์„ div ํƒœ๊ทธ๋กœ ๊ตฌ๋ถ„ํ•ด์ฃผ๊ณ , ๋ชจ๋“  ์š”์†Œ๋ฅผ ํ•˜๋‚˜์˜ div ํƒœ๊ทธ๋กœ ๊ฐ์‹ธ์ค€ ํ›„, class๋ฅผ ์„ค์ •ํ•ด์ค๋‹ˆ๋‹ค.

 

 

์ด ๋ถ€๋ชจ class์— display๋ฅผ grid๋กœ ์„ค์ •ํ•˜๋ฉด ๊ทธ๋ฆฌ๋“œ ๋ ˆ์ด์•„์›ƒ์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

<style>
.wrapper {
	display: grid;
}
</style>

display: grid ๋Š” block ํŠน์„ฑ, display: inline-grid๋Š” inline ํŠน์„ฑ์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค.

 


2. ํ–‰, ์—ด ์„ค์ •ํ•˜๊ธฐ

<style>
.wrapper {
    display: grid;
    grid-template-columns: 50px 100px 50px;
    grid-template-rows: 100px 50px;
}
</style>

grid-template-columns์™€ grid-template-rows์— ๊ฐ๊ฐ์˜ ํ–‰๊ณผ ์—ด์˜ ํฌ๊ธฐ๋ฅผ ๋‚˜์—ดํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‘œํ˜„๋ฉ๋‹ˆ๋‹ค.

 

1

2

3

4

5

6

 

<style>
.wrapper {
    display:grid;
    grid-template-columns: 1fr 2fr 1fr;
    grid-template-rows: 2fr 1fr;
}
</style>

fr์„ ์‚ฌ์šฉํ•˜๋ฉด ์ „์ฒด์— ๋Œ€ํ•œ ๋น„์œจ๋กœ๋„ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋˜ํ•œ, grid-template-columns: 100px 100px 100px 100px๊ณผ ๊ฐ™์ด ๊ฐ™์€ ์ˆซ์ž๋‚˜ ํŒจํ„ด์ด ๋ฐ˜๋ณต๋œ๋‹ค๋ฉด repeatํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

์œ„ ์˜ˆ์ œ๋Š” 2๊ฐœ์˜ ํ–‰๊ณผ 3๊ฐœ์˜ ์—ด์— ๋Œ€ํ•œ ํฌ๊ธฐ๋งŒ ์ •์˜๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์— ์•„์ดํ…œ์„ ์ถ”๊ฐ€ํ•˜๋ฉด ์•„์ดํ…œ์˜ ํฌ๊ธฐ์— ๋งž์ถฐ ๋‹ค์Œ ํ–‰์— ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋ช…์‹œ์  ํ–‰ ์™ธ๋ถ€์˜ ์•”์‹œ์  ํ–‰์˜ ํฌ๊ธฐ๋ฅผ ์›ํ•˜๋Š” ๊ฐ’์œผ๋กœ ์ •์˜ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” grid-auto๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

<style>
.wrapper {
    display: grid;
    grid-template-columns: 50px 100px 50px;
    grid-template-rows: 100px 50px;
    grid-template-columns: 100px;
    grid-quto-rows: 100px;
}
</style>

 


3. ์•„์ดํ…œ ๋ฐฐ์น˜ํ•˜๊ธฐ

 

์•„์ดํ…œ์„ ์ž„์˜๋กœ ๋ฐฐ์น˜ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋จผ์ € ์ž์‹ ์š”์†Œ์— class๋ฅผ ์„ค์ •ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

<div class="wrapper">
    <div class="item1">1</div>
    <div class="item2">2</div>
    <div class="item3">3</div>
    <div class="item4">4</div>
    <div class="item5">5</div>
    <div class="item6">6</div>
</div>

์™ผ์ชฝ ์„ ๋ถ€ํ„ฐ ์ˆซ์ž 1, 2, 3, 4๊ฐ€ ๋ถ€์—ฌ๋˜๋ฏ€๋กœ,

.item1 {
    grid-column-start: 1;
    grid-column-end: 4;
}

.item3 {
    grid-column-start: 2;
    grid-column-end: 4;
}

์œ„ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

1

2

3

4

5

6

 

grid-column: 1 / 4; , grid-column: 2 / 4;๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ‘œํ˜„ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

 

- 2020/05/24 ์ˆ˜์ •

Comments