老师,为什么我的div居中不了呢?
来源:2-1 结构和样式
幕布斯1062488
2020-07-27 11:51:33
<!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>
body {
background-color: #eee;
}
#calculator {
margin: 100px 0 0 150px;
}
</style>
</head>
<body>
<!-- 简易计算器 -->
<div id="calculator">
<p><input type="text" class="formerInput" value="1">
<span class="sign">+</span>
<input type="text" class="laterInput" value="1">
<span>=</span>
<span class="resuleOutput">2</span></p>
<p>
<input type="button" value="➕" />
<input type="button" value="➖" />
<input type="button" value="✖️" />
<input type="button" value="➗" />
</p>
</div>
</body>
</html>
2回答
好帮手慕慕子
2020-07-27
同学你好,只给div设置了上方和左侧固定的margin值,是无法实现居中的。
给同学提供一种实现居中的思路,可以参考下图代码进行修改:通过定位和transform属性,实现div居中效果。
如果我的回答帮助到了你,欢迎采纳,祝学习愉快~
一个意外
2020-07-27
方式一:在css添加 text-align: center;
#calculator {
margin: 100px 0 0 150px;
text-align: center;
}
方式二: 在css 中使用 margin: 0 auto; 居中
#calculator {
width: 400px;
margin: 0 auto;
}
相似问题