如何使用纯 CSS 在表单中设置标签和字段集的样式并获得正确的对齐方式?
作为最后的努力,我要求遵守我的良心并使用 CSS 来做到这一点。我想使用 CSS 来布局这个简单的表单,但我花了两个小时试图得到它,但最终总是遇到对齐问题。我用表格在十分钟内完成了。
我需要右对齐标签,将名称行一分为二,标签与输入文本正确垂直对齐,以及输入的所有右边缘对齐。在 CSS 中需要做什么才能做到这一点?
编辑: 添加我的 CSS 以查看哪里出错了。
这是我对 CSS 的了解:
.form_row {
width: 500px;
}
.form_row label {
text-align: right;
width: 150px;
float: left;
margin-top: 6px;
padding-right: 6px;
}
#id_first_name, #id_last_name {
width: 130px;
}
#id_email, #id_password {
width: 300px;
}
标记:
<div class="form_row">
<label for="id_first_name">Name:</label>
<input id="id_first_name" type="text" name="first_name" />
<input id="id_first_name" type="text" name="last_name" />
</div>
<div class="form_row">
<label for="id_email">Email:</label>
<input type="text" name="email" id="id_email"/>
</div>
<div class="form_row">
<label for="id_password">Password:</label>
<input id="id_password" type="password" name="password" />
</div>
结果:
I'm asking as a last-ditch effort to comply with my conscience and do this with CSS. I want to layout this simple form using CSS, but I've spent two hours trying to get it and always end up with alignment issues. I did it in ten minutes using tables.
I need labels right-justified, the name row split in two, the labels properly vertically aligned with the input text, and all the right edges of the inputs to line up. What does it take to do this in CSS?
EDIT: Adding my CSS to see where I'm going wrong.
Here's as far as I got with the CSS:
.form_row {
width: 500px;
}
.form_row label {
text-align: right;
width: 150px;
float: left;
margin-top: 6px;
padding-right: 6px;
}
#id_first_name, #id_last_name {
width: 130px;
}
#id_email, #id_password {
width: 300px;
}
The Markup:
<div class="form_row">
<label for="id_first_name">Name:</label>
<input id="id_first_name" type="text" name="first_name" />
<input id="id_first_name" type="text" name="last_name" />
</div>
<div class="form_row">
<label for="id_email">Email:</label>
<input type="text" name="email" id="id_email"/>
</div>
<div class="form_row">
<label for="id_password">Password:</label>
<input id="id_password" type="password" name="password" />
</div>
And the result:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你诱惑我接受挑战:) 我使用 CSS 在 10 分钟内就完成了。
只要您同意调整某些元素的
line-height
和设置尺寸(以 px 为单位),我认为这是可以实现的。其他需要注意的事项是
font-size
、padding
和line-height
如何影响文本框及其尺寸。看看这个:http://jsbin.com/osibu3/4
在 IE6+、FF3 中测试。 6+、Chrome、Safari
粘贴供参考:
You tempted me into taking up the challenge :) I just about did it in 10 minutes using CSS.
As long as you're ok with tweaking
line-height
's and settings dimensions in px for some elements I think its achievable.Other things to note are how
font-size
,padding
andline-height
's affect textboxes and their dimensions.Have a look at this: http://jsbin.com/osibu3/4
Tested in IE6+, FF3.6+, Chrome, Safari
Pasting for reference as well:
http://www.blueprintcss.org/
blueprint css 框架可以帮助您使用 div 进行表格布局。它具有简单的用法和良好的文档。
由于您的问题中的信息如此之少,很难给出另一个答案。
http://www.blueprintcss.org/
The blueprint css framework can help you with tabular layout with divs. It has simple usage and good documentation.
It's hard to give another answer with such little information in your question.