@mixin CSS At-Rule
The @mixin at-rule defines a reusable block of declarations that is inserted into other rules with @apply. It is experimental and not yet supported in browsers.
Description
The @mixin at-rule defines a reusable block of styles that can be inserted into other style rules with the companion @apply at-rule. While a custom function created with @function returns a single value, a mixin returns whole declarations and even nested rules, which makes it similar to the mixins found in preprocessors such as Sass. It is defined in the CSS Functions and Mixins specification.
A mixin's name starts with two dashes, for example --visually-hidden or --button-variant(). Like a custom function, it can take parameters, each with an optional type and default value, and these are read inside the body with var(). When a rule contains @apply --button-variant(#2563eb);, the declarations and nested rules from the mixin are substituted at that point, using the arguments given. A mixin can also accept a block of content from the place where it is applied, which is inserted wherever the mixin uses @contents.
Mixins help keep stylesheets consistent by putting repeated patterns, such as focus rings, truncated text or color variants of a component, in one place, without needing a build step. Because they are expanded by the browser, they can use custom properties and respond to the element's context, which preprocessor mixins cannot.
@mixin and @apply are experimental and not yet supported in any browser without a flag. The syntax may still change before it ships.
Syntax
@mixin <mixin-name>( <mixin-parameter>#? ) {
<declaration-rule-list>
}
/* used with */
@apply <mixin-name>( <arguments>? );Values
- <mixin-name>The name of the mixin, starting with two dashes, for example --truncate(. Parentheses are used even when there are no parameters.
- <mixin-parameter>Optional, comma-separated. Each parameter is a custom property name, optionally followed by a type and a default value after a colon.
- @applyInserts a mixin's declarations into the current rule, for example @apply --truncate();. Arguments are passed in the parentheses.
- @contentsUsed inside a mixin to mark where a block of styles passed to @apply should be inserted.
Example
Browser Support
The following information will show you the current browser support for the CSS at-rule @mixin. Hover over a browser icon to see the version that first introduced support for this CSS at-rule.
Desktop





Tablets & Mobile





