Using CSS, how can I style the following:
- Mercury
- Mercury (0.4 AU from the Sun) is the closest planet to th
I've found a solution that seems perfect to me, but it needs extra <div>
tags.
It turns out that it is not necessary to use <table>
tag to align as in a table, it suffices to use display:table-row;
and display:table-cell;
styles:
<style type="text/css">
dl > div {
display: table-row;
}
dl > div > dt {
display: table-cell;
background: #ff0;
}
dl > div > dd {
display: table-cell;
padding-left: 1em;
background: #0ff;
}
</style>
<dl>
<div>
<dt>Mercury</dt>
<dd>Mercury (0.4 AU from the Sun) is the closest planet to the Sun and the smallest planet.</dd>
</div>
<div>
<dt>Venus</dt>
<dd>Venus (0.7 AU) is close in size to Earth, (0.815 Earth masses) and like Earth, has a thick silicate mantle around an iron core.</dd>
</div>
<div>
<dt>Earth</dt>
<dd>Earth (1 AU) is the largest and densest of the inner planets, the only one known to have current geological activity.</dd>
</div>
</dl>
Most of what people here suggested works, however you should only place generic code in to the style sheet, and place the specific code in to the html code as shown below. Otherwise you will end up with a bloated style sheet.
Here is how I do it:
Your style sheet code:
<style>
dt {
float:left;
}
dd {
border-left:2px dotted #aaa;
padding-left: 1em;
margin: .5em;
}
</style>
Your html code:
<dl>
<dt>1st Entity</dt>
<dd style="margin-left: 5em;">Consumer</dd>
<dt>2nd Entity</dt>
<dd style="margin-left: 5em;">Merchant</dd>
<dt>3rd Entity</dt>
<dd style="margin-left: 5em;">Provider, or cToken direct to Merchant</dd>
<dt>4th Entity</dt>
<dd style="margin-left: 5em;">cToken to Provider</dd>
</dl>
Looks like this
Assuming you know the width of the margin:
dt { float: left; width: 100px; }
dd { margin-left: 100px; }
I recently needed to mix inline and non-inline dt/dd pairs, by specifying the class dl-inline
on <dt>
elements that should be followed by inline <dd>
elements.
dt.dl-inline {
display: inline;
}
dt.dl-inline:before {
content:"";
display:block;
}
dt.dl-inline + dd {
display: inline;
margin-left: 0.5em;
clear:right;
}
<dl>
<dt>The first term.</dt>
<dd>Definition of the first term. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque a placerat odio viverra fusce.</dd>
<dt class="dl-inline">The second term.</dt>
<dd>Definition of the second term. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque a placerat odio viverra fusce.</dd>
<dt class="dl-inline">The third term.</dt>
<dd>Definition of the third term. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque a placerat odio viverra fusce.</dd>
<dt>The fourth term</dt>
<dd>Definition of the fourth term. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque a placerat odio viverra fusce.</dd>
</dl
>
I need to do this and have the <dt>
content vertically centered, relative to the <dd>
content. I used display: inline-block
, together with vertical-align: middle
See full example on Codepen here
.dl-horizontal {
font-size: 0;
text-align: center;
dt, dd {
font-size: 16px;
display: inline-block;
vertical-align: middle;
width: calc(50% - 10px);
}
dt {
text-align: right;
padding-right: 10px;
}
dd {
font-size: 18px;
text-align: left;
padding-left: 10px;
}
}
You can use CSS Grid:
dl {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
}
<dl>
<dt>Title 1</dt>
<dd>Description 1</dd>
<dt>Title 2</dt>
<dd>Description 2</dd>
<dt>Title 3</dt>
<dd>Description 3</dd>
<dt>Title 4</dt>
<dd>Description 4</dd>
<dt>Title 5</dt>
<dd>Description 5</dd>
</dl>