将定义列表设置为简单键值集
我想使用一个简单的定义列表:
<dl>
<dt>name:</dt>
<dd>Tomas</dd>
<dt>address:</dt>
<dd>this is a very long wrapping address</dd>
<dt>age:<dt>
<dd>29</dd>
<dl>
渲染类似的内容:
name: Tomas
address: this is a very long
wrapping address
age: 29
定义列表在语义上似乎是这里的最佳选择。
使用新的运行显示样式可以解决问题:
<style> dt { display: run-in; } </style>
但这尚未得到广泛支持。如何在不更改 html 的情况下设置定义列表的样式以获得更好的跨浏览器支持(不需要 ie6)(目前我使用内联显示并添加丑陋的 br)?
编辑以澄清:
dt { clear: left; }
dd { float: left; }
不会工作,因为它会呈现为:
name: Tomas
address: this is a very long
wrapping address
age: 29
设计指定这些多行字段应换行到行首以保留空间。
I'd like to use a definition list as simple as:
<dl>
<dt>name:</dt>
<dd>Tomas</dd>
<dt>address:</dt>
<dd>this is a very long wrapping address</dd>
<dt>age:<dt>
<dd>29</dd>
<dl>
to render something like:
name: Tomas
address: this is a very long
wrapping address
age: 29
The definition list seems semantically the best option here.
Using the new run-in display style will do the trick:
<style> dt { display: run-in; } </style>
But this isn't widely supported yet. How can I style my definition list for better cross-browser support (ie6 not necessary), without changing the html (currently I use display inline and add ugly br's) ?
Edit to clarify :
dt { clear: left; }
dd { float: left; }
won't work because it would render as:
name: Tomas
address: this is a very long
wrapping address
age: 29
The design specifies that these multi-line field should wrap to the start of line to preserve space.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为这可行:
另一个有用的选项是
:after
伪类。您可以跳过每个 dt 元素上的冒号并具有:这使其更加灵活,您可以在整个列表和网站中更改该字符,或者如果您愿意,可以将其删除它。
要解决您提到的换行问题,您需要设置 dt 和 dd 元素的宽度。或者,使用表格,其中
th
表示名称/地址等,td
表示“数据”。 IMO 表格是语义上可接受的解决方案 - 它是表格数据。I think this would work:
Another option that is useful is the
:after
pseudo-class. You could skip the colon on each of thedt
elements and have:This makes it more flexible and you can change that character across the list and website as a whole, or get rid of it if you felt like it.
To solve the wrapping problem you mentioned, you'll need to set widths on both the
dt
anddd
elements. Or, use a table, withth
for name/address etc andtd
for the "data". IMO a table is a semantically acceptable solution here - it is tabular data.您可以像这样将 dd 浮动到前一个元素的左侧。
您还可以添加宽度属性以进行额外对齐。
You could just float the dd to the left of the previous element like so.
You could also add a width attribute to either for extra alignment.
好吧,你可以简单地让 dt float: left
或者让 dt 内联显示:
Well, you could simply let dt float: left
or else let dt display inline:
您需要从 dd 中删除默认的左边距。
You need to remove the default left margin from the dd.
我认为这个(取消注释第一行以测试小宽度):
I think this (uncomment first line to test for small width):