将定义列表设置为简单键值集

发布于 2024-08-08 13:38:07 字数 838 浏览 6 评论 0原文

我想使用一个简单的定义列表:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

北城半夏 2024-08-15 13:38:07

我认为这可行:

dt {
  float: left;
  clear: left;
  margin: 0;
  padding: 0 .5em 0 0;
}
dd {
  margin: 0;
  padding: 0;
}

另一个有用的选项是 :after 伪类。您可以跳过每个 dt 元素上的冒号并具有:

dt:after {
  content: ":";
}

这使其更加灵活,您可以在整个列表和网站中更改该字符,或者如果您愿意,可以将其删除它。

要解决您提到的换行问题,您需要设置 dt 和 dd 元素的宽度。或者,使用表格,其中 th 表示名称/地址等,td 表示“数据”。 IMO 表格是语义上可接受的解决方案 - 它是表格数据。

I think this would work:

dt {
  float: left;
  clear: left;
  margin: 0;
  padding: 0 .5em 0 0;
}
dd {
  margin: 0;
  padding: 0;
}

Another option that is useful is the :after pseudo-class. You could skip the colon on each of the dt elements and have:

dt:after {
  content: ":";
}

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 and dd elements. Or, use a table, with th for name/address etc and td for the "data". IMO a table is a semantically acceptable solution here - it is tabular data.

若沐 2024-08-15 13:38:07

您可以像这样将 dd 浮动到前一个元素的左侧。

dt { clear: left; }
dd { float: left; }

您还可以添加宽度属性以进行额外对齐。

dt { clear: left; width: 20%; }
dd { float: left; }

You could just float the dd to the left of the previous element like so.

dt { clear: left; }
dd { float: left; }

You could also add a width attribute to either for extra alignment.

dt { clear: left; width: 20%; }
dd { float: left; }
混浊又暗下来 2024-08-15 13:38:07

好吧,你可以简单地让 dt float: left

dt { float: left; }

或者让 dt 内联显示:

dt { display: inline; }

Well, you could simply let dt float: left

dt { float: left; }

or else let dt display inline:

dt { display: inline; }
墨洒年华 2024-08-15 13:38:07

您需要从 dd 中删除默认的左边距。

dt {float:left;clear:left;margin-right:5px;}
dd {margin:0;}

You need to remove the default left margin from the dd.

dt {float:left;clear:left;margin-right:5px;}
dd {margin:0;}
等风来 2024-08-15 13:38:07

我认为这个(取消注释第一行以测试小宽度):

dl, dd, dt { margin: 0; }
/*dl { width: 80px; }*/
dl dt, dl dd { display: inline; }
dl dd:after { content: ' '; padding-left: 100%; }

I think this (uncomment first line to test for small width):

dl, dd, dt { margin: 0; }
/*dl { width: 80px; }*/
dl dt, dl dd { display: inline; }
dl dd:after { content: ' '; padding-left: 100%; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文