According to this well-rated SO post Proper way to make HTML nested list? the best-practice way to make a nested list is this:
<ul>
<li>List item one</li>
<li>List item two with subitems:
<ul>
<li>Subitem 1</li>
<li>Subitem 2</li>
</ul>
</li>
<li>Final list item</li> </ul>
however I'm having real problems styling a list made in this way. I want each item in the list to have a specific height but I can't use li { height: 40px; } because the height of the second li also includes all the inner list. See here for an example http://jsfiddle.net/rujg3zyk.
The problem comes down to the fact that the second outer li element contains both some plain text and a block display element. This seems like a 'code smell' to me.
what's the best way of formatting this list so that each line is 40px high?
Apply line-height instead of height
ul li {
background-color:yellow;
line-height:40px;
}
ul li li {
background-color:red;
line-height:40px;
}
height:40px will apply 40px for all the listed items, so that two clild 'li' wont fit inside the 40px of the parent 'li'
The way you have given here, is not a valid syntax:
<ul>
<li>List item one</li>
<li>List item two with subitems:</li>
<!-- Problem here... -->
<ul>
<li>Subitem 1</li>
<li>Subitem 2</li>
</ul>
<li>Final list item</li>
</ul>
You cannot nest <ul> directly under <ul> in this case. You need to do is:
<ul>
<li>List item one</li>
<li>List item two with subitems:
<ul>
<li>Subitem 1</li>
<li>Subitem 2</li>
</ul>
</li>
<li>Final list item</li>
</ul>
And the above code is perfectly valid. You don't need to use a height but try using min-height. I strongly advice you against using height (as that has to be calculated by the contents).
Your code
Your code :
<ul>
<li>List item one</li>
<li>List item two with subitems:
<ul>
<li>Subitem 1</li>
<li>Subitem 2</li>
</ul>
</li>
<li>Final list item</li>
</ul>
is correct you need some changes read below:
The nested list should be inside a <li> element of the list in which it is nested.
Link to the W3C Wiki on Lists (taken from comment below): HTML Lists Wiki.
Link to the HTML5 W3C ul spec: HTML5 ul. Note that a ul element may contain exactly zero or more li elements. The same applies to HTML5 ol.
The description list (HTML5 dl) is similar, but allows both dt and dd elements.
More Notes:
dl = definition list.
ol = ordered list (numbers).
ul = unordered list (bullets).
I don't know if this is what you are looking for, but you could use min-height instead of height:
ul li {
background-color:yellow;
min-height: 40px;
}
ul li li {
background-color:red;
}
<ul>
<li>List item one</li>
<li>List item two with subitems:
<ul>
<li>Subitem 1</li>
<li>Subitem 2</li>
</ul>
</li>
<li>Final list item</li>
</ul>
Of course, it could expand to higher heights if there is more content, so that is why I am not sure if that is what you are looking for.
Hope this helps.
Related
I am trying to hide several menu items from my mobile menu using the nth-child selector in CSS.
Here is the source code HTML and CSS: https://jsfiddle.net/jf1r12wh/
The HTML is something like this:
<ul class="mobile">
<li>Item 1</l1>
<li>Item 2</li>
<li>Item 3</li>
<ul><li>Submenu item 1</li>
<li>Submenu item 2</li>
<li>Submenu item 3</li></ul></ul>
I want to use the nth-child (or similar) to hide Item 1 and 2 on the mobile menu, but I don't want it to hide Submenu item 1 and Submenu Item 2, which it's doing.
I'm using this:
.mobile li:nth-child(1){
display: none !important;
}
.mobile li:nth-child(2) {
display: none !important;
}
The problem is that it's applying this to the submenu as well. How can I make it not to do that, and only apply to the main menu items?
All you have to do is show that the rule should only apply to direct children via the use of >
Like this:
.mobile > li:nth-child(2) {
display: none !important;
}
As Paulie_D mentioned in his comment, this is a part of specificity.
EDIT:
Here is a working snippet:
.mobile li:nth-child(1){
color: red;
}
.mobile > li:nth-child(2) {
color: red;
}
<ul class="mobile">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>
<ul>
<li>Submenu item 1</li>
<li>Submenu item 2</li>
<li>Submenu item 3</li>
</ul>
</li>
</ul>
For future reference, I would also like to point out that the correct semantic for a ul inside a ul is for the second ul to be inside it's own li
"The children (direct descendants) of a ul element must all be li elements". I've made sure that my code snippet reflects this for you.
hi and thanks for checking.
i have a list, that has multiple nestings. i'd like to change the order of these two lists and i wonder if this is possible with only css?
List item 1
List item 2
List item 2.1
List item 2.2
List item 3
i'd like to change the order to:
List item 1
List item 2
List item 3
List item 2.1
List item 2.2
since the length of the list is dynamic, i can't use absolute positioning or stuff like that.
No, you cannot. you must order that data where it is rendered, at client side (javascript) or back side; css is helpless in this case
Its a tricky one, but you can do it in a way by using CSS Flexbox order property.
You need to change the properties of <ul> and <li> elements. Have a look at the code snippet below.
body {
padding: 2em;
}
/* Resetting properties of <ul> */
ul {
padding: 0;
margin: 0;
list-style: disc;
}
/* Applying Flex properties to <ul> */
ul.normal-list {
display: flex;
flex-direction: column;
}
/* Changing the order of all the <li>s except the <ul> with the class 'nested-list' */
li.nested-list {
order: 1;
}
<ul class="normal-list">
<li>List Item 1</li>
<li>List Item 2</li>
<li class="nested-list">
<ul>
<li>List Item 2.1</li>
<li>List Item 2.2</li>
</ul>
</li>
<li>List Item 3</li>
<li class="nested-list">
<ul>
<li>List Item 3.1</li>
<li>List Item 3.2</li>
</ul>
</li>
<li>List Item 4</li>
</ul>
I want to create css to generate the following nested list.
1. item1
subitem
subitem
2. item2
subitem
subitem
What I want is to modify the numbers (either bold or red). I searched in the internet but what I found was css for an ordered list. When I create a nested list with that css, what I obtain is extra numbers in place of the bullets. Can someone help?
You can use CSS counter only on li's that are direct children of ol with this HTML structure and then change color and font-weight.
ol {
list-style: none;
counter-reset: ol-counter;
}
ol > li:before {
counter-increment: ol-counter;
content: counter(ol-counter) ". ";
color: red;
font-weight: bold;
}
<ol>
<li>item1
<ul>
<li>sub item</li>
<li>sub item</li>
</ul>
</li>
<li>item2
<ul>
<li>sub item</li>
<li>sub item</li>
</ul>
</li>
</ol>
Can you please take a look at this demo and let me know how I can fix the left indent of the inner <ul> and get rid of the dropped gray area before the child red background?
<ul class="sidebar-navigation">
<li>List 1
<ul class="sidebar-inner-list">
<li> Link</li>
<li> Link </li>
</ul>
</li>
<li>List 2
<ul class="sidebar-inner-list">
<li> Link 2</li>
<li> Link 2</li>
<li> Link 2</li>
</ul>
</li>
</ul>
body{color:#fff;}
ul{list-style-type:none;}
li{background-color:#2d2d2d;}
li:hover{ background-color:#ccc;}
.sidebar-inner-list>li{ background-color:red; margin-left:-40px;}
http://jsfiddle.net/sQWE6/4/
ul { padding:0 }
Just remove the automatic padding from the list. I guess the real question here is why don't you just use <div>s? They don't have automatic padding, and you don't seem to want an actual list (list-style-type: none).
The ul list items has a padding-left by default, remove this and you should be good.
.sidebar-inner-list{
padding-left:0;
}
DEMO
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is there a CSS parent selector?
CSS selector for “foo that contains bar”?
I have a set of nested unordered lists, and I want to be able to style just the <li> parent items that have children. The html look something like this:
<ul>
<li>item one</li>
<li>item two</li>
<li>item three
<ul>
<li>child item</li>
</ul>
</li
</ul>
I want to be able to add a background image, i.e., to ONLY "item three". What's the easiest way to do this?
I'd use jQuery to loop through the UL and find LI elements with UL children. Like so maybe?:
<style type="text/css">
.newClass {
background: #059;
}
</style>
<script>
$(document).ready(function() {
$('#mainUL li').each(function(i) {
if ($(this).children('ul').length > 0) {
$(this).addClass('newClass');
}
});
});
</script>
<ul id="mainUL">
<li>item one</li>
<li>item two</li>
<li>item three
<ul>
<li>child item</li>
</ul>
</li>
</ul>