style의 display: flex를 사용해서 중앙에 정렬하기

 

두가지를 적용하면 되고

justify-content: center

align-items: center

전제 조건으로 display: flex 사용해야 한다.

 

 

 

 

 

 

사용예>

<html>

  <head>

    <meta charset="UTF-8">

    <title>Insert title here</title>

    <style>

    .div_test{

      display: flex;

      justify-content: center;

      align-items: center;

      height: 100vh;

    }

    </style>

  </head>

  <body>

    <div class="div_test">

      Hello World!

    </div>

  </body>

</html>

 

 

출처>

모질라 개발자 사이트

Aligning Items in a Flex Container

https://developer.mozilla.org/ko/docs/Web/CSS/CSS_Flexible_Box_Layout/Aligning_Items_in_a_Flex_Container

 

display

https://developer.mozilla.org/en-US/docs/Web/CSS/display

 

The display CSS property sets whether an element is treated as a block or inline element and the layout used for its children, such as flow layoutgrid or flex.

Formally, the display property sets an element's inner and outer display types. The outer type sets an element's participation in flow layout; the inner type sets the layout of children. Some values of display are fully defined in their own individual specifications; for example the detail of what happens when display: flex is declared is defined in the CSS Flexible Box Model specification. See the table at the end of this document for all of the individual specifications.

 

728x90

+ Recent posts