본문으둜 κ±΄λ„ˆλ›°κΈ°

CSS 속성 11. float-position

❗️ ν•΄λ‹Ή 글은 패슀트캠퍼슀 - ν”„λ‘ νŠΈμ—”λ“œ 개발 κ°•μ˜μ—μ„œ HTML & CSS, SASS(SCSS) Part의 λ°•μ˜μ›… κ°•μ‚¬λ‹˜μ˜ κ°•μ˜μžλ£Œλ₯Ό μ •λ¦¬ν•œ κ²ƒμž…λ‹ˆλ‹€.

float#

μš”μ†Œλ₯Ό 쒌우 λ°©ν–₯으둜 띄움(μˆ˜ν‰ μ •λ ¬)

☝️ CSS3μ—μ„œ Flexible box λΌλŠ” κ°œλ…μ΄ λ“±μž₯ν•˜λ©΄μ„œ μ΅œκ·Όμ—λŠ” float 보닀 Flexible box λ₯Ό μ΄μš©ν•œ μˆ˜ν‰ 정렬을 ν•˜λŠ” νŽΈμ΄λ‹€.


속성 κ°’#

κ°’μ˜λ―ΈκΈ°λ³Έκ°’
noneμš”μ†Œ 띄움 μ—†μŒnone
leftμ™Όμͺ½μœΌλ‘œ 띄움
right였λ₯Έμͺ½μœΌλ‘œ 띄움

λ‹¨μˆœ 띄움 μ‚¬μš©λ²•#

<article><div class="picture"></div>Lorem ipsum...dolore voluptatum!</article>
article .picture {width: 200px;height: 150px;background: tomato;float: left;margin-right: 20px;margin-bottom: 10px;}

2020-02-03-css-속성-11-float-position-image-0

float-left


μˆ˜ν‰ μ •λ ¬ μ‚¬μš©λ²•#

float 을 μ΄μš©ν•˜λ©΄ block μš”μ†Œλ₯Ό μˆ˜ν‰μœΌλ‘œ μ •λ ¬ν•˜λŠ” 것이 κ°€λŠ₯ν•˜λ‹€.

<div class="box">1</div><div class="box">2</div><div class="box">3</div>
.box {width: 150px;height: 100px;background: tomato;color: white;font-size: 30px;margin: 10px;float: left;}

2020-02-03-css-속성-11-float-position-image-1

float-horizon

float: right; 둜 λ³€κ²½ μ‹œ μŒ“μ΄λŠ” μˆœμ„œ λ˜ν•œ λ³€κ²½λœλ‹€.

2020-02-03-css-속성-11-float-position-image-2

float-horizon-right


float ν•΄μ œ#

float 속성이 적용된 μš”μ†Œμ˜ μ£Όμœ„λ‘œ λ‹€λ₯Έ μš”μ†Œλ“€μ΄ 흐λ₯΄κ²Œ λ˜λŠ”λ° 이λ₯Ό λ°©μ§€ν•˜κΈ° μœ„ν•΄ 속성을 ν•΄μ œν•΄μ•Ό ν•œλ‹€.

  1. ν˜•μ œ μš”μ†Œμ— clear: (left, right, both) μΆ”κ°€ν•˜μ—¬ ν•΄μ œ

  2. λΆ€λͺ¨ μš”μ†Œμ— overflow: (hidden, auto) μΆ”κ°€ν•˜μ—¬ ν•΄μ œ

  3. λΆ€λͺ¨ μš”μ†Œμ— clearfix 클래슀 μΆ”κ°€ν•˜μ—¬ ν•΄μ œ (μΆ”μ²œ!)


ν˜•μ œ μš”μ†Œμ—μ„œ ν•΄μ œ#

float 속성이 μΆ”κ°€λœ μš”μ†Œμ˜ λ‹€μŒ ν˜•μ œ μš”μ†Œμ— clear 속성 μΆ”κ°€

<div class="float-left"></div><div class="float-left"></div><div class="next"></div>
.float-left {width: 100px;height: 100px;background: tomato;float: left;}.next {clear: both;}

☝️ 항상 λ‹€μŒ μš”μ†Œλ₯Ό λ§Œλ“€μ–΄μ£Όμ–΄μ•Ό ν•œλ‹€λŠ” 문제점


λΆ€λͺ¨ μš”μ†Œμ—μ„œ ν•΄μ œ - overflow#

float 속성이 μΆ”κ°€λœ μš”μ†Œμ˜ λΆ€λͺ¨ μš”μ†Œμ— overflow 속성을 μΆ”κ°€ν•΄ 보이지 μ•Šκ²Œ ν•˜λŠ” 방법.

<div class="parent"><div class="child"></div><div class="child"></div></div>
.parent {overflow: hidden; /* or auto */}.child {float: left;}

☝️ λ‹€μŒ μš”μ†Œλ₯Ό λ§Œλ“€μ§€ μ•Šμ•„λ„ λ˜λŠ” μž₯점이 μžˆμ§€λ§Œ κ΄€λ ¨ μ—†λŠ” μ†μ„±μœΌλ‘œ ν•΄κ²°ν•˜λŠ” μΌμ’…μ˜ β€™νŽΈλ²•β€™μœΌλ‘œ μ‚¬μš©μ„ ꢌμž₯ν•˜μ§€ μ•ŠμŒ


