front 수업을 듣고, 간단한 플젝을 해보며 느낀점 중 하나가 display랑 position에 대한 확실한 이해가 필요하다는 것.
해당 요소를 block이나 inline요소 중 어떤 방식으로 처리할지 그리고 자식 요소의 배치를 flow, grid, flex와 같은 방식 중 어떤 레이아웃으로 배치할 지 정하는 CSS 속성이다.
요소의 내부와 외부 display 유형으로 나뉜다.
1. 외부 : 플로우 레이아웃에 요소가 참여하는 방법 -> 자신 자체의 유형을 나타낸다. (block, inline 과 같은)
2. 내부 : 자식의 레이아웃 방식을 설정
키워드
1. block
2. inline
3. inline-block
4. none
5. flex
6. grid
7. table
block
div, h1~h6, p, ol, ul, li, hr, table, form
inline
span, a, strong, img, br, input, select, textarea, button
inline-block
none
flex
<!DOCTYPE html>
<html>
<head>
<title>Flex Example</title>
<style>
.container {
display: flex;
justify-content: space-between; /* 아이템들을 컨테이너 양 끝에 정렬 */
align-items: center; /* 아이템들을 수직 가운데로 정렬 */
height: 200px; /* 컨테이너의 높이 설정 */
background-color: #f0f0f0;
}
.item {
width: 50px;
height: 50px;
background-color: #ffcc00;
margin: 5px;
}
</style>
</head>
<body>
<div class="container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
</body>
</html>
grid
<!DOCTYPE html>
<html>
<head>
<title>Grid Example</title>
<style>
.container {
display: grid;
grid-template-columns: 100px 100px; /* 두 개의 열로 정의 */
grid-template-rows: 50px 50px; /* 두 개의 행으로 정의 */
grid-gap: 10px; /* 열과 행 사이의 간격을 10px로 설정 */
}
.item {
background-color: #ffcc00;
padding: 10px;
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 class="item">4</div>
</div>
</body>
</html>
table
<!DOCTYPE html>
<html>
<head>
<title>Table Layout Example</title>
<style>
.table {
display: table;
width: 100%;
border: 1px solid #ddd;
}
.row {
display: table-row;
}
.cell {
display: table-cell;
padding: 8px;
border: 1px solid #ddd;
}
</style>
</head>
<body>
<div class="table">
<div class="row">
<div class="cell">Header 1</div>
<div class="cell">Header 2</div>
<div class="cell">Header 3</div>
</div>
<div class="row">
<div class="cell">Data 1</div>
<div class="cell">Data 2</div>
<div class="cell">Data 3</div>
</div>
<div class="row">
<div class="cell">Data 4</div>
<div class="cell">Data 5</div>
<div class="cell">Data 6</div>
</div>
</div>
</body>
</html>
문서 상에 요소를 배치하는 방법을 지정한다.
키워드
1. static
2. relative
3. absolute
4. fixed
5. sticky
static
relative
absolute
fixed
sticky
fixed, sticky 차이
position: fixed
position: sticky
position
mdn
좋은 글 감사합니다. 자주 올게요 :)