[CSS] 원하는 요소 중앙에 배치하는 방법

2022. 4. 13. 16:58·Stack/CSS

position을 이용해 요소의 중앙에 배치하기

1. top: 50% / left: 50% / transform: translate(-50%, -50%)

 

2. top: 50% / left: 50%

margin-left: -50px; 쌤이 여기 왜 -50이라 적으셨을까

margin-top: -100px;

 

3. calc()함수를 사용

calc(50% - 100px)

초록 부분 띄어쓰기 필수 ! 띄어쓰기 안하니까 안되넹

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body > div {
            width: 1000px;
            height: 600px;
            background: lightcoral;
            margin: 0 auto;
            position: relative;
        }
        div div {
            width: 300px;
            height: 200px;
            background: darkblue;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
        }
    </style>
</head>
<body>
    <div>
        <div></div>
    </div>
</body>
</html>

'Stack > CSS' 카테고리의 다른 글

[CSS] Indigo (페이지 따라 만들기 3)  (0) 2022.04.15
[CSS] flex-box 레이아웃 연습  (0) 2022.04.14
[CSS] Igloo (페이지 따라 만들기 2)  (0) 2022.04.13
[CSS] hover / position / overflow / transition / transform 연습  (0) 2022.04.12
[CSS] Isaknox / Organic (페이지 따라 만들기)  (0) 2022.04.11
'Stack/CSS' 카테고리의 다른 글
  • [CSS] Indigo (페이지 따라 만들기 3)
  • [CSS] flex-box 레이아웃 연습
  • [CSS] Igloo (페이지 따라 만들기 2)
  • [CSS] hover / position / overflow / transition / transform 연습
7ingout
7ingout
  • 7ingout
    Hello, 7ingout world!
    7ingout
  • 전체
    오늘
    어제
    • 분류 전체보기 (205)
      • Project (5)
      • Stack (173)
        • React (40)
        • JavaScript (50)
        • TypeScript (14)
        • HTML (11)
        • CSS (31)
        • Spring (9)
        • PHP (15)
        • SQL (3)
        • Python (0)
      • ETC (9)
      • Design (13)
        • Illustrator (6)
        • Photoshop (7)
      • Articloid (4)
        • 7ingout (4)
  • 공지사항

    • ☻
  • 인기 글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.0
7ingout
[CSS] 원하는 요소 중앙에 배치하는 방법
상단으로

티스토리툴바