admin管理员组文章数量:1335887
I thought it would look good if I change the font that is typed into the text box but the letters are not aligned in the middle. enter image description here The letters are instead sticking at the top right corner. How do I fix this? (I don't want to center the letters)
#textInput {
border-radius: 5px;
border-style: solid;
border-color: grey;
height: 20px;
width: 210px;
position: absolute;
font-family: MathGG;
font-size: 20px;
}
<input id="textInput" type="text">
I thought it would look good if I change the font that is typed into the text box but the letters are not aligned in the middle. enter image description here The letters are instead sticking at the top right corner. How do I fix this? (I don't want to center the letters)
#textInput {
border-radius: 5px;
border-style: solid;
border-color: grey;
height: 20px;
width: 210px;
position: absolute;
font-family: MathGG;
font-size: 20px;
}
<input id="textInput" type="text">
I tried using
text-align: middle;
but that didn't work.
Share Improve this question edited Nov 20, 2024 at 20:44 mrconcerned 2,0051 gold badge17 silver badges28 bronze badges asked Nov 19, 2024 at 21:48 AllenAllen 394 bronze badges 2 |4 Answers
Reset to default 0Without seeing it with the actual font you are using we can't provide an exact solution, but you should be able to do this with padding. For example, if the text is too high in the input simply add some top padding.
Adjusting the line-height
might help as well.
#textInput {
border-radius: 5px;
border-style: solid;
border-color: grey;
height: 20px;
width: 210px;
position: absolute;
font-family: MathGG;
font-size: 20px;
line-height: 20px; /* Adjust this */
padding-top:5px; /* And this */
}
<input id="textInput" type="text">
The text in the input field is vertically centered by default. You have to consider all letters/characters, not only the ones that don't descend below the baseline. See the example below:
#textInput {
border-radius: 5px;
border-style: solid;
border-color: grey;
height: 20px;
width: 500px;
position: absolute;
font-family: MathGG;
font-size: 20px;
}
<input id="textInput" type="text" value="it is vertically centered: consider *all* letters like g t p b y etc.">
If you only use uppercase letters (which don't have any descenders), you can use different top and bottom padding – you'll have to find out the right values by trial and error. Example (I only used padding-top
here):
#textInput {
border-radius: 5px;
border-style: solid;
border-color: grey;
height: 20px;
width: 500px;
position: absolute;
font-family: Helvetica;
font-size: 20px;
padding-top:4px;
}
<input id="textInput" type="text" value="UPPER CASE LETTERS ONLY, WITH TOP PADDING">
The line-height
property specifies the height of a line.
Normal height property is used to set height of other html elements but to set height of a text line line-height
property is used.
To vertically align middle of text the line-height
property should be equal to the height
of the element. In this case the line-height
and height
of textbox should be equal.
#textInput {
border-radius: 5px;
border: 1px solid grey;
width: 210px;
font-family: MathGG;
font-size: 20px;
/* added below code to understand better */
height: 50px;
padding: 5px 10px;
margin: auto;
display: block;
line-height: 50px;
}
<input id="textInput" type="text" value="the quick brown fox " />
I tried using
text-align: middle
It should be text-align: center
See MDN
本文标签: htmlHow to align the input text so that it39s in the middle of the text boxStack Overflow
版权声明:本文标题:html - How to align the input text so that it's in the middle of the text box - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742396701a2467070.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
text-align: center
? – PM 77-1 Commented Nov 19, 2024 at 21:56