Boost the figures in an HTML input without affecting the letters

Dealing with a challenge where I need an input field to accept both numbers and letters, but only allow the numbers to be increased or decreased while keeping the letters intact. Essentially, users should not be able to delete the letters. The desired functionality is illustrated in the image below:

Input field accepting numbers and letters only:
https://i.sstatic.net/1MMLJ.png

I managed to come up with a workaround by inserting <span>mm</span> next to numbers using position: absolute;.

Is there a more efficient or effective approach to achieving this?

Answer №1

Verify my answer below:

.input-num {position: relative;}
.input-num span { position: absolute; left: 18px;}
<div class="input-num">
  <input value="50" type="number" name="quantity" min="10" max="99">
  <span>mm</span>
</div>

Answer №2

One way to achieve this effect is by using the following CSS and HTML code:

div {
  display: inline-block;
  position: relative;
}

div::after {
  position: absolute;
  top: 2px;
  right: .5em;
  transition: all .05s ease-in-out;
}

div:hover::after,
div:focus-within::after {
  right: 1.5em;
}

.ms::after {
  content: 'mm';
}
<div class="ms">
  <input type="number" id="milliseconds" />
</div>

Answer №3

If you want to adjust the width of the container, follow these steps:

Here is an example in HTML :

<div class="input__container">
  <input type="number" class="input" />
  <span class="input__text">mm</span>
</div> 

And here is how you can style it in CSS :

.input__container{
  position:relative;
  width:60px;
}
.input{
  position:absolute;
  width:100%;
}
.input__text{
  position:absolute;
  top:50%;
  transform:translateY(-50%);
  right:10px;
}

You can view and test this code on Code pen : https://codepen.io/abdelhedi/pen/JjodrRq

Answer №4

Here is a different method: using text-align, padding, and a pseudo element from a generic structure that can be reused and updated.

Suggested layout:

* {
  padding: 0;
  margin: 0;
}


/* 101*/

[data-unit] {
  font-family: courier;  /* selecting a font for precise size and letter spacing */
  font-size: 1.25rem;
  letter-spacing: 0.05em;
}

[data-unit]::after {
  content: attr(data-unit);
  margin-left: -1.5em;  /* adjusted to fit font-family:courier; */
}

[data-unit] [type="number"] {
  font-size: 1.25rem;
  text-align: right;
  padding-right: 1.8em;  /* adjusted to fit font-family:courier; */
}


/* styling */

[data-unit] {
  box-sizing: border-box;
  display: flex;
  width: max-content;
  padding: 0.5em;
  background: lightblue;
  margin: 1em;
  border: outset;
  align-items: center;
}

[data-unit]::after {
  font-weight: bold;
}
Apply styles only to boxes with data-unit attributes.
<div data-unit="mm">
  <input type="number" value="0" />
</div>

<div data-unit="km">
  <input type="number" value="0" />
</div>

<div data-unit="cm">
  <input type="number" value="0" />
</div>
Leave other elements untouched
<div>
  <input type="number" value="0" />
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is the fixed header see-through?

I have a query regarding my website design. Currently, the fixed header on my site is transparent and when I scroll down, the content overlaps the header. However, I want the header to remain above the content. Can anyone provide assistance with this issue ...

magnificPopup experiencing difficulties when attempting to invoke a class that is dynamically generated within the HTML document

Currently, I am encountering an issue with the magnificPopup. Whenever I try to trigger the popup using the class 'popup-with-zoom-anim', it doesn't seem to work as expected. Has anyone else faced a similar problem before? <body> < ...

Tips for transferring data when clicking in Angular 5 from the parent component to the child component

I need assistance with passing data from a parent component to a child component in Angular 5. I want the child component to render as a separate page instead of within the parent's template. For example, let's say my child component is called & ...

Tips for ensuring the Search button always remains alongside the input bar during resizing with percentages

I've been trying to figure out why the search button is still showing at the bottom left and creating an "l" shape even though I'm using style="overflow: hidden; padding-right: .5em;". Does anyone have any ideas as to what might be causing this i ...

Ways to enable a user to edit a calculated property in VueJS?

