Is using <li> without enclosing <ul> tags dangerous? [closed]
What are the dangers of inserting <li>...</li>
into a page without enclosing the item(s) in a <ul>
block? For example:
<div style="border:solid 1px red;">
<li>Item</li>
<li>Another Item</li>
<li>Yet Another Item</li>
</div>
Validation is the least of my concerns, I'm wondering what this might break in browsers for the end user's ability to view the page as intended.
Solution 1:
It's not valid markup at all. If it gets displayed correctly, it's only a matter of luck.
As you seem to define dangerous by "break in browsers for the end user's ability to view the page as intended", then yes it's dangerous.
Browsers are trying their best to compensate for invalid markup but there is no guarantee at all your page gets displayed correctly.
You say validation is the least of your concerns, please reconsider and have a look at Why Validate?. If you care about your page being displayed correctly with no quirks, then validate.
Finally, HTML Tidy may help you fixing existing html.
EDIT: I submitted your fragment to browsershots.org to see how it gets rendered by different browsers.
Solution 2:
Using invalid markup like your example can cause unexpected behavior in different pages. If you use valid markup, browsers will (or should) display your content based on the spec. But if you use invalid markup, the browser will try and interperet the markup and display the page how it thinks you meant it to be. Sometimes they will display it how you want, sometimes not. Here's an example from Firefox 3.5 on a Mac.
The first list is your code, but with the proper <ul>
tag replacing the <div>
tag. The second list is your code exactly. Notice that the second list is missing the default margins on the left and bullets.
Basically, nothing will die if you use invalid markup like this, but it's really bad practice since it will lead to unexpected and inconsistent results.
Solution 3:
That's the same as using <td>
tags inside <p>
. Sure, it might work somehow on some browsers, but it will definitely be broken. The behaviour of such construct is undefined, there are no guarantees on how it will work on different browsers. Not to speak of accessibility, screen reader programs would be quite perplexed about this kind of structure.
Why can't you use a proper <ul>
or <ol>
tag? It can be styled and handles the same way as a <div>
.
Solution 4:
This is echoing everybody else's answer, in part: with any markup, there's an empirical question of whether it works or not on the browsers that you are testing it on. You tested it on all your browsers, and your happy. That's cool and is a huge part of the work.
But beyond that, there's also the hypothetical question of how well it might work on browser-platform combinations that you haven't tested, either because:
- you didn't include them in the test set
- they don't exist yet or
- they're not available for testing (web-crawlers, for instance)
For this set, which always becomes relevant sooner or later, you should follow standards -- when possible -- and use the enclosing UL or OL tags (in this case).
It's also worth mentioning that it makes the LI tags easier to track down if you are doing scripting, or if someone is doing scripting on your page (e.g., Greasemonkey).