Selector for one tag directly followed by another tag

后端 未结 4 1126
死守一世寂寞
死守一世寂寞 2020-12-30 18:18

This selects all tags directly preceded by tags:

A+B {
    /* styling */
}

What is t

4条回答
  •  借酒劲吻你
    2020-12-30 18:37

    Although it's not very handy, nowadays you could achieve this behavior by reversing the order of your elements both when you generate the HTML and by applying the CSS rules: display: flex and flex-direction: column-reverse

    ul {
      display: flex;
      flex-direction: column-reverse;
    }
    
    .b ~ .a {
      color: red;
    }
    • A 3
    • C 2
    • C 1
    • B 1
    • A 2
    • A 1

    Also, if you have 2 or more inline elements, you could achieve it by applying float: right, as they will be displayed in reverse order:

    ul {
      float: left;
      list-style-type: none;
    }
    
    li {
      float: right;
    }
    
    li:not(:first-child) {
      margin-right: 20px;
    }
    
    .b ~ .a {
      color: red;
    }
    • A 3
    • C 2
    • C 1
    • B 1
    • A 2
    • A 1

提交回复
热议问题