DEV Community

Cover image for Making a Calculator UI with HTML5 and CSS3
Razvan Zamfir
Razvan Zamfir

Posted on

Making a Calculator UI with HTML5 and CSS3

This tutorial explains how to build a sleek calculator UI using HTML5 and CSS.


HTML Structure (What we are building)

<div class="container">
  <div class="calculator">
    <div class="display">
      <div class="track">
        12 &times; 4 =
      </div>

      <div class="result">
        48
      </div>
    </div>

    <div class="buttons-container">
      <ol>
        <li class="orange">C</li>
        <li class="orange">&radic;</li>
        <li class="orange">&percnt;</li>
        <li class="orange">+/-</li>
        <li>7</li>
        <li>8</li>
        <li>9</li>
        <li class="orange">&divide;</li>
        <li>4</li>
        <li>5</li>
        <li>6</li>
        <li class="orange">&times;</li>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li class="orange">-</li>
        <li>0</li>
        <li>&middot;</li>
        <li class="orange">+</li>
        <li class="orange">=</li>
      </ol>
    </div>
  </div>
</div>
Enter fullscreen mode Exit fullscreen mode

We split the UI into 3 main parts:

  • .calculator → the main calculator body
  • .display → shows calculation and result
  • .buttons-container → holds all buttons in a grid

I chose to use an ordered list for the calculator buttons part.


Base Styling

body {
  margin: 0;
  padding: 0;
  background-color: #80d0c7;
  font-family: Montserrat, sans-serif;
}

body * {
  box-sizing: border-box;
}
Enter fullscreen mode Exit fullscreen mode

Centering the Calculator

.container {
  min-height: 100vh;
  width: 100%;
  padding: 5px;
  display: flex;
  align-items: center;
  justify-content: center;
}
Enter fullscreen mode Exit fullscreen mode

This is classic Flexbox centering:

  • display: flex enables flex layout
  • align-items: center centers vertically
  • justify-content: center centers horizontally
  • min-height: 100vh makes it fill the full screen height

This way, the calculator always stays centered.


Styling the Calculator's Body

.calculator {
  width: 100%;
  max-width: 360px;
  min-width: 300px;
  margin: 10px auto;
  padding: 36px;
  background: #515151;
  border-radius: 18px;
  box-shadow:
    0 10px 30px rgba(0, 0, 0, 0.25),
    inset -4px -4px 10px rgba(0, 0, 0, 0.25);
}
Enter fullscreen mode Exit fullscreen mode

The last line gives the calculator the appearance of depth.


Display Area

.display {
  position: relative;
  display: flex;
  flex-direction: column;
  padding: 12px 20px;
  margin-bottom: 34px;
  background: #c0d3c0;
  border-radius: 16px;
  border: 1px solid rgba(0,0,0,0.6);
  box-shadow: inset 0 0 4px 2px rgba(0,0,0,0.25);
  overflow: hidden;
}
Enter fullscreen mode Exit fullscreen mode

Explanation

  • background mimics LCD screen
  • inset shadow gives screen depth

Display Text Alignment

.track,
.result {
  display: flex;
  justify-content: flex-end;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
Enter fullscreen mode Exit fullscreen mode

Explanation

This ensures calculator-style alignment:

  • justify-content: flex-end → numbers align right
  • white-space: nowrap → prevents line breaks
  • text-overflow: ellipsis → adds “…” if too long

Calculation History

.track {
  position: absolute;
  top: 8px;
  left: 20px;
  right: 20px;
  font-size: 12px;
  font-weight: 600;
  opacity: 0.6;
}
Enter fullscreen mode Exit fullscreen mode

Explanation

  • position: absolute floats it inside display
  • opacity: 0.6 makes it less important than the result

Result Styling

.result {
  margin-top: 16px;
  font-size: clamp(2rem, 8vw, 3rem);
  font-weight: 700;
}
Enter fullscreen mode Exit fullscreen mode

Explanation

  • clamp() makes font responsive:
  • font-weight: 700 makes the result visually dominant

Button Grid Layout (CSS Grid)

.buttons-container ol {
  list-style-type: none;
  padding: 0;
  margin: 0;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
}
Enter fullscreen mode Exit fullscreen mode

How it works:

  • display: grid enables grid system
  • repeat(4, 1fr) creates 4 equal columns
  • gap controls spacing between buttons

Button Styling

.buttons-container li {
  display: flex;
  align-items: center;
  justify-content: center;
  aspect-ratio: 1;
  font-size: 36px;
  font-weight: 600;
  background: #e7e7e7;
  color: #242424;
  border-radius: 10px;
  border: 1px solid rgba(0,0,0,0.7);
  box-shadow: inset -3px -3px 3px rgba(0,0,0,0.15);
  cursor: pointer;
  user-select: none;
  transition: transform 0.08s ease, filter 0.15s ease;
}
Enter fullscreen mode Exit fullscreen mode

How it works:

We make sure the buttons are always square with aspect-ratio: 1. We use box-shadow: inset -3px -3px 3px rgba(0,0,0,0.15) for the illusion of depth.


Click Effect

We simulate real button press with:

.buttons-container li:active {
  transform: translateY(2px);
}
Enter fullscreen mode Exit fullscreen mode

Operator Buttons (Orange)

We use a nice orange color for the operation buttons:

.buttons-container .orange {
  background: #d88d43;
  color: #fff;
  border: 1px solid rgba(0,0,0,0.5);
}
Enter fullscreen mode Exit fullscreen mode

Here is a Codepen with the final result I got! 😊

Top comments (0)