μΉ νμ΄μ§λ₯Ό λμμΈν λ κ° μμλ₯Ό μ ννκ² flat μ λ ¬μ νλ κ²μ λ§€μ° μ€μν©λλ€. νΉν, κ°λ‘ λ° μΈλ‘ λ°©ν₯ λͺ¨λμμ μμλ₯Ό μ€μμ λ°°μΉνλ©΄ μ¬μ©μμκ² κ· ν μ‘ν λ μ΄μμμ μ 곡ν©λλ€. μ΄λ² κΈμμλ CSSμ float
μμ±μ νμ©νμ¬ μμλ₯Ό κ°λ‘ λ° μΈλ‘ λ°©ν₯μΌλ‘ μ€μ μ λ ¬νλ λ°©λ²μ μμλ³΄κ² μ΅λλ€.
![[CSS] float μ λ ¬ κ°μ΄λ° κ°λ‘ μΈλ‘ μ€μ μμ± μ¬μ© λ°©λ²](http://t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png)
λͺ©μ°¨
float μμ±μ νκ³μ λμ
λ¨Όμ , float
μμ±μ μμλ₯Ό μΌμͺ½μ΄λ μ€λ₯Έμͺ½μΌλ‘ λ λ€λκ² λ°°μΉνλ λ° μ μ©νμ§λ§, μμλ₯Ό μ νν κ°μ΄λ°μ λ°°μΉνλ λ°λ μ μ½μ΄ μμ΅λλ€. λ°λΌμ, κ°λ‘ λ° μΈλ‘ μ€μ μ λ ¬μ ꡬννκΈ° μν΄μλ λ€λ₯Έ CSS μμ±μ ν¨κ» μ¬μ©ν΄μΌ ν©λλ€.
float κ°λ‘ μ€μ μ λ ¬ λ°©λ²
1. text-align: center
μ¬μ©
λΆλͺ¨ μμμ text-align: center
λ₯Ό μ μ©νλ©΄ λ΄λΆμ μΈλΌμΈ λλ μΈλΌμΈ-λΈλ‘ μμλ€μ΄ κ°λ‘ λ°©ν₯μΌλ‘ μ€μμ μ λ ¬λ©λλ€. μλ₯Ό λ€μ΄:
<div class="parent">
<div class="child">λ΄μ©</div>
</div>
.parent {
text-align: center;
}
.child {
display: inline-block;
}
μ΄λ κ² νλ©΄ .child
μμκ° .parent
μμ λ΄μμ κ°λ‘λ‘ μ€μμ λ°°μΉλ©λλ€.
2. margin: 0 auto
μ¬μ©
λΈλ‘ λ 벨 μμμ κ²½μ°, margin: 0 auto
λ₯Ό μ¬μ©νμ¬ κ°λ‘ μ€μ μ λ ¬μ ν μ μμ΅λλ€. μ΄λ, μμμ λλΉ(width
)λ₯Ό λͺ
μμ μΌλ‘ μ§μ ν΄μΌ ν©λλ€.
.child {
width: 50%;
margin: 0 auto;
}
μ΄λ κ² μ€μ νλ©΄ .child
μμλ λΆλͺ¨ μμ λ΄μμ κ°λ‘λ‘ μ€μμ λ°°μΉλ©λλ€.
float μΈλ‘ μ€μ μ λ ¬ λ°©λ²
1. line-height
μ¬μ©
ν
μ€νΈκ° ν μ€μΈ κ²½μ°, line-height
λ₯Ό μ¬μ©νμ¬ μΈλ‘ μ€μ μ λ ¬μ ν μ μμ΅λλ€. μμμ λμ΄(height
)μ λμΌν κ°μ line-height
μ μ§μ νλ©΄ ν
μ€νΈκ° μΈλ‘λ‘ μ€μμ λ°°μΉλ©λλ€.
.child {
height: 100px;
line-height: 100px;
text-align: center;
}
μ΄ λ°©λ²μ ν μ€νΈκ° ν μ€μΌ λλ§ ν¨κ³Όμ μ λλ€.
2. display: table-cell
μ¬μ©
μ¬λ¬ μ€μ ν
μ€νΈλ λΈλ‘ μμλ₯Ό μΈλ‘λ‘ μ€μ μ λ ¬νλ €λ©΄ display: table-cell
κ³Ό vertical-align: middle
μ μ‘°ν©νμ¬ μ¬μ©ν μ μμ΅λλ€.
<div class="parent">
<div class="child">λ΄μ©</div>
</div>
.parent {
display: table;
width: 100%;
height: 200px;
}
.child {
display: table-cell;
vertical-align: middle;
text-align: center;
}
μ΄λ κ² νλ©΄ .child
μμκ° .parent
μμ λ΄μμ μΈλ‘λ‘ μ€μμ λ°°μΉλ©λλ€.
float κ°λ‘ λ° μΈλ‘ μ€μ μ λ ¬ λ°©λ²
1. position
κ³Ό transform
μ¬μ©
position
κ³Ό transform
μμ±μ μ‘°ν©νμ¬ μμλ₯Ό κ°λ‘ λ° μΈλ‘ λͺ¨λ μ€μμ λ°°μΉν μ μμ΅λλ€.
<div class="parent">
<div class="child">λ΄μ©</div>
</div>
.parent {
position: relative;
width: 100%;
height: 200px;
}
.child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
μ΄λ κ² μ€μ νλ©΄ .child
μμλ .parent
μμμ μ νν μ€μμ λ°°μΉλ©λλ€.
float μ λ ¬ μμ μ½λ
μλλ μμμ μ€λͺ ν λ°©λ²λ€μ μ μ©ν μ 체 HTMLκ³Ό CSS μ½λμ λλ€.
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS μ€μ μ λ ¬ μμ </title>
<style>
.parent {
position: relative;
width: 100%;
height: 200px;
border: 1px solid #000;
}
.child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
padding: 20px;
background-color: #f0f0f0;
border: 1px solid #ccc;
}
</style>
</head>
<body>
<div class="parent">
<div class="child">κ°λ‘ λ° μΈλ‘ μ€μμ λ°°μΉλ μμ</div>
</div>
</body>
</html>
μ μ½λλ₯Ό λΈλΌμ°μ μμ μ΄λ©΄ .child
μμκ° .parent
μμμ κ°λ‘ λ° μΈλ‘ μ€μμ μ ννκ² λ°°μΉλ κ²μ νμΈν μ μμ΅λλ€.

