我正在尝试为我的网站开发一个布局,其中定义列表的元素将水平排列,有点像这样:
term 1 term 2 term 3 definition 1 definition 2 definition 3
任何人都知道如何使用有效的CSS制作定义列表?或者,如果我不能使用<dl>
,那么推荐的结构是什么?
答案 0 :(得分:7)
这应该这样做:
<dl>
<dt>term 1</dt>
<dd>definition 1</dd>
</dl>
<dl>
<dt>term 2</dt>
<dd>definition 2</dd>
</dl>
<dl>
<dt>term 3</dt>
<dd>definition 3</dd>
</dl>
在CSS中:
dl {
float: left;
}
dl dd {
display: block;
margin: 0;
}
根据需要应用其他样式。可以在这里找到一个工作示例:
答案 1 :(得分:1)
Zombie question resurrection but the accepted answer totally breaks the whole reason for using a dl
in the first place. There's a better way.
From the HTML specification for DLs (emphasis mine):
In order to annotate groups with microdata attributes, or other global attributes that apply to whole groups, or just for styling purposes, each group in a dl element can be wrapped in a div element. This does not change the semantics of the dl element.
Of course, easily solved by flexbox.
dl {
display: flex;
justify-content: space-between;
}
/* Below is just styling. Adjust as needed. */
dt {
font-weight: bold;
}
dt,
dd {
margin: 0;
padding: 0;
}
/* Small screens */
@media screen and (max-width: 400px) {
dl {
flex-direction: column;
}
dl div {
padding: 1em 1em 0 1em;
}
}
<dl>
<div>
<dt>term 1</dt>
<dd>definition 1</dd>
</div>
<div>
<dt>term 2</dt>
<dd>definition 2</dd>
<dd>definition 2.1</dd>
<dd>definition 2.2</dd>
</div>
<div>
<dt>term 3</dt>
<dd>definition 3</dd>
</div>
<div>
<dt>term 4</dt>
<dd>definition 4</dd>
</div>
</dl>