如何设置dt和dd的样式以便它们在同一条线上?

ave*_*net 197 html css

使用CSS,我如何设置以下样式:

<dl>
    <dt>Mercury</dt>
    <dd>Mercury (0.4 AU from the Sun) is the closest planet to the Sun and the smallest planet.</dd>
    <dt>Venus</dt>
    <dd>Venus (0.7 AU) is close in size to Earth, (0.815 Earth masses) and like Earth, has a thick silicate mantle around an iron core.</dd>
    <dt>Earth</dt>
    <dd>Earth (1 AU) is the largest and densest of the inner planets, the only one known to have current geological activity.</dd>
</dl>
Run Code Online (Sandbox Code Playgroud)

所以dt一列中的节目内容和dd另一列中的内容,每个dt和相应dd的行在同一行?即生成看起来像这样的东西:

表格式

3zz*_*zzy 137

dl {
  width: 100%;
  overflow: hidden;
  background: #ff0;
  padding: 0;
  margin: 0
}
dt {
  float: left;
  width: 50%;
  /* adjust the width; make sure the total of both is 100% */
  background: #cc0;
  padding: 0;
  margin: 0
}
dd {
  float: left;
  width: 50%;
  /* adjust the width; make sure the total of both is 100% */
  background: #dd0
  padding: 0;
  margin: 0
}
Run Code Online (Sandbox Code Playgroud)
<dl>
  <dt>Mercury</dt>
  <dd>Mercury (0.4 AU from the Sun) is the closest planet to the Sun and the smallest planet.</dd>
  <dt>Venus</dt>
  <dd>Venus (0.7 AU) is close in size to Earth, (0.815 Earth masses) and like Earth, has a thick silicate mantle around an iron core.</dd>
  <dt>Earth</dt>
  <dd>Earth (1 AU) is the largest and densest of the inner planets, the only one known to have current geological activity.</dd>
</dl>
Run Code Online (Sandbox Code Playgroud)

  • 我建议在dt样式中添加一个`clear:left`,以确保它们保持内联,即使它们需要换行. (7认同)
  • 谢谢!这个CSS提示帮助我格式化我的Zend表单,而无需编写表单装饰器类. (2认同)

Nav*_*eth 116

我有一个解决方案,没有使用浮动!
codepen上检查这个

dl.inline dd {
  display: inline;
  margin: 0;
}
dl.inline dd:after{
  display: block;
  content: '';
}
dl.inline dt{
  display: inline-block;
  min-width: 100px;
}
Run Code Online (Sandbox Code Playgroud)



更新- 3 2017年1月:我已经加入柔性盒的问题基础的解决方案.检查链接的codepen中的内容并根据需要对其进行优化.谢谢!

dl.inline-flex {
  display: flex;
  flex-flow: row;
  flex-wrap: wrap;
  width: 300px;      /* set the container width*/
  overflow: visible;
}
dl.inline-flex dt {
  flex: 0 0 50%;
  text-overflow: ellipsis;
  overflow: hidden;
}
dl.inline-flex dd {
  flex:0 0 50%;
  margin-left: auto;
  text-align: left;
  text-overflow: ellipsis;
  overflow: hidden;
}
Run Code Online (Sandbox Code Playgroud)

  • 这适用于DD部分的短文本; 如果文本太长,它将环绕并显示在DT部分下面. (7认同)
  • 即使列表中的某些项为空,这也可以工作.大! (3认同)
  • 完善!那个邪恶的浮动只解决了一个问题,同时给了我两个新问题(比如没有垂直对齐).很好,它现在已经消失了. (2认同)

sil*_*r27 57

如果你使用Bootstrap 3(或更早)...

<dl class="dl-horizontal">
    <dt>Label:</dt>
    <dd>
      Description of planet
    </dd>
    <dt>Label2:</dt>
    <dd>
      Description of planet
    </dd>
</dl>
Run Code Online (Sandbox Code Playgroud)

  • 如果你看一下Bootstrap的CSS,即使不使用Bootstrap也可以了解如何设置样式,尽管我喜欢这个框架.这是一个片段,忽略了响应式设计的媒体查询以简化示例:dl {margin-top:0; margin-bottom:20px} dt,dd {line-height:1.428571429} dt {font-weight:700} dd {margin-left:0} .dl-horizo​​ntal dt {float:left; width:160px; clear:left; text-align:right; overflow:hidden; text-overflow:ellipsis; white-space:nowrap} .dl-水平dd {margin-left:180px} (13认同)
  • `dl-horizo​​ntal`实际上是从Bootstrap 4中删除的.在BS4中,你需要使用网格类. (5认同)

yck*_*art 38

CSS网格布局

与表格一样,网格布局使作者能够将元素对齐到列和行中.
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout

要更改列大小,请查看grid-template-columns属性.

dl {
  display: grid;
  grid-template-columns: max-content auto;
}

dt {
  grid-column-start: 1;
}

