之前去东方红面试某军工项目
对方问如何画一个正方形,不用固定尺寸
当时没想出来
上代码
<!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>
.inner_wrapper {
width: 10%;
height: 0;
padding-bottom: 10%;
background-color: brown;
}
</style>
</head>
<body>
<div class="outer_wrapper">
<div class="inner_wrapper">
</div>
</div>
</body>
</html>
效果
宽度为页面宽度的10%
高度为页面宽度的10%
padding 的百分比计算是参照父元素的 width属性
第二种方法
<!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>
.inner_wrapper {
width: 10vw;
height: 10vw;
background: red;
}
</style>
</head>
<body>
<div class="outer_wrapper">
<div class="inner_wrapper">
</div>
</div>
</body>
</html>