λΆ€λͺ¨ μš”μ†Œμ—μ„œ ν•΄μ œ2 (μΆ”μ²œ!)#

float 속성이 μΆ”κ°€λœ μš”μ†Œμ˜ λΆ€λͺ¨ μš”μ†Œμ— 미리 μ§€μ •λœ clearfix 클래슀 μΆ”κ°€

<div class="parent clearfix"><div class="child"></div><div class="child"></div></div>

πŸ’‘ μ°Έκ³  : 가상 μš”μ†Œ μ„ νƒμž

.clearfix::after {content: "";clear: both;display: block; /* or table */}.child {float: left;}

display μˆ˜μ •#

inline μš”μ†Œμ˜ 경우 float 속성이 λΆ€μ—¬λ˜μ—ˆμ„ λ•Œ μžλ™μ μœΌλ‘œ block μš”μ†Œμ˜ νŠΉμ„±μ„ κ°–κ²Œ λœλ‹€. λ”°λΌμ„œ float 속성을 μ‚¬μš©ν•  λ•Œ display: block; 을 λ”°λ‘œ μ§€μ •ν• ν•„μš”κ°€ μ—†λ‹€.

지정값변화값
inlineblock
inline-blockblock
inline-tableblock
table-rowblock
table-row-groupblock
table-columnblock
table-column-groupblock
table-cellblock
table-captionblock
table-header-groupblock
table-footer-groupblock
flexflex / float 속성 νš¨κ³Όμ—†μŒ
inline-flexinline-flex / float 속성 νš¨κ³Όμ—†μŒ
κ·Έ μ™Έλ³€ν™”μ—†μŒ

☝️ λŒ€λΆ€λΆ„μ˜ 값이 block 으둜 λ³€κ²½λ˜λŠ”λ° flex , inline-flex 의 경우 λ³€κ²½λ˜μ§€ μ•ŠλŠ”λ‹€.


clear#

float 속성이 μ μš©λ˜μ§€ μ•Šλ„λ‘ 지정(ν•΄μ œ)

속성 κ°’#

κ°’μ˜λ―ΈκΈ°λ³Έκ°’
noneμš”μ†Œ 띄움 ν—ˆμš©none
leftμ™Όμͺ½ 띄움 ν•΄μ œ
right였λ₯Έμͺ½ 띄움 ν•΄μ œ
bothμ–‘μͺ½(μ™Όμͺ½, 였λ₯Έμͺ½) λͺ¨λ‘ 띄움 ν•΄μ œ

position#

μš”μ†Œμ˜ μœ„μΉ˜ 지정 λ°©λ²•μ˜ μœ ν˜•(κΈ°μ€€)을 μ„€μ •


속성 κ°’#

κ°’μ˜λ―ΈκΈ°λ³Έκ°’
staticμœ ν˜•(κΈ°μ€€) μ—†μŒ / 배치 λΆˆκ°€λŠ₯static
relativeμš”μ†Œ μžμ‹ μ„ κΈ°μ€€μœΌλ‘œ 배치
absoluteμœ„μΉ˜ 상 λΆ€λͺ¨ μš”μ†Œλ₯Ό κΈ°μ€€μœΌλ‘œ 배치
fixedλΈŒλΌμš°μ €(뷰포트)λ₯Ό κΈ°μ€€μœΌλ‘œ 배치
sticky슀크둀 μ˜μ—­ κΈ°μ€€μœΌλ‘œ 배치

relative#

μ›λž˜ μœ„μΉ˜ν•΄μ•Ό ν•  μœ„μΉ˜μ— λŒ€ν•œ μƒλŒ€μ μΈ μœ„μΉ˜

<div class="box">1</div><div class="box relative">2</div><div class="box">3</div>
.box {width: 100px;height: 100px;background: tomato;border: 4px dashed red;border-radius: 20px;display: flex;justify-content: center;align-items: center;font-size: 30px;}.relative {position: relative;top: 100px;left: 30px;}

☝️ relative 속성값을 가진 μš”μ†ŒλŠ” 자기 μžμ‹ μ΄ μ›λž˜ μžˆμ—ˆλ˜ μœ„μΉ˜μ—μ„œ μ£Όλ³€ μš”μ†Œμ—μ˜ν–₯을 μ€€λ‹€.


absolute#

<div class="grand-parent"><div class="parent"><div class="child">1</div><div class="child absolute">2</div><div class="child">3</div></div></div>
.grand-parent {width: 400px;height: 300px;padding: 30px 100px 100px 30px;border: 4px dashed lightgray;}.parent {width: 400px;height: 300px;border: 4px dashed gray;position: relative;}.child {width: 120px;height: 80px;background: tomato;border: 4px dashed red;border-radius: 10px;font-size: 30px;display: flex;justify-content: center;align-items: center;}.absolute {position: absolute;bottom: 50px;right: 50px;}

2020-02-03-css-속성-11-float-position-image-3

position-absolute

