{"id":5804,"date":"2016-11-26T20:44:54","date_gmt":"2016-11-26T12:44:54","guid":{"rendered":"http:\/\/www.zhangxinxu.com\/wordpress\/?p=5804"},"modified":"2020-06-08T23:44:40","modified_gmt":"2020-06-08T15:44:40","slug":"css-css3-variables-var","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2016\/11\/css-css3-variables-var\/","title":{"rendered":"\u5c0ftips:\u4e86\u89e3CSS\u53d8\u91cfvar"},"content":{"rendered":"<p>by <a href=\"http:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"http:\/\/www.zhangxinxu.com\/wordpress\/?p=5804\">http:\/\/www.zhangxinxu.com\/wordpress\/?p=5804<\/a><br \/>\n\u672c\u6587\u53ef\u5168\u6587\u8f6c\u8f7d\uff0c\u4f46\u9700\u5f97\u5230\u539f\u4f5c\u8005\u4e66\u9762\u8bb8\u53ef\uff0c\u540c\u65f6\u4fdd\u7559\u539f\u4f5c\u8005\u548c\u51fa\u5904\uff0c\u6458\u8981\u5f15\u6d41\u5219\u968f\u610f\u3002<\/p>\n<h3>\u4e00\u3001\u53d8\u91cf\u662f\u4e2a\u597d\u4e1c\u897f<\/h3>\n<p>\u5728\u4efb\u4f55\u8bed\u8a00\u4e2d\uff0c\u53d8\u91cf\u7684\u6709\u4e00\u70b9\u4f5c\u7528\u90fd\u662f\u4e00\u6837\u7684\uff0c\u90a3\u5c31\u662f\u53ef\u4ee5\u964d\u4f4e\u7ef4\u62a4\u6210\u672c\uff0c\u9644\u5e26\u8fd8\u6709\u66f4\u9ad8\u6027\u80fd\uff0c\u6587\u4ef6\u66f4\u9ad8\u538b\u7f29\u7387\u7684\u597d\u5904\u3002<\/p>\n<p>\u968f\u7740CSS\u9884\u7f16\u8bd1\u5de5\u5177Sass\/Less\/<a href=\"http:\/\/www.zhangxinxu.com\/wordpress\/2012\/06\/stylus-nodejs-expressive-dynamic-robust-css\/\">Stylus<\/a>\u7684\u5173\u6ce8\u548c\u9010\u6e10\u6d41\u884c\uff0cCSS\u5de5\u4f5c\u7ec4\u8fc5\u901f\u8ddf\u8fdb<a href=\"https:\/\/drafts.csswg.org\/css-variables\/\" target=\"_blank\">CSS\u53d8\u91cf\u7684\u89c4\u8303<\/a>\u5236\u5b9a\uff0c\u5e76\u4e14\uff0c\u5f88\u591a\u6d4f\u89c8\u5668\u5df2\u7ecf\u8ddf\u8fdb\uff0c\u76ee\u524d\uff0c\u5728\u90e8\u5206\u9879\u76ee\u4e2d\u5df2\u7ecf\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u4e86\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201611\/2016-11-26_195612.png\" width=\"622\" height=\"321\" alt=\"CSS\u53d8\u91cfvar\u517c\u5bb9\u6027\u622a\u56fe\" class=\"alignnone\" \/><\/p>\n<p>Chrome\/Firefox\/Safari\u6d4f\u89c8\u5668\u90fd\u662f\u7eff\u6cb9\u6cb9\u7684\uff0c\u517c\u5bb9\u6027\u5927\u5927\u8d85\u51fa\u6211\u7684\u9884\u671f\uff0c\u4e8e\u662f\u679c\u65ad\u5c1d\u9c9c\u8bb0\u5f55\u4e0b\u8bed\u6cd5\u7528\u6cd5\u548c\u7279\u6027\u3002<\/p>\n<h3>\u4e8c\u3001CSS\u53d8\u91cfvar()\u8bed\u6cd5\u548c\u7528\u6cd5\u548c\u7279\u6027<\/h3>\n<p>CSS\u4e2d\u539f\u751f\u7684\u53d8\u91cf\u5b9a\u4e49\u8bed\u6cd5\u662f\uff1a<code>--*<\/code>\uff0c\u53d8\u91cf\u4f7f\u7528\u8bed\u6cd5\u662f\uff1a<code>var(--*)<\/code>\uff0c\u5176\u4e2d<code>*<\/code>\u8868\u793a\u6211\u4eec\u7684\u53d8\u91cf\u540d\u79f0\u3002\u5173\u4e8e\u547d\u540d\u8fd9\u4e2a\u4e1c\u897f\uff0c\u5404\u79cd\u8bed\u8a00\u90fd\u6709\u4e9b\u663e\u793a\uff0c\u4f8b\u5982CSS\u9009\u62e9\u5668\u4e0d\u80fd\u662f\u6570\u5b57\u5f00\u5934\uff0cJS\u4e2d\u7684\u53d8\u91cf\u662f\u4e0d\u80fd\u76f4\u63a5\u6570\u503c\u7684\uff0c\u4f46\u662f\uff0c\u5728CSS\u53d8\u91cf\u4e2d\uff0c\u8fd9\u4e9b\u9650\u5236\u901a\u901a\u6ca1\u6709\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>:root {\r\n  --1: #369;\r\n}\r\nbody {\r\n  background-color: var(--1);\r\n}<\/pre>\n<p>\u7ed3\u679c\u80cc\u666f\u8272\u5982\u4e0b\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201611\/2016-11-26_201503.png\" width=\"130\" height=\"41\" alt=\"\u53d8\u91cf\u80cc\u666f\u8272\u793a\u610f\" class=\"alignnone\" \/><\/p>\n<p>\u4f46\u662f\uff0c\u4e0d\u80fd\u5305\u542b<code>$<\/code>\uff0c<code>[<\/code>\uff0c<code>^<\/code>\uff0c<code>(<\/code>\uff0c<code>%<\/code>\u7b49\u5b57\u7b26\uff0c\u666e\u901a\u5b57\u7b26\u5c40\u9650\u5728\u53ea\u8981\u662f\u201c\u6570\u5b57<code>[0-9]<\/code>\u201d\u201c\u5b57\u6bcd<code>[a-zA-Z]<\/code>\u201d\u201c\u4e0b\u5212\u7ebf<code>_<\/code>\u201d\u548c\u201c\u77ed\u6a2a\u7ebf<code>-<\/code>\u201d\u8fd9\u4e9b\u7ec4\u5408\uff0c\u4f46\u662f\u53ef\u4ee5\u662f\u4e2d\u6587\uff0c\u65e5\u6587\u6216\u8005\u97e9\u6587\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>body {\r\n  --\u6df1\u84dd: #369;\r\n  background-color: var(--\u6df1\u84dd);\r\n}<\/pre>\n<p>\u6240\u4ee5\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u4e2d\u6587\u540d\u79f0\u4f5c\u4e3a\u53d8\u91cf\uff0c\u5373\u4f7f\u82f1\u8bed4\u7ea7\u6ca1\u8fc7\u7684\u5c0f\u4f19\u4f34\u4e5f\u4e0d\u4f1a\u6709\u538b\u529b\u4e86\uff0c\u6211\u4eec\u4e5f\u4e0d\u9700\u8981\u968f\u65f6\u6302\u4e2a\u7ffb\u8bd1\u5668\u5728\u8eab\u8fb9\u4e86\u3002<\/p>\n<p>\u65e0\u8bba\u662f\u53d8\u91cf\u7684\u5b9a\u4e49\u548c\u4f7f\u7528\u53ea\u80fd\u5728\u58f0\u660e\u5757<code>{}<\/code>\u91cc\u9762\uff0c\u4f8b\u5982\uff0c\u4e0b\u9762\u8fd9\u6837\u662f\u65e0\u6548\u7684\uff1a<\/p>\n<pre style=\"color:#999;cursor:not-allowed;\">--\u6df1\u84dd: #369;\r\nbody {\r\n  background-color: var(--\u6df1\u84dd);\r\n}<\/pre>\n<p>\u53d8\u91cf\u7684\u5b9a\u4e49\uff0c\u6216\u8005\u8bf4\u58f0\u660e\u8ddf<a href=\"http:\/\/www.zhangxinxu.com\/wordpress\/2014\/08\/css-counters-automatic-number-content\/\">CSS\u8ba1\u6570\u5668<\/a>\u7684\u58f0\u660e\u7c7b\u4f3c\u7684\uff0c\u4f60\u5e94\u8be5\u6446\u8131Sass\/Less\u7b49\u9884\u7f16\u8bd1\u5de5\u5177\u8bed\u6cd5\u5148\u5165\u4e3a\u4e3b\u7684\u8bed\u6cd5\u5f71\u54cd\uff0c\u628aCSS\u7684\u539f\u751f\u53d8\u91cf\u7406\u89e3\u4e3a\u4e00\u79cdCSS\u5c5e\u6027\u3002<\/p>\n<p>\u8fd9\u6837\uff0c\u4f60\u5c31\u5bf9\u5176\u6743\u91cd\u548c\u53d8\u91cf\u5e94\u7528\u89c4\u5219\u8981\u5bb9\u6613\u7406\u89e3\u5730\u591a\u3002<\/p>\n<p>\u4f8b\u5982\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre class=\"highlight\"><span class=\"nt\">:root <\/span><span class=\"p\">{<\/span> <span class=\"k\">--color<\/span><span class=\"p\">:<\/span> purple<span class=\"p\">;<\/span> <span class=\"p\">}<\/span>\r\n<span class=\"nt\">div <\/span><span class=\"p\">{<\/span> <span class=\"k\">--color<\/span><span class=\"p\">:<\/span> green<span class=\"p\">;<\/span> <span class=\"p\">}<\/span>\r\n<span class=\"nt\">#alert <\/span><span class=\"p\">{<\/span> <span class=\"k\">--color<\/span><span class=\"p\">:<\/span> red<span class=\"p\">;<\/span> <span class=\"p\">}<\/span>\r\n<span class=\"nt\">* <\/span><span class=\"p\">{<\/span> <span class=\"k\">color<\/span><span class=\"p\">:<\/span> <span class=\"nf\">var<\/span><span class=\"p\">(<\/span>--color<span class=\"p\">)<\/span><span class=\"p\">;<\/span> <span class=\"p\">}<\/span>\r\n\r\n&lt;p&gt;<span style=\"color: purple\">\u6211\u7684\u7d2b\u8272\u7ee7\u627f\u4e8e\u6839\u5143\u7d20<\/span>&lt;\/p&gt;\r\n&lt;div&gt;<span style=\"color: green\">\u6211\u7684\u7eff\u8272\u6765\u81ea\u76f4\u63a5\u8bbe\u7f6e<\/span>&lt;\/div&gt;\r\n&lt;div id='alert'&gt;\r\n  <span style=\"color: red\">ID\u9009\u62e9\u5668\u6743\u91cd\u66f4\u9ad8\uff0c\u56e0\u6b64\u963f\u62c9\u662f\u7ea2\u8272\uff01<\/span>\r\n  &lt;p&gt;<span style=\"color: red\">\u6211\u4e5f\u662f\u7ea2\u8272\uff0c\u5360\u4e86\u7ee7\u627f\u7684\u5149<\/span>&lt;\/p&gt;\r\n&lt;\/div&gt;\r\n<\/pre>\n<p>\u4e0a\u9762\u8fd9\u4e2a\u4f8b\u5b50\u6211\u4eec\u53ef\u4ee5\u83b7\u5f97\u8fd9\u4e9b\u4fe1\u606f\uff1a<\/p>\n<ol>\n<li>\u53d8\u91cf\u4e5f\u662f\u8ddf\u7740CSS\u9009\u62e9\u5668\u8d70\u7684\uff0c\u5982\u679c\u53d8\u91cf\u6240\u5728\u7684\u9009\u62e9\u5668\u548c\u4f7f\u7528\u53d8\u91cf\u7684\u5143\u7d20\u6ca1\u6709\u4ea4\u96c6\uff0c\u662f\u6ca1\u6709\u6548\u679c\u7684\u3002\u4f8b\u5982<code>#alert<\/code>\u5b9a\u4e49\u7684\u53d8\u91cf\uff0c\u53ea\u6709<code>id<\/code>\u4e3a<code>alert<\/code>\u7684\u5143\u7d20\u624d\u80fd\u4eab\u6709\u3002\u5982\u679c\u4f60\u60f3\u53d8\u91cf\u5168\u5c40\u4f7f\u7528\uff0c\u5219\u4f60\u53ef\u4ee5\u8bbe\u7f6e\u5728<code>:root<\/code>\u9009\u62e9\u5668\u4e0a\uff1b<\/li>\n<li>\u5f53\u5b58\u5728\u591a\u4e2a\u540c\u6837\u540d\u79f0\u7684\u53d8\u91cf\u65f6\u5019\uff0c\u53d8\u91cf\u7684\u8986\u76d6\u89c4\u5219\u7531CSS\u9009\u62e9\u5668\u7684\u6743\u91cd\u51b3\u5b9a\u7684\uff0c\u4f46\u5e76\u65e0<code>!important<\/code>\u8fd9\u79cd\u7528\u6cd5\uff0c\u56e0\u4e3a\u6ca1\u6709\u5fc5\u8981\uff0c<code>!important<\/code>\u8bbe\u8ba1\u521d\u8877\u662f\u5e72\u6389JS\u7684<code>style<\/code>\u8bbe\u7f6e\uff0c\u4f46\u5bf9\u4e8e\u53d8\u91cf\u7684\u5b9a\u4e49\u5219\u6ca1\u6709\u8fd9\u6837\u7684\u9700\u6c42\u3002<\/li>\n<\/ol>\n<h4>CSS\u5c5e\u6027\u540d\u53ef\u4ee5\u8d70\u53d8\u91cf\u5417\uff1f<\/h4>\n<p>\u7c7b\u4f3c\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n<pre style=\"color:#999;cursor:not-allowed;\">body {\r\n    --bc: background-color;    \r\n    var(--bc): #369;\r\n}<\/pre>\n<p>\u7b54\u6848\u662f\u201c\u4e0d\u53ef\u4ee5\u201d\uff0c\u8981\u662f\u53ef\u4ee5\u652f\u6301\u7684\u8bdd\uff0c\u90a3CSS\u7684\u538b\u7f29\u53ef\u5c31\u8981\u9006\u5929\u4e86\uff0c\u4f30\u8ba1\u6240\u6709\u7684\u5c5e\u6027\u90fd\u4f1a\u53d8\u62101~2\u4e2a\u5b57\u7b26\u3002<\/p>\n<h4>CSS\u53d8\u91cf\u652f\u6301\u540c\u65f6\u591a\u4e2a\u58f0\u660e\u5417\uff1f<\/h4>\n<p>\u7c7b\u4f3c\u4e0b\u9762\u8fd9\u6837\uff1a<br \/>\n&#8230;<br \/>\n\u4e0d\u597d\u610f\u601d\uff0c\u7c7b\u4f3c\u4e0d\u4e86\uff0c\u8bed\u6cd5\u4e0a\u5c31\u6839\u672c\u4e0d\u652f\u6301\u3002<\/p>\n<p><strong>CSS\u53d8\u91cf\u4f7f\u7528\u5b8c\u6574\u8bed\u6cd5<\/strong><br \/>\nCSS\u53d8\u91cf\u4f7f\u7528\u7684\u5b8c\u6574\u8bed\u6cd5\u4e3a\uff1a<code>var( &lt;custom-property-name&gt; [, &lt;declaration-value&gt; ]? )<\/code>\uff0c\u7528\u4e2d\u6587\u8868\u793a\u5c31\u662f\uff1a<code>var( <\u81ea\u5b9a\u4e49\u5c5e\u6027\u540d> [, <\u9ed8\u8ba4\u503c ]? )<\/code>\uff0c<\/p>\n<p>\u610f\u601d\u5c31\u662f\uff0c\u5982\u679c\u6211\u4eec\u4f7f\u7528\u7684\u53d8\u91cf\u6ca1\u6709\u5b9a\u4e49\uff08\u6ce8\u610f\uff0c\u4ec5\u9650\u4e8e\u6ca1\u6709\u5b9a\u4e49\uff09\uff0c\u5219\u4f7f\u7528\u540e\u9762\u7684\u503c\u4f5c\u4e3a\u5143\u7d20\u7684\u5c5e\u6027\u503c\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre>.box {\r\n  --1: #369;\r\n}\r\nbody {\r\n  background-color: var(--1, #cd0000);\r\n}<\/pre>\n<p>\u5219\u6b64\u65f6\u7684\u80cc\u666f\u8272\u662f<code>#cd0000<\/code>\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201611\/2016-11-26_212334.png\" width=\"136\" height=\"35\" alt=\"\u7ea2\u8272\u80cc\u666f\u8272\" class=\"alignnone\" \/><\/p>\n<h4>CSS\u53d8\u91cf\u4e0d\u5408\u6cd5\u7684\u7f3a\u7701\u7279\u6027<\/h4>\n<p>\u8bf7\u770b\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre>body {\r\n  --color: 20px;\r\n  background-color: #369;\r\n  background-color: var(--color, #cd0000);\r\n}<\/pre>\n<p>\u8bf7\u95ee\uff0c\u6b64\u65f6<code>&lt;body&gt;<\/code>\u7684\u80cc\u666f\u8272\u662f\uff1f<\/p>\n<pre>A. transparent    B. 20px     C. #369      D. #cd0000<\/pre>\n<p>\u7b54\u6848\u662f\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026<strong style=\"color:red;\">A. transparent<\/strong><\/p>\n<p>\u4e0d\u77e5\u5927\u5bb6\u7b54\u5bf9\u4e86\u6ca1\u6709\uff01<\/p>\n<p>\u8fd9\u662fCSS\u53d8\u91cf\u975e\u5e38\u6709\u610f\u601d\u7684\u4e00\u4e2a\u70b9\uff0c\u5bf9\u4e8eCSS\u53d8\u91cf\uff0c\u53ea\u8981\u8bed\u6cd5\u662f\u6b63\u786e\u7684\uff0c\u5c31\u7b97\u53d8\u91cf\u91cc\u9762\u7684\u503c\u662f\u4e2a\u4e71\u4e03\u516b\u7cdf\u7684\u4e1c\u897f\uff0c\u4e5f\u662f\u4f1a\u4f5c\u4e3a\u6b63\u5e38\u7684\u58f0\u660e\u89e3\u6790\uff0c\u5982\u679c\u53d1\u73b0\u53d8\u91cf\u503c\u662f\u4e0d\u5408\u6cd5\u7684\uff0c\u4f8b\u5982\u4e0a\u9762\u80cc\u666f\u8272\u663e\u7136\u4e0d\u80fd\u662f<code>20px<\/code>\uff0c\u5219\u4f7f\u7528\u80cc\u666f\u8272\u7684\u7f3a\u7701\u503c\uff0c\u4e5f\u5c31\u662f\u9ed8\u8ba4\u503c\u4ee3\u66ff\uff0c\u4e8e\u662f\uff0c\u4e0a\u9762CSS\u7b49\u540c\u4e8e\uff1a<br \/>\nbody {<br \/>\n  --color: 20px;<br \/>\n  background-color: #369;<br \/>\n  background-color: transparent;<br \/>\n}<\/p>\n<p>\u5343\u4e07\u4e0d\u80fd\u60f3\u5f53\u7136\u5f97\u8ba4\u4e3a\u7b49\u540c\u4e8e<code>background-color:20px<\/code>\uff0c\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u4e0a\u9762\u8981\u5f3a\u8c03CSS\u9ed8\u8ba4\u503c\u7684\u4f7f\u7528\u4ec5\u9650\u4e8e\u53d8\u91cf\u672a\u5b9a\u4e49\u7684\u60c5\u51b5\uff0c\u5e76\u4e0d\u5305\u62ec\u53d8\u91cf\u4e0d\u5408\u6cd5\u3002<\/p>\n<h4>CSS\u53d8\u91cf\u7684\u7a7a\u683c\u5c3e\u968f\u7279\u6027<\/h4>\n<p>\u8bf7\u770b\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre>body {\r\n  --size: 20;   \r\n  font-size: var(--size)px;\r\n}<\/pre>\n<p>\u8bf7\u95ee\uff0c\u6b64\u65f6<code>&lt;body&gt;<\/code>\u7684<code>font-size<\/code>\u5927\u5c0f\u662f\u591a\u5c11\uff1f<\/p>\n<p>\u5982\u679c\u4f60\u4ee5\u4e3a\u662f<code>20px<\/code>\u5c31\u592a\u5929\u771f\u4e86\uff0c\u5b9e\u9645\u4e0a\uff0c\u6b64\u5904<code>font-size:var(--size)px<\/code>\u7b49\u540c\u4e8e<code>font-size:20 px<\/code>\uff0c\u6ce8\u610f\uff0c<code>20<\/code>\u540e\u9762\u6709\u4e2a\u7a7a\u683c\uff0c\u6240\u4ee5\uff0c\u8fd9\u91cc\u7684<code>font-size<\/code>\u4f7f\u7528\u7684\u662f<code>&lt;body&gt;<\/code>\u5143\u7d20\u9ed8\u8ba4\u7684\u5927\u5c0f\u3002\u56e0\u6b64\uff0c\u5c31\u4e0d\u8981\u5984\u56fe\u53d6\u6d88\u5c31\u4f7f\u7528\u4e00\u4e2a\u6570\u503c\u6765\u8d2f\u7a7f\u5168\u573a\uff0c\u8fd8\u662f\u4f7f\u7528\u7a33\u59a5\u7684\u505a\u6cd5\uff1a<\/p>\n<pre>body {\r\n  --size: 20px;   \r\n  font-size: var(--size);\r\n}<\/pre>\n<p>\u6216\u8005\u4f7f\u7528CSS3 <code>calc()<\/code>\u8ba1\u7b97\uff1a<\/p>\n<pre>body {\r\n  --size: 20;   \r\n  font-size: calc(var(--size) * 1px);\r\n}<\/pre>\n<p>\u6b64\u65f6\uff0c<code>&lt;body&gt;<\/code>\u7684<code>font-size<\/code>\u5927\u5c0f\u624d\u662f<code>20px<\/code>\uff0c<\/p>\n<h4>CSS\u53d8\u91cf\u7684\u76f8\u4e92\u4f20\u9012\u7279\u6027<\/h4>\n<p>\u5c31\u662f\u8bf4\uff0c\u6211\u4eec\u5728CSS\u53d8\u91cf\u5b9a\u4e49\u7684\u65f6\u5019\u53ef\u4ee5\u76f4\u63a5\u5f15\u5165\u5176\u4ed6\u53d8\u91cf\u7ed9\u81ea\u5df1\u4f7f\u7528\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>body {\r\n  --green: #4CAF50;   \r\n  --backgroundColor: var(--green);\r\n}<\/pre>\n<p>\u6216\u8005\u66f4\u590d\u6742\u7684\u4f7f\u7528CSS3 <code>calc()<\/code>\u8ba1\u7b97\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>body {\r\n  --columns: 4;\r\n  --margins: calc(24px \/ var(--columns));\r\n}<\/pre>\n<p>\u5bf9\u4e8e\u590d\u6742\u5e03\u5c40\uff0cCSS\u53d8\u91cf\u7684\u8fd9\u79cd\u76f8\u4e92\u4f20\u9012\u548c\u76f4\u63a5\u5f15\u7528\u7279\u6027\u53ef\u4ee5\u7b80\u5316\u6211\u4eec\u7684\u4ee3\u7801\u548c\u5b9e\u73b0\u6210\u672c\uff0c\u5c24\u5176\u548c\u52a8\u6001\u5e03\u5c40\u5728\u4e00\u8d77\u7684\u65f6\u5019\uff0c\u65e0\u8bba\u662fCSS\u7684\u54cd\u5e94\u5f0f\u540e\u8005\u662fJS\u9a71\u52a8\u7684\u5e03\u5c40\u53d8\u5316\u3002<\/p>\n<p>\u6211\u4eec\u6765\u770b\u4e00\u4e2aCSS\u53d8\u91cf\u4e0e\u54cd\u5e94\u5f0f\u5e03\u5c40\u7684\u4f8b\u5b50\uff0c\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"http:\/\/www.zhangxinxu.com\/study\/201611\/css-var-media-query-layout.html\">CSS\u53d8\u91cf\u4e0e\u54cd\u5e94\u5f0f\u5e03\u5c40\u5b9e\u4f8bdemo<\/a><\/p>\n<p>\u9ed8\u8ba4\u8fdb\u53bb\u662f4\u680f\uff0c\u5982\u4e0b\u56fe\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201611\/2016-11-26_224859.png\" width=\"519\" height=\"308\" alt=\"\u9ed8\u8ba4\u8fdb\u53bbdemo UI \u622a\u56fe\" class=\"alignnone\" \/><\/p>\n<p>\u968f\u7740\u6d4f\u89c8\u5668\u5bbd\u5ea6\u51cf\u5c0f\uff0c<code>4<\/code>\u680f\u53ef\u80fd\u5c31\u53d8\u6210<code>3<\/code>\u680f\uff0c<code>2<\/code>\u680f\u751a\u81f3<code>1<\/code>\u680f\uff0c\u6211\u4eec\u5b9e\u9645\u5f00\u53d1\u7684\u65f6\u5019\uff0c\u663e\u7136\u4e0d\u4ec5\u4ec5\u662f\u680f\u76ee\u6570\u91cf\u53d8\u5316\uff0c\u5bbd\u5ea6\u5c0f\uff0c\u5f80\u5f80\u610f\u5473\u7740\u8bbf\u95ee\u8bbe\u5907\u5c3a\u5bf8\u6709\u9650\uff0c\u6b64\u65f6\u6211\u4eec\u5f80\u5f80\u4f1a\u7f29\u5c0f\u7a7a\u767d\u95f4\u8ddd\u4ee5\u53ca\u6587\u5b57\u5b57\u53f7\u5927\u5c0f\uff0c\u8fd9\u6837\uff0c\u6709\u9650\u5c4f\u5e55\u624d\u80fd\u663e\u793a\u66f4\u591a\u5185\u5bb9\u3002<\/p>\n<p>\u4e5f\u5c31\u662f\u8bf4\uff0c\u5f53\u6211\u4eec\u54cd\u5e94\u5f0f\u53d8\u5316\u7684\u65f6\u5019\uff0c\u6539\u53d8\u7684CSS\u5c5e\u6027\u503c\u4e0d\u662f1\u4e2a\uff0c\u800c\u662f3\u4e2a\u6216\u8005\u66f4\u591a\uff0c\u5982\u679c\u6211\u4eec\u67093\u4e2a\u54cd\u5e94\u70b9\uff0c\u662f\u4e0d\u662f\u5c31\u81f3\u5c11\u9700\u89819\u4e2aCSS\u58f0\u660e\uff1f\u4f46\u662f\uff0c\u7531\u4e8e\u6211\u4eec\u6709\u4e86CSS\u53d8\u91cf\uff0c\u540c\u65f6\uff0cCSS\u53d8\u91cf\u53ef\u4ee5\u4f20\u9012\uff0c\u5f53\u6211\u4eec\u906d\u9047\u54cd\u5e94\u70b9\u7684\u65f6\u5019\uff0c\u6211\u4eec\u53ea\u9700\u8981\u6539\u53d8\u4e00\u4e2aCSS\u5c5e\u6027\u503c\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<p>\u4e0b\u9762\u5c31\u662f\u672cdemo\u6838\u5fc3CSS\u4ee3\u7801\uff08\u53ea\u9700\u8981\u6539\u53d8<code>--columns<\/code>\u8fd9\u4e00\u4e2a\u53d8\u91cf\u5373\u53ef\uff09\uff1a<\/p>\n<pre>.box {\r\n    --columns: 4;\r\n    --margins: calc(24px \/ var(--columns));\r\n    --space: calc(4px * var(--columns));\r\n    --fontSize: calc(20px - 4 \/ var(--columns));\r\n}\r\n@media screen and (max-width: 1200px) {\r\n    .box {\r\n        --columns: 3;\r\n    }\r\n}\r\n@media screen and (max-width: 900px) {\r\n    .box {\r\n        --columns: 2;\r\n    }\r\n}\r\n@media screen and (max-width: 600px) {\r\n    .box {\r\n        --columns: 1;\r\n    }\r\n}<\/pre>\n<p>\u4e8e\u662f\uff0c\u6211\u4eec\u57282\u680f\u4e0b\u7684\u6548\u679c\u5c31\u662f\u8fd9\u6837\uff0c\u5b57\u53f7\uff0c\u95f4\u8ddd\u968f\u7740\u680f\u76ee\u6570\u91cf\u7684\u51cf\u5c0f\u4e5f\u4e00\u5e76\u51cf\u5c0f\u4e86\uff0c\u7136\u540e\u6bcf\u680f\u4e4b\u95f4\u95f4\u8ddd\u662f\u6269\u5927\u4e86\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201611\/2016-11-26_225536.png\" width=\"519\" height=\"278\" alt=\"2\u680f\u4e0b\u7684\u6548\u679c\u622a\u56fe\" class=\"alignnone\" \/><\/p>\n<p>\u6709\u6ca1\u6709\u89c9\u5f97CSS\u8d8a\u6765\u8d8a\u5c4c\u4e86\u5462\uff01\u54c8\u54c8~<\/p>\n<p><strong>\u8865\u5145\u4e8e2019-07-08<\/strong><\/p>\n<ol>\n<li>\u5982\u4f55\u4f7f\u7528JS\u8bbe\u7f6e\u548c\u83b7\u53d6CSS\u53d8\u91cf\u53ef\u4ee5\u53c2\u8003\u8fd9\u7bc7\u6587\u7ae0\uff1a\u201c<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=8163\">HTML\u6807\u7b7e\u548cJS\u4e2d\u8bbe\u7f6eCSS3 var\u53d8\u91cf<\/a>\u201d<\/li>\n<li>IE\u6d4f\u89c8\u5668\u4e0d\u652f\u6301CSS\u53d8\u91cf\uff0c\u8bd5\u8bd5\u53c2\u8003\u8fd9\u4e2aGithub\u9879\u76ee\uff1a<a href=\"https:\/\/github.com\/jhildenbiddle\/css-vars-ponyfill\" rel=\"noopener nofollow\" target=\"_blank\">css-vars-ponyfill<\/a><\/li>\n<\/ol>\n<h3>\u4e09\u3001\u7ed3\u675f\u8bed<\/h3>\n<p>\u7531\u4e8e\u76ee\u524d\u51e0\u4e4e\u6ca1\u6709\u5173\u4e8eCSS3 <code>var()<\/code>\u7684\u6587\u7ae0\uff0c\u56e0\u6b64\uff0c\u4e0a\u9762\u5173\u4e8e<code>var()<\/code>\u7684\u8bed\u6cd5\u7279\u6027\u7b49\u90fd\u662f\u81ea\u5df1\u901a\u8fc7\u770b\u89c4\u8303\u6587\u6863\uff0c\u5916\u52a0\u7ec6\u81f4\u7684\u6d4b\u8bd5\u5f97\u5230\u7684\u3002\u4f46\u662f\uff0c\u4e00\u4e2a\u4eba\u7684\u80fd\u529b\u603b\u662f\u6709\u9650\u7684\uff0c\u56e0\u6b64\uff0c\u5fc5\u7136\u8fd8\u6709\u5f88\u591a<code>var()<\/code>\u53d8\u91cf\u6709\u610f\u601d\u7684\u70b9\u6ca1\u53d1\u73b0\uff0c\u56e0\u6b64\uff0c\u5c31\u5e0c\u671b\u5927\u5bb6\u82e5\u662f\u53d1\u73b0<code>var()<\/code>\u5176\u4ed6\u6709\u610f\u601d\u7684\u5730\u65b9\uff0c\u6b22\u8fce\u8bc4\u8bba\u544a\u77e5\uff0c\u6211\u4eec\u53ca\u65f6\u6dfb\u52a0\u5728\u6587\u7ae0\u4e2d\uff0c\u65b9\u4fbf\u4f60\u6211\u4ed6\u5979\u5b83\u3002<\/p>\n<p>\u591a\u4eba\u5408\u4f5c\u9879\u76ee\u6211\u4e5f\u4f1a\u4f7f\u7528Less\/Sass\u4e4b\u7c7b\u7684\u9884\u7f16\u8bd1\u5de5\u5177\uff0c\u4f46\u662f\uff0c\u57fa\u672c\u4e0a\u7528\u5230\u7684\u5c31\u662f\u53d8\u91cf\uff0c\u5176\u4ed6\u9ad8\u7ea7\u529f\u80fd\uff0c\u51e0\u4e4e\u90fd\u4e0d\u600e\u4e48\u4f7f\u7528\u3002\u6240\u4ee5\uff0c\u5982\u679c\u6d4f\u89c8\u5668\u5168\u65b9\u4f4d\u652f\u6301\u4e86\u539f\u751f\u7684CSS\u53d8\u91cf\uff0c\u6211\u5341\u6709\u516b\u4e5d\u5c31\u4f1a\u629b\u5f03Less\/Sass\u4e4b\u7c7b\u7684\u5de5\u5177\u3002<\/p>\n<p>\u55ef\u54ea\uff0c\u5c31\u8fd9\u4e9b\uff0c\u611f\u8c22\u9605\u8bfb\uff01<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201611\/14.png\" align=\"absmiddle\"><\/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=\"http:\/\/www.zhangxinxu.com\/wordpress\/?p=5804\">http:\/\/www.zhangxinxu.com\/wordpress\/?p=5804<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u4efb\u4f55\u8bed\u8a00\u4e2d\uff0c\u53d8\u91cf\u7684\u6709\u4e00\u70b9\u4f5c\u7528\u90fd\u662f\u4e00\u6837\u7684\uff0c\u90a3\u5c31\u662f\u53ef\u4ee5\u964d\u4f4e\u7ef4\u62a4\u6210\u672c\uff0c\u9644\u5e26\u8fd8\u6709\u66f4\u9ad8\u6027\u80fd\uff0c\u6587\u4ef6\u66f4\u9ad8\u538b\u7f29\u7387\u7684\u597d\u5904\u3002<\/p>\n<p>\u968f\u7740CSS\u9884\u7f16\u8bd1\u5de5\u5177Sass\/Less\/Stylus\u7684\u5173\u6ce8\u548c\u9010\u6e10\u6d41\u884c\uff0cCSS\u5de5\u4f5c\u7ec4\u8fc5\u901f\u8ddf\u8fdbCSS\u53d8\u91cf\u7684\u89c4\u8303\u5236\u5b9a\uff0c\u5e76\u4e14\uff0c\u5f88\u591a\u6d4f\u89c8\u5668\u5df2\u7ecf\u8ddf\u8fdb\uff0c\u76ee\u524d\uff0c\u5728\u90e8\u5206\u9879\u76ee\u4e2d\u5df2\u7ecf\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u4e86\u3002<\/p>\n<p>\u672c\u6587\u51e0\u4e4e\u5c31\u662f\u4e1a\u754c\u5173\u4e8eCSS3 var()\u53d8\u91cf\u9996\u53d1\u7684\u6587\u7ae0\uff0c\u5e0c\u671b\u91cc\u9762\u7684\u5185\u5bb9\u80fd\u591f\u5bf9\u60a8\u7684\u5de5\u4f5c\u548c\u5b66\u4e60\u6709\u6240\u5e2e\u52a9~~<\/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":[971,177,1506,1271,934,1089,503],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/5804"}],"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=5804"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/5804\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=5804"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=5804"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=5804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}