【发布时间】:2020-09-24 18:23:21
【问题描述】:
我想要一个按钮图像在水平和垂直大小固定的 div 中居中。
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
div#fixed-size {
width: 800px;
height: 600px;
border: 1px solid red;
display: block;
}
button {
padding: 0;
margin: auto;
display: block;
width: 256px;
height: 256px;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
</style>
</head>
<body>
<div id="fixed-size">
<button>
<img src="aqua.png">
</button>
</div>
</body>
</html>
我应该写什么而不是 button { /* ... */ } 以使按钮居中在 div 中而不是在整个页面中?
【问题讨论】:
-
先试试
position:relative