使用填充设计 HTML FORM 元素,宽度为 100%
我通过使用具有固定宽度列的表格来设计表单,并且希望 内的输入元素填充容器。我知道 CSS 盒模型,也知道元素会随着
width: 100%
渗透,但问题在于它的一致性。
元素按预期渗透,但
元素有什么用?我可以在 Firebug 中看到它们与输入元素具有相同的框模型属性,但它们的渲染效果不同。
我正在使用 HTML 5 文档类型,这在 Firefox 和 Chrome 中都会发生。
现在,我正在使用 JS 函数修复此问题,该函数选择所有具有拉伸类的元素并计算和设置静态宽度以使其适合容器内。这完美地排列了表格的元素。 (我必须排除
有没有一个纯 CSS 解决方案?我不想每次更新页面的一部分时都运行这个函数,比如 AJAX 调用......
I'm styling a form by using a table with fixed-width columns and I want the input elements inside the <td>
to fill the container. I know the CSS box model and I know the elements would bleed through with width: 100%
, but the problem is with its consistency.
<input>
elements bleed through as expected but <select>
elements don’t. This results in making my fields not line up properly. I've tried all properties like overflow, display, whitespace... it doesn’t make any difference. What’s with the <select>
element? I can see in Firebug that they have the same box model properties with the input element, but they don’t render the same.
I’m using HTML 5 doctype and this happens both in Firefox and Chrome.
Right now, I’m fixing this using a JS function which selects all elements with class stretch and computes and sets the static width to make it fit inside the container. This perfectly lines up the elements of the form. (I had to exclude <select>
elements because their widths were already okay... weird quirk.)
Is there a pure CSS solution to this? I wouldn’t want to run this function everytime a part of the page is updated, like on AJAX calls...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以在文本字段和文本区域上使用
box-sizing: border-box;
。它解决了与选择框的差异。
You could use
box-sizing: border-box;
on textfields and textarea's.It solves te difference with the selectbox.
最好的方法是用 div 来伪造元素的边框。
有了这个CSS:
当然,您可以将其扩展到其他领域。某些浏览器可能会给您带来问题。 Chrome 和 webkit 允许您调整文本区域的大小,但如果您将
resize: none;
添加到 CSS,它应该禁用它,但 YMMV。The best way is to fake the borders of the elements with a div.
With this CSS:
Of course, you can expand that for other fields. Some browsers might give you issues. Chrome and webkit allow you to resize textareas but if you add
resize: none;
to your CSS, it should disable it but YMMV.它可能会帮助您了解以下各种可用性研究的结果。
1) 对于大多数表单,人们更喜欢看到表单元素上方的标签:
2) 人们发现,如果表单元素的大小适当,有助于表明需要多少信息,那么它会很有用。
注意:此示例中的列表将被设置样式,以便它不会显示为项目符号列表。以这种方式使用列表有助于提高可访问性,因为屏幕阅读器会告诉用户列表中包含多少项。
我认为这可能很有用,因为它表明您尝试在表格中布局表单并将所有输入拉伸到相同的长度可能有点浪费精力。
http://dev.w3.org/html5/markup/input.html#输入
It may help you to know the following results from various usability studies.
1) For most forms, people prefer to see the label just above the form element:
2) People find it useful if the form elements are sized appropriately to help suggest how much information is expected.
Note: the list in this example would be styled so that it doesn't appear as a bullet-point list. Using lists in this way helps with accessibility as screen readers will tell the user how many items are contained in the list.
I thought this might be useful as it suggests that your efforts may be a bit wasted trying to layout the form in a table and stretch all inputs to the same length.
http://dev.w3.org/html5/markup/input.html#input
这不是最有帮助的答案,但表单元素的 CSS 样式在浏览器之间非常不可靠。像您这样的 JavaScript 解决方案是最好的选择。
不可靠的两个原因:
表单元素的某些功能无法用 CSS 来描述。
元素在不同操作系统上的不同外观。
尝试找出哪些 CSS 属性应该影响表单元素以及如何影响表单元素,对于浏览器制造商来说是一个麻烦事,因此他们大多不去管它。 Safari 是一个明显的例外。参见例如 http://webkit.org/blog/17 /the-new-form-controls-checkbox-2/
您可以认为,无论网站所有者的意图如何,网站之间的表单元素应该看起来相同,以便用户知道他们正在点击什么.
请参阅http://meyerweb.com/eric/thoughts/2007/ 05/15/formal-weirdness/ 进行更深入的检查。
Not the most helpful answer, but CSS styling of form elements is pretty unreliable between browsers. A JavaScript solution like yours is the best bet.
Two reasons for the unreliability:
Some features of form elements can’t be described by CSS. The
<select>
element is a good example: there aren’t any CSS properties that can describe the different ways a<select>
element looks on different operating systems.Trying to work out which CSS properties should affect form elements, and how, is a rat’s nest for browser makers, so they’ve mostly left it alone. Safari is a notable exception; see e.g. http://webkit.org/blog/17/the-new-form-controls-checkbox-2/
You can argue that form elements should look the same between sites regardless of the site owners’ intentions, so that users know what they’re clicking on.
See http://meyerweb.com/eric/thoughts/2007/05/15/formal-weirdness/ for a deeper examination.
假设您的 html 看起来有点像这样:
仅使用
input
和select
来设置宽度怎么样?还是我把你的问题理解错了?
Say your html looks somewhat like this:
How about just using the
input
andselect
for setting the width?Or did I get your problem wrong?
以下 CSS 适用于 Moz Firefox、html 输入元素(提交、按钮、文本)、文本区域元素,甚至选择元素。在我尝试的浏览器中,选择的元素的长度几乎相等。
The following CSS works for Moz Firefox, for html input elements (submit, button, text), textarea elements, and even select elements. The select elements are nearly equal length in the browser I'm trying.