Style Sheet/CSS

🎨 float 속성 μ‚¬μš©λ²• ν•œλ°© 정리

인파_ 2021. 10. 4. 19:00

css-float

CSS Float

float ν”„λ‘œνΌν‹°λŠ” ν•΄λ‹Ή μš”μ†Œλ₯Ό λ‹€μŒ μš”μ†Œ μœ„μ— λ–  있게 ν•œλ‹€. μ—¬κΈ°μ„œ λ–  μžˆλ‹€(float)λŠ” μ˜λ―ΈλŠ” μš”μ†Œκ°€ κΈ°λ³Έ λ ˆμ΄μ•„μ›ƒ νλ¦„μ—μ„œ λ²—μ–΄λ‚˜ μš”μ†Œμ˜ λͺ¨μ„œλ¦¬κ°€ νŽ˜μ΄μ§€μ˜ μ™Όμͺ½μ΄λ‚˜ 였λ₯Έμͺ½μ— μ΄λ™ν•˜λŠ” 것이닀. κ·Έλž˜μ„œ λ³΄ν†΅ λ ˆμ΄μ•„μ›ƒμ„ ꡬ성할 λ•Œ μš”μ†Œλ₯Ό κ°€λ‘œ μ •λ ¬ν•˜κΈ° μœ„ν•΄ μ‚¬μš©λ˜λŠ” 기법이닀. 예λ₯Ό λ“€μ–΄, λ¬Έμ„œμ— 사진과 그림이 μžˆμ„ λ•Œ, 그림을 μ™Όμͺ½μ΄λ‚˜ 였λ₯Έμͺ½μœΌλ‘œ λ„μ›Œμ„œ μ •λ ¬ ν•˜κ±°λ‚˜ 각 객체λ₯Ό 였λ₯Έμͺ½μ΄λ‚˜ μ™Όμͺ½μœΌλ‘œ μ •λ ¬ν•˜μ—¬ 전체 λ¬Έμ„œλ₯Ό 배치(layout)ν•  λ•Œλ„ μ‚¬μš©ν•  수 μžˆλ‹€


float 속성

ν”„λ‘œνΌν‹°κ°’ Description
none μš”μ†Œλ₯Ό λ–  있게 ν•˜μ§€ μ•ŠλŠ”λ‹€. (κΈ°λ³Έκ°’)
right μš”μ†Œλ₯Ό 였λ₯Έμͺ½μœΌλ‘œ μ΄λ™μ‹œν‚¨λ‹€
left μš”μ†Œλ₯Ό μ™Όμͺ½μœΌλ‘œ μ΄λ™μ‹œν‚¨λ‹€.

μ‚¬μš©λ²•μ€ κ°„λ‹¨ν•˜λ‹€. λΆ€μœ ν•˜κ²Œ ν•˜κ³  싢은 μš”μ†Œμ— float: left ν”„λ‘œνΌν‹°λ₯Ό μ‚¬μš©ν•˜λ©΄ μ™Όμͺ½λΆ€ν„° κ°€λ‘œ μ •λ ¬λ˜κ³ , float: right ν”„λ‘œνΌν‹°λ₯Ό μ‚¬μš©ν•˜λ©΄ 였λ₯Έμͺ½λΆ€ν„° κ°€λ‘œ μ •λ ¬λœλ‹€.

img {
    float: left;
}

float μ·¨μ†Œ

 

clear 속성

CSS의 clear 속성은 λ³„λ„μ˜ μ†μ„±μœΌλ‘œμ„œ, μ£Όμš” κΈ°λŠ₯은 float 속성이 적용된 μš”μ†Œ λ‹€μŒμ— μœ„μΉ˜ν•˜λŠ” μš”μ†Œλ“€μ΄ float 속성이 적용된 μš”μ†Œμ™€ κ²ΉμΉ˜λŠ” ν˜„μƒμ„ λ°©μ§€ν•˜κΈ° μœ„ν•΄ μ‚¬μš©λœλ‹€. μš”μ†Œμ— float 속성이 적용되면 κ·Έ 이후에 λ“±μž₯ν•˜λŠ” λͺ¨λ“  μš”μ†Œλ“€μ€ μ •ν™•ν•œ μœ„μΉ˜λ₯Ό μ„€μ •ν•˜κΈ°κ°€ 맀우 νž˜λ“€μ–΄ μ§€κ²Œ λ˜λŠ”λ°, λ”°λΌμ„œ clear 속성을 μ‚¬μš©ν•˜μ—¬, μ΄ν›„μ˜ μš”μ†Œλ“€μ΄ λ”λŠ” float 속성에 영ν–₯을 받지 μ•Šλ„λ‘ μ„€μ •ν•΄μ£Όμ–΄ μš”μ†Œλ“€μ΄ float 속성이 적용된 μš”μ†Œ μ•„λž˜μ— μœ„μΉ˜ν•˜λ„λ‘ ν•œλ‹€.

