How to hide number input spin box using CSS?


The number input spin box is a commonly used component in web forms that allows users to increase or decrease numeric values with the help of up and down arrows. While this spin box provides convenience for selecting values, there are cases where you may want to hide it to achieve a specific design or behavior.

In this article, we will explore how to hide the number input spin box using CSS. We'll discuss two methods that leverage CSS properties to achieve the desired result without any JavaScript or external libraries. Whether you want to create a custom-styled number input or simply remove the spin box altogether, this guide will provide you with the techniques you need.

By the end of this article, you'll have a clear understanding of how to hide the number input spin box using CSS, along with considerations for browser compatibility and accessibility.

Understanding the Number Input Element

The number input element is an HTML form control that allows users to enter numeric values. It provides a convenient way to input numbers and includes a spin box that enables users to increment or decrement the value using up and down arrows. Before we delve into hiding the spin box, let's first understand the default appearance and behavior of the number input element.

When you use the number input in your HTML form, it typically renders as a text box with the spin box arrows displayed on the right side. The appearance of the spin box may vary slightly across different browsers, but the overall functionality remains the same. By default, users can manually enter a numeric value or use the arrows to increase or decrease the value within a predefined range.

The number input element also supports additional attributes such as min, max, and step, which allow you to define the range and increment/decrement behavior of the input. These attributes are useful for enforcing constraints on the input values and providing validation.

Hiding the Number Input Spin Box using CSS

There are two main approaches to hiding the number input spin box using CSS. We will explore both methods, providing step-by-step instructions and code examples for each. These techniques leverage CSS properties to customize the appearance of the number input and remove the spin box.

Method 1: Styling the Number Input with CSS

The first method involves styling the number input element using CSS to achieve the desired appearance while hiding the spin box. Here's how you can do it −

  • Target the number input element in your CSS code using the input[type="number"] selector.

  • Apply CSS properties to modify the appearance of the number input. For example, you can adjust the width, height, border, background color, and text color to match your desired design.

  • Hide the spin box by setting the appearance property to none. This property removes the default styling of the spin box arrows.

  • Optionally, customize the input's focus and hover states to provide visual feedback to users.

By following these steps, you can effectively hide the number input spin box and create a customized appearance for your number inputs.

Example 

Here’s how the example looks like −

<!DOCTYPE html>
<html>
<head>
   <style>
      input[type="number"] {
         width: 200px;
         height: 30px;
         border: 1px solid #ccc;
         background-color: #f7f7f7;
         color: #333;
         appearance: none;
      }
   
      input[type="number"]:focus,
      input[type="number"]:hover {
         border: 1px solid #999;
      }
   </style>
</head>
<body>
   <label for="quantity">Quantity:</label>
   <input type="number" id="quantity" name="quantity" min="1" max="10" value="1">
</body>
</html>

Now, let's move on to the second method of hiding the spin box using CSS.

Method 2: Using the "::-webkit-inner-spin-button" Pseudo-element

The second method involves targeting the "-webkit-inner-spin-button" pseudo-element, which is specific to WebKit-based browsers (such as Chrome and Safari). Here's how you can use this pseudo-element to hide the spin box:

  • Target the number input element in your CSS code using the input[type="number"] selector.

  • Use the "::webkit-inner-spin-button" pseudo-element selector to target the spin box specifically.

  • Apply CSS properties to the pseudo-element to hide it. For example, you can set the display property to none.

  • To ensure compatibility with other browsers, include a fallback solution using the "appearance" property and its respective vendor prefixes.

Example 

Here’s how the example looks like −

<!DOCTYPE html>
<html>
<head>
   <style>
      input[type="number"]::-webkit-inner-spin-button {
         display: none;
      }
   
      /* Fallback for other browsers */
      input[type="number"] {
         appearance: textfield;
         -moz-appearance: textfield;
         -webkit-appearance: textfield;
      }
   </style>
</head>
<body>
   <label for="quantity">Quantity:</label>
   <input type="number" id="quantity" name="quantity" min="1" max="10" value="1">
</body>
</html>

This method specifically targets the WebKit-based browsers and may not be applicable to all browser environments. However, when combined with Method 1, it provides broader compatibility for hiding the spin box.

In the next section, we'll discuss another method for this purpose.

Method 3: Using JavaScript to Disable Spin Box Functionality

By using JavaScript, we can disable the spin box functionality of the number input field altogether. This means that users won't be able to change the value by clicking on the up or down arrows or by using the scroll wheel.

To disable the spin box functionality, we can listen for the "wheel" and "keydown" events on the number input field and prevent the default behavior associated with these events.

Example 

Here's an example of how you can achieve this −

<!DOCTYPE html>
<html>
<head>
   <style>
      /* CSS styles for the number input field */
      .number-input {
         width: 200px;
         height: 30px;
         border: 1px solid lightgray;
         background: #fff;
         color: #333;
      }
   </style>
</head>
<body>
   <label for="quantity">Quantity:</label>
   <input type="number" id="quantity" class="number-input" min="1" max="10" value="1">
   
   <script>
      // Get the number input element
      const numberInput = document.getElementById('quantity');
   
      // Disable the spin box functionality
      numberInput.addEventListener('wheel', (event) => {
         event.preventDefault();
      });
   
      numberInput.addEventListener('keydown', (event) => {
         // Prevent the up and down arrow keys from changing the value
         if (event.key === 'ArrowUp' || event.key === 'ArrowDown') {
            event.preventDefault();
         }
      });
   </script>
</body>
</html>

In this example, the JavaScript code adds event listeners to the number input field for the "wheel" and "keydown" events. When these events occur, we call preventDefault() to prevent the default behavior associated with them. This effectively disables the spin box functionality.

By using this method, the number input field will still be visually present with its default appearance, but the spin box arrows and scroll wheel functionality will be disabled. Users will only be able to enter numeric values manually using the keyboard or other input methods.

Conclusion

In this article, we explored different techniques to hide the number input spin box using CSS and JavaScript. We discussed various methods that allow you to customize the appearance and behavior of the spin box arrows based on your requirements.

Using CSS, you can hide the spin box arrows by leveraging pseudo-elements, adjusting the appearance property, or setting the opacity to 0. These techniques provide a simple and straightforward way to hide the spin box arrows while preserving the functionality of the number input field.

Updated on: 07-Aug-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements