The current more effective solution is transform, which doubles the size and then reduces it by half, but it is cumbersome to write and affects the layout. I also want to know if there is any good and convenient method
I have been struggling with this problem for an afternoon today. The performance on Android is abnormal, but the performance on PC and Mac is good. If it is normal, use margin offset to align. In extreme cases, it is more reliable to transform absolute positioning and center.
You can try vertical-align. In addition, it is difficult for others to answer without code. You can use codepen to make an online demo to facilitate the answerer
Compatibility: The Android2.3 system browser does not support the direct use of fixed for container positioning, and adding a fixed container can solve the problem.
It is true that the simulator simulates vertical centering, but in actual mobile phones, Apple phones render it vertically centered, and Android phones render it higher. The compatible method is not to set height, line- The height is set to 1, and the padding value is equal to the top and bottom to maintain vertical centering.
Pixel density problem, for example, if your height is 20px, the line height is also 20px. If the pixel density is an even number, there is no problem. If it is an odd number, there will be a problem, so it is generally best to match the height and font size settings, such as The text is 14px, and the height is an even number. The text is 15px, and the height is an odd number
The current more effective solution is transform, which doubles the size and then reduces it by half, but it is cumbersome to write and affects the layout. I also want to know if there is any good and convenient method
I have been struggling with this problem for an afternoon today. The performance on Android is abnormal, but the performance on PC and Mac is good. If it is normal, use margin offset to align. In extreme cases, it is more reliable to transform absolute positioning and center.
You can try vertical-align. In addition, it is difficult for others to answer without code. You can use codepen to make an online demo to facilitate the answerer
Use flex layout to achieve vertical centering with just a few lines of code. Just search for the tutorial and you’ll find it
1. Table method:
Implementation method: The table content is originally vertically centered and can be processed by simulating the table.
2.vertical-align: middle
Implementation method: Use empty element placeholder to implement
3. Absolute positioning
4. Use transform to achieve
Compatibility: The Android2.3 system browser does not support the direct use of fixed for container positioning, and adding a fixed container can solve the problem.
Conclusion:
Recommended transform implementation
Try changing your line-height to 30px
It is more suitable to use line-height and vertical-align together
It is true that the simulator simulates vertical centering, but in actual mobile phones, Apple phones render it vertically centered, and Android phones render it higher. The compatible method is not to set height, line- The height is set to 1, and the padding value is equal to the top and bottom to maintain vertical centering.
Pixel density problem, for example, if your height is 20px, the line height is also 20px. If the pixel density is an even number, there is no problem. If it is an odd number, there will be a problem, so it is generally best to match the height and font size settings, such as The text is 14px, and the height is an even number. The text is 15px, and the height is an odd number