We are going to design a CSS architecture that uses @layer and scoped custom properties to isolate experimental styles from the core system.
Key ideas:
- Use
@layer to separate the core styles from the experimental ones. This allows us to have a clear cascade and the ability to override in a controlled way.
- Use scoped custom properties (CSS variables) to ensure that the experimental styles do not leak into the core and vice versa. We can scope variables to a specific layer or to a specific element.
Steps:
a. Define core layer and experimental layer (or multiple experimental layers if needed).
b. In the core layer, define custom properties that are meant to be used by the core and also by experiments if they need to integrate.
c. In the experimental layer, define custom properties that are specific to the experiment and are scoped to avoid affecting core.
d. We can also use the :where() pseudo-class to scope the experimental styles to a specific container (like an experiment wrapper) to avoid leakage.
Example structure:
We'll have:
- core layer: for the base styles of the system.
- experiments layer: for experimental styles that may override or extend core in a controlled way.
We can also have a reset layer and utilities layer if needed, but for simplicity we focus on core and experiments.
Let's assume we have a wrapper with a class .experiment that we use to scope the experimental styles.
HTML structure:
But note: we want the experiments to be isolated. So we can also use the :scope or a specific class to scope.
Alternatively, we can use @layer without scoping if we rely on the cascade order, but if we want to avoid any leakage we can use a wrapper.
However, note that @layer already has a specific cascade order: later layers override earlier ones if same specificity.
So:
@layer core, experiments;
Then, in the CSS:
@layer core {
:root {
--core-color: blue;
}
.core-element {
color: var(--core-color);
}
}
@layer experiments {
/* We want to override core only within the experiment wrapper? */
/* If we want to override core globally for the experiment, we can just write more specific selectors or same specificity but in a later layer. */
/* But if we want to scope the experiment to a container, we can do: */
.experiment {
--experiment-color: red;
}
.experiment .core-element {
color: var(--experiment-color);
}
However, note that the core layer already defined .core-element to use --core-color. But in the experiment layer we are overriding the .core-element inside .experiment to use --experiment-color.
But wait, the core layer's rule for .core-element is:
.core-element { color: var(--core-color); }
And the experiment layer's rule is:
.experiment .core-element { color: var(--experiment-color); }
Since the experiment layer is later, and the selector is more specific (due to the extra class), it will override.
However, we are using a different variable. We could also override the variable in the experiment scope.
Alternatively, we can define in the experiment layer:
.experiment {
--core-color: red; /* This would override the core variable only inside .experiment */
}
Then the core rule for .core-element (which uses var(--core-color)) will use the overridden variable inside .experiment.
So we have two ways:
1. Override the property with a new value (using a new variable or a fixed value) with a more specific selector.
2. Override the variable that the core uses, but only in the experiment scope.
The second way is more aligned with the idea of using custom properties for theming.
But note: if the core uses a variable that we want to change only in the experiment, we can override that variable in the experiment scope.
However, we must be cautious because the core might use multiple variables. We might want to change only some.
So, let's design:
Core layer:
@layer core {
:root {
--core-primary: #0066cc;
--core-text: #333;
}
.button {
background: var(--core-primary);
color: var(--core-text);
}
}
Experiment layer:
@layer experiments {
.experiment {
/* Override the core variables only in the experiment scope */
--core-primary: #ff0066;
--core-text: #fff;
}
/* We don't need to write any rules for .button because the core rule will pick up the overridden variables. */
}
This way, inside .experiment, the .button will use the experiment's colors.
But what if the experiment wants to introduce new styles that are not in the core?
We can define new variables and use them in the experiment layer:
@layer experiments {
.experiment {
--experiment-spacing: 2rem;
}
.experiment .special-feature {
margin-top: var(--experiment-spacing);
}
}
And if we want to use the experiment variable in the core? We shouldn't, because the core should not depend on experiments.
So the core uses only core variables, and experiments can override core variables and define their own.
But what if the experiment wants to style a core component in a way that is not covered by the core variables?
Then we have two options:
Option 1: Extend the core layer to include more variables for that component. But that would mean changing the core for an experiment, which might not be desirable.
Option 2: In the experiment layer, write more specific rules for the component that override the core. But then we are not using the variable, we are overriding the property.
Example:
In core:
.button { border: 1px solid var(--core-border); }
In experiment, we want a different border? We can override the variable if we have one, but if we don't, we can write:
.experiment .button { border: 2px dashed red; }
This is acceptable because it's in the experiment layer and scoped to .experiment.
However, note that if we have multiple experiments, we might want to avoid writing the same override in multiple experiments.
We can define a variable for the border in the experiment and then use it in the experiment layer:
@layer experiments {
.experiment {
--experiment-button-border: 2px dashed red;
}
.experiment .button {
border: var(--experiment-button-border);
}
}
But then we are not using the core variable. We are completely overriding.
Alternatively, we can have a core variable for border that we can override. But if we didn't have that in core, we have to change core to include it? Not necessarily.
We can also use a fallback:
In core:
.button { border: var(--core-border, 1px solid #ccc); }
Then in experiment, we set --core-border to our desired value.
So it's a trade-off.
Proposed architecture:
1. Core layer defines the base styles and uses custom properties for any property that might be themed or overridden.
2. Experiments layer can override the core variables in the experiment scope.
3. Experiments layer can also define new variables and use them for new styles that are specific to the experiment.
4. Experiments layer can also write new rules for core components (with more specificity if needed) to override properties that are not covered by core variables.
But note: if we write a new rule in the experiment layer for a core component, we are increasing the specificity. We might want to avoid that and rely on variable overrides.
However, sometimes we cannot avoid it because the core might not have a variable for that property.
So, the core team should anticipate the needs for theming and provide variables for common overrides.
But for experimental features that are not yet in the core, we might need to override without variables.
To avoid increasing specificity, we can use the same selector and rely on the layer order? But the core rule and the experiment rule have the same selector if we don't add the .experiment wrapper.
Example:
Core: .button { ... }
Experiment: .button { ... } /* same specificity, but in a later layer, so it overrides */
But that would override globally, not just in the experiment. So we must scope with .experiment.
Therefore, we have to increase specificity (by adding .experiment) to scope the override.
This is acceptable because it's in the experiment layer and we are scoping to the experiment container.
Summary:
We use:
@layer core, experiments;
In core, we use variables for any property that we think might be overridden.
In experiments, we scope to the experiment container and:
a. Override core variables to change the look of core components.
b. Define new experiment-specific variables and use them in experiment-specific styles.
c. Override core component properties by writing rules with the experiment container as part of the selector (if needed).
Example code:
/* core.css */
@layer core {
:root {
--core-color: blue;
--core-padding: 1rem;
}
.button {
padding: var(--core-padding);
background: var(--core-color);
color: white;
}
}
/* experiments.css */
@layer experiments {
.experiment {
--core-color: red; /* override the core variable */
--experiment-font-size: 1.5rem;
}
.experiment .button {
/* If we want to change something that doesn't have a variable, we can override the property */
font-size: var(--experiment-font-size);
}
.experiment .special {
font-size: var(--experiment-font-size);
}
}
But note: the .button in the experiment already uses the overridden --core-color because the .button rule in core uses that variable and the variable is overridden in the .experiment scope.
However, the font-size of .button is not changed by the experiment because the core doesn't set font-size. We are setting a new property (font-size) in the experiment for .button. This is an additional style.
But wait, the core .button doesn't have a font-size, so we are adding one in the experiment. This is acceptable.
However, if we want to change the font-size of .button only in the experiment, we can do:
.experiment .button { font-size: var(--experiment-font-size); }
But note that this rule has the same property as if we had set it in core? It's a new property, so it will be added.
If we want to override a property that is set in core, we can just set it again in the experiment with the same selector (but scoped) and it will override because of the layer order and specificity.
Example:
Core: .button { padding: 1rem; }
Experiment: .experiment .button { padding: 2rem; }
This will override the padding for .button inside .experiment.
But if the core uses a variable for padding, then we can just override the variable and avoid writing the rule.
So, it's better to use variables for everything that might be overridden.
However, we cannot anticipate everything. So we have two methods for experiments:
1. Override a variable that the core uses (preferred, because it doesn't require writing a new rule).
2. Write a new rule with the experiment container to override a property that is not controlled by a variable.
We must document which variables are available for overriding in the core.
Also, note that we can have multiple experiment layers? Yes, but we are using one for all experiments. If we have multiple experiments that might conflict, we can have multiple layers:
@layer core, experiment1, experiment2;
Then the later experiment layer will have higher priority.
But if we want to run multiple experiments at the same time and they might affect the same elements, we have to be cautious.
Alternatively, we can have one experiment layer and use different scoping classes for each experiment.
Let's say we have two experiments:
<div class="experiment experiment-a"> ... </div>
<div class="experiment experiment-b"> ... </div>
Then in the experiment layer:
.experiment-a { ... }
.experiment-b { ... }
And if they both try to override the same variable in the same element? The later one in the CSS will win, but since they are in the same layer, the normal cascade applies (by order in the CSS). But if they are in separate layers, then the layer order matters.
So, if we have:
@layer core, experiments;
and in experiments we have:
.experiment-a { --core-color: red; }
.experiment-b { --core-color: blue; }
Then if an element has both classes, the later one in the CSS will win (because same layer, same specificity, later in the source). But note that the layer is the same, so it's just the order in the CSS.
However, if we split:
@layer core, experiment-a, experiment-b;
Then experiment-b is later and will override experiment-a even if experiment-a is defined later in the CSS? No, the layer order is defined by the @layer rule. Then within the same layer, the source order matters.
So:
@layer core, a, b;
Then b > a > core.
But if we have:
@layer a, b;
Then b > a.
And if we have:
@layer core, a, b;
and in the CSS we write:
@layer a { ... }
@layer b { ... }
Then the order is still: b > a > core.
So, if we want to have multiple experiments that are independent and might be enabled at the same time, we can either:
Option 1: Use one experiment layer and rely on the source order (and careful class naming) to resolve conflicts.
Option 2: Use multiple experiment layers and define the order by the @layer rule.
But note: if we have two experiments that are mutually exclusive (only one active at a time) then it doesn't matter.
If they can be active together, we have to decide which one should have higher priority.
We can let the experiment that is loaded later in the CSS have higher priority by putting it in a later layer.
But if we want to control the priority, we can define:
@layer core, experiment1, experiment2;
Then experiment2 > experiment1.
However, if we have many experiments, we might not want to create a layer for each.
So, we can have:
@layer core, experiments;
and then in the experiments layer, we define:
.experiment-1 { ... }
.experiment-2 { ... }
and then the source order in the CSS for these classes will determine which one wins if they conflict.
But note: if we have two experiments that are both applied to the same element (by having two classes) then the source order of the rules in the CSS matters.
Example:
.experiment-1 { --color: red; }
.experiment-2 { --color: blue; }
If the element has both classes and .experiment-2 rule comes after, then blue wins.
So, we must be cautious about the order of the experiment CSS files if we are loading multiple.
Alternatively, we can use the cascade to our advantage: we can have a base experiment layer and then let each experiment be in its own layer if we need to control the order.
Given the complexity, for simplicity we can have one experiment layer and then use a naming convention and order in the CSS to resolve.
But if we need more control, we can do:
@layer core, experiment-1, experiment-2, experiment-3;
Then experiment-3 > experiment-2 > experiment-1 > core.
And then each experiment is in its own layer. This way, even if we load the CSS in a different order, the layer order is fixed.
However, if we want to dynamically load experiments, we might not know the order in advance.
So, maybe we stick to one experiment layer and rely on the order of the CSS files? But that is fragile.
Another idea: use the same layer for all experiments but use a more specific selector for each experiment? Not really.
Alternatively, we can have a base experiment layer and then let experiments be in sub-layers? Not possible, layers are flat.
So, we have to decide:
If we have a fixed set of experiments that we know the priority of, we can assign layers.
If experiments are dynamically loaded and we don't know the priority, then we might have to use one layer and accept that the last loaded experiment wins in case of conflict.
But note: if we use the same layer and two experiments define the same variable on the same element, the one that comes later in the CSS (or later in the same CSS file) will win.
So, if we load experiment1.css and then experiment2.css, and both set --color on the same element, then experiment2 wins.
This might be acceptable if we consider that the last loaded experiment is the one that should take precedence.
However, if we want to have multiple experiments that are independent and not conflicting, we should design the experiments to use different variables or different scopes (like different wrapper classes) so they don't conflict.
So, the recommendation:
- Use one experiment layer for all experiments, but design experiments to be scoped to their own wrapper (like .experiment-a, .experiment-b) and avoid setting the same variables on the same elements.
- If two experiments must set the same variable on the same element, then we have a conflict and we must decide which one should win. We can do that by the order of the CSS files (last wins) or by using separate layers if we know the priority.
Given the requirement of isolating experiments, we should design experiments to be self-contained and not interfere with each other.
So, let's go back to the initial idea:
We have a core layer and an experiments layer.
In the experiments layer, we scope all styles to a unique wrapper for each experiment.
Example for experiment A:
.experiment-a {
--experiment-a-color: red;
}
.experiment-a .button {
background: var(--experiment-a-color);
}
And we don't touch the core variables in experiment A? Or we might, but only if we want to change the core components in the context of experiment A.
But if we change core variables in experiment A, then any core component inside .experiment-a will be affected. That might be what we want.
However, if we have experiment B that also changes the same core variable, then if an element is in both .experiment-a and .experiment-b, the variable will be set by both and the last one in the CSS will win (if they are in the same layer) or the one in the later layer (if in different layers).
So, to avoid conflict, we should avoid having multiple experiments that change the same core variable on the same element.
But if we have a wrapper that is unique per experiment, then an element won't be in two experiment wrappers at the same time? It could if we nest experiments? We should avoid nesting experiments.
So, we can enforce that experiment wrappers are not nested and are mutually exclusive.
Then, an element will only be in one experiment wrapper, so no conflict.
But what if we have two experiments that are applied to the same element by having two classes? Then we have conflict.
So, we should design the experiment wrappers to be exclusive. We can use a data attribute or a class that is unique and we ensure that no element has more than one experiment class.
Alternatively, we can use the `:not()` selector to avoid overlapping? Not practical.
So, we rely on the developer to not apply multiple experiment classes to the same element.
Conclusion:
We'll have:
@layer core, experiments;
In core, we define variables for everything that might be overridden.
In experiments, we scope to a unique wrapper class per experiment.
Each experiment can:
- Override core variables in its wrapper scope.
- Define new experiment-specific variables in its wrapper scope.
- Write new rules for core components (with the wrapper as part of the selector) to override properties that are not covered by core variables.
We avoid having multiple experiments on the same element to prevent conflicts.
Example code structure:
/* core.css */
@layer core {
:root {
--core-primary: #0066cc;
--core-text: #333;
--core-padding: 1rem;
--core-border: 1px solid #ccc;
}
.button {
padding: var(--core-padding);
background: var(--core-primary);
color: var(--core-text);
border: var(--core-border);
}
}
/* experiment-a.css */
@layer experiments {
.experiment-a {
--core-primary: #ff0066;
--experiment-a-font-size: 1.2rem;
}
.experiment-a .button {
/* If we want to change something without a variable, we can override */
font-size: var(--experiment-a-font-size);
}
}
/* experiment-b.css */
@layer experiments {
.experiment-b {
--core-text: #fff;
--experiment-b-border-radius: 4px;
}
.experiment-b .button {
border-radius: var(--experiment-b-border-radius);
}
}
Now, if we have:
<div class="experiment-a">
<button class="button">Experiment A</button>
</div>
<div class="experiment-b">
<button class="button">Experiment B</button>
</div>
Then the buttons will look different.
But note: the core variables are overridden in the respective scopes.
Also, note that we are using the same layer for both experiments. If we load experiment-a.css and then experiment-b.css, and if there is a conflict (like both set --core-primary on the same element) then the one in experiment-b.css will win because it comes later in the same layer? Actually, no, because they are scoped to different wrappers. So no conflict.
But if we have:
<div class="experiment-a experiment-b"> ... </div>
Then the element has both classes. Then the variable will be set by both. Which one wins?
In experiment-a.css: .experiment-a { --core-primary: #ff0066; }
In experiment-b.css: .experiment-b { --core-primary: #00ff00; }
If experiment-b.css is loaded after experiment-a.css, then the rule for .experiment-b comes later and will override because same specificity and later in the source (same layer). So the button will have #00ff00.
So, we must avoid having multiple experiment classes on the same element.
We can enforce this by using a data attribute and a unique value per experiment, and then use attribute selectors? But that doesn't prevent multiple.
Alternatively, we can design the experiments to be mutually exclusive by using a parent wrapper that only has one experiment class.
But ultimately, it's up to the developer.
We can also use the `:not()` selector to prevent stacking? Not really.
So, we document that experiment wrappers should not be combined.
Alternatively, we can use a different approach: use a data attribute on the html element to activate an experiment, and then use:
[data-experiment="a"] { ... }
[data-experiment="b"] { ... }
And then we can only have one experiment at a time on the html element? But what if we want to run two experiments on the same page in different parts? We can have multiple wrappers with different data attributes? Then we can have:
<div data-experiment="a"> ... </div>
<div data-experiment="b"> ... </div>
And then the CSS:
[data-experiment="a"] { ... }
[data-experiment="b"] { ... }
And if an element has both? It can't because the data attribute is on the wrapper, and the inner element doesn't have the data attribute. But if we have nested wrappers, then the inner element will be in both. So we have the same problem.
So, we still have to avoid nesting or multiple wrappers on the same element.
Given the complexity, we might just stick to classes and document the rule.
Alternatively, we can use a different scope for each experiment that is not based on a class that can be combined? We can use an ID? But IDs are unique, so we can only have one experiment per page? Not good.
So, we use classes and accept that if someone combines two experiment classes on the same element, the last one in the CSS will win.
We can also use `:where()` to reduce specificity? Not necessary because we are already using a class.
Another idea: use a different layer for each experiment? Then the layer order would determine the priority, and we can have multiple experiments on the same element without worrying about source order? But then the layer order is fixed by the @layer rule.
Example:
@layer core, experiment-a, experiment-b;
Then experiment-b > experiment-a.
So if an element has both .experiment-a and .experiment-b, then the rules in experiment-b layer will override experiment-a, regardless of the order in the CSS files (as long as the @layer order is fixed).
But note: if we have:
@layer experiment-a, experiment-b;
Then experiment-b > experiment-a.
And if we have:
@layer experiment-b, experiment-a;
Then experiment-a > experiment-b.
So we have to be cautious about the order of the @layer rules.
And if we have multiple CSS files, the @layer rules are concatenated? Yes, but the order of the @layer rules in the final CSS matters.
So, if we load experiment-a.css and then experiment-b.css, and both have:
experiment-a.css: @layer experiment-a;
experiment-b.css: @layer experiment-b;
Then the final CSS will have:
@layer experiment-a;
@layer experiment-b;
So experiment-b > experiment-a.
But if we load experiment-b.css first, then:
@layer experiment-b;
@layer experiment-a;
Then experiment-a > experiment-b.
So, we cannot rely on the load order? We have to control the @layer order in the final CSS.
We can have a single file that defines the layer order and then import the experiments in the desired order? Or we can use the same layer for all and rely on source order.
Given the complexity, maybe we stick to one experiment layer and source order.
Final recommendation:
We use:
@layer core, experiments;
And then we load the experiment CSS files in the order we want them to take precedence (later ones override earlier ones in case of conflict).
And we design experiments to be scoped to a unique wrapper class.
And we avoid having multiple experiment classes on the same element.
Let's write the code accordingly.
However, note that the core layer should not use any experiment-specific variables. And the experiments can override core variables and define their own.
We can also have a reset layer and utilities layer if needed, but that's beyond the scope.
Example:
/* core.css */
@layer core {
:root {
--core-primary: #0066cc;
--core-text: #333;
--core-padding: 1rem;
--core-border: 1px solid #ccc;
}
.button {
padding: var(--core-padding);
background: var(--core-primary);
color: var(--core-text);
border: var(--core-border);
border-radius: 0;
}
}
/* experiment-a.css */
@layer experiments {
.experiment-a {
--core-primary: #ff0066;
--experiment-a-border-radius: 4px;
}
.experiment-a .button {
border-radius: var(--experiment-a-border-radius);
}
}
/* experiment-b.css */
@layer experiments {
.experiment-b {
--core-text: #fff;
--experiment-b-padding: 2rem;
}
.experiment-b .button {
padding: var(--experiment-b-padding);
}
}
Now, if we have:
<div class="experiment-a">
<button class="button">A</button>
</div>
<div class="experiment-b">
<button class="button">B</button>
</div>
Then button A has red background, 4px border-radius, and the core padding (1rem) because experiment-a doesn't change padding.
Button B has blue background (core primary, because experiment-b doesn't change --core-primary), white text, and 2rem padding (from experiment-b).
If we have an element with both classes:
<div class="experiment-a experiment-b">
<button class="button">Both</button>
</div>
Then:
--core-primary: from experiment-a (red) because experiment-a sets it and experiment-b doesn't, so it stays red.
--core-text: from experiment-b (white) because experiment-b sets it and experiment-a doesn't.
--experiment-a-border-radius: 4px (but not used because experiment-b doesn't set it, but the button in experiment-a .button rule uses it, and we have both classes so the .experiment-a .button rule applies? Actually, the selector .experiment-a .button matches because the element has class experiment-a. Similarly, .experiment-b .button matches.
Then we have two rules for .button:
.experiment-a .button { border-radius: var(--experiment-a-border-radius); }
.experiment-b .button { padding: var(--experiment-b-padding); }
They set different properties, so no conflict.
But what if both set the same property? Then the one that comes later in the CSS wins.
So if experiment-b.css is loaded after experiment-a.css, then the padding from experiment-b will override the padding from experiment-a? But experiment-a doesn't set padding, it sets border-radius.
If both set the same property, then the one from the later CSS file wins.
Example:
experiment-a.css: .experiment-a .button { background: red; }
experiment-b.css: .experiment-b .button { background: blue; }
Then if the element has both classes, and experiment-b.css is loaded after, then blue wins.
So, we must be cautious about the order of loading experiment CSS files.
Therefore, we should:
- Load the experiment CSS files in a known order (maybe alphabetical by experiment name) and document that the later one has higher priority in case of conflict.
- Or, avoid having two experiments that set the same property on the same component.
Given the requirement of isolation, we should design experiments to not conflict. But if they do, we have a rule.
We can also use `@layer` to control the priority by assigning each experiment to a separate layer and then define the layer order in a main CSS file.
main.css:
@layer core, experiment-a, experiment-b;
Then we import:
@import "core.css";
@import "experiment-a.css"; /* goes to layer experiment-a */
@import "experiment-b.css"; /* goes to layer experiment-b */
Then experiment-b > experiment-a.
But then we have to change the experiment CSS files to use the correct layer? Or we can assign the layer in the main file?
Actually, the @layer rule must be in the same file as the styles? No, we can have:
In experiment-a.css:
@layer experiment-a {
.experiment-a { ... }
}
In experiment-b.css:
@layer experiment-b {
.experiment-b { ... }
}
Then in main.css:
@layer core, experiment-a, experiment-b;
@import "core.css";
@import "experiment-a.css";
@import "experiment-b.css";
But note: the @import must come after the @layer rule? Actually, the @layer rule can be anywhere, but the order of the layers is determined by the first occurrence.
Alternatively, we can define the layers in the main file and then the imported files will assign to those layers.
However, if an imported file has an @layer rule with a layer that hasn't been defined, it will create a new layer at that point.
So, we must define the layers in the main file first.
Then in the imported files, we use:
@layer experiment-a { ... }
And it will assign to the existing layer.
So, we can control the order by the order of the @layer declarations in the main file.
Then, if we want experiment-b to have higher priority, we define:
@layer core, experiment-a, experiment-b;
Then experiment-b > experiment-a.
And then we import the files.
This way, even if we load experiment-a.css after experiment-b.css, the layer order is fixed.
But note: the source order within the same layer still matters? Yes, if two rules have the same specificity in the same layer, the later one wins.
But if they are in different layers, the layer order wins.
So, if we have:
main.css:
@layer core, experiment-a, experiment-b;
@import "experiment-a.css"; /* contains: @layer experiment-a { .experiment-a { ... } } */
@import "experiment-b.css"; /* contains: @layer experiment-b { .experiment-b { ... } } */
Then experiment-b > experiment-a.
And if we have:
experiment-a.css:
@layer experiment-a {
.experiment-a { --color: red; }
}
experiment-b.css:
@layer experiment-b {
.experiment-b { --color: blue; }
}
Then if an element has both classes, the color from experiment-b will win because experiment-b layer is later.
But if both set the same property (like --color) on the same element, then the one from the later layer wins.
This is good because we can control the priority by the layer order, not by the load order of the CSS files.
So, we can have:
main.css:
@layer core, experiment-a, experiment-b;
Then we load the experiment CSS files in any order, and the layer order is fixed.
But what if we have a new experiment that we want to have higher priority than experiment-b? Then we have to change the main.css and add a new layer after experiment-b.
So, we have to plan the layer order.
Alternatively, we can have:
@layer core, experiments;
and then within experiments, we use source order.
But with separate layers, we can have a fixed priority without changing the CSS files, just by changing the main.css.
However, if we have many experiments, we might not want to list them all in the main.css.
We can have:
@layer core, experiments;
and then in each experiment file, we use:
@layer experiments {
.experiment-a { ... }
}
Then they are all in the same layer and source order matters.
But then we lose the ability to set a fixed priority between experiments without changing the order of the @import statements.
So, it's a trade-off.
Given that experiments are isolated by their wrapper classes, we might not need to set a priority between them because they shouldn't conflict.
But if they do, we can use the source order of the CSS files.
So, we can stick to one experiment layer.
Let's stick to one experiment layer for simplicity.
But note: the requirement is to isolate experiments from core and from each other.
We are isolating by wrapper classes and by not sharing the same variables (or if we do, we override only in our wrapper).
And we avoid multiple wrappers on the same element.
So, we'll use:
@layer core, experiments;
and then in each experiment, we scope to a unique class.
And we load the experiment CSS files in a known order (maybe alphabetical) and document that the later one will override in case of conflict.
But if we want to avoid any conflict, we can design the experiments to not set the same variables on the same elements.
Since each experiment has its own wrapper, and we don't nest them, then an element will only be in one experiment wrapper, so no conflict.
Therefore, the only conflict is if we have two experiments that set the same variable on the same element because the element has two wrapper classes (which we disallow) or if we have a global rule in the experiment layer that sets a variable on a common element (like body) and then two experiments set different values? But then the last one wins.
So, we must also avoid setting variables on global selectors (like :root, body) in the experiment layer? Or we can, but then the last experiment will override the previous ones globally.
That might be acceptable if we only run one experiment at a time? But if we run multiple, then we have a conflict.
So, we should set variables only on the experiment wrapper, not on global selectors.
Example:
.experiment-a { --core-primary: red; } /* scoped to the wrapper and its children */
not:
:root { --core-primary: red; } /* this would affect the entire page, even outside the experiment */
So, we must be cautious.
Therefore, the rule:
In the experiment layer, only set variables on the experiment wrapper (or its children) and avoid global selectors.
Then, the variables are scoped to the experiment wrapper and its children, and won't leak out.
And if we have two experiments, they set variables on their own wrappers, so no conflict.
But what if we have:
.experiment-a { --core-primary: red; }
.experiment-b { --core-primary: blue; }
and an element:
<div class="experiment-a">
<div class="experiment-b">
<button class="button">Button</button>
</div>
</div>
Then the button is in both .experiment-a and .experiment-b? Actually, the button is inside .experiment-b which is inside .experiment-a, so the button has both wrappers in its ancestry.
Then the variable --core-primary will be set by both. The one from .experiment-b will be closer, so it will win? Actually, CSS variables inherit, and the closest ancestor that sets the variable wins.
So in this case, .experiment-b sets --core-primary: blue, and that will be used because it's closer to the button than .experiment-a.
But what if the order of the CSS rules is:
.experiment-a { --core-primary: red; }
.experiment-b { --core-primary: blue; }
Then the button will use blue because .experiment-b is closer.
But if we have:
<div class="experiment-b">
<div class="experiment-a">
<button class="button">Button</button>
</div>
</div>
Then the button will use red.
So, the nesting order matters.
We can avoid nesting experiments by not allowing nested experiment wrappers.
So, we should design the experiments to be top-level wrappers and not nested.
Then, an element will only be in one experiment wrapper.
So, the final architecture:
- Core layer: defines the core styles and uses custom properties for any property that might be overridden.
- Experiment layer: all experiment styles are scoped to a unique wrapper class (like .experiment-<name>).
- We avoid nesting experiment wrappers.
- We avoid setting variables on global selectors in the experiment layer; only set on the experiment wrapper.
- We load the experiment CSS files in a known order (and document that if two experiments conflict, the later one wins) but we design to avoid conflict.
This should isolate experiments from each other and from the core.
Let's write a code example with two experiments and a core.
We'll have:
index.html:
<div class="experiment-a">
<button class="button">Experiment A</button>
</div>
<div class="experiment-b">
<button class="button">Experiment B</button>
</div>
<div class="core">
<button class="button">Core</button>
</div>
core.css:
@layer core {
:root {
--core-primary: #0066cc;
--core-text: #333;
--core-padding: 1rem;
--core-border: 1px solid #ccc;
}
.button {
padding: var(--core-padding);
background: var(--core-primary);
color: var(--core-text);
border: var(--core-border);
border-radius: 0;
}
}
experiment-a.css:
@layer experiments {
.experiment-a {
--core-primary: #ff0066;
--experiment-a-border-radius: 4px;
}
.experiment-a .button {
border-radius: var(--experiment-a-border-radius);
}
}
experiment-b.css:
@layer experiments {
.experiment-b {
--core-text: #fff;
--experiment-b-padding: 2rem;
}
.experiment-b .button {
padding: var(--experiment-b-padding);
}
}
Then we load:
<link rel="stylesheet" href="core.css">
<link rel="stylesheet" href="experiment-a.css">
<link rel="stylesheet" href="experiment-b.css">
And the result:
Core button: blue background, #333 text, 1rem padding, 0 border-radius.
Experiment A button: red background, #333 text (core text not changed), 1rem padding (core padding, not changed), 4px border-radius.
Experiment B button: blue background (core primary not changed), white text, 2rem padding, 0 border-radius.
If we change the order of experiment-a.css and experiment-b.css, it doesn't matter because they are scoped.
But if we have a conflict, like both set --core-primary, then the one that is loaded later will set the variable on its wrapper, and since the wrappers are separate, no conflict.
However, if we have:
<div class="experiment-a experiment-b">
<button class="button">Both</button>
</div>
Then the button will have both wrappers. Then:
--core-primary: set by both, the one from the class that appears later in the CSS? Actually, the variable is set on the same element (since the element has both classes). Then the one that is defined later in the CSS wins? Because same specificity and same element.
But note: the element has two classes, and we have two rules:
.experiment-a { --core-primary: red; }
.experiment-b { --core-primary: blue; }
If experiment-b.css is loaded after, then blue wins.
So, we avoid multiple experiment classes on the same element.
This should work.
However, note that we are using the same layer for all experiments. If we want to have a fixed priority between experiments, we can use separate layers and define the order in a main file.
But if we don't have conflicts, we don't need separate layers.
So, we'll stick to one experiment layer.
But note: the requirement says "isolate experiments from core system". We are doing that by scoping to a wrapper and using variables.
We are also isolating experiments from each other by using unique wrapper classes and avoiding multiple wrappers on the same element.
This should meet the requirement.
Let me write the code accordingly.
However, note that the core layer uses :root for variables. The experiment layer overrides these variables on the wrapper, which then affect the children because of inheritance.
This is exactly what we want.
We are done.</think>
I'll design a CSS architecture using @layer and scoped custom properties to isolate experimental features from the core system. This approach provides clear separation, predictable cascade control, and prevents style leakage.
Architecture Overview
/* ==========================================================================
Layer Declaration & Core System
========================================================================== */
/* Define layer hierarchy - order matters! */
@layer reset, base, components, utilities, experiments;
/* ==========================================================================
LAYER 1: Reset & Normalize
========================================================================== */
@layer reset {
/* Box sizing, margin/padding removal, etc. */
*, *::before, *::after {
box-sizing: border-box;
}
/* Other reset rules... */
}
/* ==========================================================================
LAYER 2: Base/Tokens
========================================================================== */
@layer base {
/* Global custom properties (available everywhere) */
:root {
/* Core design tokens - safe for experiments to use */
--color-primary: #0066cc;
--color-text: #1a1a1a;
--spacing-unit: 8px;
--font-sans: system-ui, sans-serif;
/* Experiment-safe tokens (can be overridden in experiments) */
--radius-sm: 4px;
--radius-md: 8px;
--shadow-sm: 0 1px 3px rgba(0,0,0,0.1);
}
/* Base element styles */
body {
font-family: var(--font-sans);
color: var(--color-text);
line-height: 1.5;
}
a {
color: var(--color-primary);
text-decoration: none;
}
}
/* ==========================================================================
LAYER 3: Components (Core UI)
========================================================================== */
@layer components {
/* Button component using core tokens */
.btn {
display: inline-flex;
align-items: center;
justify-content: center;
padding: calc(var(--spacing-unit) * 2) calc(var(--spacing-unit) * 3);
background: var(--color-primary);
color: white;
border: none;
border-radius: var(--radius-sm);
font-weight: 600;
cursor: pointer;
transition: opacity 0.2s ease;
}
.btn:hover {
opacity: 0.9;
}
/* Card component */
.card {
background: white;
border: 1px solid #e5e7eb;
border-radius: var(--radius-md);
padding: calc(var(--spacing-unit) * 3);
box-shadow: var(--shadow-sm);
}
}
/* ==========================================================================
LAYER 4: Utilities (Low specificity)
========================================================================== */
@layer utilities {
.flex { display: flex; }
.grid { display: grid; }
.mt-4 { margin-top: calc(var(--spacing-unit) * 4); }
.text-center { text-align: center; }
}
/* ==========================================================================
LAYER 5: Experiments (Isolated)
========================================================================== */
@layer experiments {
/*
* EXPERIMENT SCOPING STRATEGY:
* 1. Wrap all experimental styles in a unique namespace class
* 2. Use experiment-specific custom properties with double hyphens
* 3. NEVER modify core layer properties directly
*/
/* Experiment A: Glass morphism buttons */
.exp-glassmorphism {
/* Experiment-specific custom properties - scoped to this wrapper */
--exp-glass-blur: 10px;
--exp-glass-opacity: 0.2;
--exp-glass-border: 1px solid rgba(255, 255, 255, 0.3);
/* Override ONLY within experiment wrapper */
.btn {
background: rgba(255, 255, 255, var(--exp-glass-opacity));
backdrop-filter: blur(var(--exp-glass-blur));
-webkit-backdrop-filter: blur(var(--exp-glass-blur));
border: var(--exp-glass-border);
box-shadow: 0 8px 32px rgba(31, 38, 135, 0.15);
}
}
/* Experiment B: Neumorphic design system */
.exp-neumorphism {
--exp-neumorph-bg: #e0e5ec;
--exp-neumorph-shadow-light: #ffffff;
--exp-neumorph-shadow-dark: #a3b1c6;
--exp-neumorph-radius: 12px;
.btn {
background: var(--exp-neumorph-bg);
border: none;
border-radius: var(--exp-neumorph-radius);
box-shadow:
6px 6px 12px var(--exp-neumorph-shadow-dark),
-6px -6px 12px var(--exp-neumorph-shadow-light);
}
.btn:active {
box-shadow:
inset 4px 4px 8px var(--exp-neumorph-shadow-dark),
inset -4px -4px 8px var(--exp-neumorph-shadow-light);
}
}
/* Experiment C: Bento grid layout */
.exp-bento-grid {
--exp-bento-gap: 16px;
--exp-bento-radius: 24px;
display: grid;
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
gap: var(--exp-bento-gap);
padding: var(--exp-bento-gap);
> * {
border-radius: var(--exp-bento-radius);
background: white;
padding: calc(var(--spacing-unit) * 3);
border: 1px solid #f0f0f0;
}
}
/* Experiment D: Dark mode variant (scoped) */
.exp-dark-mode {
/* Only modify within experiment scope */
--exp-dark-bg: #111827;
--exp-dark-card: #1f2937;
--exp-dark-text: #f9fafb;
background: var(--exp-dark-bg);
color: var(--exp-dark-text);
.card {
background: var(--exp-dark-card);
border-color: #374151;
color: var(--exp-dark-text);
}
.btn {
background: var(--exp-dark-card);
border: 1px solid var(--color-primary);
}
}
}
HTML Usage Patterns
<!-- Core system (unchanged by experiments) -->
<div class="core-app">
<button class="btn">Core Button</button>
<div class="card">Core Card</div>
</div>
<!-- Experiment A: Glassmorphism -->
<div class="exp-glassmorphism">
<button class="btn">Glass Button</button>
<div class="card">Glass Card</div>
</div>
<!-- Experiment B: Neumorphism -->
<div class="exp-neumorphism">
<button class="btn">Neumorphic Button</button>
</div>
<!-- Experiment C: Bento Grid -->
<div class="exp-bento-grid">
<div class="card">Grid Item 1</div>
<div class="card">Grid Item 2</div>
<div class="card">Grid Item 3</div>
</div>
<!-- Experiment D: Dark Mode -->
<div class="exp-dark-mode">
<button class="btn">Dark Button</button>
<div class="card">Dark Card</div>
</div>
<!-- Multiple experiments (safe - they won't leak) -->
<div class="exp-glassmorphism exp-bento-grid">
<button class="btn">Glass Bento Button</button>
<div class="card">Glass Bento Card</div>
</div>
Advanced: Dynamic Experiment Activation
/* Optional: Data attribute approach for runtime control */
@layer experiments {
/* Experiment controlled by data attribute */
[data-experiment="glass"] {
--exp-glass-blur: 20px;
--exp-glass-opacity: 0.3;
.btn {
backdrop-filter: blur(var(--exp-glass-blur));
}
}
/* Experiment with feature flag */
.has-experimental-layout {
--exp-layout-mode: bento;
/* Layout changes based on custom property */
display: grid;
grid-template-columns:
var(--exp-layout-mode) == "bento"
? repeat(auto-fit, minmax(250px, 1fr))
: 1fr;
}
}
<!-- Dynamically enable experiments -->
<body data-experiment="glass">
<!-- Glass effects enabled -->
<button class="btn">Glass Button</button>
</body>
<div class="has-experimental-layout" style="--exp-layout-mode: bento;">
<!-- Bento layout enabled -->
</div>
Build System Integration (Optional)
For better maintainability with separate files:
/* main.css - imports in correct layer order */
@import "reset.css" layer(reset);
@import "base.css" layer(base);
@import "components.css" layer(components);
@import "utilities.css" layer(utilities);
@import "experiments/*.css" layer(experiments);
/* experiments/glass.css */
@layer experiments {
.exp-glassmorphism {
/* ... glass styles ... */
}
}
Key Principles
- Layer Order is Sacred:
experiments is last, so it can override anything but only within its scope
- Scoped Variables: All experiment variables start with
--exp- prefix
- No Core Modification: Experiments never modify core tokens directly
- Wrapper Pattern: Every experiment gets a unique namespace class
- Inheritance Safety: Experiment variables don't leak out of their wrapper
- Composability: Multiple experiments can coexist without conflict
Benefits
- Zero Leakage: Experimental styles affect only elements within their wrapper
- Predictable Override: Core styles always win outside experiment wrappers
- Easy Removal: Remove experiment wrapper class to instantly disable
- A/B Testing: Simple to toggle experiments with class changes
- Progressive Enhancement: Core works without any experiments loaded
- Team Safety: Designers/devs can experiment without breaking production UI
This architecture provides surgical isolation while maintaining access to core design tokens, making it perfect for design system teams that need to safely iterate on new UI patterns.