sarajw,
@sarajw@front-end.social avatar

Hopefully quick CSS question:

If you declare layers in one stylesheet, do you have to have the styles associated with those layers within or imported into the same stylesheet, or can they exist in completely different files, as long as they're all linked into the same html via the head?

Thank you!

ryan,
@ryan@hachyderm.io avatar

@sarajw And to answer your primary question, once a layer name is established you can use it in different files. It's recommended to do order-setting for all your layers as the first step in your first imported file, eg

@layer reset, default, primary, secondary;

That way it's clear where the order is established

sarajw,
@sarajw@front-end.social avatar

@ryan perfect, thank you!

mrtrimble,
@mrtrimble@front-end.social avatar

@sarajw You can have two separate CSS files that both contain the same @layer name declaration and the browser should consider them the same layer.

It's a neat feature, you can use it to extend Layers from a design system and write your own styles within that same layer.

hi_mayank,
@hi_mayank@hachyderm.io avatar

@mrtrimble @sarajw please don't extend others' layers. instead, add your own right after theirs. you can even wrap the design system layer in your own layer.

sarajw,
@sarajw@front-end.social avatar

@hi_mayank @mrtrimble what if it's bootstrap tho?

hi_mayank,
@hi_mayank@hachyderm.io avatar

@sarajw @mrtrimble then you should probably find a way to delete it 😅

but if you must keep it, then use the same technique: wrap it in your own layer.

if you start adding random rules into others' layers, it messes with the assumptions they make for ordering rules in their own layer.

sarajw,
@sarajw@front-end.social avatar

@hi_mayank @mrtrimble true.

Yeah it's just the legacy bit we're moving away from but haven't completely eradicated yet...

sarajw,
@sarajw@front-end.social avatar

@mrtrimble yessss. Thank you!

johnnydecimal,
@johnnydecimal@hachyderm.io avatar

@sarajw @mrtrimble Argh, how did I not even know about layers until now? Super glad I read this before tearing out all my CSS and re-writing it, which I 100% need to do because it’s a shambles.

sarajw,
@sarajw@front-end.social avatar

@johnnydecimal @mrtrimble it's definitely still on the new side, I've not actually used them yet!

hi_mayank,
@hi_mayank@hachyderm.io avatar

@sarajw layers transcend the idea of stylesheets, so yes you can use the same layers in different css files.

the only thing to pay attention to in the end is the order of rules: make sure your layer statements come before all other stylesheets are included in the head.

sarajw,
@sarajw@front-end.social avatar

@hi_mayank excellent, so just keep the layer order declaration first in the list of linked stylesheets.

sarajw,
@sarajw@front-end.social avatar

Follow-up question - if I'd rather not have all the CSS in one big long file, what's more performant, importing into one central file or just linking separately?

I'm talking plain CSS here, not preprocessed.

dams,
@dams@disabled.social avatar

@sarajw you can read this, it explains everything. Especially the CSSOM section.

https://developer.mozilla.org/en-US/docs/Web/Performance/Critical_rendering_path

sarajw,
@sarajw@front-end.social avatar

@dams thank you!

  • All
  • Subscribed
  • Moderated
  • Favorites
  • CSS
  • kavyap
  • thenastyranch
  • Durango
  • DreamBathrooms
  • ngwrru68w68
  • magazineikmin
  • cisconetworking
  • Youngstown
  • mdbf
  • slotface
  • osvaldo12
  • GTA5RPClips
  • rosin
  • InstantRegret
  • provamag3
  • everett
  • cubers
  • vwfavf
  • normalnudes
  • tacticalgear
  • tester
  • ethstaker
  • khanakhh
  • modclub
  • Leos
  • anitta
  • megavids
  • JUstTest
  • All magazines