css自適應佈局

shaoyudong發表於2019-04-02

寬度自適應,高度是寬度的2倍

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    .content {
      background: lightblue;
      width: 100%;
      height: 0;
      padding-bottom: 200%;
    }
    .container {
      width: 200px;
    }
  </style>
</head>
<body>
  <div class="container">
    <div class="content"></div>
  </div>
</body>
</html>
複製程式碼

原理: padding、margin根據父元素的width計算百分比數值

相關文章