 |
|
|  |
CSS¸¦ »ç¿ëÇÏ¿© ¿ä¼ÒÀÇ ³»¿ë¹°¿¡ µû¶ó width¸¦ Á¶Á¤ÇÏ´Â ¹æ¹ý |
|
|
 |
7´Þ Àü |
CSS¸¦ »ç¿ëÇÏ¿© ¿ä¼ÒÀÇ ³»¿ë¹°¿¡ µû¶ó width¸¦ Á¶Á¤ÇÏ´Â ¹æ¹ýÀº ¿©·¯ °¡Áö°¡ ÀÖ½À´Ï´Ù. °¢ ¹æ¹ýÀº ƯÁ¤ »óȲ¿¡ ´õ ÀûÇÕÇÒ ¼ö ÀÖ½À´Ï´Ù. ¾Æ·¡¿¡ ¸î °¡Áö ¹æ¹ýÀ» ¼³¸íÇÕ´Ï´Ù.
1. ÄÜÅÙÃ÷ Å©±â¿¡ ¸ÂÃç ÀÚµ¿À¸·Î Å©±â Á¶Á¤
±âº»ÀûÀ¸·Î HTML ¿ä¼Ò´Â ÄÜÅÙÃ÷¿¡ ¸Â°Ô ÀÚµ¿À¸·Î Å©±â°¡ Á¶Á¤µË´Ï´Ù. ¿¹¸¦ µé¾î, inline, inline-block, flex, ¶Ç´Â grid ÄÁÅ×À̳ʸ¦ »ç¿ëÇÏ¸é ¿ä¼Ò°¡ ³»¿ë¿¡ µû¶ó ÀÚµ¿À¸·Î Å©±â°¡ Á¶Á¤µË´Ï´Ù.
¿¹½Ã:
<div style="display: inline-block; background-color: lightblue;">
ÀÌ ÅؽºÆ®¿¡ ¸ÂÃç ³Êºñ°¡ ÀÚµ¿À¸·Î Á¶Á¤µË´Ï´Ù.
</div>
2. flexbox¸¦ »ç¿ëÇÏ¿© ³»¿ë ±â¹ÝÀ¸·Î ³Êºñ Á¶Á¤
flexbox¸¦ »ç¿ëÇϸé ÀÚ½Ä ¿ä¼ÒÀÇ ³»¿ë¿¡ µû¶ó ºÎ¸ðÀÇ ³Êºñ¸¦ ½±°Ô Á¶ÀýÇÒ ¼ö ÀÖ½À´Ï´Ù.
¿¹½Ã:
<div style="display: flex; justify-content: flex-start; background-color: lightgray;">
<div style="flex: 0 1 auto; background-color: lightblue;">
ÀÌ ÅؽºÆ®¿¡ ¸ÂÃç ³Êºñ°¡ Á¶Á¤µË´Ï´Ù.
</div>
</div>
¿©±â¼ flex: 0 1 auto;´Â ¿ä¼Ò°¡ ±âº» Å©±â¿Í ³»¿ë¿¡ ¸ÂÃç Å©±â¸¦ Á¶Á¤Çϵµ·Ï ÇÕ´Ï´Ù.
3. fit-content¸¦ »ç¿ë
CSS fit-content ¼Ó¼ºÀ» »ç¿ëÇÏ¿© ¿ä¼ÒÀÇ ³Êºñ¸¦ ³»¿ë¿¡ ¸Â°Ô Á¶ÀýÇÒ ¼ö ÀÖ½À´Ï´Ù.
¿¹½Ã:
<div style="width: fit-content; background-color: lightgreen; border: 1px solid black;">
ÀÌ ¹Ú½ºÀÇ ³Êºñ´Â ³»¿ë¹°¿¡ ¸ÂÃç Á¶Á¤µË´Ï´Ù.
</div>
fit-content´Â ¿ä¼ÒÀÇ Å©±â¸¦ ³»¿ë¹°¿¡ ¸ÂÃç Á¶ÀýÇϵÇ, max-width µîÀÇ Á¦ÇÑ¿¡ ÀÇÇØ Á¶ÀýµÉ ¼ö ÀÖ½À´Ï´Ù.
4. JavaScript·Î µ¿ÀûÀ¸·Î Å©±â Á¶Á¤
³»¿ë¹°ÀÌ ÀÚÁÖ º¯°æµÇ°Å³ª º¹ÀâÇÑ ·ÎÁ÷ÀÌ ÇÊ¿äÇÑ °æ¿ì JavaScript¸¦ »ç¿ëÇÏ¿© µ¿ÀûÀ¸·Î ¿ä¼ÒÀÇ ³Êºñ¸¦ Á¶Á¤ÇÒ ¼ö ÀÖ½À´Ï´Ù.
¿¹½Ã:
<div id="dynamicWidth" style="background-color: lightcoral;">
ÀÌ ÅؽºÆ®¿¡ µû¶ó ³Êºñ°¡ º¯°æµË´Ï´Ù.
</div>
<script>
const element = document.getElementById('dynamicWidth');
element.style.width = element.scrollWidth + 'px';
</script>
¿©±â¼´Â scrollWidth¸¦ »ç¿ëÇÏ¿© ÄÜÅÙÃ÷ÀÇ Àüü ³Êºñ¸¦ °¡Á®¿Í¼ ¿ä¼ÒÀÇ width·Î ¼³Á¤ÇÕ´Ï´Ù.
5. ÃÖ´ë ¹× ÃÖ¼Ò ³Êºñ ¼³Á¤
min-width ¹× max-width ¼Ó¼ºÀ» »ç¿ëÇÏ¿© ÄÜÅÙÃ÷¿¡ µû¶ó Å©±â°¡ Á¶Á¤µÇ´õ¶óµµ ÃÖ¼Ò ¹× ÃÖ´ë Å©±â¸¦ Á¦ÇÑÇÒ ¼ö ÀÖ½À´Ï´Ù.
¿¹½Ã:
<div style="display: inline-block; min-width: 100px; max-width: 300px; background-color: lightyellow;">
ÀÌ ¿ä¼Ò´Â ÃÖ¼Ò 100px, ÃÖ´ë 300pxÀÇ ³Êºñ¸¦ °¡Áý´Ï´Ù.
</div>
ÀÌ ¼Ó¼ºµéÀº ÄÜÅÙÃ÷ÀÇ Å©±â¿¡ µû¶ó ÀÚµ¿À¸·Î Á¶Á¤µÇÁö¸¸, ÁöÁ¤µÈ ¹üÀ§ ³»¿¡¼¸¸ Á¶Á¤µÇµµ·Ï ÇÕ´Ï´Ù. |
|
̵̧ : 28 |
̵̧
¸ñ·Ï
|
|
|  |
|