简体   繁体   English

如何将 dd 和 dt 元素设置在同一行,dt 右对齐和 dd 左对齐?

[英]How do I style dd and dt elements to be on the same line with dt right aligned and dd left aligned?

I have a definition list of key value pairs, and I want to put them on the same line and align their text so that the dt elements are right aligned and the dd elements are left aligned.我有一个键值对的定义列表,我想将它们放在同一行并对齐它们的文本,以便 dt 元素右对齐而 dd 元素左对齐。 I also would like to add a: inbetween the two, all via CSS.我还想补充一点:在两者之间,都通过 CSS。 I've seen examples of how to get them on the same line by using float: left, but once I add text-align they go back to being on different lines.我已经看到了如何使用 float:left 将它们放在同一行的示例,但是一旦我添加了 text-align 他们 go 就会回到不同的行。 I don't need to use that method, but I'm not familiar enough with CSS to know how to style this to get the desired result.我不需要使用该方法,但我对 CSS 不够熟悉,不知道如何设置此样式以获得所需的结果。 Does anyone know how I could do this?有谁知道我该怎么做?

在此处输入图像描述

Here is my HTML Code:这是我的 HTML 代码:

  <dl>
    <dt>Username</dt> <dd>Username02</dd>
    <dt>Password</dt> <dd>p1HvkNAAx6P</dd>
    <dt>Security Question</dt> <dd>What is your pets name?</dd>
    <dt>Security Answer</dt> <dd>Fred</dd>
    <dt>Security Question</dt> <dd>What is your favorite color?</dd>
    <dt>Security Answer</dt> <dd>Blue</dd>
  </dl>

You can use this CSS to get desired results您可以使用此 CSS 来获得所需的结果

dt,
dd {
  display: inline-block;
  width: calc(50% - 0.5rem);
  margin: 0;
}

dt {
  text-align: right;
}

dt::after {
  content: ":";
}

Example Code 示例代码

This should give you a very good impression:这应该会给你一个很好的印象:

dl {
  display: flex;
  flex-wrap: wrap;
}

dt,
dd {
  box-sizing: border-box;
  width: 50%;
  margin: 0;
}

dt:nth-of-type(2n),
dd:nth-of-type(2n) {
  margin-bottom: 1em;
}

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

dd {
  padding-left: 10px;
}

dt:after {
  content: ":";
}

Working example here: https://codesandbox.io/s/small-bash-qgw43?file=/index.html:255-684此处的工作示例: https://codesandbox.io/s/small-bash-qgw43?file=/index.html:255-684

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM