Ok, I'm stuck trying to solve this one with CSS only although I'm not entirely sure if it's possible, so before I resort to JavaScript I need to ask here.
I need to target a sibling with the class column-title of the first <li> in the following list:
<ul>
<li class="column-title">Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li class="column-title">Item 4</li>
<li>Item 5</li>
</ul>
Consider that the sibling can be in any position, for example:
(5th spot)
<ul>
<li class="column-title">Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li class="column-title">Item 5</li>
</ul>
(3rd spot)
<ul>
<li class="column-title">Item 1</li>
<li>Item 2</li>
<li class="column-title">Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
</ul>
I don't think this is possible with CSS only, right?
Thanks.
EDIT--
Oh boy, I found the solution:
Use the sibling combinator: ~
.column-title ~ .column-title { background:#ddd; }
This way the property will be applied to ANY sibling of the first .column-title li.
PS. I found the answer at the same time crowjonah was posting his. I chose his answer as the selected answer anyway.
You can try the semi-supported "general sibling combinator", ~. Something like:
li.column-title ~ li.column-title {color: red;}
Here's a basic fiddle showing it being done.
And then you might need a shim like this for IE7 support, if that's up your alley.
Seeing your update, you should know that for best results, if you are excluding the first instance of li.column-title from your siblings rule, you should specify :first-child:
ul li.column-title:first-child {color: blue;}
ul li.column-title:first-child ~ li.column-title {color: red;}
How about using a not filter:
Here's a jsfiddle to demonstrate.
ul>li.column-title:not(:first-child) {
color: red;
}
Since it's css3, it won't be supported in pre IE9
Related
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.
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 */
}
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>
Can something like li:nth-child be used to style the first ten items in a list?
<ol>
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
<li>item 4</li>
<li>item 5</li>
<li>item 6</li>
<li>item 7</li>
<li>item 8</li>
<li>item 9</li>
<li>item 10</li>
<li>item 11</li>
<li>item 12</li>
</ol>
so 1 to 10 will be fancy and 11 and 12 will be normal.
I'd rather not use a class if possible.
nth childs example:
:nth-child(-n+10)
this in works here: link.
more on understanding this check out this site.
I guess if you want IE support, i can't really make this any prettier. Atleast I don't know how with this cheap hack.
ul>li + li + li + li + li + li + li + li + li + li + li{
text-align: center; /*makes everything after 10 centered*/
}
http://jsfiddle.net/TzLqZ/ for an example of this above
Here is the IE way with first 10 being center and the last 2 being normal:
http://jsfiddle.net/TzLqZ/3/
ol>li{
text-align: center;
color: blue;
}
ol>li+li+li+li+li+li+li+li+li+li+li
{
text-align: left;
color: red;
}
I think you are looking for the nth child pseudo-selector seen here http://css-tricks.com/how-nth-child-works/
I think for what what you want to do:
select all but top ten:
ul li:nth-child(n+11){}
or just top ten:
ul li:nth-child(-n+10){}
should do the trick.
However, Internet Explorer does not support this at least until ie8 according to the article. So don't rely on it for anything critical (although I don't know what child specific styling would be critical).
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/