简体   繁体   English

无法弄清楚如何将文本水平对齐到输入的左侧

[英]Can't figure out how to align horizontally text to left of input

this is a very beginner question, but I have been trying to figure out a way to have the Name, Email, and Age aligned to the left of the input fields. 这是一个非常初学者的问题,但是我一直在尝试找出一种将名称,电子邮件和年龄对齐到输入字段左侧的方法。 And then have it centered on the input boxes. 然后将其放在输入框中。

Here is a link to my codepen.io 是我的codepen.io的链接

I have tried floating the text to the left and making the inputs display as blocks. 我尝试将文本浮动到左侧并使输入显示为块。 The input seems to be stuck having an extremely wide width which is making the text not display properly. 输入似乎被卡住,具有非常宽的宽度,这使得文本无法正确显示。 If I manage to shrink the width of the box, and want to adjust the padding it makes the input box bigger! 如果我设法缩小框的宽度,并想要调整填充,它将使输入框变大! Instead of moving the the text and input left or right. 而不是移动文本并向左或向右输入。

 #text-label { width: 50%; } #text-input { width: 40%; display: block; margin: auto; } #email-input { width: 80%; display: block; margin: auto; } #number-input { width: 80%; display: block; margin: auto; } 
 <div id="name-email-age"> <div id="name" class="center"> <label id="text-label"> * Name: </label> <input id="text-input" type="text" name="Enter your name" placeholder="Enter your name" required /> <div id="email" class="center"> <label id="email-label"> * Email: </label> <input id="email-input" type="email" name="Enter your email" placeholder="Enter your Email" required /> </div> <div id="age" class="center"> <label id="age-label"> * Age: </label> <input id="number-input" type="number" name="Enter your age" placeholder="Enter your Age" required max="122" min="5" /> </div> </div> 

Change the input display styles to inline-block . 将输入显示样式更改为inline-block

https://codepen.io/AustinGordon/pen/yEodLo https://codepen.io/AustinGordon/pen/yEodLo

Maybe your can try this: 也许您可以尝试以下方法:

Wrap labels and controls in .control-group for optimum spacing and change box to ideal size =). 将标签和控件包装在.control-group中以获得最佳间距,然后将框更改为理想大小=)。

 #text-label { width: 50%; } #text-input { width: 50%; display: block; } #email-input { width: 100%; display: block; } #number-input { width: 100%; display: block; } .control-group { width: 80%; margin: auto; text-align: left; } 
 <div id="name-email-age"> <div id="name" class="center"> <div class="control-group"> <label id="text-label">* Name:</label> <input id="text-input" type="text" name="Enter your name" placeholder="Enter your name" required /> </div> </div> <div id="email" class="center"> <div class="control-group"> <label id="email-label">* Email:</label> <input id="email-input" type="email" name="Enter your email" placeholder="Enter your Email" required /> </div> </div> <div id="age" class="center"> <div class="control-group"> <label id="age-label">* Age:</label> <input id="number-input" type="number" name="Enter your age" placeholder="Enter your Age" max="122" min="5" required /> </div> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM