{"id":9105,"date":"2019-11-30T17:59:17","date_gmt":"2019-11-30T09:59:17","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9105"},"modified":"2019-12-06T00:26:45","modified_gmt":"2019-12-05T16:26:45","slug":"css-grid-minmax","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2019\/11\/css-grid-minmax\/","title":{"rendered":"CSS minmax()\u51fd\u6570\u7b80\u4ecb"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9105\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=9105<\/a><br \/>\n\u672c\u6587\u6b22\u8fce\u5206\u4eab\u4e0e\u805a\u5408\uff0c\u5168\u6587\u8f6c\u8f7d\u5c31\u4e0d\u5fc5\u4e86\uff0c\u5c0a\u91cd\u7248\u6743\uff0c\u5708\u5b50\u5c31\u8fd9\u4e48\u5927\uff0c\u82e5\u6025\u7528\u53ef\u4ee5\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<p><script>window.wxShareImgUrl = 'https:\/\/www.zhangxinxu.com\/study\/image\/share\/201911-css-minmax.png';<\/script><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201911\/css-minmax.png\" width=\"480\" height=\"300\" alt=\"CSS minmax\u6587\u7ae0\u5360\u4f4d\u56fe\" class=\"alignnone clip thumb size-medium\" \/><\/p>\n<h3>\u5f15\u8a00\u4e48\u4e48\u54d2<\/h3>\n<p>\u53bb\u5e74\u867d\u7136\u6709\u4e13\u95e8\u4ecb\u7ecd\u8fc7CSS grid\u5e03\u5c40\uff0c\u89c1\u201c<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/2018\/11\/display-grid-css-css3\/\">\u5199\u7ed9\u81ea\u5df1\u770b\u7684display: grid\u5e03\u5c40\u6559\u7a0b<\/a>\u201d\u3002\u4f46\u662f\u5176\u4e2d\u7684\u5185\u5bb9\u548c\u8bed\u6cd5\u90fd\u662f\u57fa\u7840\u7248\uff0c\u540e\u6765\u968f\u7740Grid\u5e03\u5c40\u89c4\u8303\u7684\u6210\u719f\uff0c\u5404\u5927\u6d4f\u89c8\u5668\u53c8\u652f\u6301\u4e86\u591a\u4e2a\u5168\u65b0\u7684\u8bed\u6cd5\u7279\u6027\uff0c\u5176\u4e2d\u4e00\u4e2a\u5f88\u91cd\u8981\u7684\u7279\u6027\u5c31\u662fCSS <code>minmax()<\/code>\u51fd\u6570\u3002<\/p>\n<p>\u76ee\u524d\u8be5\u51fd\u6570\u7684<a href=\"https:\/\/caniuse.com\/#search=minmax\" rel=\"noopener\" target=\"_blank\">\u517c\u5bb9\u6027<\/a>\u5982\u4e0b\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201911\/2019-11-30_152836.png\" width=\"720\" height=\"316\" alt=\"minmax\u51fd\u6570\u517c\u5bb9\u6027\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u79fb\u52a8\u7aef\u9879\u76ee\u53ef\u4ee5\u653e\u5fc3\u4f7f\u7528\u7684\u6709\u6728\u6709\uff1f\u6709\uff01<br \/>\n\u5185\u7f51\u9879\u76ee\u53ef\u4ee5\u653e\u5fc3\u4f7f\u7528\u7684\u6709\u6728\u6709\uff1f\u6709\uff01<br \/>\n\u5b66\u4e60\u4ef7\u503c\u5f88\u9ad8\u7684\u6709\u6728\u6709\uff1f\u4f17\u4eba\uff1a\u989d\u2026\u2026<\/p>\n<p>\u6b64\u523b\u7684\u6211\u2198<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201911\/2019-11-30_153712.png\" width=\"100\" height=\"100\" alt=\"\u6b64\u65f6\u7684\u6211\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u4eb2\u4eec\uff01\u9192\u9192\uff01<\/p>\n<p><code>minmax()<\/code>\u53ef\u4e0d\u662f\u666e\u901a\u7684\u51fd\u6570\uff0c\u5979\u53ef\u4ee5\u8ba9\u4f60\u5fc3\u65f7\u795e\u6021\uff0c\u84ec\u835c\u751f\u8f89\u3002\u662f\u4fee\u58eb\u5fc3\u4e2d\u7684\u7b51\u57fa\u4e39\uff0c\u5973\u5deb\u5fc3\u4e2d\u7684\u5929\u5723\u5c71\u3002\u96c6\u5929\u5730\u7cbe\u534e\u6d69\u7136\u6b63\u6c14\u4e8e\u4e00\u4f53\uff0c\u6563\u4e07\u4e08\u5149\u8292\u6625\u98ce\u96e8\u9732\u4e8e\u4e07\u7269\u3002<\/p>\n<p>\u53ef\u4ee5\u8fd9\u4e48\u8bf4\uff0c\u5c1a\u672a\u652f\u6301<code>minmax()<\/code>\u51fd\u6570\u7684Grid\u5e03\u5c40\u5c31\u50cf\u590f\u5929\u7684\u889c\u5b50\u2014\u2014\u53ef\u6709\u53ef\u65e0\u3002\u867d\u7136\u540d\u58f0\u5f88\u54cd\uff0c\u770b\u8d77\u6765\u4e5f\u4e0d\u9519\uff0c\u4e0d\u8fc7\u8bed\u6cd5\u6709\u70b9\u591a\uff0c\u5b66\u8d77\u6765\u6709\u70b9\u7d2f\uff0c\u53c8\u6709\u66ff\u4ee3\u7684\u5b9e\u73b0\u65b9\u6cd5\uff0c\u4e3a\u4ec0\u4e48\u8981\u975e\u4f60\u4e0d\u53ef\u5462\uff1f<\/p>\n<p>\u800c\u652f\u6301<code>minmax()<\/code>\u51fd\u6570\u7684Grid\u5e03\u5c40\u5c31\u50cf\u62e5\u6709\u4e86\u4e16\u95f4\u65e0\u53cc\u7684\u72ec\u95e8\u7edd\u5b66\uff0c\u65b9\u5706\u767e\u91cc\uff0c\u53ea\u6b64\u4e00\u5bb6\uff0c\u65e0\u53ef\u53d6\u4ee3\uff0c\u5b9e\u73b0\u7684\u6548\u679c\u5929\u5730\u64bc\u52a8\u4e07\u9a6c\u5954\u817e\u5251\u6307\u4e5d\u9704\u3002<\/p>\n<p>\u4e0d\u4fe1\u4f60\u770b\u3002<\/p>\n<h3>\u773c\u89c1\u4e3a\u5b9e\u7684\u5b9e\u4f8b<\/h3>\n<p><a href=\"https:\/\/github.com\/zhangxinxu\/quiz\/issues\/52\" rel=\"noopener\" target=\"_blank\">\u672c\u5468\u5c0f\u6d4b<\/a>\u8ba9\u5927\u5bb6\u5b9e\u73b0\u5982\u4e0b\u56fe\u6240\u793a\u7684\u5e03\u5c40\uff0c\u5173\u952e\u662f\u4e2d\u95f4\u5206\u9694\u7ebf\uff0c\u9700\u8981\u65e0\u8bba\u662f\u56db\u5217\u8fd8\u662f\u4e09\u884c\uff0c\u90fd\u80fd\u591f\u663e\u793a\u826f\u597d\uff0c\u8fd8\u662f\u6709\u4e00\u5b9a\u96be\u5ea6\u7684\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201911\/css-quiz-15.png\" width=\"320\" height=\"233\" alt=\"\u5e03\u5c40\u6548\u679c\u793a\u610f\u56fe\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u4f46\u662f\uff0c\u5982\u679c\u4f7f\u7528Grid\u5e03\u5c40\uff0c\u914d\u5408<code>minmax()<\/code>\u51fd\u6570\uff0c\u5219\u975e\u5e38\u667a\u80fd\u7684\u5e03\u5c40\u6548\u679c\u5c31\u51fa\u6765\u4e86\u3002<\/p>\n<p>\u6bd4\u65b9\u8bf4<a href=\"https:\/\/codepen.io\/xboxyan\/pen\/zYYgYdL\" rel=\"noopener\" target=\"_blank\">\u8fd9\u4f4d\u540c\u5b66\u7684\u5b9e\u73b0<\/a>\uff08codepen\u7f51\u5740\uff09\u3002<\/p>\n<p>\u53ef\u4ee5\u53c2\u8003\u4e0b\u9762\u7684\u89c6\u9891\u5f55\u5c4f\u6548\u679c\uff08\u4e0d\u52a8\u70b9\u51fb\u64ad\u653e\uff09\uff1a<\/p>\n<p><video src='\/\/image.zhangxinxu.com\/video\/blog\/201911\/grid-minmax.mp4' type=\"video\/mp4\" width=\"638\" height=\"375\" preload=\"auto\" autoplay autobuffer loop x-webkit-airplay=\"allow\" x5-video-player-type=\"h5\" x5-video-player-fullscreen=\"true\" x5-video-orientation=\"portraint\" webkit-playsinline=\"true\" playsinline=\"true\" \u200b\u200b\u200b\u200bposter=\"\/\/image.zhangxinxu.com\/image\/blog\/201806\/poster.png\"><\/video><\/p>\n<p>\u5176\u4e2d\uff0c\u5b9e\u73b0\u7684\u5173\u952e\u4ee3\u7801\u5c31\u662f\u4e0b\u9762\u8fd9\u4e24\u884c\uff1a<\/p>\n<pre>.container {\r\n    display: grid;\r\n    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));\r\n}<\/pre>\n<p>\u5176\u4e2d<code>minmax(150px, 1fr)<\/code>\u7684\u542b\u4e49\u662f\u6bcf\u4e00\u4e2a\u5217\u7684\u5bbd\u5ea6\u6700\u5c0f\u662f150\u50cf\u7d20\uff0c\u6700\u5927\u662f1fr\uff0c\u4e5f\u5c31\u662f\u7b49\u5206\u5bbd\u5ea6\u3002<\/p>\n<p>\u5047\u8bbe\u5bb9\u5668\u73b0\u5728\u7684\u5bbd\u5ea6\u662f500\u50cf\u7d20\uff0c\u6b64\u65f6\u6bcf\u4e00\u5217\u7684\u5bbd\u5ea6\u5e94\u8be5\u662f<code>166.67px<\/code>\uff0c\u56e0\u4e3a\u6b64\u65f6\u4e00\u5b9a\u662f\u4e09\u5217\uff0c\u56e0\u4e3a\u6bcf\u4e00\u5217\u7684\u6700\u5c0f\u5bbd\u5ea6\u662f150\u50cf\u7d20\uff0c\u81f3\u5c11\u5e94\u8be5\u662f500\/150\u5217\uff0c\u7531\u4e8e\u6709<code>1fr<\/code>\u7684\u6700\u5927\u5bbd\u5ea6\u9650\u5236\uff0c\u56e0\u6b64\uff0c\u6bcf\u4e00\u5217\u7684\u5bbd\u5ea6\u662f3\u5217\u7b49\u5206\u5c3a\u5bf8\u4e5f\u5c31\u662f<code>500px\/3<\/code>\u3002<\/p>\n<p>\u4e00\u4e2a\u667a\u80fd\u7684\u5f39\u6027\u7684\u5e03\u5c40\u6548\u679c\u5c31\u8fd9\u6837\u8fbe\u6210\u4e86\uff0c\u800c\u4e14\u8fd9\u79cd\u5f39\u6027\u5e03\u5c40\u662f\u4e8c\u7ef4\u5c42\u9762\u7684\u3002Table\u5e03\u5c40\u7684\u5f39\u6027\u53d8\u5316\u662f1\u7ef4\u7684\uff0c\u56e0\u4e3a\u5217\u6570\u65e0\u6cd5\u53d8\u5316\u3002<\/p>\n<h3>\u8bed\u6cd5\u3001\u53c2\u6570\u4e0e\u8bf4\u660e<\/h3>\n<h4>\u8bed\u6cd5<\/h4>\n<pre>\r\nminmax(min, max)\r\nminmax( [ &lt;length&gt; | &lt;percentage&gt; | &lt;flex&gt; | min-content | max-content | auto ] , [ &lt;length&gt; | &lt;percentage&gt; | &lt;flex&gt; | min-content | max-content | auto ] )<\/pre>\n<h4>\u53c2\u6570<\/h4>\n<dl>\n<dt><strong>&lt;length&gt;<\/strong><\/dt>\n<dd>\u5177\u4f53\u7684\u5c3a\u5bf8\u503c\uff0c\u4f8b\u5982150px\uff0c30vw\u7b49\u3002<\/dd>\n<dt><strong>&lt;percentage&gt;<\/strong><\/dt>\n<dd>\u767e\u5206\u6bd4\u503c\u3002<\/dd>\n<dt><strong>&lt;flex&gt;<\/strong><\/dt>\n<dd><code>&lt;flex&gt;<\/code>\u6570\u636e\u7c7b\u578b\u8868\u793agrid\u5bb9\u5668\u53ef\u4f38\u7f29\u7684\u957f\u5ea6\uff0c\u5982<code>1fr<\/code>, <code>1.5fr<\/code>\u3002\u5177\u4f53\u53c2\u89c1\u8fd9\u7bc7\u6587\u7ae0\uff1a\u201c<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/2019\/11\/css-value-type\/#flex-zxx\">CSS\u503c\u7c7b\u578b\u5927\u5168<\/a>\u201d<\/dd>\n<dt><strong>min-content<\/strong><\/dt>\n<dd>\u6700\u5c0f\u5185\u5bb9\u5c3a\u5bf8\u3002\u5177\u4f53\u542b\u4e49\u53ef\u53c2\u89c1<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=5392\">\u8fd9\u7bc7\u6587\u7ae0<\/a>\u3002<\/dd>\n<dt><strong>max-content<\/strong><\/dt>\n<dd>\u6700\u5927\u5185\u5bb9\u5c3a\u5bf8\u3002\u5177\u4f53\u542b\u4e49\u53ef\u53c2\u89c1<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=5392\">\u8fd9\u7bc7\u6587\u7ae0<\/a>\u3002<\/dd>\n<dt><strong>auto<\/strong><\/dt>\n<dd>\u51fa\u73b0\u51fa\u73b0\u5728min\u7684\u53c2\u6570\u4f4d\u7f6e\uff0c\u5219\u4f5c\u7528\u7b49\u540c\u4e8e<code>min-content<\/code>\uff0c\u5982\u679c\u51fa\u73b0\u5728max\u53c2\u6570\u4f4d\u7f6e\uff0c\u4f5c\u7528\u7b49\u540c\u4e8e<code>max-content<\/code>\u3002<\/dd>\n<\/dl>\n<h4>\u8bf4\u660e<\/h4>\n<ol>\n<li><code>minmax(min, max)<\/code>\u4e2d\uff0c\u5982\u679c<code>min<\/code>\u7684\u8ba1\u7b97\u503c\u6bd4<code>max<\/code>\u8fd8\u8981\u5927\uff0c\u5219<code>max<\/code>\u7684\u503c\u4f1a\u88ab\u5ffd\u7565\u3002<\/li>\n<li><code>&lt;flex&gt;<\/code>\u6570\u636e\u7c7b\u578b\u4e0d\u80fd\u4f5c\u4e3a<code>min<\/code>\u53c2\u6570\u4f7f\u7528\u3002<\/li>\n<li>minmax\u51fd\u6570\u53ea\u80fd\u7528\u5728\u4e0b\u97624\u4e2aCSS\u5c5e\u6027\u4e2d\uff1a\n<ul>\n<li>grid-template-columns<\/li>\n<li>grid-template-rows<\/li>\n<li>grid-auto-columns<\/li>\n<li>grid-auto-rows<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h4>\u793a\u610f<\/h4>\n<p>\u4e0b\u9762\u662f\u7528\u6cd5\uff0c\u90fd\u662f\u5408\u6cd5\u7684\uff0c\u6e90\u81ea<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/minmax\" rel=\"noopener\" target=\"_blank\">MDN\u6587\u6863<\/a>\uff1a<\/p>\n<pre>minmax(200px, 1fr)\r\nminmax(400px, 50%)\r\nminmax(30%, 300px)\r\nminmax(100px, max-content)\r\nminmax(min-content, 400px)\r\nminmax(max-content, auto)\r\nminmax(auto, 300px)\r\nminmax(min-content, auto)<\/pre>\n<h3>\u7ed3\u8bed\u4e48\u4e48\u54d2<\/h3>\n<p>\u54e6\u5fd8\u8bb0\u8bf4\u4e86\uff0cCSS Grid\u5e03\u5c40\u53d1\u751f\u8715\u53d8\u5f00\u59cb\u5251\u6307\u4e5d\u9704\u8fd8\u79bb\u4e0d\u5f00\u4e00\u4e2a\u51fd\u6570\uff0c\u90a3\u5c31\u662fCSS <code>repeat()<\/code>\u51fd\u6570\u3002<\/p>\n<p>\u4f8b\u5982\u4e0b\u9762\u4e00\u4e9b\u4f7f\u7528\u793a\u610f\uff1a<\/p>\n<pre>repeat(4, 1fr)\r\nrepeat(4, [col-start] 250px [col-end])\r\nrepeat(4, [col-start] 60% [col-end])\r\nrepeat(4, [col-start] 1fr [col-end])\r\nrepeat(4, [col-start] min-content [col-end])\r\nrepeat(4, [col-start] max-content [col-end])\r\nrepeat(4, [col-start] auto [col-end])\r\nrepeat(4, [col-start] minmax(100px, 1fr) [col-end])\r\nrepeat(4, [col-start] fit-content(200px) [col-end])\r\nrepeat(4, 10px [col-start] 30% [col-middle] auto [col-end])\r\nrepeat(4, [col-start] min-content [col-middle] max-content [col-end])\r\nrepeat(auto-fill, 250px)\r\nrepeat(auto-fit, 250px)\r\nrepeat(auto-fill, [col-start] 250px [col-end])\r\nrepeat(auto-fit, [col-start] 250px [col-end])\r\nrepeat(auto-fill, [col-start] minmax(100px, 1fr) [col-end])\r\nrepeat(auto-fill, 10px [col-start] 30% [col-middle] 400px [col-end])<\/pre>\n<p>\u770b\u8d77\u6765\u8fd8\u6709\u70b9\u590d\u6742\u54df\uff0c\u8fd9\u4e2a\u51fd\u6570\u6211\u4eec\u4e0b\u56de\u5206\u89e3\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201911\/memeda-567.png\" width=\"100\" height=\"100\" alt=\"\u4e48\u4e48\u54d2\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u6b22\u8fce\u5206\u4eab\uff0c\u52ff\u5168\u6587\u8f6c\u8f7d\uff0c\u5982\u679c\u5b9e\u5728\u559c\u6b22\uff0c\u53ef\u6536\u85cf\uff0c\u6c38\u4e0d\u8fc7\u671f\uff0c\u4e14\u4f1a\u53ca\u65f6\u66f4\u65b0\u77e5\u8bc6\u70b9\u53ca\u4fee\u6b63\u9519\u8bef\uff0c\u9605\u8bfb\u4f53\u9a8c\u4e5f\u66f4\u597d\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9105\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=9105<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201911\/css-minmax.png\" width=\"160\" height=\"100\" alt=\"CSS minmax\u6587\u7ae0\u5360\u4f4d\u56fe\" class=\"alignright size-medium\" \/><\/p>\n<p>\u4ecb\u7ecdCSS Grid\u5e03\u5c40\u4e2d\u975e\u5e38\u5b9e\u7528\u7684minmax()\u51fd\u6570\uff0c\u5305\u62ec\u8bed\u6cd5\uff0c\u53c2\u6570\uff0c\u7ec6\u8282\u548c\u6848\u4f8b\u7b49\uff0crepeat()\u51fd\u6570\uff0cauto-fill\u548cauto-fit\u7b49\u4e5f\u4f1a\u767b\u573a\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":[1525,1526,1271,1528,1514,1057,1058,1527,1283],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/9105"}],"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=9105"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/9105\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=9105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=9105"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=9105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}