views:

69

answers:

6

For example my HTML is this

<ul>
    <li>Sample 1</li>
    <li>Sample 2
        <ul>
            <li>Sub 1</li>
            <li>Sub 2</li>
            <li>Sub 3
                <ul>
                    <li>Grandsub 1</li>
                    <li>Grandsub 2</li>
                    <li>Grandsub 3
                        <ul>
                            <li>verySub 1</li>
                            <li>verySub 2</li>
                        </ul>
                    </li>
                </ul>
            </li>
        </ul>
    </li>
    <li>Sample 3</li>
</ul>

I want to use different styles on every child <UL> without defining any class or id on them.

I dont know how many child <ul> might occur inside one another so inline css will not to the job

Is this possible?

A: 

You can use the "Inline Styling" for each element to have different styles.

Here it is:

<ul style="property:value;">
     <li>..</li>
</ul>
Mr.Expert
I dont know how many child `<ul>` might occur inside one another so inline css will not to the job
Starx
A: 

@artlung has the right answer, misunderstood the question...

Rob
But I dont know how many inside UL it can have
Starx
+3  A: 

All you need is to specify each level like so:

<style type="text/css">
ul li { color: red; }
ul li ul li { color: blue; }
ul li ul li ul li { color: black; }
ul li ul li ul li ul li { color: green; }
</style>

No inline style attributes, no classes required.

Works perfectly on the HTML snippet you provided. Keep in mind, that each successive level will inherit from the one before it. That's the whole idea of the "cascading" part of CSS, but I've burned myself forgetting margins at a lower level and having things go haywire.

artlung
But I dont know how many inside UL it can have
Starx
You still can use relative positioning (left: 20px;) as it'll add up on each li child. Otherwise I wonder what can be represented by more than 6 or 10 or 20 nested lists!
Felipe Alsacreations
Did you understand that the last rule with color: green; will be applied to every list item with depth of 4 AND MORE? What'd be styled by "ul li ul li ul li ul li ul li" will be styled by "ul li ul li ul li ul li" if the former rule doesn't exist.
Felipe Alsacreations
One more note to artlung: you can safely remove "ul" from each selectors, except maybe the first one (that is "ul li li li li"). If you've priority problems, than it'll be time to add some ul to add priority weight.
Felipe Alsacreations
Starx -- is there an upper limit? 20 levels would be 20 lines of code, and it's hard to imagine an outline deeper than 20 levels. Felipe, I'm aware that strictly speaking the `ul`s are optional. But what if some of the lists are `ol` instead? I'd rather be specific, but that's a matter of coding style.
artlung
A: 

If you don't know how many child UL/LI's there may be inside each other, then this won't be possible in CSS.

CSS doesn't support "fuzzy logic" such as: if there are over 5 <li>'s then do something.

Javascript Is the way forward me-thinks!

Neurofluxation
A: 

It looks like you want some way of programmatically defining your style. This is not possible using CSS alone. It does not support you defining your own symbolic names, let alone attempts to do something more 'programmery'. If you were able to generate your CSS dynamically then you could use this to work out the number of levels and algorithmically define the style each time

Otherwise the alternative is to put a maximum on the level of nesting (say 20 levels) and define a style for each one like artlung suggests. Most of the time the lower level definintions won't get used, but they will be there if you need them. This isn't perfect but it's the best you can do with writing directly in CSS.

Rodion Ingles
A: 

This uses jQuery, and cycles through a list of three background colours:

function nestedcolour(elements, level) {
    if (elements.length > 0) {
        var colour = ["#fafafa", "#fbf9ea", "#eeeeee"][level % 3];
        elements.css('background-color', colour);
        nestedcolour(elements.children("ul").children("li"), level + 1);
    }
}

$(document).ready(function() {
    nestedcolour($(".classofparentelement"), 0);
});

The .classofparentelement is not really necessary, you can use any method to find the parent element(s).

gpvos