@property<\/code> for?<\/h4>\nCSS 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. <\/p>\n
However, one limitation has been the inability to specify a property\u2019s 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.<\/p>\n
This is where the @property<\/code> 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. <\/p>\nThis ensures that a variable can only accept a specific data type, such as a length, color, or number.<\/p>\n
The type of the property is defined with the syntax<\/code> property. It accepts a string value defining the CSS type value, as follows:<\/p>\n\n
\n\n\nType<\/th>\n | Description<\/th>\n<\/tr>\n<\/thead>\n |
\n\n<length><\/code><\/td>\nAny valid <length><\/code> values e.g., 10px, 2em, 50%, 3rem<\/td>\n<\/tr>\n\n<number><\/code><\/td>\nAny valid <number><\/code> values e.g., 1, 0.5, -3, 100<\/td>\n<\/tr>\n\n<percentage><\/code><\/td>\nAny valid <percentage><\/code> values e.g., 50%, 100%, 25%<\/td>\n<\/tr>\n\n<length-percentage><\/code><\/td>\nAny valid <length-percentage><\/code> values e.g., 10px, 50%, 2em, 75%<\/td>\n<\/tr>\n\n<color><\/code><\/td>\nAny valid <color><\/code> values e.g., #ff0000, rgb(255, 0, 0), rgba(255, 0, 0, 0.5), blue<\/td>\n<\/tr>\n\n<image><\/code><\/td>\nAny valid <image><\/code> values e.g., url(image.jpg), linear-gradient(to right, red, blue)<\/td>\n<\/tr>\n\n<url><\/code><\/td>\nAny valid url()<\/code> values e.g., url(\u2018https:\/\/example.com\/image.jpg\u2019)<\/td>\n<\/tr>\n\n<integer><\/code><\/td>\nAny valid <integer><\/code> values e.g., 1, -10, 42<\/td>\n<\/tr>\n\n<angle><\/code><\/td>\nAny valid <angle><\/code> values e.g., 45deg, 0.5turn, 1rad<\/td>\n<\/tr>\n\n<time><\/code><\/td>\nAny valid <time><\/code> values e.g., 1s, 500ms, 0.75s<\/td>\n<\/tr>\n\n<resolution><\/code><\/td>\nAny valid <resolution><\/code> values e.g., 300dpi, 2dppx<\/td>\n<\/tr>\n\n<transform-function><\/code><\/td>\nAny valid <transform-function><\/code> values e.g., rotate(45deg), scale(1.5), translateX(100px)<\/td>\n<\/tr>\n\n<custom-ident><\/code><\/td>\nAny valid <custom-ident><\/code> values e.g., \u2013my-variable, custom-identifier<\/td>\n<\/tr>\n\n<transform-list><\/code><\/td>\nA list of valid <transform-function><\/code> values e.g., rotate(45deg) scale(1.5) translateX(100px)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\nExample<\/h4>\nLet\u2019s say we have a button component. We\u2019d 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:<\/p>\n \r\n.button {\r\n background-color: #fff;\r\n border-radius: 8px;\r\n}\r\n<\/pre>\nOr, use CSS variables to define the values once and reuse them throughout the stylesheet:<\/p>\n \r\n:root {\r\n --button-bg: #fff;\r\n --button-rounded: 8px;\r\n}\r\n<\/pre>\nBut, 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? <\/p>\n We can use the @property<\/code> rule to define the type of these custom properties and set default values. <\/p>\nTo do so, we could create a couple of custom properties defined with the following options in the @property<\/code> rule:<\/p>\n\r\n@property --button-bg {\r\n syntax: '<color>';\r\n initial-value: #0D74CE;\r\n inherits: false;\r\n}\r\n@property --button-rounded {\r\n syntax: '<length>';\r\n initial-value: 8px;\r\n inherits: false;\r\n}\r\n<\/pre>\nIn this example, we have two custom properties defining the background color and border radius of the button component. <\/p>\n We use the syntax<\/code> property to define the type of the custom property, while the initial-value<\/code> property sets the default value. <\/p>\nWe also use the inherits<\/code> 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.<\/p>\nOnce they are set, we can now use these custom properties in our styles, like so:<\/p>\n \r\n.button {\r\n background-color: var(--button-bg);\r\n border-radius: var(--button-rounded);\r\n}\r\n<\/pre>\n See the Pen CSS @property<\/a> by HONGKIAT (@hkdc<\/a>) \n on CodePen<\/a>.<\/span> <\/p>\n<\/p>\nWrapping up<\/h4>\nThe CSS @property<\/code> rule brings a significant step forward to CSS custom properties, or CSS variables. All major and latest browsers already support the CSS @property<\/code> rule.<\/p>\n\n \n\n\nBrowser<\/th>\n | Desktop Version<\/th>\n | Mobile Version<\/th>\n<\/tr>\n<\/thead>\n | \n\nGoogle Chrome<\/td>\n | 85 and later<\/td>\n | 85 and later<\/td>\n<\/tr>\n | \nMozilla Firefox<\/td>\n | 128 and later<\/td>\n | Not supported<\/td>\n<\/tr>\n | \nSafari<\/td>\n | 15.4 and later<\/td>\n | 15.4 and later (iOS)<\/td>\n<\/tr>\n | \nMicrosoft Edge<\/td>\n | 85 and later<\/td>\n | 85 and later<\/td>\n<\/tr>\n | \nOpera<\/td>\n | 71 and later<\/td>\n | 71 and later<\/td>\n<\/tr>\n | \nSamsung Internet<\/td>\n | 14.0 and later<\/td>\n | 14.0 and later<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n To sum up, the CSS @property<\/code> 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\u2019t already, give it a try in your next project!<\/p>\nThe post Getting Started with CSS @property Rule<\/a> appeared first on Hongkiat<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"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 […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[12],"tags":[],"_links":{"self":[{"href":"http:\/\/www.diveintoaccessibility.com\/index.php\/wp-json\/wp\/v2\/posts\/927"}],"collection":[{"href":"http:\/\/www.diveintoaccessibility.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.diveintoaccessibility.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.diveintoaccessibility.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.diveintoaccessibility.com\/index.php\/wp-json\/wp\/v2\/comments?post=927"}],"version-history":[{"count":1,"href":"http:\/\/www.diveintoaccessibility.com\/index.php\/wp-json\/wp\/v2\/posts\/927\/revisions"}],"predecessor-version":[{"id":928,"href":"http:\/\/www.diveintoaccessibility.com\/index.php\/wp-json\/wp\/v2\/posts\/927\/revisions\/928"}],"wp:attachment":[{"href":"http:\/\/www.diveintoaccessibility.com\/index.php\/wp-json\/wp\/v2\/media?parent=927"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.diveintoaccessibility.com\/index.php\/wp-json\/wp\/v2\/categories?post=927"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.diveintoaccessibility.com\/index.php\/wp-json\/wp\/v2\/tags?post=927"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}} | | | | | | | | | | | | | | | | | | | | | | | | | | | | |