3

I am making a HTML calculator, and I am using tables to align everything. However, when to many characters are typed in, the calculator gets bigger instead of scrolling, even though there is the overflow-x: scroll property. This is my code so far:

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>

When ever I press more characters than the response span can handle, it gets larger rather than scrolling.

1
  • 2
    It's a bad practice to use tables to position elements in webpage. You will have multiple problems with that. Better use css grid. Commented Aug 21, 2020 at 22:25

1 Answer 1

2

For overflow-x: scroll to work, your span element should have a display of inline-block (or block), as well as a defined width, e.g.:

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

Demo:

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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.