`<dl>` - Description list
<dl>
is a tag in HTML used to create a description list, associating terms with their descriptions through the child tags <dt>
and <dd>
. This structure is particularly suitable for displaying glossaries, metadata, or any key-value paired content.
Basic Structure of <dl>
A standard <dl>
consists of multiple <dt>
(definition term) and <dd>
(definition description) pairs. Browsers typically indent <dd>
content by default to create a visual hierarchy:
<dl>
<dt>HTML</dt>
<dd>HyperText Markup Language, used for building webpage structures</dd>
<dt>CSS</dt>
<dd>Cascading Style Sheets, controlling webpage presentation</dd>
</dl>
Rendered effect:
- HTML HyperText Markup Language, used for building webpage structures
- CSS Cascading Style Sheets, controlling webpage presentation
Complex Nesting Usage
<dl>
supports multi-level nesting, making it ideal for grouped displays:
<dl>
<dt>Frontend Technologies</dt>
<dd>
<dl>
<dt>React</dt>
<dd>A UI library developed by Facebook</dd>
<dt>Vue</dt>
<dd>A progressive JavaScript framework</dd>
</dl>
</dd>
</dl>
Practical Use Cases
Metadata Display
<dl class="metadata">
<dt>Author</dt>
<dd>John Doe</dd>
<dt>Publish Date</dt>
<dd>2023-08-20</dd>
<dt>Tags</dt>
<dd>HTML</dd>
<dd>Frontend Development</dd>
</dl>
Frequently Asked Questions
<dl class="faq">
<dt>How to reset password?</dt>
<dd>Click the "Forgot Password" link on the login page</dd>
<dt>What payment methods are supported?</dt>
<dd>Credit cards, Alipay, WeChat Pay</dd>
</dl>
Styling Tips
CSS can be used to create richer visual effects:
/* Horizontally aligned description list */
dl.horizontal {
display: grid;
grid-template-columns: max-content 1fr;
gap: 0.5rem 2rem;
}
/* Add dividers */
dl.divider dd {
padding-bottom: 1rem;
border-bottom: 1px solid #eee;
margin-bottom: 1rem;
}
Accessibility Considerations
- Screen readers clearly distinguish the relationship between
<dt>
and<dd>
- Avoid using
<dl>
as a substitute for tables to display data - Each
<dt>
should correspond to at least one<dd>
Comparison with Other Tags
Tag | Purpose | Example Scenario |
---|---|---|
<ul> |
Unordered list | Navigation menu items |
<ol> |
Ordered list | Step-by-step instructions |
<dl> |
Key-value pairs | Term definitions/Metadata |
Dynamic Content Handling
JavaScript can dynamically manipulate description lists:
// Add new term
function addTerm(term, definition) {
const dl = document.querySelector('dl');
const dt = document.createElement('dt');
dt.textContent = term;
const dd = document.createElement('dd');
dd.textContent = definition;
dl.append(dt, dd);
}
// Example call
addTerm('ES6', 'ECMAScript 2015 Standard');
Browser Compatibility
<dl>
is fully supported in all modern browsers, including:
- Chrome 1+
- Firefox 1+
- Safari 3+
- Edge 12+
- Opera 7+
For IE browsers, note:
- IE7 and earlier versions have limited styling support for
<dl>
- Complex nesting in IE may require additional CSS fixes
Advanced Layout Example
Combine with Flexbox for responsive layouts:
<style>
dl.responsive {
display: flex;
flex-wrap: wrap;
}
dl.responsive dt {
flex-basis: 20%;
font-weight: bold;
}
dl.responsive dd {
flex-basis: 80%;
margin-left: auto;
}
@media (max-width: 600px) {
dl.responsive dt,
dl.responsive dd {
flex-basis: 100%;
}
}
</style>
<dl class="responsive">
<dt>Project Name</dt>
<dd>Responsive Website Redesign</dd>
<dt>Project Duration</dt>
<dd>2023.06-2023.12</dd>
</dl>
Semantic Best Practices
- Prioritize
<dl>
when displaying name/value pairs - Avoid using
<dl>
solely for indentation effects - Commonly used as flexible field containers in CMS systems
- Combine with
<figure>
to create captioned diagrams:
<figure>
<img src="chart.png" alt="Sales Trend Chart">
<figcaption>
<dl>
<dt>Chart Description</dt>
<dd>2023 Quarterly Sales Data Comparison</dd>
<dt>Data Source</dt>
<dd>Finance Department Reports</dd>
</dl>
</figcaption>
</figure>
Integration with Other Technologies
Dynamically generate description lists in Vue/React:
// React example
function Glossary(props) {
return (
<dl>
{props.items.map(item => (
<>
<dt key={`dt-${item.id}`}>{item.term}</dt>
<dd key={`dd-${item.id}`}>{item.definition}</dd>
</>
))}
</dl>
);
}
<!-- Vue example -->
<template>
<dl>
<template v-for="item in items" :key="item.id">
<dt>{{ item.term }}</dt>
<dd>{{ item.definition }}</dd>
</template>
</dl>
</template>
Historical Evolution
The HTML specification for <dl>
has undergone significant changes:
- HTML4: Strictly required
<dt>
and<dd>
to appear in pairs - HTML5: Allowed more flexible structures, supporting multiple
<dd>
for one<dt>
- Current WHATWG standard: Further clarified that
<div>
can be used to wrap groups
Usage in Forms
Though uncommon, <dl>
can organize form controls:
<dl class="form-group">
<dt><label for="username">Username</label></dt>
<dd><input type="text" id="username"></dd>
<dt><label for="email">Email</label></dt>
<dd><input type="email" id="email"></dd>
</dl>
Microformat Applications
Enhance semantics with microformats:
<dl class="vcard">
<dt class="fn">Name</dt>
<dd class="n">Jane Doe</dd>
<dt class="tel">Phone</dt>
<dd>138-0013-8000</dd>
</dl>
Print Optimization
Special styling for print media:
@media print {
dl {
page-break-inside: avoid;
}
dt {
font-weight: bold;
border-bottom: 1pt solid #999;
}
}
本站部分内容来自互联网,一切版权均归源网站或源作者所有。
如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn