괴발개발/Html+CSS

CSS_두 상자 사이에 여백이 있는 분할하기(2가지 방법)

moonday 2021. 6. 16. 22:10

방법1

<!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>분할예제 방법1</title>
  <style>
    /* 부모의 길이의 반에서 가운데 여백길이만큼을 빼기*/
    .box{width: 400px; height: 200px; background-color: thistle; padding: 10px;}
    .box::after{clear: both; content: ''; display: block;}
    .inner{width: calc(50% - 5px); background-color: teal; height: 100%; float:left;}
    .box2{float: right;}    
  </style>
</head>
<body>
  <div class="box">
    <div class="inner box1"></div>
    <div class="inner box2"></div>
  </div>
</body>
</html>

꼭더보기↓

더보기

하늘색배경box안에

토마토색inner-box가 있는데 걜 margin-:10px으로 오른쪽으로 좀 더 넓혀둠

보라색테두리가inner인데, 그걸 반으로 쪼깸( width:50%;)

파란색테두리가 sub인데 보라색반쪽짜리 안에서 margin:10px으로 왼쪽으로 밀어서 박스를 작게 만들어서

=> 파란색박스 사이에 공간이 생김

<!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>분할예제 방법2</title>
  <style>
    .box{width: 400px; background-color: turquoise; padding: 10px;}
    .inner-box{margin-right: -10px; border: 1px solid black; height: 100%;}
    .inner{width: 50%; height:100%; background-color: tomato; float: left;}
    .inner-box::after{clear: both; content: ''; display: block;}
    .sub{margin-right: 10px; border: 2px solid blue; height: 200px;}
  </style>
</head>
<body>
  <div class="box">
    <div class="inner-box">
      <div class="inner left">
        <div class="sub box1"></div>
      </div>
      <div class="inner right">
        <div class="sub box2"></div>
      </div>
    </div>
  </div>
</body>
</html>