I am currently working on collecting data that the user can download, and I am facing a challenge in determining the best way to handle the filename. To ensure the default filename is dynamic and based on the current date, I believe creating a computed pro ...

Discover the art of concurrently listening to both an event and a timer in JavaScript

Upon loading the page, a timer with an unpredictable duration initiates. I aim to activate certain actions when the countdown ends and the user presses a button. Note: The action will only commence if both conditions are met simultaneously. Note 2: To cl ...

Submitting form by clicking a link on the page

To submit a POST request with "amount=1" without displaying it in the URL, I need the site to send this request when any link on the site is clicked. This JavaScript code achieves that with a GET request: window.onload = function () { document.body.oncli ...

What is the method to adjust the font size for section, subsection, and other sections in Doxygen?

I am looking to customize the font size of \section and \subsection in doxygen's html output. Additionally, I want to include numbering for the sections and sub(sub)sections in the format: 1 Section1 1.1 Subsection1.1 1.1.1 Subsubsection ...

Modifying the color of a placeholder in an HTML input using CSS

Version 4 of Chrome now supports the placeholder attribute on input[type=text] elements (and likely other browsers do too). Despite this, the following CSS code does not affect the color of the placeholder text: input[placeholder], [placeholder], *[pla ...

What is the best way to dynamically set an ID in Angular using the pound symbol (#)?

I am currently developing a dynamic Angular 6 application that utilizes dynamic components. My approach involves using @ViewChild('<id>', { read: ViewContainerRef }) <id>; to reference the divs where I intend to populate with dynamic ...

How to Eliminate Lower Borders from DataGrid Component in Material UI (mui)

I've been trying to customize the spacing between rows in a MUI Data Grid Component by overriding the default bottom border, but haven't had much success. I've experimented with different approaches such as using a theme override, adding a c ...

Link embedded in prism formatting, encased in code snippet

In the HTML template, I have the following code snippet: <pre> <code class="language-markup"> {% filter force_escape %} <Item> <MarkUp><a href="http://google.com">Google</a></MarkUp> <No ...

Organizing an array of objects within MUI cards based on social media platforms

I'm currently developing an application that showcases athlete data in a grid layout using MUI Grid. The colored borders on the left side of each card are determined by the corresponding social network associated with that athlete. https://i.sstatic. ...

Troubleshooting: Issues with jQuery's Class selector

Having trouble setting up an alert to trigger when a specific class anchor tag is clicked inside a div. This is what my HTML section looks like... <div id="foo"> <a class='bar' href='#'>Next</a> </div> And h ...

Keep scrolling! There's no need to worry about reaching the end of the page and having to start over

I'm experiencing an issue where scrolling only works once when the page is initially loaded. I need it to be able to repeat from the beginning each time. <div class="content"> <div class="next-section blue">First Section</div> & ...

Looking to create an anchor tag that navigates to a specific ID on the page while accommodating a fixed header placement

In my application, the homepage's carousel displays multiple images with dynamically generated anchor tags that link to different routes. When clicking on the anchor tag, the page scrolls to the linked image but is obstructed by a fixed header. I want ...

What is the best way to use jQuery to position a <div> at the bottom of another <div>?

Is there a way to achieve something similar to this concept? https://i.sstatic.net/xhewF.png The dimensions displayed are just for visualization purposes, but feel free to utilize them. I am interested in moving the smaller red <div> box and attac ...

Issue with plotted point labels failing to display correctly on X Range Chart for the initial date of each month - Highcharts

Currently, I am implementing a chart that displays the timeline of activities using an x range. However, I have encountered an issue with the popup displaying data information when hovering over the bars. The problem arises specifically on the first date ...

Calculate the combined total of two inputted values instantly

Is there a way to add two separate numerical inputs together in real-time and display the sum in a third variable? Here's a clearer explanation: First Variable: 100 (input) Second Variable: 150 (input) Sum: 250 (calculated, not input) Any suggesti ...

What methods can be used to disable the back button functionality within an iframe?

I am facing an issue with embedding YouTube links on my webpage using iframes. When a user clicks on the link, it plays the video in the iframe. Below is the HTML and jQuery code I am using: HTML: <a href="http://www.youtube.com/embed/Q5im0Ssyyus"> ...