惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
J
Java Code Geeks
MongoDB | Blog
MongoDB | Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Blog — PlanetScale
Blog — PlanetScale
A
About on SuperTechFans
Engineering at Meta
Engineering at Meta
美团技术团队
N
Netflix TechBlog - Medium
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
F
Fortinet All Blogs
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
MyScale Blog
MyScale Blog
aimingoo的专栏
aimingoo的专栏
Last Week in AI
Last Week in AI
The Cloudflare Blog
量子位
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
腾讯CDC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
D
DataBreaches.Net
H
Hackread – Cybersecurity News, Data Breaches, AI and More

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Making a Calculator UI with HTML5 and CSS3
Razvan Zamfi · 2026-05-24 · via DEV Community
Cover image for Making a Calculator UI with HTML5 and CSS3

Razvan Zamfir

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! 😊