html & css/개념

box-sizing

Doo Hee 2023. 11. 21. 14:35
<!DOCTYPE html>
<html>
  <head>
    <style>
      div {
        margin: 10px;
        width: 150px;
        box-sizing: border-box;
      }
      #small {
        border: 10px solid black;
      }
      #large {
        border: 30px solid black;
      }
    </style>
  </head>
  <body>
    <div id="small">Hello</div>
    <div id="large">Hello</div>
  </body>
</html>

위의 코드를 웹페이지로 나타내면 #small과 #large의 크기는 다르게 보인다.

- content를 기준으로 너비가 150px가 적용되었기 때문.

 

두 요소의 크기를 같게 하려면 box-sizing이라는 속성을 이용하면 된다.

- box-sizing의 기본값은 content-box (content의 크기만큼 width와 height 값이 지정되는 것)이므로 값을 border-box로 변경하면 된다.

- 대부분 border-box를 사용한다.