Home > Article > Web Front-end > How to center the text box in html
There are many ways to center HTML text boxes: text input box: use CSS code input[type="text"] { text-align: center; } text area: use CSS code textarea { text-align: center; }Horizontal centering: Use text-align: center style on the parent element of the text box. Vertical centering: Use vertical-align attribute input[type="text"] { vertical-align: middle; }Flexbox: Use display:
How to center a text box in HTML
There are many ways to center a text box in HTML, depending on what you want Centered text box type and overall layout.
Text input box
For text input box, you can use the following CSS code:
<code class="css">input[type="text"] { text-align: center; }</code>
Text area
For the text area, you can use the following CSS code:
<code class="css">textarea { text-align: center; }</code>
Horizontal centering
If you need to center the text box horizontally, you can do it on the parent element of the text box Use text-align: center
Style:
<code class="html"><div style="text-align: center;"> <input type="text" value="文本"> </div></code>
Vertical center
For vertical centering, you can use vertical-align
Properties:
<code class="css">input[type="text"] { vertical-align: middle; }</code>
Via Flexbox
Flexbox is a powerful layout system that can easily center text boxes:
<code class="html"><div style="display: flex; justify-content: center;"> <input type="text" value="文本"> </div></code>
Note Make sure the parent element of the text box has a clear width and height.
If you have long text in the text box, consider using the
The above is the detailed content of How to center the text box in html. For more information, please follow other related articles on the PHP Chinese website!