clear 속성에도 float 속성과 같이 left, right, both, none 값이 μžˆλ‹€.

ν”„λ‘œνΌν‹°κ°’ Description
none clearλ₯Ό μ„€μ •ν•˜μ§€ μ•Šμ€ κΈ°λ³Έκ°’.
μš”μ†Œκ°€ μ–΄λŠ μͺ½μœΌλ‘œλ“  λΆ€μœ ν•  수 μžˆλ‹€.
right float: right λ₯Ό μ·¨μ†Œμ‹œν‚΄
μš”μ†Œκ°€ μ™Όμͺ½μ— λΆ€μœ ν•œ μš”μ†Œ λ‹€μŒμ— μœ„μΉ˜ν•˜λ„λ‘ ν•œλ‹€. 
left float :left λ₯Ό μ·¨μ†Œμ‹œν‚΄
μš”μ†Œκ°€ 였λ₯Έμͺ½μ— λΆ€μœ ν•œ μš”μ†Œ λ‹€μŒμ— μœ„μΉ˜ν•˜λ„λ‘ ν•œλ‹€.
both float:left , float:right λ‘˜λ‹€ μ·¨μ†Œμ‹œν‚΄
μš”μ†Œκ°€ μ™Όμͺ½κ³Ό 였λ₯Έμͺ½μ— λΆ€μœ ν•œ μš”μ†Œ λ‹€μŒμ— μœ„μΉ˜ν•˜λ„λ‘ ν•œλ‹€. 

 

예λ₯Όλ“€μ–΄ μ•„λž˜ μ˜ˆμ œμ™€ 같이 이미지에 float: right 속성을 주게되면 μ΄λ―Έμ§€λŠ” 였λ₯Έμͺ½μ— 배치되고 κ·Έ λ‹€μŒ 문단 μš”μ†ŒλŠ” μžλ™μœΌλ‘œ μ™Όμͺ½μ— 배치되게 λœλ‹€.

<div>
  <img src="https://taegon.kim/wp-content/uploads/2018/05/image-5.png">
</div>
<p>
  Lorem Ipsum is simply dummy text of ...
</p>
img {
  float: right;
  width: 25%;
}

float-clear

만일 문단 μ˜μ—­μ„ 이미지 μ•„λž˜μ— μœ„μΉ˜ν•˜λ„λ‘ ν•˜κ³  μ‹Άλ‹€λ©΄ float: right λ₯Ό μ·¨μ†Œμ‹œν‚€λŠ” clear: left λ₯Ό 지정 ν•΄μ£Όλ©΄ λœλ‹€.

img {
  float: right;
  width: 25%;
}

p {
  clear: right; // p νƒœκ·ΈλŠ” float속성에 영ν–₯을 받지 μ•ŠμŒ
}

float-clear


float μ‚¬μš©μ‹œ μ£Όμ˜μ‚¬ν•­

float ν”„λ‘œνΌν‹°λ₯Ό μ‚¬μš©ν•  λ•Œ μš”μ†Œμ˜ μœ„μΉ˜λ₯Ό κ³ μ •μ‹œν‚€λŠ” position ν”„λ‘œνΌν‹°μ˜ absoluteλ₯Ό μ‚¬μš©ν•˜λ©΄ μ•ˆλœλ‹€. float 속성이 relativeν•œ μœ„μΉ˜ 지정을 ν•˜κΈ° λ•Œλ¬Έμ— position: absolute μ†μ„±μ΄ μ μš©λ˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ΄λ‹€.