dd {
  grid-column-start: 2;
}
Run Code Online (Sandbox Code Playgroud)
<dl>
  <dt>Mercury</dt>
  <dd>Mercury (0.4 AU from the Sun) is the closest planet to the Sun and the smallest planet.</dd>
  <dt>Venus</dt>
  <dd>Venus (0.7 AU) is close in size to Earth, (0.815 Earth masses) and like Earth, has a thick silicate mantle around an iron core.</dd>
  <dt>Earth</dt>
  <dd>Earth (1 AU) is the largest and densest of the inner planets, the only one known to have current geological activity.</dd>
</dl>
Run Code Online (Sandbox Code Playgroud)

  • 现在是2019年,这应该是公认的,并且只是考虑的答案而已! (4认同)

anc*_*ral 21

假设您知道边距的宽度:

dt { float: left; width: 100px; }
dd { margin-left: 100px; }
Run Code Online (Sandbox Code Playgroud)

  • 清洁通常用于这些问题风格的标记.无论如何,欢迎来到StackOverflow,我建议您将这些假设放在问题的正文中,只是为了清楚起见. (6认同)
  • 清洁:具有简单,明确且令人愉悦的形状.答案很简短.它解决了三个陈述中的问题.作为问题的一部分,不知道保证金并不是必需的. (5认同)
  • 我不会把它称为干净,因为你已经硬编码了边缘. (2认同)

Tyl*_*ton 8

因为我还没有看到适用于我的用例的示例,所以这是我能够实现的最全面的解决方案.

dd {
    margin: 0;
}
dd::after {
    content: '\A';
    white-space: pre-line;
}
dd:last-of-type::after {
    content: '';
}
dd, dt {
    display: inline;
}
dd, dt, .address {
    vertical-align: middle;
}
dt {
    font-weight: bolder;
}
dt::after {
    content: ': ';
}
.address {
    display: inline-block;
    white-space: pre;
}
Run Code Online (Sandbox Code Playgroud)
Surrounding

<dl>
  <dt>Phone Number</dt>
  <dd>+1 (800) 555-1234</dd>
  <dt>Email Address</dt>
  <dd><a href="#">example@example.com</a></dd>
  <dt>Postal Address</dt>
  <dd><div class="address">123 FAKE ST<br />EXAMPLE EX  00000</div></dd>
</dl>

Text
Run Code Online (Sandbox Code Playgroud)

奇怪的是,它不起作用display: inline-block.我想,如果你需要设置任何规模dt要素或dd元素,你可以设置dl的显示为display: flexbox; display: -webkit-flex; display: flex;flex的速记dd元素和dt元素,就像这样flex: 1 1 50%display作为display: inline-block.但我没有测试过,所以谨慎行事.


Cas*_*oem 7

您可以使用 CSS 网格:

dl {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}
Run Code Online (Sandbox Code Playgroud)
<dl>
<dt>Title 1</dt>
<dd>Description 1</dd>
<dt>Title 2</dt>
<dd>Description 2</dd>
<dt>Title 3</dt>
<dd>Description 3</dd>
<dt>Title 4</dt>
<dd>Description 4</dd>
<dt>Title 5</dt>
<dd>Description 5</dd>
</dl>
Run Code Online (Sandbox Code Playgroud)


thi*_*der 6

jsFiddle截图

请参阅jsFiddle演示

我需要一个完全按照项目描述的列表,该项目显示公司的员工,左边是他们的照片,右边是信息.我设法在每个之后使用psuedo-elements完成清除DD:

.myList dd:after {
  content: '';
  display: table;
  clear: both;
}
Run Code Online (Sandbox Code Playgroud)

另外,我希望文本只显示在图像的右侧,而不包裹在浮动图像下(伪列效果).这可以通过在标签内容周围添加DIV带有CSS 的元素来实现.您可以省略此额外内容,但标记的内容将包含在浮动内容中.overflow: hidden;DDDIVDDDT

与它玩了一会儿后,我就能够支持多种DT每个元素DD,但不是多个DD每个元素DT.我尝试添加另一个可选类,仅在最后一个之后清除DD,但后续DD元素包含在DT元素下(不是我想要的效果......我希望DTDD元素形成列,而额外DD元素太宽).

通过所有权利,这应该只适用于IE8 +,但由于IE7中的怪癖,它也可以在那里工作.


And*_*nes 5

这是另一个选项,它通过显示 dt 和 dd 内联然后在 dd 之后添加换行符来工作。

dt, dd {
 display: inline;
}
dd:after {
 content:"\a";
 white-space: pre;
}
Run Code Online (Sandbox Code Playgroud)

这类似于上面 Navaneeth 的解决方案,但使用这种方法,内容不会像表格一样排列,但 dd 将在每一行上立即跟在 dt 之后,而不管长度如何。


Ast*_*tim 5

我需要这样做并使<dt>内容相对于内容垂直居中<dd>.我display: inline-block一起用过vertical-align: middle

请在此处查看Codepen上的完整示例

.dl-horizontal {
  font-size: 0;
  text-align: center;

  dt, dd {
    font-size: 16px;
    display: inline-block;
    vertical-align: middle;
    width: calc(50% - 10px);
  }

  dt {
    text-align: right;
    padding-right: 10px;
  }

  dd {
    font-size: 18px;
    text-align: left;
    padding-left: 10px;
  } 
}
Run Code Online (Sandbox Code Playgroud)