How to offset the nth child of an element

前端 未结 2 2078
星月不相逢
星月不相逢 2021-01-22 11:02

I am trying to extract the nth child of an element, so that elements appear stacked within the same container.

I\'ve tried reading over https://developer.mozilla.org/en

相关标签:
2条回答
  • 2021-01-22 11:34

    You could use padding left and do something like

    p:nth-child(2){
     padding-left: 50px;
    }

    0 讨论(0)
  • 2021-01-22 11:40

    This isn't possible with plain CSS unfortunately.

    You can use Javascript or a CSS preprocessor (probably a postprocessor as well?).

    Here is how I would approach it with Sass, which would compile to CSS:

    @for $i from 1 through 2 {
      p:nth-child(#{$i}) {
        top: $i * 100px;
        left: $i * 50px;
        position: relative;
      }  
    }
    

    and here's a quick demo: http://www.sassmeister.com/gist/8af65851d1c404be698f

    0 讨论(0)
提交回复
热议问题