๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ”ฅ TIL

21.04.29 // width % ๊ฐ’ ์ฃผ๋Š” ๋ฐฉ๋ฒ•.

๋ฐ˜์‘ํ˜•

[CSS] ๋„“์ด๊ฐ€ ๋‹ค๋ฅธ width %๊ฐ’ ์ฃผ๊ธฐ.

1. ํด๋ž˜์Šค ๋ช…์œผ๋กœ ์ง€์ •ํ•˜๊ธฐ.

  • ์žฅ์ 
    1. ์ฒซ ๋ฒˆ์งธ, ๋งˆ์ง€๋ง‰ ์ž์‹ ์š”์†Œ์—์„œ ํ•ญ์ƒ margin์ด๋‚˜ padding์š”์†Œ๋ฅผ ๋„ฃ์ง€ ์•Š์•„๋„ ๋œ๋‹ค.
    2. ํด๋ž˜์Šค๋ฅผ ์ง€์ •ํ•ด์„œ ์šด์˜ํ•˜๊ธฐ ํŽธ๋ฆฌํ•˜๋‹ค.
  • ๋‹จ์ 
    1. ํƒœ๊ทธ ์ด๋ฆ„์ด ๋งŽ์•„์ง„๋‹ค.
<div class="container">
    <div class="row">
        <div class="col col-3">
            <div class="box" style="background: blue;"></div>
        </div>
        <div class="col col-7">
            <div class="box" style="background: green;"></div>
        </div>
    </div>
</div>
.container {
    width: 1280px;
    margin: auto;
    background: #ddd;
}
/* Q. ์™œ row์— margin์„ ์ค„๊นŒ? */
.row {
    margin: 0 -50px;
}
.col {
    float: left;
    padding: 0 50px;
}

/* col์— %๋กœ ๋œ ํด๋ž˜์Šค ๋ช…์„ ์ง€์ •. */
.col-1 { 10%; }
.col-2 { 20%; }
.col-3 { 30%; }
.col-4 { 40%; }
.col-5 { 50%; }
.col-6 { 60%; }
.col-7 { 70%; }
.col-8 { 80%; }
.col-9 { 90%; }
.full-width { 100%; }

๊ฒฐ๊ณผ.

blue์™€ green์— ๊ฐ๊ฐ padding์ด 25px์ด ๋“ค์–ด๊ฐ€์žˆ๋‹ค.

๊ตณ์ด first-child , last-child๋ฅผ ์จ์„œ margin๊ฐ’์„ ์ฃผ์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

๊ทธ๋Ÿผ

Q. ์™œ row์— margin์„ ์ค„๊นŒ?

margin์„ ์ฃผ์ง€ ์•Š์œผ๋ฉด ์–‘ ์˜†์— ์—ฌ๋ฐฑ์ด ์ƒ๊ธด๋‹ค.

๊ทธ๋ž˜์„œ ์–‘์˜†์„ ๋‹น๊ฒจ์ฃผ๊ธฐ ์œ„ํ•ด (col์— padding์„ ์ฃผ๊ธฐ ์œ„ํ•ด) row์— margin์„ ์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.

์ •๋ฆฌ.

row์— margin์„ ์ฃผ๋Š” ์ด์œ ๋Š”, col์— padding ๊ฐ’์„ ์ผ์ •ํ•˜๊ฒŒ ์ฃผ๊ธฐ ์œ„ํ•จ์ด๋‹ค.

๋ง๋ถ™์—ฌ์„œ,

col์—๋Š” width ๊ฐ’๋งŒ ์ง€์ •ํ•ด์ฃผ๊ณ  box์—์„œ ์Šคํƒ€์ผ์„ ์ง€์ •ํ•ด์ค€๋‹ค.

col์— ์Šคํƒ€์ผ์„ ์ง€์ •ํ–ˆ์„ ๋•Œ ํ›„์— ์ˆ˜์ •ํ•˜๋ฉด ๋‹ค ํ‹€์–ด์ง.

 

2. -webkit-box ์‚ฌ์šฉํ•˜๊ธฐ.

์ €๋ฒˆ ๋ธ”๋กœ๊ทธ์—์„œ ์ธ์šฉ.

2021.04.07 - [๐Ÿ”ฅ TIL] - 2021.04.07

-webkit-box๋Š” float ๊ณผ ๋น„์Šทํ•ด์„œ col์— float์„ ์ฃผ์ง€ ์•Š์•„๋„ ์ˆ˜์ง์œผ๋กœ ๋งž์ถ”๊ฒŒ ๋œ๋‹ค.

<div class="container">
    <div class="row-box">
        <div class="col-box col-2" style="background: orange;">
            <div class="box">box1</div>
        </div>
        <div class="col-box col-5" style="background: tomato;">
            <div class="box">box2</div>
        </div>
        <div class="col-box col-3" style="background: red;">
            <div class="box">box3</div>
        </div>
    </div>
</div>
.row-box {
    display:-webkit-box;
    -webkit-box-orient:horizontal;
    -webkit-box-pack: center;
    /* -webkit-box-direction:reverse; */
    margin: 0 -50px;
}
.col-box {
    -webkit-box-ordinal-group:1;
    -webkit-box-flex: .1;
    padding: 0 50px;
}
.col-2 { width: 20%;}
.col-3 { width: 30%; }
.col-5 { width: 50%; }

๊ฒฐ๊ณผ.

์œ„์™€ ๋˜‘๊ฐ™์ด ๋‚˜์˜จ๋‹ค.

 

3. ๋ฒˆ์™ธ

2021.03.18 - [๐Ÿ”ฅ TIL] - [CSS] ์„ ํƒ์ž ์‚ฌ์šฉํ•˜์—ฌ % ๊ฐ’์œผ๋กœ ํ…œํ”Œ๋ฆฟ ์„ค์ •ํ•˜๊ธฐ

๋งŒ์•ฝ ๋‘๊ฐœ ์ผ ๋•Œ๋Š” 50%, ํ•˜๋‚˜์ผ ๋•Œ๋Š” 100%๋กœ ํ•˜๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ๋ ๊นŒ?

์œ„์˜ 1, 2 ์ฒ˜๋Ÿผ ํด๋ž˜์Šค ๋ช…์„ ๋”ฐ๋กœ ์ฃผ๊ฒŒ ๋˜๋ฉด ๊ณ ์ •๊ฐ’์ด๋ฏ€๋กœ ์ˆ˜์ •์ด ๋  ๋•Œ ์†์ด ๋งŽ์ด ๊ฐ„๋‹ค. ๊ทธ๋Ÿผ์œผ๋กœ ์• ์ดˆ์— ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ๋„ฃ์–ด ๋ฏธ๋ฆฌ ๋Œ€๋น„ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

.row .col:first-child:last-child {
    width: 100% !important;
}
.row .col:first-child:nth-last(2)-child,
row .col:first-child:nth-last(2)-child + .col {
    width: 50% !important;
}

๊ทธ๋Ÿผ ํƒœ๊ทธ๋ฅผ ์‚ญ์ œํ•ด๋„ ์ง€์ •ํ•ด๋†“์€ width ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝ๋œ๋‹ค.

 

๋ฐ˜์‘ํ˜•

'๐Ÿ”ฅ TIL' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[CSS] ~ , + / nth-child(), nth-of-type()  (0) 2021.06.08
2021.04.30  (0) 2021.04.30
2021.04.16 // fullpageScroll.js plugIn  (0) 2021.04.16
2021.04.07  (0) 2021.04.07
2021.04.07 // width calc() ์ฃผ๊ธฐ  (0) 2021.04.07