λ˜ν•œ float 속성을 μ‚¬μš©ν•˜λ©΄ ν•΄λ‹Ή μš”μ†ŒλŠ” 일반적인 νλ¦„μ—μ„œ λ²—μ–΄λ‚˜κ²Œ λ˜μ–΄ μš”μ†Œμ˜ λΆ€λͺ¨ μš”μ†ŒλŠ” ν•΄λ‹Ή μš”μ†Œμ˜ 높이λ₯Ό μΈμ‹ν•˜μ§€ λͺ»ν•˜κ²Œ λ˜λŠ”λ°, 이 경우 λΆ€λͺ¨ μš”μ†Œμ— overflow: hidden 속성을 μΆ”κ°€ν•˜μ—¬ ν•΄κ²°ν•  수 μžˆλ‹€.


float 속성을 μ΄μš©ν•œ λ ˆμ΄μ•„μ›ƒ

float 속성을 μ΄μš©ν•˜λ©΄ μš”μ†Œλ“€μ„ 쒌우둜 λΆ€μœ μ‹œμΌœμ„œ λ°°μΉ˜ν•  수 있기 λ•Œλ¬Έμ— μ˜ˆμ „μ—λŠ” float 속성을 μ΄μš©ν•˜μ—¬ λ ˆμ΄μ•„μ›ƒμ„ κ΅¬μ„±ν•˜λŠ” 것이 일반적 μ΄μ—ˆλ‹€. ν•˜μ§€λ§Œ float 속성은 μš”μ†Œλ“€μ΄ λΆ€μœ ν•˜λ©΄μ„œ λ‹€λ₯Έ μš”μ†Œλ“€κ³Ό κ²ΉμΉ˜κΈ°λ„ ν•˜κ³ , 높이λ₯Ό μžλ™μœΌλ‘œ κ³„μ‚°ν•˜μ§€ λͺ»ν•˜λŠ” λ“±μ˜ 문제점이 μžˆμ–΄μ„œ, ν˜„μž¬λŠ” flexboxλ‚˜ CSS Grid와 같은 λ ˆμ΄μ•„μ›ƒ κΈ°μˆ μ„ μ΄μš©ν•˜μ—¬ λ”μš± 효율적이고 λ‹€μ–‘ν•œ λ ˆμ΄μ•„μ›ƒμ„ κ΅¬μ„±ν•˜κ³  μžˆμ–΄ μ°Έκ³  μ •λ„λ‘œλ§Œ 보자

<h1>float 속성을 μ΄μš©ν•œ λ ˆμ΄μ•„μ›ƒ</h1>
<div class="page">

    <header>
        <h2>header μ˜μ—­</h2>
    </header>
    <nav>
        <h2>nav μ˜μ—­</h2>
        <p>μ—¬κΈ°μ—λŠ” 보톡 메뉴가 λ“€μ–΄κ°‘λ‹ˆλ‹€.</p>
    </nav>
    <section>
        <h2>section μ˜μ—­</h2>
        <p>μ—¬κΈ°μ—λŠ” νŽ˜μ΄μ§€μ— ν•΄λ‹Ήν•˜λŠ” λ‚΄μš©μ΄ λ“€μ–΄κ°‘λ‹ˆλ‹€.<br>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam ornare sapien suscipit tincidunt ullamcorper. Cras ac sem sed mauris maximus rhoncus vel in metus. Nam pharetra arcu sit amet dolor interdum, eget scelerisque libero finibus. Phasellus quis vulputate ante. Fusce sit amet viverra justo. Donec id elementum mauris. Nam id porttitor nisl, et suscipit nunc. Vestibulum sit amet volutpat quam. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Duis placerat sem eu facilisis ultricies.
        </p>
    </section>
    <footer>
        <h2>footer μ˜μ—­</h2>
    </footer>
</div>
div.page {
    border: 3px solid #CD5C5C;
    overflow: auto;
}

h2 { text-align: center; }

header { border: 3px solid #FFD700; }

nav {
    border: 3px solid #FF1493;
    width: 150px;
    float: left; // λ„€λΉ„κ²Œμ΄μ…˜μ€ μ™Όμͺ½ μœ„μΉ˜
}

section {
    border: 3px solid #00BFFF;
    margin-left: 156px;
}

footer{ border: 3px solid #00FA9A; }

See the Pen float layout by barzz12 (@inpaSkyrim) on CodePen.


# 참고자료

https://tcpschool.com/css/css_position_float

https://css-tricks.com/all-about-floats/

https://poiemaweb.com/css3-float