Day 58: ordering nested layers

On day 43, we’ve learned how to group layers and on day 46, how to order them. In this post, we’ll look into ordering grouped layers.

If we take the following layer, the background color of the <p> will be red because the last defined layer has precedence over previously defined layers.

@layer base {
  @layer reset {
    p {
      background-color: #fff;
    }
  }

  @layer theme {
    p {
      background-color: aqua;
    }
  }

  @layer defaults {
    p {
      background-color: red;
    }
  }
}

yo!

We can change the order by defining the layers within the base layer upfront.

@layer base {
  @layer reset, defaults, theme;

  @layer reset {
    p {
      background-color: #fff;
    }
  }

  @layer theme {
    p {
      background-color: aqua;
    }
  }

  @layer defaults {
    p {
      background-color: red;
    }
  }
}

reset has the lowest priority and theme the highest.

yo!

If we move the list outside of the base layer, our custom order has no effect because the layers in the list only exist inside the base layer.

@layer reset, defaults, theme;
@layer base {
  @layer reset {
    p {
      background-color: #fff;
    }
  }

  @layer theme {
    p {
      background-color: aqua;
    }
  }

  @layer defaults {
    p {
      background-color: red;
    }
  }
}

yo!

If we want to change the order inside a layer from the outside, we can do that by referencing the nested layer on the parent layer, similar to how you would reference a property in a JavaScript object.

@layer base.reset, base.defaults, base.theme;
@layer base {
  @layer reset {
    p {
      background-color: #fff;
    }
  }

  @layer theme {
    p {
      background-color: aqua;
    }
  }

  @layer defaults {
    p {
      background-color: red;
    }
  }
}

yo!

If we add another parent layer to the mix, you can see how the background color is hotpink even though we’ve added the component.first layer early in the list. That’s because top level layers are sorted first, and then the layers within each layer group. The parent layer component has precedence over the base layer because it comes later in the list.

@layer base.reset, component.first, base.defaults, base.theme;
@layer base {
  @layer reset {
    p {
      background-color: #fff;
    }
  }

  @layer theme {
    p {
      background-color: aqua;
    }
  }

  @layer defaults {
    p {
      background-color: red;
    }
  }
}

@layer component {
  @layer first {
    p {
      background-color: hotpink;
    }
  }
}

yo!

My blog doesn’t support comments yet, but you can reply via blog@matuzo.at.


This content originally appeared on Manuel Matuzović - Blog and was authored by Manuel Matuzović

On day 43, we've learned how to group layers and on day 46, how to order them. In this post, we’ll look into ordering grouped layers.

If we take the following layer, the background color of the <p> will be red because the last defined layer has precedence over previously defined layers.

@layer base {
  @layer reset {
    p {
      background-color: #fff;
    }
  }

  @layer theme {
    p {
      background-color: aqua;
    }
  }

  @layer defaults {
    p {
      background-color: red;
    }
  }
}

yo!

We can change the order by defining the layers within the base layer upfront.

@layer base {
  @layer reset, defaults, theme;

  @layer reset {
    p {
      background-color: #fff;
    }
  }

  @layer theme {
    p {
      background-color: aqua;
    }
  }

  @layer defaults {
    p {
      background-color: red;
    }
  }
}

reset has the lowest priority and theme the highest.

yo!

If we move the list outside of the base layer, our custom order has no effect because the layers in the list only exist inside the base layer.

@layer reset, defaults, theme;
@layer base {
  @layer reset {
    p {
      background-color: #fff;
    }
  }

  @layer theme {
    p {
      background-color: aqua;
    }
  }

  @layer defaults {
    p {
      background-color: red;
    }
  }
}

yo!

If we want to change the order inside a layer from the outside, we can do that by referencing the nested layer on the parent layer, similar to how you would reference a property in a JavaScript object.

@layer base.reset, base.defaults, base.theme;
@layer base {
  @layer reset {
    p {
      background-color: #fff;
    }
  }

  @layer theme {
    p {
      background-color: aqua;
    }
  }

  @layer defaults {
    p {
      background-color: red;
    }
  }
}

yo!

If we add another parent layer to the mix, you can see how the background color is hotpink even though we’ve added the component.first layer early in the list. That’s because top level layers are sorted first, and then the layers within each layer group. The parent layer component has precedence over the base layer because it comes later in the list.

@layer base.reset, component.first, base.defaults, base.theme;
@layer base {
  @layer reset {
    p {
      background-color: #fff;
    }
  }

  @layer theme {
    p {
      background-color: aqua;
    }
  }

  @layer defaults {
    p {
      background-color: red;
    }
  }
}

@layer component {
  @layer first {
    p {
      background-color: hotpink;
    }
  }
}

yo!

My blog doesn't support comments yet, but you can reply via blog@matuzo.at.


This content originally appeared on Manuel Matuzović - Blog and was authored by Manuel Matuzović


Print Share Comment Cite Upload Translate Updates
APA

Manuel Matuzović | Sciencx (2022-12-14T00:00:00+00:00) Day 58: ordering nested layers. Retrieved from https://www.scien.cx/2022/12/14/day-58-ordering-nested-layers-2/

MLA
" » Day 58: ordering nested layers." Manuel Matuzović | Sciencx - Wednesday December 14, 2022, https://www.scien.cx/2022/12/14/day-58-ordering-nested-layers-2/
HARVARD
Manuel Matuzović | Sciencx Wednesday December 14, 2022 » Day 58: ordering nested layers., viewed ,<https://www.scien.cx/2022/12/14/day-58-ordering-nested-layers-2/>
VANCOUVER
Manuel Matuzović | Sciencx - » Day 58: ordering nested layers. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/12/14/day-58-ordering-nested-layers-2/
CHICAGO
" » Day 58: ordering nested layers." Manuel Matuzović | Sciencx - Accessed . https://www.scien.cx/2022/12/14/day-58-ordering-nested-layers-2/
IEEE
" » Day 58: ordering nested layers." Manuel Matuzović | Sciencx [Online]. Available: https://www.scien.cx/2022/12/14/day-58-ordering-nested-layers-2/. [Accessed: ]
rf:citation
» Day 58: ordering nested layers | Manuel Matuzović | Sciencx | https://www.scien.cx/2022/12/14/day-58-ordering-nested-layers-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.