본문 바로가기
개발/CSS

css 이미지 가운데 놓는 법

by 안뇽! 2022. 2. 19.
반응형

1. flex이용

justify-content:center, align-items:center이용

<!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>CSS 연습</title>
    <link rel="stylesheet" href="index.css" />
    <style>
      .containor {
        background: yellow;
        height: 400px;

        display: flex;
        justify-content: center;
        align-items: center;
      }
      #img {
        width: 100px;
        height: 100px;
      }
    </style>
  </head>
  <body>
    <div class="containor">
      <img id="img" src="cafelatte.png" />
    </div>
  </body>
</html>

2. margin 이용

컨테이너에 display:flex를 설정하고

이미지에 margin을 auto 로 준다.

<!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>CSS 연습</title>
    <link rel="stylesheet" href="index.css" />
    <style>
      .containor {
        background: yellow;
        height: 400px;
        display: flex;
      }
      #img {
        width: 100px;
        height: 100px;
        margin: auto;
      }
    </style>
  </head>
  <body>
    <div class="containor">
      <img id="img" src="cafelatte.png" />
    </div>
  </body>
</html>

 

 

반응형