Geek Answers Handbook

    How to create a CSS Selector that selects an Xn tag, including descendants

    Let's say I have the following DOM tree:

    <div class="box">
    <ul class="gallery">
        <li id="1">text</li>
        <li id="2">text</li>
        <li id="3">text</li>
        <li id="4">text</li>
        <li id="5">text</li>
        <li id="6">text</li>
        <li id="7">text</li>
        <li id="8">text</li>
        <li id="9">text</li>
    </ul>
    <div id="random"></div>
    <ul class="gallery">
        <li id="10">text</li>
        <li id="11">text</li>
        <li id="12">text</li>
        <li id="13">text</li>
        <li id="14">text</li>
        <li id="15">text</li>
        <li id="16">text</li>
        <li id="17">text</li>
        <li id="18">text</li>
        <li id="19">text</li>
        <li id="20">text</li>
    </ul>
    </div>
    

    I want to create a CSS selector that will select every 6th tag <li>under a div with a class field. But I need the selector to take into account whole tags <li>on the page and not take them into account in the tag <ul>. Therefore, at the end, the selector must choose <li>with identifiers 6,12,18. Currently, I was able to create a selector that selects identifiers 6 and 15 when I used:

    div.box li:nth-of-type(6n)
    
    • Note 1: ID numbers are added for reference only. In reality, tags <li>do not have a class or identifier.
    • 2: <li> <ul> . </ul> <li>.
    • 3: HTML, , , CSS .. JS . DOM jQuery , , .

    +4
    css css-selectors
    Roy Peleg 17 . '13 22:14
    3

    , , ...

    Sych Fabrício, CSS. , , html.

    , .

    ?

    , , , , CSS:

    • ul, .box, .gallery.
    • (, , ) li ( , , ).

    html , , ( color, ):

    ul.gallery:nth-of-type(2n+1) li:nth-of-type(6n) {
        color: red;
    }
    
    ul.gallery:nth-of-type(2n+2) li:nth-of-type(6n+3) {
        color: red;
    }
    

    , , , , , html , , , , .gallery , ( , 6 , .gallery 7 ).

    , , html , , css . ( , # 1 # 2 ), css .

    +3
    ScottS 18 . '13 16:25

    nth-child nth-of-type .

    , CSS-, li . 6- JavaScript.

    , CSS n- : . , .

    , . nth-child , , , , , DOM . , , "scoped" nth selectors a la CSS Counters. .

    +1
    Fabrício Matté 17 . '13 22:41

    CSS , " DOM". .

    jQuery, , , , ": nth-from-top (n)", JQuery DOM.

    Note that this type of selector will be much slower because it cannot take advantage of the performance improvements provided by the built-in DOM methods.

    +1
    Sych Nov 17 '13 at 10:53
    source share

    More articles:

    • How to build very individual line styles with matplot in R - r
    • How to show text in an image - javascript
    • Web Based Versioning - c #
    • Type error while writing max function - haskell
    • Grails Error "java.lang.instrument FAULT" - grails
    • Rails File type html.erb, the difference between <%%> and <% =%> - ruby ​​| fooobar.com
    • Visual Studio 2013 intellisense not enough information while typing - c #
    • bash quotes in a variable handled differently when expanded to a command - bash
    • UILocalNotification scheduled in the background does not work - ios
    • Chrome HTML5 video frame radius not working - javascript

    All Articles

    Geek Answers | 2019