How to apply CSS to list in HTML -


i had create working html/css following nestes list

   root         child1     child11         child2     child21     child22         child3     child31 

so created following

html

<ul class="list-view">             <li>                     <ul><li>chlid11</li></ul>             </li>             <li>                     <ul>                         <li>chlid21</li>                         <li>chlid22</li>                     </ul>             </li>             <li>                     <ul>                         <li>chlid31</li>                     </ul>             </li>         </ul> 

now how able apply css leaf parent , root node .

i have make leaf green , parent red , root should parent underline

here leaf child: 11 , 21, 22 , 31 parent: 3 li root :the first ul

this question asked me in interview trying solve

css has dynamic . mean not suppose add classes directly saying leaf , root .

something

jsfiddle

update

css

.list-view> li:first-child{      color:red;     text-decoration: underline;  }  .list-view> li ul li {      color:red; }  .list-view> li ul li ul li{      color:green; } 

i not able make root node underline thanks

first of all, markup not make sense me. nesting ul's inside li's not useful when li's not contain other content. suppose markup should more this:

<ul>     <li>         <span>root</span>         <ul>             <li>parent</li>             <li>parent                 <ul>                     <li>leaf</li>                     <li>leaf</li>                 </ul>             </li>         </ul>     </li>     <li>root</li> </ul> 

when comes targeting each level css, have number of options. adding classes each level may seem straight forward, can harder maintain, , easier make mistakes. others have demonstrated technique, i'll limit myself few alternatives:

option 1a:

ul { /* root + parent + leaf */ } ul ul { /* parent + leaf */ } ul ul ul { /* leaf */ } 

option 1b:

li { /* root + parent + leaf */ } li li { /* parent + leaf */ } li li li { /* leaf */ } 

option 2:

ul > li { /* root + parent + leaf */ } ul > li > ul > li { /* parent + leaf */ } ul > li > ul > li > ul > li { /* leaf */ } 

that guess, though come variations. option 1a , 1b equivalent. option 2 more specific, , can useful when trying overwrite styles. considered practice keep selectors little specific possible though. way can overwrite them easier later on, , selectors not ridiculously long. keeps code easier read , maintain, go option 1 in case.

note technique requires overwrite styles. styling requested ie. achieved doing this:

li {     color:red;     } li span {     text-decoration: underline; } li li li {       color:green; } 

the pseudo classes speak of in comments (:nth-child, ...) irrelevant here. meant distinguishing between siblings, not parent-child relations.

edit:
text-decoration property bit tricky overwrite. have @ specs on mdn: https://developer.mozilla.org/en-us/docs/web/css/text-decoration

text decorations draw across descendant elements. means not possible disable on descendant text decoration specified on 1 of ancestors.

to solve this, have make sure element underline not parent of rest of tree. th easiest way put in span , apply underline that:

http://jsfiddle.net/r616k0ks/3/

(i have updated code samples above accordingly)


Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -