1차원 레이아웃
유연한 크기 조정
정렬 및 배치
정렬 속성
const UserProfileMainContainer = styled.main`
display: flex;
`;
const UserProfileMainProfile = styled.section`
flex: 2.5;
`;
const UserProfileMainIntroduce = styled.article`
flex: 4.5;
`;
const UserProfileMainLevel = styled.aside`
flex: 3;
`;
const UserProfileSubContainer = styled.div`
display: flex;
flex: 1;
`;
const UserProfileStudy = styled.section`
flex: 2.6;
`;
const UserProfileWritePost = styled.section`
flex: 7.4;
`;
display: flex
를 설정하면, 이 요소의 자식들이 flex 컨테이너 안에서 배치됩니다. 자식 요소들은 자동으로 flex 아이템이 되며, 한 줄 혹은 여러 줄로 나란히 배치됩니다.flex: <숫자>;
flex: 1;
은 아이템이 flex 컨테이너 안에서 동일한 비율로 공간을 차지하겠다는 의미입니다. UserProfileSubContainer가 그 예입니다. 다른 아이템들과 비슷한 공간을 차지하게 됩니다.<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flexbox 예제</title>
<style>
.container {
display: flex;
justify-content: center; /* 수평 중앙 정렬 */
align-items: center; /* 수직 중앙 정렬 */
height: 100vh; /* 전체 화면 높이 */
}
.item {
flex: 1; /* 모든 아이템이 동일한 크기로 확장 */
margin: 10px;
padding: 20px;
background-color: #4CAF50;
color: white;
text-align: center;
}
</style>
</head>
<body>
<div class="container">
<div class="item">아이템 1</div>
<div class="item">아이템 2</div>
<div class="item">아이템 3</div>
</div>
</body>
</html>
2차원 레이아웃
, 정확한 위치 지정
,반응형 디자인
, 간격 조정
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Grid 예제</title>
<style>
.container {
display: grid;
grid-template-columns: repeat(3, 1fr); /* 3개의 열을 동일한 비율로 설정 */
gap: 10px; /* 요소 간의 간격 */
padding: 20px;
}
.item {
background-color: #2196F3;
color: white;
text-align: center;
padding: 20px;
}
.item:nth-child(2) {
grid-column: span 2; /* 2개의 열을 차지 */
}
</style>
</head>
<body>
<div class="container">
<div class="item">아이템 1</div>
<div class="item">아이템 2</div>
<div class="item">아이템 3</div>
<div class="item">아이템 4</div>
<div class="item">아이템 5</div>
<div class="item">아이템 6</div>
</div>
</body>
</html>
특성 | Flexbox | Grid |
---|---|---|
차원 | 1차원 레이아웃 | 2차원 레이아웃 |
사용 용도 | 간단한 레이아웃 및 요소 정렬 | 복잡한 레이아웃 구성 |
정렬 방식 | 요소의 크기와 정렬을 유연하게 조정 | 각 요소의 위치를 명확하게 지정 |
간격 조정 | margin을 사용 | grid-gap 속성 사용 |
반응형 디자인 | 유연한 크기 조정 | 비율 기반의 레이아웃 구성 가능 |
Flex랑 Grid 둘 다 웹 개발할 때 핵심이 되는 기술입니다! 상황에 따라 잘 골라 쓰는 게 관건입니다. 간단하게 정렬하고 싶다면 Flex로 가고, 복잡하고 디테일한 레이아웃을 짜야 한다면 Grid가 찰떡인 것 같습니다.. 진짜 개발할 때 Flex와 Grid를 적재적소에 잘 활용하면 웹 디자인 고수가 될 것 같습니다.. 결국, 뭐든 상황에 맞는 선택이 중요하다는 것!