• About Us
  • Privacy Policy
  • Disclaimer
  • Contact Us
AimactGrow
  • Home
  • Technology
  • AI
  • SEO
  • Coding
  • Gaming
  • Cybersecurity
  • Digital marketing
No Result
View All Result
  • Home
  • Technology
  • AI
  • SEO
  • Coding
  • Gaming
  • Cybersecurity
  • Digital marketing
No Result
View All Result
AimactGrow
No Result
View All Result

@operate | CSS-Methods

Admin by Admin
June 7, 2026
Home Coding
Share on FacebookShare on Twitter


The @operate at-rule defines CSS customized capabilities. These customized capabilities are reusable blocks of CSS that may settle for arguments, comprise advanced logic, and return values based mostly on that logic. The function is analogous in nature to a extra dynamic model of customized properties (CSS variables).

Notice: There may be additionally a @operate at-rule in Sass which has similarities in function however totally different in operate to the native CSS @operate. Concentrate on this if Sass is a part of your stack or when looking for assets as it’s simple to conflate one with the opposite.

Syntax

The @operate at-rule defines a customized operate, utilizing the next syntax:

@operate --function-name(#?) [returns ]? {
  
}

 =  ? [ :  ]?

In different phrases, we outline the operate’s title as a dashed ident (--my-function), provide some situation we wish to match (), and say what kind of factor we wish to return, say, a CSS[] worth. And, if that situation matches, we apply kinds ().

Let’s dig deeper into what these issues really imply.

Arguments and Descriptors

There are a selection of components to the syntax for @operate to deal with totally different components of the function. It might all look very advanced — and it’s — but it surely’ll turn out to be clearer later once we have a look at some examples.

--function-token

A user-defined identifier that should begin with two dashes (--), much like the dashed-ident of customized properties. Identical to customized properties, the title is case-sensitive. For instance, --conversion and --Conversion would confer with totally different customized operate definitions.

@operate --progression()

(optionally available)

An optionally available comma-separated listing of inputs that may embody:

  • --param-name: The title of the argument (should begin with --).
  • (optionally available): A key phrase or sort (e.g., , ) that tells the operate what kind of enter or consequence it’s returning when it hits a matched situation.
  • (optionally available): A fallback worth that’s returned if the result’s invalid, such because the argument is omitted throughout the operate name. In the event you present a default worth, it should be legitimate to the aforementioned (e.g. a should default to a legitimate CSS size). It’s separated from the remainder of the parameter definition with a colon (:).
  • returns (optionally available): Defines the anticipated output sort of the operate. This helps the browser validate logic earlier than rendering. If a kind isn’t specified then, something will probably be legitimate (like writing returns sort(*)).
  • : CSS declarations and at-rules that assemble the operate’s physique and logic. It might embody customized properties and the consequence descriptor — both on the root or nested inside an at-rule.
@operate --progression(--current , --total ) returns  {
  consequence:
}

The consequence descriptor that defines what the customized operate will return. If a customized operate forgoes the consequence descriptor, it’s going to all the time return guaranteed-invalid worth, similar to a damaged customized property.

@operate --progression(--current , --total ) returns  {}

Fundamental Utilization

For an instance of probably the most primary operate you would make, we’ve a operate that calculates a offered worth (e.g. 20px) in half (e.g. 10px), and returns returns it as a size unit (e.g. px):

@operate --half(--size ) {
  consequence: calc(var(--size) / 2);
}

Right here, we’re ‘naming’ our operate by setting the function-token to --half. We’re then making a function-parameter referred to as --size, and setting the css-type to , so that it’s going to solely settle for size values. The consequence descriptor is ready to calc(--size / 2), which makes use of the CSS Calculating Perform to halve the worth sourced from the dimension function-parameter.

We then use the operate like so:

.container {
  margin-inline: --half(20px); /* It will resolve to 10px */
}

Kind Checking

