HTML <dt> Tag
The HTML <dt>
tag is used to define a term or name in a description list. It is a child element of the <dl>
(description list) element and is paired with the <dd>
tag, which provides the description or value associated with the term.
The <dt>
tag is a semantic element that helps structure data in a meaningful way, making it useful for glossaries, key-value pairs, and other descriptive content.
Basic Syntax of HTML <dt> Tag
The basic syntax for the <dt>
tag is:
<dl>
<dt>Term</dt>
<dd>Description</dd>
</dl>
The <dt>
tag must always appear inside a <dl>
element and can be followed by one or more <dd>
elements.
Attributes of HTML <dt> Tag
- Global Attributes: The
<dt>
tag supports all global attributes, such asid
,class
,style
, anddata-*
. - Event Attributes: The
<dt>
tag supports event attributes such asonclick
,onmouseover
, andonkeydown
.
Basic Example of HTML <dt> Tag
Here’s a simple example using the <dt>
tag:
index.html
<!DOCTYPE html>
<html>
<body>
<h2>Description List Example</h2>
<dl>
<dt>HTML</dt>
<dd>A markup language for creating web pages.</dd>
<dt>CSS</dt>
<dd>A stylesheet language used to style web pages.</dd>
<dt>JavaScript</dt>
<dd>A programming language used to create interactive web pages.</dd>
</dl>
</body>
</html>
Explanation: The <dt>
tags define the terms (“HTML”, “CSS”, “JavaScript”), and the <dd>
tags provide their descriptions.
Styling the <dt> Tag with CSS
The <dt>
tag can be styled using CSS to enhance its appearance and distinguish it from the descriptions:
index.html
<!DOCTYPE html>
<html>
<head>
<style>
dt {
font-weight: bold;
font-size: 1.2em;
color: #333;
margin-top: 10px;
}
dd {
margin-left: 20px;
color: #555;
}
</style>
</head>
<body>
<h2>Styled Description List</h2>
<dl>
<dt>Planet Earth</dt>
<dd>The third planet from the Sun in the Solar System.</dd>
<dt>Sun</dt>
<dd>The star at the center of the Solar System.</dd>
<dt>Moon</dt>
<dd>The natural satellite of Earth.</dd>
</dl>
</body>
</html>
Result: The terms appear bold and larger, while the descriptions are indented for better readability.
Using Multiple Descriptions with a Single Term
A single <dt>
tag can be followed by multiple <dd>
tags to provide more detailed descriptions:
index.html
<!DOCTYPE html>
<html>
<body>
<h2>Multiple Descriptions Example</h2>
<dl>
<dt>JavaScript</dt>
<dd>A programming language for the web.</dd>
<dd>Used for building dynamic and interactive web applications.</dd>
</dl>
</body>
</html>
Explanation: The term “JavaScript” is defined once using a single <dt>
, but it has two associated descriptions.
Practical Applications of the <dt> Tag
- Glossaries: Define terms and their meanings in a structured format.
- Specifications: Represent product features or technical details.
- FAQs: Use
<dt>
for questions and<dd>
for answers.
Advanced Example: Nested Description Lists
You can nest description lists using the <dt>
tag to create hierarchical structures:
index.html
<!DOCTYPE html>
<html>
<body>
<h2>Nested Description List</h2>
<dl>
<dt>Fruits</dt>
<dd>
<dl>
<dt>Apple</dt>
<dd>A sweet fruit.</dd>
<dt>Banana</dt>
<dd>A tropical fruit.</dd>
</dl>
</dd>
<dt>Vegetables</dt>
<dd>
<dl>
<dt>Carrot</dt>
<dd>A root vegetable.</dd>
<dt>Broccoli</dt>
<dd>A green vegetable.</dd>
</dl>
</dd>
</dl>
</body>
</html>
Explanation: In this example, the parent description list contains terms (e.g., “Fruits”) that lead to nested lists for more detailed descriptions.