如何将 html 列宽度固定为其内容的最大宽度?
我正在尝试格式化输入表单的表格,如下所示。该表看起来有点像这样:
Name:
Time of day:
等等,其中每个字段标签后面都跟着正确的输入字段。 我的目标是拥有一个包含两列的表,一列用于字段标签,另一列用于输入字段,这样:
- 左列的大小由最长的字段标签自动确定,以便字段标签永远不会环绕。
- 输入字段本身尽可能向右延伸。
我当然知道我可以将左列固定为 20em 长,另一列固定为 100%,但这不是我要的——我不想测量然后输入固定宽度,例如20em;我希望宽度自动从该列中最长项目的宽度导出。这听起来是一个相当普遍的愿望。有没有办法简单地做到这一点?
I am trying to format a table for an input form as follows. The table looks a bit like this:
Name:
Time of day:
and so forth, where each of those field labels is then followed by the input field proper.
My goals is to have a table with two columns, one for the field labels, the other for the input fields, such that:
- The size of the left column is automatically determined by the longest field label, so that field labels are never wrapped around.
- The input fields themselves extend as much to the right as possible.
I know of course that I can fix the left column to be e.g. 20em long, and the other column 100%, but this is not what I am asking -- I don't want to have to measure and then enter a fixed width like 20em; I would like the width to be automatically derived from the width of the longest item in that column. This sounds like a fairly common wish; is there a way to do it simply?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
white-space: nowrap;
CSS 样式,如下面的代码所示。当然,您需要在两列之间添加边距或填充,这样冒号 (:
) 就不会压在输入框上。 以下代码的 jsFiddle 示例HTML:
CSS:
Use the
white-space: nowrap;
CSS style, as in the code below. Of course, you will want to add margin or padding between the two columns so the colon (:
) isn't pressed against the input box. jsFiddle example of the code belowHTML:
CSS: