代码如下
(兼容 ie8以上的浏览器)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.box {
width: 200px;
height: 200px;
background-color: yellow;
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
margin: auto;
}
</style>
</head>
<!-- 该方法兼容ie8以上的浏览器 此方法代码的核心点: (1)上下左右均0位置定位 (2)margin:auto -->
<body>
<div class="box">
我有固定的宽度和高度
</div>
</body>
</html>
结果如下: