Getting Started with CSS @property Rule (2024)

By Thoriq Firdaus. in Coding. Updated on.

The CSS @property is a powerful feature that brings more control and flexibility to custom properties, also known as CSS variables.

It is introduced as part of the CSS Houdini project, which is designed to provide developers with deeper access to the browser’s rendering engine. The @property rule allows you to define custom properties with specific types, default values, and even the capability to animate CSS properties.

In this article, we’ll explore what the @property rule is, why it’s useful, and how you can leverage it in your web projects.

What is @property for?

CSS custom properties, also known as CSS variables, have made styles more reusable. It allows you to define a value once and use it throughout your stylesheet.

However, one limitation has been the inability to specify a property’s type or set default values directly in CSS. This means any value can be assigned to a custom property, which can lead to unexpected results and make it harder to maintain your styles in some situations.

This is where the @property rule comes in. It allows you to set a default value, provides a fallback value that will be used if the custom property is not explicitly set, enables custom property animation, and defines a type for a custom property.

This ensures that a variable can only accept a specific data type, such as a length, color, or number.

The type of the property is defined with the syntax property. It accepts a string value defining the CSS type value, as follows:

TypeDescription
<length>Any valid <length> values e.g., 10px, 2em, 50%, 3rem
<number>Any valid <number> values e.g., 1, 0.5, -3, 100
<percentage>Any valid <percentage> values e.g., 50%, 100%, 25%
<length-percentage>Any valid <length-percentage> values e.g., 10px, 50%, 2em, 75%
<color>Any valid <color> values e.g., #ff0000, rgb(255, 0, 0), rgba(255, 0, 0, 0.5), blue
<image>Any valid <image> values e.g., url(image.jpg), linear-gradient(to right, red, blue)
<url>Any valid url() values e.g., url(‘https://example.com/image.jpg’)
<integer>Any valid <integer> values e.g., 1, -10, 42
<angle>Any valid <angle> values e.g., 45deg, 0.5turn, 1rad
<time>Any valid <time> values e.g., 1s, 500ms, 0.75s
<resolution>Any valid <resolution> values e.g., 300dpi, 2dppx
<transform-function>Any valid <transform-function> values e.g., rotate(45deg), scale(1.5), translateX(100px)
<custom-ident>Any valid <custom-ident> values e.g., –my-variable, custom-identifier
<transform-list>A list of valid <transform-function> values e.g., rotate(45deg) scale(1.5) translateX(100px)

Example

Let’s say we have a button component. We’d like to define some defaults on this component. Traditionally, we could use custom properties to define the background color and border radius of the button component, like so:

.button { background-color: #fff; border-radius: 8px;}

Or, use CSS variables to define the values once and reuse them throughout the stylesheet:

:root { --button-bg: #fff; --button-rounded: 8px;}

But, what if we want to ensure that the background color is always a valid color value, and the border radius is always a valid length value?

We can use the @property rule to define the type of these custom properties and set default values.

To do so, we could create a couple of custom properties defined with the following options in the @property rule:

@property --button-bg { syntax: '<color>'; initial-value: #0D74CE; inherits: false;}@property --button-rounded { syntax: '<length>'; initial-value: 8px; inherits: false;}

In this example, we have two custom properties defining the background color and border radius of the button component.

We use the syntax property to define the type of the custom property, while the initial-value property sets the default value.

We also use the inherits property to specify whether the custom property inherits its value from its parent element, in which case we set them all to false to avoid inheritance.

Once they are set, we can now use these custom properties in our styles, like so:

.button { background-color: var(--button-bg); border-radius: var(--button-rounded);}

See the Pen CSS @property by HONGKIAT (@hkdc) on CodePen.

Wrapping up

The CSS @property rule brings a significant step forward to CSS custom properties, or CSS variables. All major and latest browsers already support the CSS @property rule.

BrowserDesktop VersionMobile Version
Google Chrome85 and later85 and later
Mozilla Firefox128 and laterNot supported
Safari15.4 and later15.4 and later (iOS)
Microsoft Edge85 and later85 and later
Opera71 and later71 and later
Samsung Internet14.0 and later14.0 and later

To sum up, the CSS @property rule is a powerful feature and a great addition to the CSS language that can help you write more maintainable and type-safe stylesheets. If you haven’t already, give it a try in your next project!

Getting Started with CSS @property Rule (2024)

FAQs

How to use setProperty in CSS? ›

CSSStyleDeclaration setProperty() Method
  1. Set a new CSS property: var declaration = document. styleSheets[0]. cssRules[0]. ...
  2. Set a new CSS property with "important" priotrity: var declaration = document. styleSheets[0]. cssRules[0]. ...
  3. Modify an existing CSS property: var declaration = document. styleSheets[0]. cssRules[0].

What does @property do in CSS? ›

Controls whether the custom property registration specified by @property inherits by default. Sets the initial value for the property. The @property rule must include both the syntax and inherits descriptors; if either are missing, the entire @property rule is invalid and ignored.

How do I start a rule in CSS? ›

Create a CSS rule
  1. Place the insertion point in the document, and then do one of the following to open the New CSS Rule dialog box: ...
  2. In the New CSS Rule dialog box, specify the selector type for the CSS rule you want to create: ...
  3. Select the location in which you want to define the rule, and then click OK:
May 16, 2021

What is the syntax of setProperty? ›

The setProperty() method is used to modify the existing CSS property or set the new CSS in the declaration block. Syntax: It is used to return the length property. Parameters: It accepts three parameters: propertyname: It is a required parameter that contains a string which represents the name of the property to set.

Where do I put CSS rules? ›

How to Add CSS to HTML
  • Inline CSS is written inside an HTML tag with the style attribute. ...
  • Internal CSS is written inside a <style> element, which goes inside the <head> of the HTML document.
  • External CSS is written in a separate file called an external stylesheet, and linked to the HTML document with a <link> tag.
Aug 20, 2024

What is an example for CSS property? ›

A CSS property determines an HTML element's style or behavior. Examples include font style, transform, border, color, and margin.

What is the most used property in CSS? ›

Common CSS Properties
  • background. Set a background color using a common color name: ...
  • color. Set the font color of text using a common color name: ...
  • font-family. Set the font of the element: ...
  • height and width. Set the size of the element: ...
  • float. Set the alignment of an element on the page. ...
  • padding. ...
  • margin. ...
  • text-align.

What is the difference between CSS rule and property? ›

A CSS rule consists of a CSS selector and a set of CSS properties. The CSS selector determines what HTML elements to target with the CSS rule. The CSS properties specifies what to style of the targeted HTML elements.

What is the starting style rule in CSS? ›

The @starting-style CSS at-rule is used to define starting values for properties set on an element that you want to transition from when the element receives its first style update, i.e. when an element is first displayed on a previously loaded page.

What is an example of a rule set in CSS? ›

CSS Rule Sets

A CSS rule set contains one or more selectors and one or more declarations. The selector(s), which in this example is h1 , points to an HTML element. The declaration(s), which in this example are color: blue and text-align: center style the element with a property and value.

What is the first part of a CSS rule? ›

A CSS selector is the first part of a CSS Rule. It is a pattern of elements and other terms that tell the browser which HTML elements should be selected to have the CSS property values inside the rule applied to them.

What is the first thing to do in CSS? ›

The very first thing we need to do is to tell the HTML document that we have some CSS rules we want it to use.

What is the easiest CSS framework to learn? ›

Bulma is more of a collection of CSS classes than UI components. It has a clean and intuitive syntax and is less complex and easier to understand compared to other component-based frameworks like Foundation and Bootstrap.

How does system setProperty work? ›

Value: This is the value of the system property. setProperty() method helps to set the system property key to have the value as value. Returns: This method would return the previous value of the system property in the form of String, or null if it did not have one.

How to set CSS property in HTML? ›

CSS may be added to HTML in three different ways. To style a single HTML element on the page, use Inline CSS in a style attribute. By adding CSS to the head section of our HTML document, we can embed an internal stylesheet. We can also connect to an external stylesheet that separates our CSS from our HTML.

How do you set custom properties in CSS? ›

They are set using the @property at-rule or by custom property syntax (e.g., --primary-color: blue; ). Custom properties are accessed using the CSS var() function (e.g., color: var(--primary-color); ). Complex websites have very large amounts of CSS, and this often results in a lot of repeated CSS values.

How to set value using CSS? ›

The var() function is used to insert the value of a CSS variable. CSS variables have access to the DOM, which means that you can create variables with local or global scope, change the variables with JavaScript, and change the variables based on media queries.

References

Top Articles
Great Clips, 10079 Fremont Pike, Perrysburg, OH 43551, US - MapQuest
Great Clips, 2200 Sean Dr, # Liberty, Fremont, OH 43420, US - MapQuest
Maxtrack Live
Pollen Count Los Altos
Edina Omni Portal
Housing near Juneau, WI - craigslist
Yogabella Babysitter
Coverage of the introduction of the Water (Special Measures) Bill
Kokichi's Day At The Zoo
Craigslist Cars And Trucks For Sale By Owner Indianapolis
Vaya Timeclock
St Petersburg Craigslist Pets
Aiken County government, school officials promote penny tax in North Augusta
CA Kapil 🇦🇪 Talreja Dubai on LinkedIn: #businessethics #audit #pwc #evergrande #talrejaandtalreja #businesssetup…
Roblox Character Added
How Quickly Do I Lose My Bike Fitness?
Find your energy supplier
Caroline Cps.powerschool.com
Unit 1 Lesson 5 Practice Problems Answer Key
The Weather Channel Facebook
Oppenheimer Showtimes Near Cinemark Denton
Radio Aleluya Dialogo Pastoral
Q33 Bus Schedule Pdf
Water Trends Inferno Pool Cleaner
Td Small Business Banking Login
Morristown Daily Record Obituary
Is The Yankees Game Postponed Tonight
Sussyclassroom
Uncovering The Mystery Behind Crazyjamjam Fanfix Leaked
Toothio Login
No Limit Telegram Channel
Mini-Mental State Examination (MMSE) – Strokengine
Uno Fall 2023 Calendar
Sam's Club Gas Price Hilliard
Home Auctions - Real Estate Auctions
Tenant Vs. Occupant: Is There Really A Difference Between Them?
Otter Bustr
Petsmart Northridge Photos
Trivago Myrtle Beach Hotels
NHL training camps open with Swayman's status with the Bruins among the many questions
Yogu Cheshire
Craigslist Lakeside Az
Foxxequeen
8776725837
Squalicum Family Medicine
Florida Lottery Powerball Double Play
Terrell Buckley Net Worth
Brutus Bites Back Answer Key
Billings City Landfill Hours
Famous Dave's BBQ Catering, BBQ Catering Packages, Handcrafted Catering, Famous Dave's | Famous Dave's BBQ Restaurant
Unity Webgl Extreme Race
Dr Seuss Star Bellied Sneetches Pdf
Latest Posts
Article information

Author: Errol Quitzon

Last Updated:

Views: 5423

Rating: 4.9 / 5 (59 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Errol Quitzon

Birthday: 1993-04-02

Address: 70604 Haley Lane, Port Weldonside, TN 99233-0942

Phone: +9665282866296

Job: Product Retail Agent

Hobby: Computer programming, Horseback riding, Hooping, Dance, Ice skating, Backpacking, Rafting

Introduction: My name is Errol Quitzon, I am a fair, cute, fancy, clean, attractive, sparkling, kind person who loves writing and wants to share my knowledge and understanding with you.