작업을 하면서 보통은 flex를 사용하지만, 때에 따라 margin을 사용해 정렬이 필요한 경우 막상 사용하려면 기억이 나지 않아서 기록해두려한다. 😅
<!Doctype ~>
선언이 제대로 되었는지 확인margin: 0 auto;
가 반응하지 않을 시 부모의 넓이 값이 제대로 설정되어 있는지 확인<span></span>
-> display: block;
or <span></span>
-> <div></div>
fixed
또는 absolute
사용 시 margin으로 가운데 정렬position: fixed;
left: 0;
right: 0;
margin: 0 auto; // 상황에 따라 상하단 값을 설정해 주어도 무방합니다.
z-index
값이나 width
값을 추가해 주는 것도 가능합니다.<head>
<style>
.contents {
position: fixed;
left: 0;
right: 0;
margin: 0 auto;
}
<style>
</head>
<body>
<div class="container">
<div class="contents"></div>
</div>
</body>
display: flex;
를 적용justify-content: center;
적용// css
.parent {
display: flex;
justify-content: center;
}
.child {
width: 50px;
height: 50px;
background-color: orange;
}
// html
<div class="parent">
<div class="child"></div>
</div>
align-items: center;
을 적용// css
.parent {
display: flex;
align-items: center;
height: 300px;
background-color: grey;
}
.child {
width: 50px;
height: 50px;
background-color: orange;
}
// html
<div class="parent">
<div class="child"></div>
</div>