Identical to when writing JavaScript or different languages, generally we wish to guarantee a operate solely accepts sure arguments. For instance, what if we wish to be certain that solely numbers will be enter and that solely a proportion will be output?

@operate --progression(--current , --total ) returns  {
  consequence: calc(var(--current) / var(--total) * 100%);
}

.progress-bar {
  width: --progression(3, 5); /* Evaluates to 60% */
}

The is enclosed in angle brackets in a way similar to the way you type-check a customized property through @property. If an argument doesn’t match the declared sort (e.g. ), the operate name turns into invalid, which may be very precious for catching bugs early in giant codebases.

You should utilize a to permit a number of varieties by wrapping the categories in sort() and utilizing | as a separator. For instance, --alpha right here permits each and :

@operate --transparent(--color , --alpha sort( | ));

Comma-Separated Lists

CSS makes use of commas to separate the inputs of a customized operate, which begs the query: what should you want to present a listing of values? To offer a listing of values as one enter fairly than a number of separate inputs, you have to first mark a operate to count on a listing.

To do that, you suffix the # character to the . When calling the operate, you then wrap the listing of values in curly braces, which tells the browser to deal with every little thing contained in the braces as a single argument.

As an illustration:

/* Calculates the gap between the best and lowest values in a listing, plus one other enter */
@operate --get-range(--list #, --n ) {
  consequence: calc(max(var(--list)) - min(var(--list)) + var(--n));
}

div {
  /* Finds the distinction between 10px and 100px, then provides 200px */
  padding-block: --get-range({10px, 100px, 50px, 25px}, 200px); /* 290px */
}

Constructs and the CSS Cascade

The consequence descriptor follows the principles of the CSS Cascade. Meaning you possibly can declare a number of consequence values, and the final legitimate matching worth will win, similar to another properties. As such, conditional group guidelines (@media, @container, @helps) and different capabilities, equivalent to if(), present a number of further prospects.

On this case, we return a --suitable-font-size that defaults to 16px when the display screen is lower than 1000px pixels. If the display screen is larger than 1000px then the “successful” fashion is what’s within the @media block.

@operate --suitable-font-size() returns  {
  consequence: 16px;

  @media (width > 1000px) {
    consequence: 20px;
  }
}

physique {
  font-size: --suitable-font-size();
}

Understand that the final outlined worth all the time wins, so should you have been to jot down the instance under, the consequence would all the time be 16px, whatever the media question being triggered.

@operate --suitable-font-size() returns  {
  @media (width > 1000px) {
    consequence: 20px;
  }

  consequence: 16px;
}

The adherence to the established cascade additionally means that you can use customized properties inside a operate. These customized properties are domestically scoped, so they’re solely accessible in your customized operate and any customized operate that references it and thus received’t unexpectedly leak out globally and work together with the remainder of your CSS.

@operate --spacing-scale(--multiplier) {
  --base-unit: 8px;
  consequence: calc(var(--base-unit) * var(--multiplier));
}

You can even use different customized capabilities inside a customized operate, primarily nesting one operate inside one other. This permits for very clear code, the place every part solely does one job and capabilities will be broadly reused.

@operate --square(--n) {
  consequence: calc(var(--n) * var(--n));
}

@operate --circle-area(--radius) {
  --pi: 3.14159;
  consequence: calc(var(--pi) * --square(var(--radius)));
}

.blob {
  width: calc(--circle-area(10) * 1px); /* 314.159px */
}

Defaults

Capabilities can deal with a number of arguments and supply default values. The default worth is outlined by together with it on the finish of the operate parameter, separated by a colon (:).

/* Outline the operate */
@operate --brand-glass(--opacity : 0.5) returns  {
  consequence: rgb(10 120 255 / var(--opacity));
}

/* Use the operate */
.header {
  background: --brand-glass(); /* Defaults to 0.5 */
}

.header:hover {
  background: --brand-glass(0.8); /* Overrides to 0.8 */
}

