简体   繁体   English

dl、dt 和 dd 元素的 CSS,使它们显示为居中并作为键排列:值

[英]CSS for dl, dt and dd elements so that they appear centered and lined up as key: value

I'd like to have key\/values listed like this:我想要这样列出键\/值:

Username: myhandle
Password: lalalalalala
     Key: somevalue

I'm not completely sure from your question what you're looking for but is close to this or this ? 从您的问题中我不能完全确定您要寻找的是什么,但与这个这个接近吗? Here's the CSS I used: 这是我使用的CSS:

body {
    display: table;
    margin: 0 auto;
}
dt {
    text-align: right;
    font-weight: bold;
}
dt:after {
    content: ":";
}
dd {
    text-align: left;
    padding-left: 1em;
}
dl {
    display: table-row;
}
dt, dd {
    display: table-cell;
}

A couple of notes: this depends on having a parent element you can apply display: table to, it doesn't have to be body but that was the only one available in your JSFiddle. 一些注意事项:这取决于您可以将display: table应用于其父元素,它不一定要是body但这是JSFiddle中唯一可用的元素。 The other thing that will break it is if you have any markup where there are multiple dd elements for one dt . 会破坏它的另一件事是,如果您有任何标记,其中一个dt有多个dd元素。

Another way to do it is to use flex<\/code> :另一种方法是使用flex<\/code> :

dl {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: flex-start;
  margin: 0;
  word-break: break-word;
}

dt {
  width: 50%;
  text-align: right;
}

dt:after {
  content: ":";
}

dd {
  box-sizing: border-box;
  width: 50%;
  padding-left: 1rem;
}

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

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