Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 486
jball Avatar asked Jan 09 '10 00:01

jball


People also ask

Why do you need the UL and the ol tag?

The ol element is used when the list is ordered and the ul element is used when the list is unordered. Definition lists ( dl ) are used to group terms with their definitions. Although the use of this markup can make lists more readable, not all lists need markup.

Is ul necessary HTML?

The HTML Standard includes an example that use <nav> followed by <ul> and <li> . A nav element doesn't have to contain a list, it can contain other kinds of content as well. Based on the Standard, the use of ul and li is personal preference as long as you have nav to indicate navigation.

What is the difference between Li and UL in HTML?

ul stands for unordered list. li stands for list item. They are the HTML tags for "bulleted" lists as opposed to "numbered" lists (which are specified by ol for ordered list).


2 Answers

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.

like image 50
Gregory Pakosz Avatar answered Nov 01 '22 11:11

Gregory Pakosz


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.

alt text

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.

like image 30
NerdStarGamer Avatar answered Nov 01 '22 10:11

NerdStarGamer