我想列出一个定义列表<dl>,其中<dt>个元素在一列中,对应的<dd>个元素在另一列中.

棘手的部分是,我希望我的<dt>不会换行,如果它们对于第一列来说太宽,我希望<dd>个元素移动down个元素来腾出空间:

以下是我的目标截图:

the desired result, as described above

以下是ASCII艺术版:

first dt     first dd goes here and can be any length

second dt    second dd is here and also any length

a much longer dt that overlaps
             third dd moves down to make room

last dt      last dd

我得到的最接近的方法是使用FlexBox并排放置元素,然后手动调整附加到Long <dt>元素的<dd>个元素的位置:

dl {
  display: flex;
  flex-flow: row wrap;
}
dt {
  flex-basis: 10em;
  white-space: nowrap;
}
dd {
  flex-basis: calc(100% - 10em);
  margin: 0;
}
dt.long-dt {
  flex-basis: 100%;
}
dt.long-dt + dd {
  flex-basis: 100%;
  padding-left: 10em;
}                
<dl>
  <dt>one</dt>
  <dd>one</dd>
  <dt class="long-dt">two that is longer</dt>
  <dd>two only moves down because I manually told it to in the CSS</dd>
  <dt>three</dt>
  <dd>three</dd>
</dl>

我能想到几个其他的解决方案,但没有一个可以在不手动处理较长的<dt>个元素的情况下干净利落地工作.

如何在css中实现此布局?我很乐意更改HTML,只要它是合理的语义.

推荐答案

这是一个不使用Flex的解决方案,而是浮动的,dtclear-bothmin-width,ddwidthcalc(100% - [min-width of dt]):

dt {
  min-width: 5em;
  white-space: nowrap;
  clear: both;
  float: left;
}
dd {
  margin: 0;
  width: calc(100% - 5em);
  float: right;
}
<dl>
  <dt>one</dt>
  <dd>one</dd>
  <dt>two that is longer</dt>
  <dd>two moves down without having to manually tell it to in the CSS</dd>
  <dt>three</dt>
  <dd>three</dd>
  <dt>four which is also longer</dt>
  <dd>four dd which is even longer and has to wrap into two lines, at least on rather narrow screens or in narrow parent elements. It also moves down without having to manually tell it to in the CSS</dd>
  <dt>five</dt>
  <dd>five</dd>
</dl>

Html相关问答推荐

角|将动态html属性添加到子组件

CSS Flex行之间的空间很大

如何只混合部分的背景而不是内容<>

如何在Vim中删除Html标签?

如何修复与导航栏重叠的css网格?

是否可以部分列出一个HTML有序列表

具有可展开行的棱角material 表(垫子表),折叠时行之间仍有间隙

在网页上的 Select 器中显示选项时出现问题:未在GO模板中传递循环{{range}}的数据

在单独的容器之间堆叠上下文

在悬停时应用文本装饰

平移变换下的SVG剪辑路径行为

Css网格:两列,除非小于150px

如何设计缠绕锚点元素的样式?

如何使VS代码支持以模仿嵌套的方式对已经包含注释的HTML进行注释?

如果您将带有 html 标签的字符串数据(name : test) 放在 `

使用 popover api 和 `

Sass 混合动画未正确应用

如何从另一个 ng-template 获取输入值

我如何让这个动画播放,然后停止,然后在设定的时间后再次播放? (CSS)

使用模板循环每行列出 3 个 bootstrap 卡