search
  • Sign In
  • Sign Up
Password reset successful

Follow the proiects vou are interested in andi aet the latestnews about them taster

Table of Contents
✅ Recommended solution: Semantic structure style fusion
⚠️ Comparison of alternatives (not recommended)
?Advanced tips
Home Web Front-end HTML Tutorial How to statically display units (like 'min') in an input box and allow only numeric input

How to statically display units (like 'min') in an input box and allow only numeric input

Dec 29, 2025 pm 08:00 PM

How to statically display units (like

This article describes how to only allow users to enter numbers (such as minutes) in the input box, while visually fixing the unit (such as "min") to the right and making it uneditable and unselectable, taking into account both user experience and data accuracy.

In form design, it is often necessary to allow users to input values ​​with units (such as "5 min" or "30 sec"). However, if the unit is written directly into the value attribute of (such as value="0 min"), the unit will be selected, deleted, or overwritten by the cursor, destroying semantics and interaction logic. The truly robust solution is to separate "editable values" and "static unit display", and achieve visual integration through HTML structure and CSS styles.

Use to manage pure numeric input, use next to the display unit, and then use CSS to achieve seamless splicing:

 <div class="input-with-unit">
  <input type="number" id="time" min="0" max="99" value="0" step="1" aria-label="Minutes">
  <span class="unit">min</span>
</div>

Supporting CSS (eliminate browser default styles and unify vision):

 .input-with-unit {
  display: inline-flex;
  align-items: center;
  font-family: system-ui, -apple-system, sans-serif;
}

.input-with-unit input {
  margin: 0;
  padding: 6px 8px 6px 12px;
  border: 1px solid #ccc;
  border-radius: 4px 0 0 4px;
  outline: none;
  font-size: 1rem;
}

/* Hide the up and down arrows of Chrome/Safari/Edge*/
.input-with-unit input::-webkit-outer-spin-button,
.input-with-unit input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

/* Firefox compatible: displayed as text box style*/
.input-with-unit input[type="number"] {
  -moz-appearance: textfield;
}

.input-with-unit .unit {
  padding: 6px 12px 6px 4px;
  background: #f5f5f5;
  border: 1px solid #ccc;
  border-left: none;
  border-radius: 0 4px 4px 0;
  color: #666;
  font-size: 0.95rem;
  line-height: 1;
}

✅Advantage description :

  • Values ​​are natively constrained by min/max/step and do not require JavaScript verification;
  • The unit is completely uneditable, unselectable, and accessibility-friendly (aria-label has clear semantics);
  • Responsive and friendly, supports keyboard navigation and screen readers;
  • Compatible with all modern browsers, no risk of hacks.
  • maxlength="2" : Although it can limit the length, it cannot prevent the input of letters (such as "ab"). It still requires the cooperation of JS or pattern, and its semantics is weaker than number;
  • CSS::after pseudo-element coverage unit : The unit is not a real DOM node and cannot be recognized by screen readers, and the cursor may mistakenly enter the pseudo-element area, affecting accessibility;
  • value="0 min" JS prevents the cursor from entering the suffix : the logic is complex, prone to bugs, and violates the single responsibility principle of form controls.

?Advanced tips

  • If you need to support decimals (such as 1.5 min), you can set step="0.1" and adjust max (such as max="99.9");
  • It is recommended to add inputmode="numeric" on the mobile terminal to improve the soft keyboard experience:
     <input type="number" inputmode="numeric" ...>
  • If you need dynamic units (such as "min"/"sec" switching), you only need JS to control the .unit text content without affecting the input logic.

To sum up, "structural separation and style integration" is the most practical method that conforms to Web standards, has strong maintainability and good user experience - let numbers belong to numbers, units belong to units, each performs its duties, and is integrated.

The above is the detailed content of How to statically display units (like 'min') in an input box and allow only numeric input. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

ArtGPT

ArtGPT

AI image generator for creative art from text prompts.

Stock Market GPT

Stock Market GPT

AI powered investment research for smarter decisions

Popular tool

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to dynamically pass HTML form data to analytics.track() method How to dynamically pass HTML form data to analytics.track() method Mar 13, 2026 pm 10:57 PM

This article explains in detail how to safely and efficiently extract user input from HTML forms and structure it into JavaScript objects as attribute parameters of analytics.track() to avoid hard coding and syntax errors and support flexible expansion.

A complete guide to using the keyboard to control the smooth movement of HTML elements A complete guide to using the keyboard to control the smooth movement of HTML elements Mar 13, 2026 pm 10:18 PM

This article explains in detail why transform: translate() combined with the keydown event cannot move elements, and provides a reliable solution based on CSS positioning and JavaScript, covering absolute positioning settings, coordinate update logic, code robustness optimization, and common pitfalls.

How to properly override default styles and implement custom CSS layouts in Divi theme builder How to properly override default styles and implement custom CSS layouts in Divi theme builder Mar 14, 2026 am 12:00 AM

This article explains in detail the root cause of style failure when applying custom CSS in the WordPress Divi theme builder. It provides practical solutions for improving selector specificity, accurately positioning elements, and rational use of !important, as well as debugging tips and code optimization examples.

How to add prompt copy for disabled button click How to add prompt copy for disabled button click Mar 30, 2026 pm 04:30 PM

This article introduces a complete solution for disabling the "Next" button when the form does not meet the conditions, and using native HTML5 form validation or JavaScript dynamic control to display a friendly prompt message when the disabled button is clicked.

How to switch images by clicking a button (elegant implementation based on jQuery) How to switch images by clicking a button (elegant implementation based on jQuery) Apr 04, 2026 pm 08:06 PM

This article introduces how to use jQuery to dynamically switch background images after button clicks, and corrects problems such as CSS selector misuse, inline event coupling, and logical redundancy in the original code, providing a concise and maintainable interaction solution.

How to use Python to quickly set up a local development server for mobile responsive testing How to use Python to quickly set up a local development server for mobile responsive testing Apr 05, 2026 pm 08:06 PM

This article introduces how to use Python's built-in module to quickly start a lightweight HTTP server. You can access local projects in a mobile browser through the LAN without deployment, and realize real-time real-machine debugging of responsive designs.

The root cause, positioning method and viewport behavior analysis of HTML elements being misaligned during scaling The root cause, positioning method and viewport behavior analysis of HTML elements being misaligned during scaling Apr 05, 2026 pm 09:18 PM

When the page is zoomed (zoom), elements using position: relative with left/top offset will produce cumulative displacement deviations due to the browser rendering mechanism; and position: absolute combined with top/right/bottom/left and transform can achieve truly stable relative positioning.

How to make an SVG background stretch completely to fill the container (ignoring aspect ratio) How to make an SVG background stretch completely to fill the container (ignoring aspect ratio) Apr 06, 2026 pm 08:33 PM

By explicitly declaring the width and height attributes in the SVG tag, and using CSS background-size: 100% 100%, you can force the SVG background to stretch without proportions to completely cover the container, solving the scaling inconsistency problem caused by the lack of inherent dimensions of vector images.

Related articles