HTML 溢出不滚动

HTML Overflow Not Scrolling

我正在制作一个 HTML 计算器,我正在使用表格来对齐所有内容。然而,当输入太多字符时,计算器会变大而不是滚动,即使有 overflow-x: scroll 属性。到目前为止,这是我的代码:

var problem = ""
var answer = document.getElementById("responseSpan")

var chars = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "."]

function hit(key) {
  //console.log(key)
  if (chars.includes(key)) {
    problem = problem + key
  }

  answer.innerText = problem
}
body {
  background-color: #e0e0e0;
}

table {
  width: 25%;
  border-spacing: 0px;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  position: absolute;
}

td {
  border: 1px solid gray;
  padding: 1px;
  text-align: right;
}

.clickable {
  cursor: pointer;
  text-align: center;
  color: black;
  padding-top: 5%;
  padding-bottom: 5%;
  font-size: 1.5em;
  user-select: none;
  -moz-user-select: none;
  -khtml-user-select: none;
  -webkit-user-select: none;
  -o-user-select: none;
}

.clickable:hover {
  filter: brightness(85%);
}

#response {
  color: white;
  background-color: gray;
  font-size: 70px;
  padding-top: 2%;
  padding-bottom: 2%;
}

#responseSpan {
  overflow-x: scroll;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Calculator</title>
  <link href="https://fonts.googleapis.com/css2?family=Open+Sans+Condensed:wght@300&display=swap" rel="stylesheet">
</head>

<body>
  <table>
    <tr>
      <td colspan="4" id="response"><span id="responseSpan">0</span></td>
    </tr>
    <tr>
      <td class="clickable" style="background-color:darkgray;" onclick="hit('AC')">AC</td>
      <td class="clickable" style="background-color:darkgray;" onclick="hit('+/-')">+/-</td>
      <td class="clickable" style="background-color:darkgray;" onclick="hit('%')">%</td>
      <td class="clickable" style="background-color:orange;" onclick="hit('/')">➗</td>
    </tr>
    <tr>
      <td class="clickable" style="background-color:lightgray;" onclick="hit('7')">7</td>
      <td class="clickable" style="background-color:lightgray;" onclick="hit('8')">8</td>
      <td class="clickable" style="background-color:lightgray;" onclick="hit('9')">9</td>
      <td class="clickable" style="background-color:orange;" onclick="hit('x')">x</td>
    </tr>
    <tr>
      <td class="clickable" style="background-color:lightgray;" onclick="hit('4')">4</td>
      <td class="clickable" style="background-color:lightgray;" onclick="hit('5')">5</td>
      <td class="clickable" style="background-color:lightgray;" onclick="hit('6')">6</td>
      <td class="clickable" style="background-color:orange;" onclick="hit('-')">-</td>
    </tr>
    <tr>
      <td class="clickable" style="background-color:lightgray;" onclick="hit('1')">1</td>
      <td class="clickable" style="background-color:lightgray;" onclick="hit('2')">2</td>
      <td class="clickable" style="background-color:lightgray;" onclick="hit('3')">3</td>
      <td class="clickable" style="background-color:orange;" onclick="hit('+')">+</td>
    </tr>
    <tr>
      <td colspan="2" class="clickable" style="background-color:lightgray;" onclick="hit('0')">0</td>
      <td class="clickable" style="background-color:lightgray;" onclick="hit('.')">.</td>
      <td class="clickable" style="background-color:orange;" onclick="hit('=')">=</td>
    </tr>
  </table>
</body>

</html>

当我按下的字符超过响应范围可以处理的字符时,它会变大而不是滚动。

要使 overflow-x: scroll 正常工作,您的 span 元素应显示 inline-block(或 block),以及定义的宽度,例如:

#responseSpan {
  overflow-x: scroll;
  display: inline-block;
  width: 25vw;
}

演示:

var problem = ""
var answer = document.getElementById("responseSpan")

var chars = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "."]

function hit(key) {
  //console.log(key)
  if (chars.includes(key)) {
    problem = problem + key
  }

  answer.innerText = problem
}
body {
  background-color: #e0e0e0;
}

table {
  width: 25%;
  border-spacing: 0px;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  position: absolute;
}

td {
  border: 1px solid gray;
  padding: 1px;
  text-align: right;
}

.clickable {
  cursor: pointer;
  text-align: center;
  color: black;
  padding-top: 5%;
  padding-bottom: 5%;
  font-size: 1.5em;
  user-select: none;
  -moz-user-select: none;
  -khtml-user-select: none;
  -webkit-user-select: none;
  -o-user-select: none;
}

.clickable:hover {
  filter: brightness(85%);
}

#response {
  color: white;
  background-color: gray;
  font-size: 70px;
  padding-top: 2%;
  padding-bottom: 2%;
}

/* Changes are here */
#responseSpan {
  overflow-x: scroll;
  display: inline-block;
  width: 25vw;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Calculator</title>
  <link href="https://fonts.googleapis.com/css2?family=Open+Sans+Condensed:wght@300&display=swap" rel="stylesheet">
</head>

<body>
  <table>
    <tr>
      <td colspan="4" id="response"><span id="responseSpan">0</span></td>
    </tr>
    <tr>
      <td class="clickable" style="background-color:darkgray;" onclick="hit('AC')">AC</td>
      <td class="clickable" style="background-color:darkgray;" onclick="hit('+/-')">+/-</td>
      <td class="clickable" style="background-color:darkgray;" onclick="hit('%')">%</td>
      <td class="clickable" style="background-color:orange;" onclick="hit('/')">➗</td>
    </tr>
    <tr>
      <td class="clickable" style="background-color:lightgray;" onclick="hit('7')">7</td>
      <td class="clickable" style="background-color:lightgray;" onclick="hit('8')">8</td>
      <td class="clickable" style="background-color:lightgray;" onclick="hit('9')">9</td>
      <td class="clickable" style="background-color:orange;" onclick="hit('x')">x</td>
    </tr>
    <tr>
      <td class="clickable" style="background-color:lightgray;" onclick="hit('4')">4</td>
      <td class="clickable" style="background-color:lightgray;" onclick="hit('5')">5</td>
      <td class="clickable" style="background-color:lightgray;" onclick="hit('6')">6</td>
      <td class="clickable" style="background-color:orange;" onclick="hit('-')">-</td>
    </tr>
    <tr>
      <td class="clickable" style="background-color:lightgray;" onclick="hit('1')">1</td>
      <td class="clickable" style="background-color:lightgray;" onclick="hit('2')">2</td>
      <td class="clickable" style="background-color:lightgray;" onclick="hit('3')">3</td>
      <td class="clickable" style="background-color:orange;" onclick="hit('+')">+</td>
    </tr>
    <tr>
      <td colspan="2" class="clickable" style="background-color:lightgray;" onclick="hit('0')">0</td>
      <td class="clickable" style="background-color:lightgray;" onclick="hit('.')">.</td>
      <td class="clickable" style="background-color:orange;" onclick="hit('=')">=</td>
    </tr>
  </table>
</body>

</html>