{"id":11847,"date":"2025-09-05T11:08:44","date_gmt":"2025-09-05T03:08:44","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11847"},"modified":"2025-09-05T11:08:44","modified_gmt":"2025-09-05T03:08:44","slug":"css-function-at-rules","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2025\/09\/css-function-at-rules\/","title":{"rendered":"@function\u81ea\u5b9a\u4e49\u51fd\u6570\u8ba9CSS\u652f\u6301\u7f16\u7a0b\u5566"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11847\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=11847<\/a><br \/>\n\u672c\u6587\u53ef\u5168\u6587\u8f6c\u8f7d\uff0c\u4f46\u9700\u8981\u4fdd\u7559\u539f\u4f5c\u8005\u3001\u51fa\u5904\u4ee5\u53ca\u6587\u4e2d\u94fe\u63a5\uff0cAI\u6293\u53d6\u4fdd\u7559\u539f\u6587\u5730\u5740\uff0c\u4efb\u4f55\u7f51\u7ad9\u5747\u53ef\u6458\u8981\u805a\u5408\uff0c\u5546\u7528\u8bf7\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202509\/2025-9-5_110609.jpeg\" width=\"390\" height=\"238\" alt=\"@function\u5c01\u9762\u56fe\" class=\"alignright clip size-medium\" \/><\/p>\n<h3>\u4e00\u3001@function\u89c4\u5219\u6765\u5566<\/h3>\n<p>Chrome 139\u6d4f\u89c8\u5668\u9664\u4e86\u65b0\u652f\u6301\u4e86 CSS if()\u51fd\u6570\uff08\u53c2\u89c1\u201c<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/2025\/07\/css-if-function\/\">CSS\u5012\u53cd\u5929\u7f61\u5c45\u7136\u652f\u6301if()\u51fd\u6570\u4e86<\/a>\u201d\u4e00\u6587\uff09\uff0c\u8fd8\u652f\u6301\u4e86\u671f\u5f85\u5df2\u4e45\u7684<code>@function<\/code>\u89c4\u5219\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202509\/2025-09-04_155337.png\" width=\"632\" height=\"282\" alt=\"@function\u51fd\u6570\u7684\u517c\u5bb9\u6027\" class=\"alignnone size-medium\" \/><\/p>\n<p><code>@function<\/code>\u89c4\u5219\u53ef\u4ee5\u8ba9\u6211\u4eec\u81ea\u5b9a\u4e49\u8fd0\u7b97\u903b\u8f91\uff0c\u6bd4\u4e4b\u524d\u5355\u7eaf\u7684CSS <code>var()<\/code>\u53d8\u91cf\u8981\u66f4\u52a0\u5f3a\u5927\u3002<\/p>\n<p>\u4e0b\u9762\u6211\u4eec\u901a\u8fc7\u51e0\u4e2a\u6848\u4f8b\u5e26\u5927\u5bb6\u5feb\u901f\u4e86\u89e3\u4e0b<code>@function<\/code>\u89c4\u5219\u7684\u7528\u6cd5\u548c\u4f5c\u7528\u3002<\/p>\n<h3>\u4e8c\u3001@function\u89c4\u5219\u7cbe\u5f69\u6848\u4f8b<\/h3>\n<p>\u5148\u770b\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\uff0c\u662f\u6211\u5b9e\u9645\u9879\u76ee\u4e2d\u4ee3\u7801\u7684\u622a\u56fe\uff0c\u5927\u5bb6\u6ce8\u610f\u770b\u7ea2\u6846\u6846\u90e8\u5206\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202509\/2025-9-4_160531.png\" width=\"571\" height=\"224\" alt=\"\u6e90\u7801\u793a\u610f\u622a\u56fe\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u5b57\u7b26\u7248\u5982\u4e0b\uff1a<\/p>\n<pre>.board-bar-x {\r\n  \/* \u7565... *\/\r\n  --bottom: calc(var(--offset-bottom, 0px) + 15px + 0.25rem);\r\n  --bottom2: calc(var(--offset-bottom2, 0px) + 15px + 0.25rem);\r\n  --size: calc(20px + calc((var(--char-length) + 1) * 1ch));\r\n  --size2: calc(20px + calc((var(--char-length2) + 1) * 1ch));\r\n}<\/pre>\n<p>\u5b9e\u73b0\u529f\u80fd\u662f\u4f7f\u7528<code>::before<\/code>\u548c<code>::after<\/code>\u4f2a\u5143\u7d20\u7ed8\u5236\u4e24\u6761\u7ebf\u3002<\/p>\n<p>\u5728\u6ca1\u6709<code>@function<\/code>\u89c4\u5219\u4e4b\u524d\uff0c\u5f53\u4e00\u4e2a\u590d\u6742\u8ba1\u7b97\u9700\u8981\u591a\u5904\u4f7f\u7528\u7684\u65f6\u5019\uff0c\u6211\u4f1a\u5b9a\u4e49\u4e13\u95e8\u7684CSS\u53d8\u91cf\u8fdb\u884c\u7b80\u5316\uff0c\u4f8b\u5982\u4e0a\u9762\u7684<code>--bottom<\/code>\u548c<code>--bottom2<\/code>\uff0c\u53ef\u5373\u4fbf\u5982\u6b64\uff0c\u540e\u9762\u7684\u8ba1\u7b97\u903b\u8f91\u6211\u8fd8\u662f\u8981\u4e00\u4e2a\u4e00\u4e2a\u7f57\u5217\u3002<\/p>\n<p>\u4e0a\u9762\u8fd9\u53e5\u8bdd\u6709\u4e9b\u4eba\u4f30\u8ba1\u4e0d\u660e\u767d\uff0c\u90a3\u6211\u91cd\u65b0\u5904\u7406\u4e0b\uff0c\u5982\u679c\u4e0d\u4f7f\u7528CSS\u53d8\u91cf\u8fdb\u884c\u4e8c\u6b21\u53d8\u91cf\u5904\u7406\uff0c\u90a3\u4e48\u5b9e\u9645\u7684CSS\u4ee3\u7801\u4f1a\u662f\u8fd9\u6837\uff08\u4ec5\u5c55\u793a\u5173\u952e\u90e8\u5206\uff09\uff1a<\/p>\n<pre>.board-bar-x::before {\r\n  position: absolute;\r\n  bottom: calc(var(--offset-bottom, 0px) + 15px + 0.25rem);\r\n  padding-bottom: calc(var(--offset-bottom, 0px) + 15px + 0.25rem);\r\n}\r\n.board-bar-x::after {\r\n  position: absolute;\r\n  bottom: calc(var(--offset-bottom2, 0px) + 15px + 0.25rem);\r\n  padding-bottom: calc(var(--offset-bottom2, 0px) + 15px + 0.25rem);\r\n}<\/pre>\n<p>\u4f1a\u51fa\u73b04\u4e2a\u5197\u957f\u7684CSS\u5c5e\u6027\u503c\uff0c\u4f7f\u7528CSS\u53d8\u91cf\u7b80\u5316\u540e\uff0c\u5c31\u4f1a\u662f\u8fd9\u6837\uff1a<\/p>\n<pre>.board-bar-x::before {\r\n  position: absolute;\r\n  bottom: var(--bottom);\r\n  padding-bottom: var(--bottom);\r\n}\r\n.board-bar-x::after {\r\n  position: absolute;\r\n  bottom: var(--bottom2);\r\n  padding-bottom: var(--bottom2);\r\n}<\/pre>\n<p>\u4f46\u662f\uff0c\u5b9a\u4e49\u53d8\u91cf\u7684\u90a3\u4e2a\u5730\u65b9\uff0c\u8fd8\u662f\u9700\u8981\u4e00\u4e00\u7f57\u5217\uff1a<\/p>\n<pre>.board-bar-x {\r\n  --bottom: calc(var(--offset-bottom, 0px) + 15px + 0.25rem);\r\n  --bottom2: calc(var(--offset-bottom2, 0px) + 15px + 0.25rem);\r\n}<\/pre>\n<p>CSS <code>@function<\/code>\u5c31\u53ef\u4ee5\u5c06\u8fd9\u79cd\u7f57\u5217\uff0c\u4e5f\u4e00\u5e76\u5904\u7406\u6389\u3002\u4ee3\u7801\u793a\u610f\uff1a<\/p>\n<pre>@function --fn(--offset) {\r\n  result: calc(var(--offset, 0px) + 15px + 0.25rem)\r\n}\r\n.board-bar-x {\r\n  --bottom: --fn(var(--offset-bottom));\r\n  --bottom2: --fn(var(--offset-bottom2));\r\n}\r\n.board-bar-x::before {\r\n  position: absolute;\r\n  bottom: var(--bottom);\r\n  padding-bottom: var(--bottom);\r\n}\r\n.board-bar-x::after {\r\n  position: absolute;\r\n  bottom: var(--bottom2);\r\n  padding-bottom: var(--bottom2);\r\n}<\/pre>\n<p>\u6216\u8005\u4e0d\u518d\u4f7f\u7528CSS\u53d8\u91cf\u8fc7\u6e21\uff0c\u76f4\u63a5\u4e00\u6b65\u5230\u4f4d\u3002<\/p>\n<pre>@function --fn (--offset) {\r\n  result: calc(var(--offset, 0px) + 15px + 0.25rem)\r\n}\r\n.board-bar-x::before {\r\n  position: absolute;\r\n  bottom: --fn(var(--offset-bottom));\r\n  padding-bottom: --fn(var(--offset-bottom));\r\n}\r\n.board-bar-x::after {\r\n  position: absolute;\r\n  bottom: --fn(var(--offset-bottom2));\r\n  padding-bottom: --fn(var(--offset-bottom2));\r\n}<\/pre>\n<p>\u662f\u4e0d\u662f\u5c31\u5bb9\u6613\u7406\u89e3\u591a\u4e86\uff1f<\/p>\n<h3>\u4e09\u3001\u66f4\u591a\u81ea\u5b9a\u4e49\u51fd\u6570\u6848\u4f8b<\/h3>\n<h4>1. \u9002\u5408\u5b66\u4e60\u6848\u4f8b-\u53d6\u8d1f\u503c<\/h4>\n<p>CSS\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>\/* \u8fd4\u56de\u5f53\u524d\u503c\u7684\u8d1f\u503c *\/\r\n@function --negate(--value) {\r\n  result: calc(-1 * var(--value));\r\n}\r\naside {\r\n  --size: 999em;\r\n  padding-bottom: var(--size);\r\n  margin-bottom: --negate(var(--size));\r\n}<\/pre>\n<h4>2. \u9ad8\u7ea7\u6848\u4f8b &#8211; media\u67e5\u8be2<\/h4>\n<pre>\/* \u5728\u5c0f\u4e8e640px\u7684\u5c4f\u5e55\u4e0a\u4e3a\u4fa7\u8fb9\u680f\u5360\u75281fr\u7684\u7a7a\u95f4\uff0c\u5728\u8f83\u5927\u7684\u5c4f\u5e55\u4e0a\u4f7f\u7528\u76f8\u5bf9\u5177\u4f53\u5177\u4f53\u5bbd\u5ea6\u503c *\/\r\n@function --layout-sidebar(--sidebar-width: max(20ch, 20vw)) {\r\n  result: 1fr;\r\n  \r\n  @media (width > 640px) {\r\n    result: var(--sidebar-width) auto;\r\n  }\r\n}\r\n\r\n.layout {\r\n  display: grid;\r\n  \/*\u4fa7\u8fb9\u680f\u5bbd\u5ea6\u515c\u5e9520ch\u548c20vw\u7684\u8f83\u5927\u8ba1\u7b97\u503c *\/\r\n  grid-template-columns: --layout-sidebar();\r\n}<\/pre>\n<p>\u5b9e\u65f6\u6548\u679c\u5982\u4e0b\u6240\u793a\uff08\u62d6\u62fd\u53f3\u4e0b\u89d2\u7684\u5c0f\u6309\u94ae\uff0c\u6539\u53d8\u5bbd\u5ea6\uff0c\u770b\u770b\u5e03\u5c40\u6709\u6ca1\u6709\u53d8\u5316\uff09\uff1a<\/p>\n<div style=\"padding:10px;overflow:hidden;resize:horizontal;\"><iframe loading=\"lazy\" src=\"\/study\/202509\/function-media-demo.html\" width=\"100%\" height=\"400\"><\/iframe><\/div>\n<h4>3. \u9ad8\u7ea7\u6848\u4f8b &#8211; \u548c if() \u51fd\u6570\u4e00\u8d77<\/h4>\n<p>\u793a\u610f\u4ee3\u7801\uff1a<\/p>\n<pre>@function --light-dark(--light, --dark) {\r\n  result: if(\r\n    style(--scheme: dark): var(--dark);\r\n    else: var(--light)\r\n  );\r\n}<\/pre>\n<p>\u53ea\u8981\u5728body\u6216\u8005HTML\u5143\u7d20\u4e0a\u8bbe\u7f6e <code>style=\"--scheme: dark\"<\/code>\uff0c\u5c31\u4f1a\u81ea\u52a8\u4f7f\u7528\u9ed1\u591c\u6a21\u5f0f\u7684\u53d8\u91cf\u3002<\/p>\n<h3>\u56db\u3001\u7ed3\u8bed\u53ca\u788e\u788e\u5ff5<\/h3>\n<p>CSS \u7684 @function \u89c4\u5219\u4e3a\u6837\u5f0f\u8868\u5e26\u6765\u4e86\u771f\u6b63\u7684\u7f16\u7a0b\u80fd\u529b\uff0c\u5141\u8bb8\u4f60\u5b9a\u4e49\u53ef\u91cd\u7528\u7684\u8ba1\u7b97\u903b\u8f91\uff0c\u8ba9 CSS \u4ee3\u7801\u66f4\u7075\u6d3b\u3001\u6a21\u5757\u5316\u4e14\u6613\u4e8e\u7ef4\u62a4\u3002<\/p>\n<h4>\u57fa\u672c\u8bed\u6cd5<\/h4>\n<p>\u4f7f\u7528 <code>@function<\/code> \u5b9a\u4e49\uff0c<code>result<\/code> \u8fd4\u56de\u7ed3\u679c\uff0c\u793a\u610f\uff1a<\/p>\n<pre>@function --fn-name(arg) { \r\n  result: ... \r\n}<\/pre>\n<p>\u652f\u6301\u4f20\u9012\u53c2\u6570\uff0c\u5e76\u53ef\u8bbe\u7f6e\u9ed8\u8ba4\u503c\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>@function --border(--color: red) { ... }<\/pre>\n<h4>\u5176\u4ed6\u7279\u6027<\/h4>\n<p>\u652f\u6301\u58f0\u660e\u53c2\u6570\u7c7b\u578b\uff08\u5982 length, color, number, angle\uff09\uff0c\u589e\u5f3a\u5065\u58ee\u6027\u3002<\/p>\n<p>\u51fd\u6570\u5185\u90e8\u53ef\u4ee5\u8c03\u7528\u5176\u4ed6\u51fd\u6570\uff0c\u5b9e\u73b0\u903b\u8f91\u590d\u7528\uff0c\u6216\u8005\u4f7f\u7528\u5d4c\u5957\u5176\u4ed6\u89c4\u5219\uff0c\u4e0a\u9762\u6709\u6848\u4f8b\u3002<\/p>\n<p>\u603b\u7ed3\u4e00\u4e0b CSS @function \u901a\u8fc7\u5f15\u5165\u53c2\u6570\u3001\u8fd4\u56de\u503c\u3001\u7c7b\u578b\u68c0\u67e5\u548c\u7ec4\u5408\u80fd\u529b\uff0c\u8ba9 CSS \u62e5\u6709\u4e86\u66f4\u5f3a\u5927\u7684\u62bd\u8c61\u548c\u590d\u7528\u80fd\u529b\u3002\u5b83\u662f\u5b9e\u73b0 \u201cCSS \u5373\u8bbe\u8ba1\u7cfb\u7edf\u201d \u7406\u5ff5\u7684\u91cd\u8981\u5de5\u5177\uff0c\u80fd\u6709\u6548\u63d0\u5347\u5927\u578b\u6216\u52a8\u6001\u9879\u76ee\u7684\u6837\u5f0f\u4ee3\u7801\u8d28\u91cf\u3002<\/p>\n<p>OK\uff0c\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\uff0c\u5982\u679c\u4f60\u89c9\u5f97\u8fd8\u4e0d\u9519\uff0c\u6b22\u8fce\u8f6c\u53d1\uff0c\u70b9\u8d5e\uff0c\u5206\u4eab\u3002<\/p>\n<p>\ud83d\ude09\ud83d\ude0a\ud83d\ude07<br \/>\n\ud83e\udd70\ud83d\ude0d\ud83d\ude18<\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u4f1a\u7ecf\u5e38\u66f4\u65b0\u77e5\u8bc6\u70b9\u4ee5\u53ca\u4fee\u6b63\u4e00\u4e9b\u9519\u8bef\uff0c\u56e0\u6b64\u8f6c\u8f7d\u8bf7\u4fdd\u7559\u539f\u51fa\u5904\uff0c\u65b9\u4fbf\u6eaf\u6e90\uff0c\u907f\u514d\u9648\u65e7\u9519\u8bef\u77e5\u8bc6\u7684\u8bef\u5bfc\uff0c\u540c\u65f6\u6709\u66f4\u597d\u7684\u9605\u8bfb\u4f53\u9a8c\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11847\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=11847<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202509\/2025-9-5_110609.jpeg\" width=\"160\" height=\"98\" alt=\"@function\u5c01\u9762\u56fe\" class=\"alignright size-medium\" loading=\"lazy\" \/><\/p>\n<p>\u7ec8\u4e8e\u6765\u4e86\uff0cCSS\u4e5f\u652f\u6301\u7f16\u7a0b\u4e86\uff0c\u4f7f\u7528@function \u89c4\u5219\u53ef\u4ee5\u5b9a\u4e49\u53ef\u91cd\u7528\u7684\u8ba1\u7b97\u903b\u8f91\uff0c\u8ba9 CSS \u4ee3\u7801\u66f4\u7075\u6d3b\u3001\u6a21\u5757\u5316\u4e14\u6613\u4e8e\u7ef4\u62a4\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[1015,1591,1506,1322,1827],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/11847"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=11847"}],"version-history":[{"count":7,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/11847\/revisions"}],"predecessor-version":[{"id":11861,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/11847\/revisions\/11861"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=11847"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=11847"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=11847"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}