Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you have <li>s without them being under a <ul> or <ol>?

I have a navigation that I am using a list for. I have it in a <ul>, but that is messing up my UI because it has weird automatic margins. I tried without the <ul> and they seem to work. Would this work across all browsers? Is this legal? Anybody else done this before?

like image 863
chromedude Avatar asked Nov 20 '10 02:11

chromedude


People also ask

Does Li have to be in UL?

Answer: yes it does, but with: ol or menu, that's all!

How do you know if its OL or UL?

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.

What does the LI tag under the UL tag mean?

The <ul> tag defines an unordered (bulleted) list. Use the <ul> tag together with the <li> tag to create unordered lists. Tip: Use CSS to style lists.


2 Answers

It's not valid HTML to use <li> outside an ol, ul, or menu element. It's much better to assign a class to the ul element:

<ul class="nav">

And then use CSS to remove the margin and padding:

.nav {
    margin: 0;
    padding: 0;
}
like image 51
PleaseStand Avatar answered Oct 25 '22 08:10

PleaseStand


It's probably working in browsers because browsers are too forgiving, but that's not valid says the validator:

Document type does not allow element "li" here; missing one of "ul", "ol", "menu", "dir" start-tag

Well, you can remove customize margins in CSS. That's your only solution. Generally, you can remove it on all <ul> tags in the documents by:

ul {
    margin: 0;
    padding: 0;
}

In case you're wondering what padding is, see it here.

like image 36
Ruel Avatar answered Oct 25 '22 09:10

Ruel