The best solution for implementing digital plus and minus buttons with pure CSS

The best solution for implementing digital plus and minus buttons with pure CSS

Preface:

For the implementation of digital addition and subtraction buttons, many solutions have been used before, such as:

1. Use background images - This has a better effect, but the disadvantage is that the style control is a bit complicated and images are required;

2. Use "+" and "-" directly - this method is simple and crude, and the easiest to implement. The disadvantage is that the display is slightly different in different browser environments, and the size of the symbol and the thickness of the line are not easy to adjust;

3. Use unicode characters. This has almost the same problem as method 2, but the compatibility is not good, and some mobile phones have not been able to display the characters.

4. Use CSS style and tags to generate absolutely positioned horizontal and vertical lines, and then adjust their positions to form a plus sign. The disadvantage is that different browsers may have a slight misalignment of the horizontal and vertical combinations. This can be solved by using tags to generate two identical horizontal and vertical lines, and then rotating one of them 90 degrees, which will have a better effect.

For the above, the best one is the first one which uses pictures. Although it is a bit troublesome, it has the best effect and you don’t have to worry about compatibility. The others are not recommended.

Recently, I discovered a new method, which is to use background-image style of CSS3 and combine it with the linear gradient linear-gradient to synthesize the plus sign. The specific implementation is as follows.

Key code:

<a href="javascript:" class="btn btn_plus" role="button" title="Add"></a>
<input class="inputNum" value="1" size="1">
<a href="javascript:" class="btn btn_minus" role="button" title="Reduce"></a>
.inputNum {
    vertical-align: middle;
    height: 22px;
    border: 1px solid #d0d0d0;
    text-align: center;
}
.btn {
    display: inline-block;
    vertical-align: middle;
    background: #f0f0f0 no-repeat center;
    border: 1px solid #d0d0d0;
    width: 24px;
    height: 24px;
    border-radius: 2px;
    box-shadow: 0 1px rgba(100, 100, 100, .1);
    color: #666;
    transition: color .2s, background-color .2s;
}
.btn:active {
    box-shadow: inset 0 1px rgba(100, 100, 100, .1);
}
.btn:hover {
    background-color: #e9e9e9;
    color: #333;
}
.btn_plus {
    background-image: linear-gradient(to top, currentColor, currentColor), linear-gradient(to top, currentColor, currentColor);
    background-size: 10px 2px, 2px 10px;
}
.btn_minus {
    background-image: linear-gradient(to top, currentColor, currentColor);
    background-size: 10px 2px;
}

Among them, the key styles are the two ends in bold at the end. It has been verified that this method has good compatibility, which can be said to be the same as h5/css3 compatibility.

This is the simplest implementation method I have ever seen. I admire the guy who thought of this usage.

Summarize

The above is the best solution for implementing digital addition and subtraction buttons with pure CSS introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!
If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

<<:  Web Design Experience: Efficiently Writing Web Code

>>:  Introduction to JavaScript conditional access attributes and arrow functions

Recommend

JavaScript Basics Operators

Table of contents 1. Operators Summarize 1. Opera...

MySql fuzzy query json keyword retrieval solution example

Table of contents Preface Option 1: Option 2: Opt...

MySQL 5.7.33 installation process detailed illustration

Table of contents Installation package download I...

Vue implements automatic jump to login page when token expires

The project was tested these days, and the tester...

CSS sets the list style and creates the navigation menu implementation code

1. Set the list symbol list-style-type: attribute...

Why MySQL does not recommend deleting data

Table of contents Preface InnoDB storage architec...

Examples of correct use of maps in WeChat mini programs

Table of contents Preface 1. Preparation 2. Actua...

Detailed example of concatenating multiple fields in mysql

The MySQL query result row field splicing can be ...

Windows cannot start MySQL service and reports error 1067 solution

Suddenly when I logged into MySQL, it said that a...

Native JS realizes compound motion of various motions

This article shares with you a compound motion im...

A brief discussion on when MySQL uses internal temporary tables

union execution For ease of analysis, use the fol...

Detailed explanation of single-row function code of date type in MySQL

Date-type single-row functions in MySQL: CURDATE(...

How to configure pseudo-static and client-adaptive Nginx

The backend uses the thinkphp3.2.3 framework. If ...