布置“标签-值”不使用表格居中
通常,您会有许多标签(让我们使用姓名、年龄、颜色)以及每个标签的值!
如果我将这些放在 2 列 3 行表中,我可以确保这些值(让使用 Steve、19、Red)全部从相同的水平位置开始。
如果我希望将该列左对齐,并且如果我也希望将标签列右对齐。然后,这两列将在表格的中心很好地相遇。
我如何才能在不使用表格的情况下做到这一点,而且也不必设置固定宽度!
So often you will have a number of labels (lets use name, age, colour) and a value for each!
If I placed these In a 2 column 3 row table I could make sure that the values (lets use Steve, 19, Red) all start in the same horizontal position.
And if I wished by having that column left align, and if I also wanted have the label column right aligned. The two columns then would meet nicely in the center of the table..
How could I go about doing this without the use of tables but also not having to set fixed widths!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您将必须解决自己对标签使用固定宽度的问题(如果采用纯 CSS 路线)。下面是获得您想要实现的目标的最低限度(摘自 HTML Dog。)
CSS:
HTML:
如果您绝对不想要固定宽度,那么您可以使用此 javascript(带有 jQuery)并删除上面 CSS 的固定宽度。值得注意的是,如果你让人们关闭 JavaScript,这看起来会很糟糕!
我在 JSFiddle 上给出了一个实例。
考虑到您引用的用例,我实际上认为表格是更好的方法,但是我有一个 对 table/div 表单辩论的看法与大多数人不同。
You're going to have to resolve yourself to using fixed widths for your labels (if going the pure CSS route.) Below is the minimum to get what you're trying to accomplish (pulled from HTML Dog.)
CSS:
HTML:
If you absolute don't want fixed widths, then you can use this javascript (with jQuery) and remove the fixed width from the above CSS. It is worth noting that if you get people with javascript turned off, this will look awful!
I've thrown up a live example on JSFiddle.
Given the use case that you've cited, I actually think tables is the better way to go, but then I have a differing opinion on the table/div form debate than most.
我猜你需要 javascript 来解决这个问题。我不相信有一个纯粹的 CSS 解决方案。
事实上,在标签宽度可能不同,或者宽度可能根据浏览器设置(如文本大小)而变化的多语言网站中工作时,使用固定宽度可能会出现问题......
I guess you need javascript then to fix that. I don't believe there is a pure CSS solution to this.
Indeed working with fixed widths can be a problem when working in a multilanguage site where label widths can differ, or where the widths can change depending on browser settings like text size...
我听到你在说什么,但我可以提醒你,表格可以用于表格数据!
但要使用纯 CSS 来执行此操作,您可以将标签和值向左浮动,然后清除,并将两个元素设置为宽度为 50% 的显示块。
I hear what you're saying, but can I remind you that tables are ok for tabular data!
But to do this using pure CSS you could float the label and value left, clear afterwards, and set both elements to display block with 50% width.
您也可以使用 UL LI 列表来安排此操作。
You can arrange this alternatively using UL LI lists.