I'm working with a specification requiring that the following code:
<dl>
<dt>Key</dt>
<dd>012345678901234567890123456789</dd>
</dl>
...render like so:
Key 01234567890123456
7890123456789
When the contents of the DD contain spaces, it works just fine, but when there are no spaces, it fails. Even including dd { word-wrap: break-word; }
doesn't quite work, and results in the following:
Key
0123456789012345678901
23456789
My current CSS looks like this:
dl {
margin: 1em 0;
padding: 0;
width: 250px;
}
dt {
float: left;
clear: left;
margin: 0;
padding: 0 .5em 0 0;
font-weight: bold;
}
dd {
margin: 0;
padding: 0;
word-wrap: break-word;
}
dd:after {
content: ".";
visibility: hidden;
clear: left;
}
How can I achieve my intended effect using only HTML and CSS?
I have a demo up here, as well, to attempt to clarify my needs: http://cdpn.io/Ezujp (warning, contains SCSS rather than CSS, but it's not incredibly complex, so it should be easy to mentally translate).
I read, learned from, and made use of https://stackoverflow.com/a/1577397/771948, and while it got me closer to my intended solution, it did not meet all my needs.
I am not required to use a DL. If there is another way of achieving my intended effect in a cross-browser (IE7+) way, then I would be willing to use that instead, provided it is semantically correct and doesn't involve too many hacks.
I would try dt { display: run-in; }
but that doesn't work in Firefox, and it still didn't result in any difference form what I have now (when dd { word-wrap: break-word }
was included, the dd began on the next line, and then wrapped, just like it does now).
Edit: After doing a bit more searching around SO, I found this answer: https://stackoverflow.com/a/3284425/771948
It presented a few alternative options that are easier to style than a DL, but I'm not sure on the semantics. Nonetheless, I set up a working prototype using a ul structured in the following manner:
<ul>
<li><strong>Key</strong>012345678901234567890123456789</li>
</ul>
...which, when styled as follows:
ul {
outline: 1px solid #F00;
margin: 1em 0;
padding: 0;
width: 250px;
}
li {
width: 100%;
outline: 1px solid #00F;
margin: 0;
padding: 0;
word-wrap: break-word;
}
strong {
white-space: nowrap;
}
...renders as intended, but I'm not sure this is semantically correct, nor screen reader accessible. Does anyone have any useful advice for me in this scenario?
Thank you in advance. If nothing else, I hope this proves a useful resource to others in similar situations (because of links to two other SO answers for similar questions).
` option from above. I really like the ZWSP option mentioned in your article, and it would have been the correct answer if not for that meddling IE 7 and 8. When I got it working in Chrome/Firefox with a combination of `word-break` and ZWSP, it broke in IE, and getting it working in IE broke it in Chrome/Firefox. The `
– Adrian Mar 05 '13 at 19:34` option works just fine in every browser without issue, and has the advantage of still being legible to screen readers and not requiring JavaScript.