요소를 부모 안에서 가로·세로 가운데에 놓는 방법은 여러 가지이고, 상황(레이아웃 방식, 크기를 아는지)에 따라 고른다.
/* 1. Flexbox: 가장 무난 */
.parent { display: flex; justify-content: center; align-items: center; }
/* 2. Grid: 한 줄 */
.parent { display: grid; place-content: center; }
/* 3. 가로만, 블록 요소 */
.child { max-width: fit-content; margin-inline: auto; }
/* 4. 겹쳐 띄우기(모달, 배너) */
.child { position: fixed; inset: 0; margin: auto; width: 12rem; height: fit-content; }- Flexbox: 컨테이너에 정렬을 주거나, 아이템에
margin: auto를 줘도 된다 - CSS Grid:
place-content는align-content와justify-content의 단축이다. 아이템 자체를 칸 안에서 가운데 두려면place-items: center를 쓴다 margin-inline: auto:margin-left/right: auto의 논리 속성(Logical Property) 버전이다. 너비가 정해져 있어야 효과가 있어서, 콘텐츠 너비에 맞추려면fit-content와 함께 쓴다inset: 0+margin: auto: 네 변을 모두 0에 붙이고 크기를 제한하면 남는 공간을 margin이 나눠 가진다 → inset 속성.bottom만 띄우고 좌우를 0으로 두면 화면 아래 가운데에 붙는 쿠키 배너가 된다