No Facet Results

A CSS @operate can solely return a price; it can’t do anything. For instance, you can’t change a property within a operate or use a operate to generate a number of declarations. For such talents, one should look to the proposed @mixin at-rule, which would supply performance on this method, permitting a number of traces of CSS properties and different advanced logic.

Round Dependencies

CSS may be very strict about round logic. If Perform A calls Perform B, and Perform B calls Perform A, the browser will catch this cyclic dependency and instantly mark each as invalid.

This additionally applies to CSS Customized Properties and referring to the customized operate itself. If a operate depends on a customized property or operate that’s itself calculated by that very same operate, the browser will finish the calculation to forestall an infinite recursion.

Specification

The @operate at-rule is outlined within the CSS Customized Capabilities and Mixins Module Degree 1 specification.

Browser Help

Unsupported browsers ignore @operate, so fallback declarations and progressive enhancement methods will be advantageous. You should utilize @helps to examine if @operate is supported within the consumer’s browser, like so:

@helps (at-rule(@operate)) {
  /* ... */
}

Satirically, nonetheless, at time of writing, the @helps at-rule analysis performance doesn’t have full assist throughout browsers (Chrome 148+ solely), so you will have to examine whether it is supported in your case. You possibly can see the dialogue on this in CSS Drafts Difficulty #2463.

Extra Info

Tags: CSSTricksFunction
Admin

Admin

Next Post
8 Combating Video games That Are Longer Than 100 Hours

8 Combating Video games That Are Longer Than 100 Hours

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recommended.

A “ChatGPT for spreadsheets” helps remedy troublesome engineering challenges sooner | MIT Information

A “ChatGPT for spreadsheets” helps remedy troublesome engineering challenges sooner | MIT Information

March 5, 2026
Why Anthropic’s New AI Mannequin Typically Tries to ‘Snitch’

Why Anthropic’s New AI Mannequin Typically Tries to ‘Snitch’

May 29, 2025

Trending.

Nsfw Chatgpt Options – Examples I’ve Used

Nsfw Chatgpt Options – Examples I’ve Used

October 13, 2025
Digital Detox & Display Time Statistics 2025

Digital Detox & Display Time Statistics 2025

March 28, 2026
Codex CLI Is OpenAI’s Boldest Dev Transfer But, This is Why

8 Greatest AI Coding Assistants I Advocate for 2026

May 10, 2026
Shopflo Secures $20M in Funding Spherical Led by Binny Bansal, Units Its Sights on World Retail Tech Disruption

Shopflo Secures $20M in Funding Spherical Led by Binny Bansal, Units Its Sights on World Retail Tech Disruption

July 29, 2025
What’s a Ahead Deployed Engineer: The AI Position OpenAI, Anthropic, and Google Are Hiring in 2026

What’s a Ahead Deployed Engineer: The AI Position OpenAI, Anthropic, and Google Are Hiring in 2026

May 21, 2026

AimactGrow

Welcome to AimactGrow, your ultimate source for all things technology! Our mission is to provide insightful, up-to-date content on the latest advancements in technology, coding, gaming, digital marketing, SEO, cybersecurity, and artificial intelligence (AI).

Categories

  • AI
  • Coding
  • Cybersecurity
  • Digital marketing
  • Gaming
  • SEO
  • Technology

Recent News

8 Combating Video games That Are Longer Than 100 Hours

8 Combating Video games That Are Longer Than 100 Hours

June 7, 2026
distinction() | CSS-Tips

@operate | CSS-Methods

June 7, 2026
  • About Us
  • Privacy Policy
  • Disclaimer
  • Contact Us

© 2025 https://blog.aimactgrow.com/ - All Rights Reserved

No Result
View All Result
  • Home
  • Technology
  • AI
  • SEO
  • Coding
  • Gaming
  • Cybersecurity
  • Digital marketing

© 2025 https://blog.aimactgrow.com/ - All Rights Reserved