Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Styling DT to float left of DD

Tags:

html

css

I'm trying to style a definition list such that each dt floats to the left of its corresponding dd elements. I have used:

dt {
    clear: both;
    float: left;
    width: 6em;
}
dd {
    margin-left: 6.5em;
}

which works as far as it goes. It nicely handles multiple dd elements per dt and dd text that is long enough to span multiple lines.

However, I'd really like to be able to cope with multiple dt elements per dd (which is valid HTML) and dt elements that are taller than the corresponding dd elements (due to length of text causing the lines to wrap). At this point the styling falls apart and subsequent dd elements get out of line with their dt.

I have tried various things such as floating the dd as well, which breaks alignment of multiple dd elements. Adding dd + dd { clear: both; } which almost works but now long dd text sits under its dt (not to mention older browsers not respecting the rule).

Has anyone managed to do this? I really don't want to give up and use a table but maybe it's appropriate.

My test markup is here: http://pastebin.com/nmAQ5Xdm

like image 634
Rob Fletcher Avatar asked Feb 02 '11 08:02

Rob Fletcher


People also ask

How do you style DT and DD so on the same line?

This can be accomplished by adding a DIV element with the CSS overflow: hidden; around the content of the DD tag. You can omit this extra DIV , but the content of the DD tag will wrap under the floated DT .

How do you make a div float left?

Use CSS property to set the height and width of div and use display property to place div in side-by-side format. float:left; This property is used for those elements(div) that will float on left side. float:right; This property is used for those elements(div) that will float on right side.

How do you float to the left in HTML?

This can be done by using one of the CSS property called float with its position value. Float property can be used with values as Right, Left, none, inline-start, inline-end.

How do you use DT and DD?

The <dt> tag is used to specify the description list. The <dd> tag stands for definition description and used to denote the description or definition of an item in a description list. The <dl> tag is used to represent the description list. This tag is used with <dt> and <dd> tags.


1 Answers

I have figured out a way to do this when you don't mind constraining the width of the dl element:

dl {
    max-width: 30em;
    overflow: hidden;
}
dt, dd {
    margin-top: 0.5em;
}
dt {
    clear: both;
    float: left;
    width: 6em;
}
dd {
    float: right;
    margin-left: 0.5em;
    width: 22.5em;
}

It's not perfect as the styling breaks if the viewport is too narrow for the dl.

like image 117
Rob Fletcher Avatar answered Sep 30 '22 18:09

Rob Fletcher