Home > Article > Web Front-end > How can I customize the increment arrows of an input of type number using CSS?
In this scenario, you aim to transform an input of type number into a more visually appealing component, with customized increment arrows. The given example uses CSS techniques and a clever approach to achieve the desired effect.
Solution:
The first step involves hiding the default increment arrows using CSS:
.form-control[type="number"]::-webkit-inner-spin-button, .form-control[type="number"]::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }
This hides the arrows, allowing you to create your own custom design.
Next, create two separate buttons for the increment and decrement functions:
<button class="btn btn-plus">+</button> <button class="btn btn-minus">-</button>
Position these buttons on either side of the input field using an input-group:
<div class="input-group inline-group"> <div class="input-group-prepend"><button class="btn btn-outline-secondary btn-minus">-</button></div> <input class="form-control quantity" min="0" name="quantity" value="1" type="number"> <div class="input-group-append"><button class="btn btn-outline-secondary btn-plus">+</button></div> </div>
Finally, add JavaScript to handle the increment and decrement operations when the buttons are clicked:
$('.btn-plus, .btn-minus').on('click', function(e) { const isNegative = $(e.target).closest('.btn-minus').is('.btn-minus'); const input = $(e.target).closest('.input-group').find('input'); if (input.is('input')) { input[0][isNegative ? 'stepDown' : 'stepUp']() } })
This approach combines CSS to hide the default arrows, custom buttons to provide the increment and decrement functionality, and JavaScript to handle the button clicks and update the input's value accordingly, giving you the desired customized increment arrows on your input of type number.
The above is the detailed content of How can I customize the increment arrows of an input of type number using CSS?. For more information, please follow other related articles on the PHP Chinese website!