Example (using position):
<div style="position: relative; height: 100vh;">
<input type="text" placeholder="使用position定位的文本框" style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);">
</div>
Copy after login
Here, the parent element is set to relative positioning (position: relative;), and the text box is set to absolute positioning (position: absolute;). Move the top left corner of the text box to the center of the parent element via top: 50%; and left: 50%;, then use transform: translate(-50%, -50%); to move its own center to that point, thus Achieve centering effect.
Note:
The choice of alignment depends on your specific needs and layout context.
Try to avoid using inline styles, but define styles in separate CSS files for better management and reuse.
Consider using reset CSS or normalized CSS to eliminate differences in default styles between browsers.
When using modern layout technologies such as Flexbox or Grid, make sure your target browser supports these features, or provide fallback solutions for compatibility with older browsers.
The above is the detailed content of How to align text boxes in html. For more information, please follow other related articles on the PHP Chinese website!