Issue with nested Inner <ul> list style - css

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

Related

How to get the contents of a list item to line up vertically?

I like to use list items to list out the contents of a structure, though I'm frustrated by the fact the different elements don't line up. Is there a way in CSS to space the items out uniformly?
<ul class="buses">
<li>
<time>-26s</time>
<time>10m</time>
<time>21m</time>
</li>
<li>
<time>-10s</time>
<time>13m</time>
<time>35m</time>
</li>
<li>
<time>-8s</time>
<time>14m</time>
<time>25m</time>
</li>
<li>
<time>2m</time>
<time>18m</time>
<time>40m</time>
</li>
</ul>
The original MIT source is https://github.com/kaihendry/ltabus/blob/master/static/index.html#L110
a table layout
.buses {
display:table;
}
.buses li {
display:table-row;
}
.buses time {
display:table-cell;
padding:2px 5px;
}
<ul class="buses">
<li>
<time>-26s</time>
<time>10m</time>
<time>21m</time>
</li>
<li>
<time>-10s</time>
<time>13m</time>
<time>35m</time>
</li>
<li>
<time>-8s</time>
<time>14m</time>
<time>25m</time>
</li>
<li>
<time>2m</time>
<time>18m</time>
<time>40m</time>
</li>
</ul>

styling a nested list in CSS

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.

Select link before active link in css

is there a way i can select the previous link from the active link in css?
i want to apply a specific style to the link previous from the active link e.g. link 3 is the active link so i want to style just link 2:
<ul>
<li>link 1</li>
<li>link 2</li>
<li class="active">link 3</li>
<li>link 4</li>
<li>link 5</li>
<li>link 6</li>
</ul>
i know i can easily do this in jquery, but i want to know if there is away that this can be achieved in css.
those links are inline with eachother to make a the navigation menu of the website
Its counter intuitive, but can be done.
You need to reverse your items and float them in the opposite direction, then use the next sibling adjacency selector.
Demo Fiddle
HTML
<ul>
<li>link 6
</li>
<li>link 5
</li>
<li class="active">link 4
</li>
<li>link 3
</li>
<li>link 2
</li>
<li>link 1
</li>
</ul>
CSS
ul {
display:inline-block; /* <-- disguise the float:right position change */
}
li {
float:right; /* <-- re-reverse item ordering in DOM */
display:inline-block;
}
li.active + li a {
color:pink; /* <-- select the next sibling (which due to reversing the DOM elements is now the previous one */
}

How to style list items (with same class) individually

please help me to style this list , I need to set different background image for each list item, but class are same.
<ul>
<li class="sameforall">menu 1</li>
<li class="sameforall">menu 2</li>
<li class="sameforall">menu 3</li>
<li class="sameforall">menu 4</li>
</ul>
I know this one , but it works only for fist item :(
ul:first-child li{
/*my css*/
}
Why would you give all the li's the same class?
Give the ul a class to style the contained li's, then give the li's their own class, like so:
<ul class="sameforall">
<li class="one">menu 1</li>
<li class="two">menu 2</li>
<li class="three">menu 3</li>
<li class="four">menu 4</li>
</ul>
.sameforall {color: red;}
.sameforall .one {background-color: blue;}
.sameforall .two {background-color: green;}
.sameforall .three {background-color: pink;}
.sameforall .four {background-color: purple;}
You can't access the HTML, CSS3 supports :nth-child() psuedo selecting - http://css-tricks.com/how-nth-child-works/
<ul>
<li class="sameforall">menu 1</li>
<li class="sameforall">menu 2</li>
<li class="sameforall">menu 3</li>
<li class="sameforall">menu 4</li>
</ul>
.sameforall:nth-child(1) { background-color: blue; }
.sameforall:nth-child(2) { background-color: green; }
.sameforall:nth-child(3) { background-color: pink; }
.sameforall:nth-child(4) { background-color: purple; }
Note, this won't work in most old browsers.
I would avoid the use of first-child since it's not fully supported and where it is, it's probably still buggy. In regards to referring to the other elements or childs, your best shot would be to give them a different id and style them using it. Like this:
<ul class="sameforall">
<li id='first' >menu 1</li>
<li id='second'>menu 2</li>
<li id='third' >menu 3</li>
<li id='forth' >menu 4</li>
</ul>
Then you would refer to those elements in the css file like this:
#first{/*Your css*/}
If you want to see a list of support browsers for the nth-child visit this page it contains a table with some of the most popular browser versions and the support issues they may have with it.
you need :nth-child() btw it should be
ul li:fist-child
Since you can't change the markup and child selecting via CSS is not supported that well, the only way for you is to do it with JavaScript.
<ul>
<li class="sameforall">menu 1</li>
<li class="sameforall">menu 2</li>
<li class="sameforall">menu 3</li>
<li class="sameforall">menu 4</li>
</ul>
<script type="text/javascript">
var listItems = document.getElementsByTagName("ul")[0].getElementsByTagName("li");
var numChildren = listItems.length;
for(var i = 0; i < numChildren; i++) {
var item = listItems[i];
// -> do whatever you want with each item.
switch(i) {
case 0: item.style.backgroundImage = 'url(item-1.gif);'; break;
case 1: item.style.backgroundImage = 'url(item-2.gif);'; break;
case 2: item.style.backgroundImage = 'url(item-3.gif);'; break;
}
}
</script>
you need to go with the nth-child method.
Here the stuff is well detailed. Hope this will help you.
http://www.w3.org/TR/css3-selectors/

Cufon selector problems

I am using Cufon (http://cufon.shoqolate.com/generate/) to replace some text in a menu.
Problem is that I only need to style the first <li> of the first <ul>.
I have tried using:
Cufon.replace('#menu ul li > a', { fontFamily: 'Christopherhand', hover: { color: '#99c635'}});
With the > seperator, but it does not work. It still replaces the #menu ul li ul li a
This is my markup:
<div id="menu">
<ul>
<li class="current">
About JW
<ul>
<li>Subpage 1</li>
<li>Subpage 2</li>
<li>Subpage 3</li>
<li>Subpage 4</li>
</ul>
</li>
<li>Our Products</li>
<li>Best Recipes</li>
<li>Health & Diet</li>
<li>Our Ads</li>
</ul>
</div>
Can anyone see the problem? It should work without adding a class to sub <ul>. :-)
Thank you.
You can use:
Cufon.replace('#menu > ul:first > li:first > a');
but you have to include jQuery before cufon import in order to use such selector.
https://github.com/sorccu/cufon/wiki/usage

Resources