- input按钮居中无效示例
<!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>居中</title>
<style>
input {
width: 120px;
height: 40px;
background-color: red;
border: 0px;
border-radius: 6px;
margin: 0 auto;
}
</style>
</head>
<body>
<input type="button" value="居中">
</body>
</html>
可以看到,margin: 0 auto并没有生效。
这是因为,margin: 0 auto 只对块元素生效,而input标签自身就是个行内块元素,并不是块元素所以不生效。
使用display转换为块元素。
<!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>居中</title>
<style>
input {
width: 120px;
height: 40px;
background-color: red;
border: 0px;
border-radius: 6px;
margin: 0 auto;
display: block;
}
</style>
</head>
<body>
<input type="button" value="居中">
</body>
</html>
成功解决了input标签居中不生效问题。