为什么我的 div 列溢出以及如何使它们与容器齐平?

我正在 HTML/CSS 中创建一个基本计算器,而不使用 Bootstrap。将有四列按钮 - 三列主要用于数字,一列用于操作按钮(+、- 等)。操作员按钮列将有五个按钮,而不是其他容器中的四个按钮。我希望按钮 div 彼此完全齐平并与其周围的容器齐平。但是,我遇到了两个问题。

首先,当我尝试为每列按钮提供完全相同的宽度 (25%) 时,最后一列出现在框外。每列之间都有一个空格,我无法去掉它。

其次,即使我将盒子的高度设置为其容器高度的百分比,盒子也会垂直移动。

我最终会去掉按钮边框,但将它们包含在这里是为了更容易地直观地显示出了什么问题。

html,

body {

  text-align: center;

  font-family: 'Courier New', Courier, monospace;

}


.mainBody {

  display: inline-block;

  width: 350px;

  height: 350px;

}


.outputWindow {

  width: 100%;

  height: 20%;

  border-bottom: 0px;

  background-color: rgb(164, 174, 177);

  text-align: right;

  line-height: 100px;

}


.buttonsBody {

  width: 100%;

  height: 80%;

  border-top: 0px;

  background-color: rgb(138, 142, 143);

}


.numColumn,

.operatorColumn {

  display: inline-block;

  width: 25%;

  height: 100%;

}


.numButton,

.operatorButton {

  line-height: 50px;

  border: 1px solid black;

}


.numButton {

  height: 25%;

}


.operatorButton {

  height: 20%;

}

<html>


<head>

  <title>Calculator</title>

  <meta charset="UTF-8" />

  <link rel="stylesheet" type="text/css" href="styles.css" />

</head>



繁华开满天机
浏览 78回答 3
3回答

凤凰求蛊

我认为您可以使用网格轻松构建该布局。代码沙盒基本上你可以在父元素中定义列.buttonsBody {  border-top: 0px;  background-color: rgb(138, 142, 143);  display: grid;  grid-template-columns: 25% 25% 25% 1fr;}并尽量避免使用  display: inline-block;最后你需要添加盒子大小以避免盒子溢出* {  box-sizing: border-box;}

慕哥6287543

使用如下所示的溢出:隐藏功能.buttonsBody&nbsp;{ &nbsp;&nbsp;width:&nbsp;100%; &nbsp;&nbsp;height:&nbsp;80%; &nbsp;&nbsp;border-top:&nbsp;0px; &nbsp;&nbsp;background-color:&nbsp;rgb(138,&nbsp;142,&nbsp;143); &nbsp;&nbsp;overflow:&nbsp;hidden; }该功能将阻止任何内容离开您的父 div,这样您就可以调整父 div 的大小以适合您需要的内容,并且它将阻止内容出现在外部

哔哔one

你可以使用以下方法。添加你自己的类并根据它编写css。如果你不想使用bootstrap,你可以使用css grid,这对你来说很容易<!DOCTYPE html><html><head>&nbsp; <title>Calculator</title>&nbsp; <meta charset="UTF-8"/>&nbsp; &nbsp;<style>&nbsp; &nbsp;html, body {&nbsp; &nbsp; text-align: center;&nbsp; &nbsp; font-family: 'Courier New', Courier, monospace;&nbsp; &nbsp; width:100%;}.mainBody {&nbsp; &nbsp; display: inline-block;&nbsp; &nbsp; width: auto;&nbsp; &nbsp; height: auto;}.outputWindow {&nbsp; &nbsp; height: 20%;&nbsp; &nbsp; border-bottom: 0px;&nbsp; &nbsp; background-color: rgb(164, 174, 177);&nbsp; &nbsp; text-align: right;&nbsp; &nbsp; line-height: 100px;&nbsp; &nbsp; padding:20px;}.buttonsBody {&nbsp; &nbsp; border-top: 0px;&nbsp; &nbsp; background-color: rgb(138, 142, 143);}.numColumn, .operatorColumn {&nbsp; &nbsp; display: inline-block;&nbsp; &nbsp; width: 100%;&nbsp; &nbsp; height: 100%;}.numButton, .operatorButton {&nbsp; &nbsp; width:20%;&nbsp; &nbsp; float:left;&nbsp; &nbsp; line-height: 50px;&nbsp; &nbsp; margin:10px;&nbsp; &nbsp; border: 1px solid black;}.numButton {&nbsp; &nbsp; height: 25%;}.operatorButton {&nbsp; &nbsp; height: 25%;}&nbsp; &nbsp;</style></head><body>&nbsp; &nbsp; <div class="mainBody">&nbsp; &nbsp; &nbsp; &nbsp; <div class="outputWindow">Test </div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="buttonsBody">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="numColumn">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="numButton">7</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="numButton">8</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="numButton">9</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="numButton">Del</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="numColumn">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="numButton">4</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="numButton">5</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="numButton">6</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="numButton">÷</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="numColumn">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="numButton">1</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="numButton">2</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="numButton">3</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="numButton">x</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="operatorColumn">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="operatorButton">0</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="operatorButton">.</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="operatorButton">=</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="operatorButton">-</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; &nbsp; <script src="script.js"></script></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5