πŸ’‘ μœ„μΉ˜μƒ λΆ€λͺ¨ μš”μ†Œ - 쑰상 μš”μ†Œ 쀑 position 속성이 λΆ€μ—¬λ˜μ–΄ μžˆλŠ” μš”μ†Œ ( static μ œμ™Έ )쑰상 μš”μ†Œμ— μ—†λ‹€λ©΄ -> body (일반적으둜 μ—†μŒ) -> html (일반적으둜 μ—†μŒ) -> window 객체 (뷰포트)


fixed#

λΈŒλΌμš°μ €(뷰포트)λ₯Ό κΈ°μ€€μœΌλ‘œ 함 - μŠ€ν¬λ‘€μ„ 내렀도 ν•΄λ‹Ή μœ„μΉ˜μ— 고정됨.


sticky#

슀크둀 μ˜μ—­ κΈ°μ€€μœΌλ‘œ 배치

.box {position: sticky;top: 0;}

☝️ top , bottom , left , right 쀑 ν•˜λ‚˜ μ΄μƒμ˜ μš”μ†Œ μ‚¬μš©.IE 지원 λΆˆκ°€.


μš”μ†Œ μŒ“μž„ μˆœμ„œ(Stack order)#

μš”μ†Œκ°€ μŒ“μ—¬ μžˆλŠ” μˆœμ„œλ₯Ό 톡해

μ–΄λ–€ μš”μ†Œκ°€ μ‚¬μš©μžμ™€ κ°€κΉκ²Œ μžˆλŠ”μ§€(더 μœ„μ— μŒ“μ΄λŠ”μ§€)λ₯Ό κ²°μ • (ZμΆ•)

  1. static 을 μ œμ™Έν•œ position μ†μ„±μ˜ 값이 μžˆμ„ 경우 κ°€μž₯ μœ„μ— μŒ“μž„(값은 무관)

  2. position 이 λͺ¨λ‘ μ‘΄μž¬ν•œλ‹€λ©΄ z-index μ†μ„±μ˜ 숫자 값이 높을 수둝 μœ„μ— μŒ“μž„

  3. position μ†μ„±μ˜ 값이 있고, z-index μ†μ„±μ˜ 숫자 값이 κ°™λ‹€λ©΄, β€˜HTML’ 의 λ§ˆμ§€λ§‰ μ½”λ“œμΌμˆ˜λ‘ μœ„μ— μŒ“μž„(λ‚˜μ€‘μ— μž‘μ„±ν•œ μ½”λ“œ(μš”μ†Œ))

position > z-index > HTML λ§ˆμ§€λ§‰ μ½”λ“œ

☝️ z-index λŠ” position 속성이 μ„€μ •λœ μš”μ†Œμ— λŒ€ν•΄μ„œλ§Œ 의미λ₯Ό κ°–λŠ”λ‹€.


display μˆ˜μ •#

float 속성과 λΉ„μŠ·ν•˜κ²Œ absolute , fixed 속성 값이 적용된 μš”μ†ŒλŠ” display μ†μ„±μ˜ 값이 λŒ€λΆ€λΆ„ block 으둜 μˆ˜μ •λœλ‹€.

지정값변화값
inlineblock
inline-blockblock
inline-tableblock
table-rowblock
table-row-groupblock
table-columnblock
table-column-groupblock
table-cellblock
table-captionblock
table-header-groupblock
table-footer-groupblock
flexflex / float 속성 νš¨κ³Όμ—†μŒ
inline-flexinline-flex / float 속성 νš¨κ³Όμ—†μŒ
κ·Έ μ™Έλ³€ν™”μ—†μŒ

top , bottom , left , right#

μš”μ†Œμ˜ position 기쀀에 λ§žλŠ” β€˜μœ„μͺ½β€™, β€˜μ•„λž˜μͺ½β€™, β€˜μ™Όμͺ½β€™, β€™μ˜€λ₯Έμͺ½β€™μ—μ„œμ˜ 거리(μœ„μΉ˜ )λ₯Ό μ„€μ •

κ°’μ˜λ―ΈκΈ°λ³Έκ°’
autoλΈŒλΌμš°μ €κ°€ 계산
λ‹¨μœ„px , em , cm λ“± λ‹¨μœ„λ‘œ 지정2020-02-03 00:00:00
%λΆ€λͺ¨(μœ„μΉ˜ μƒμ˜ λΆ€λͺ¨(쑰상)) μš”μ†Œμ˜ κ°€λ‘œ λ˜λŠ” μ„Έλ‘œ λ„ˆλΉ„μ˜ λΉ„μœ¨λ‘œ 지정, 음수 κ°’ ν—ˆμš©

☝️ μœ„μ˜ λ„€ μš”μ†ŒλŠ” position 속성이 μ§€μ •λ˜μ–΄ μžˆμ„ λ•Œλ§Œ μ‚¬μš©ν•  수 μžˆλ‹€.


Reference#

패슀트캠퍼슀 - ν”„λ‘ νŠΈμ—”λ“œ 개발 κ°•μ˜ - HTML & CSS, SASS(SCSS) Part by ParkYoungWoong