CSSμμ float
μμ±λ§μΌλ‘λ μμλ₯Ό κ°λ‘ λ° μΈλ‘ λ°©ν₯μΌλ‘ μ ννκ² μ€μ μ λ ¬νκΈ° μ΄λ ΅μ΅λλ€. λ°λΌμ, text-align
, margin
, line-height
, display
, position
, transform
λ±μ μμ±μ μ μ ν μ‘°ν©νμ¬ μνλ λ μ΄μμμ ꡬννλ κ²μ΄ μ€μν©λλ€. μ΄λ¬ν λ°©λ²λ€μ μμ§νλ©΄ λ€μν μν©μμ μ μ°νκ² μ€μ μ λ ¬μ μ μ©ν μ μμ΅λλ€.
μΆμ²κΈ
CSS μ νμ(Selector) κ°λ μ 리μ μμ νμ© λ°©λ²
CSS(Cascading Style Sheets)λ HTML μμμ μ€νμΌμ μ§μ νλ λ° μ¬μ©λ©λλ€. κ·Έμ€μμλ 'μ νμ(Selector)'λ νΉμ μμλ₯Ό μ ννκ³ μ€νμΌμ μ μ©νλ ν΅μ¬μ μΈ μν μ ν©λλ€. μ΄λ² ν¬μ€ν μμλ CSS
findview.tistory.com
'μΉκ°λ° > CSS' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
CSS μ νμ(Selector) κ°λ μ 리μ μμ νμ© λ°©λ² (0) | 2025.01.17 |
---|