{"id":311526,"date":"2024-05-20T22:17:00","date_gmt":"2024-05-20T14:17:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/311526.html"},"modified":"2024-05-20T22:17:13","modified_gmt":"2024-05-20T14:17:13","slug":"css-%e7%bc%96%e7%a8%8b%e9%a1%b9%e7%9b%ae%e6%80%8e%e4%b9%88%e5%ae%9e%e7%8e%b0%e4%b8%80%e4%b8%aa%e7%80%91%e5%b8%83%e6%b5%81%e5%b8%83%e5%b1%80-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/311526.html","title":{"rendered":"CSS \u7f16\u7a0b\u9879\u76ee\u600e\u4e48\u5b9e\u73b0\u4e00\u4e2a\u7011\u5e03\u6d41\u5e03\u5c40"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26232231\/08f6d658-f573-4f23-bf69-f602a0bca795.webp\" alt=\"CSS \u7f16\u7a0b\u9879\u76ee\u600e\u4e48\u5b9e\u73b0\u4e00\u4e2a\u7011\u5e03\u6d41\u5e03\u5c40\" \/><\/p>\n<p><p>CSS \u7f16\u7a0b\u9879\u76ee\u4e2d\u5b9e\u73b0\u4e00\u4e2a\u7011\u5e03\u6d41\u5e03\u5c40\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528Flexbox\u3001CSS Grid\u3001\u5217\u5c5e\u6027\uff08column\uff09\u6216\u8005\u7ed3\u5408JavaScript\u3002<strong>Flexbox<\/strong>\u76f8\u5bf9\u7b80\u5355\uff0c\u53ef\u4ee5\u8f7b\u6613\u5730\u5bf9\u9f50\u5143\u7d20\u4e0e\u5904\u7406\u672a\u77e5\u6216\u52a8\u6001\u5c3a\u5bf8\u7684\u5185\u5bb9\uff0c\u4f46\u5b83\u5728\u5236\u4f5c\u4f20\u7edf\u7684\u7011\u5e03\u6d41\u5e03\u5c40\u65f6\u53ef\u80fd\u4f1a\u6709\u5c40\u9650\uff0c\u56e0\u4e3aFlexbox\u7684\u884c\u662f\u5f7c\u6b64\u72ec\u7acb\u7684\u3002<strong>CSS Grid<\/strong>\u4e3a\u590d\u6742\u5e03\u5c40\u63d0\u4f9b\u4e86\u66f4\u591a\u76f4\u63a5\u7684\u63a7\u5236\uff0c\u4f46\u662f\u9700\u8981\u9884\u5148\u77e5\u9053\u5185\u5bb9\u7684\u5c3a\u5bf8\u6216\u8005\u63a5\u53d7\u8f83\u4e3a\u590d\u6742\u7684\u81ea\u9002\u5e94\u5e03\u5c40\u8bbe\u8ba1\u3002<strong>\u5217\u5c5e\u6027<\/strong>\u662f\u4e00\u4e2a\u7b80\u5355\u4e14\u4e0d\u9700\u8981\u989d\u5916JavaScript\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u4f46\u5b83\u5728\u5404\u4e2a\u5217\u4e4b\u95f4\u4e0d\u80fd\u4fdd\u8bc1\u5747\u8861\u7684\u6761\u76ee\u5206\u5e03\u3002\u7efc\u4e0a\u6240\u8ff0\uff0c\u5982\u679c\u9879\u76ee\u8981\u6c42\u8f83\u4e3a\u7075\u6d3b\u7684\u5e03\u5c40\u4ee5\u53ca\u5bf9\u6d4f\u89c8\u5668\u7684\u5411\u540e\u517c\u5bb9\u6027\u6ca1\u6709\u4e25\u683c\u8981\u6c42\uff0c\u63a8\u8350\u4f7f\u7528CSS Grid\u3002<\/p>\n<\/p>\n<p><p><strong>CSS Grid<\/strong>\u65b9\u6cd5\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/p>\n<\/p>\n<p><p>CSS Grid\u5e03\u5c40\u662f\u76ee\u524d\u5b9e\u73b0\u7011\u5e03\u6d41\u7684\u6700\u76f4\u63a5\u65b9\u6cd5\u3002\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2agrid\u5bb9\u5668\uff0c\u5e76\u5b9a\u4e49\u5217\u6570\u548c\u884c\u7684\u81ea\u9002\u5e94\u6027\u3002Grid\u7684<code>auto-fill<\/code>\u6216<code>auto-fit<\/code>\u5173\u952e\u5b57\u80fd\u591f\u6839\u636e\u5bb9\u5668\u7684\u5927\u5c0f\u81ea\u52a8\u586b\u5145\u5217\u6570\uff0c\u800c<code>minmax()<\/code>\u51fd\u6570\u5219\u5141\u8bb8\u4f60\u8bbe\u5b9a\u884c\u9ad8\u7684\u6700\u5c0f\u503c\u548c\u6700\u5927\u503c\uff0c\u4fdd\u6301\u5e03\u5c40\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001CSS GRID\u5e03\u5c40<\/p>\n<\/p>\n<p><pre><code class=\"language-css\">.cont<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ner {<\/p>\n<p>  display: grid;<\/p>\n<p>  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));<\/p>\n<p>  grid-gap: 10px;<\/p>\n<p>}<\/p>\n<p>.item {<\/p>\n<p>  background: #f7f7f7;<\/p>\n<p>  border: 1px solid #ebebeb;<\/p>\n<p>  padding: 10px;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;div class=&quot;container&quot;&gt;<\/p>\n<p>  &lt;div class=&quot;item&quot;&gt;...&lt;\/div&gt;<\/p>\n<p>  &lt;!-- More items here --&gt;<\/p>\n<p>&lt;\/div&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2agrid\u5bb9\u5668\uff0c\u5176\u5217\u5bbd\u81f3\u5c11\u4e3a250px\uff0c\u5e76\u4f1a\u968f\u7740\u5bb9\u5668\u7684\u6269\u5927\u81ea\u52a8\u589e\u52a0\u66f4\u591a\u5217\u3002\u6bcf\u4e2a\u9879\u4e4b\u95f4\u670910px\u7684\u95f4\u9699\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001FLEXBOX\u5e03\u5c40<\/p>\n<\/p>\n<p><pre><code class=\"language-css\">.container {<\/p>\n<p>  display: flex;<\/p>\n<p>  flex-wrap: wrap;<\/p>\n<p>  align-content: flex-start;<\/p>\n<p>}<\/p>\n<p>.item {<\/p>\n<p>  width: 250px;<\/p>\n<p>  margin: 5px;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;div class=&quot;container&quot;&gt;<\/p>\n<p>  &lt;div class=&quot;item&quot;&gt;...&lt;\/div&gt;<\/p>\n<p>  &lt;!-- More items here --&gt;<\/p>\n<p>&lt;\/div&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u65b9\u6cd5\u867d\u4e0d\u662f\u4f20\u7edf\u610f\u4e49\u4e0a\u7684\u7011\u5e03\u6d41\u5e03\u5c40\uff0c\u4f46Flexbox\u7684<code>flex-wrap<\/code>\u7279\u6027\u80fd\u591f\u7b80\u5355\u5b9e\u73b0\u591a\u884c\u7684\u5143\u7d20\u6392\u5217\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5217\u5c5e\u6027\u5e03\u5c40<\/p>\n<\/p>\n<p><pre><code class=\"language-css\">.container {<\/p>\n<p>  column-count: 3;<\/p>\n<p>  column-gap: 10px;<\/p>\n<p>}<\/p>\n<p>.item {<\/p>\n<p>  break-inside: avoid;<\/p>\n<p>  margin-bottom: 10px;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;div class=&quot;container&quot;&gt;<\/p>\n<p>  &lt;div class=&quot;item&quot;&gt;...&lt;\/div&gt;<\/p>\n<p>  &lt;!-- More items here --&gt;<\/p>\n<p>&lt;\/div&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u5217\u5c5e\u6027\u53ef\u4ee5\u5bb9\u6613\u5730\u5b9e\u73b0\u7011\u5e03\u6d41\u5e03\u5c40\uff0c\u65e0\u9700\u989d\u5916\u7684CSS\u89c4\u5219\u6216JavaScript\u3002\u5728\u6b64\u65b9\u6cd5\u4e2d\uff0c<code>.item<\/code>\u4e2d\u7684<code>break-inside<\/code>\u5c5e\u6027\u7528\u4e8e\u907f\u514d\u5217\u5185\u7684\u9879\u76ee\u88ab\u5206\u5272\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001JavaScript\u589e\u5f3a\u7684\u7011\u5e03\u6d41\u5e03\u5c40<\/p>\n<\/p>\n<p><p>\u6709\u4e9b\u573a\u666f\u4e0bCSS\u5355\u72ec\u5b9e\u73b0\u7011\u5e03\u6d41\u5e03\u5c40\u53ef\u80fd\u8fbe\u4e0d\u5230\u9884\u671f\u6548\u679c\uff08\u5982\u5217\u4e4b\u95f4\u7684\u9ad8\u5ea6\u5dee\u5f02\u8fc7\u5927\uff09\uff0c\u8fd9\u65f6\u53ef\u4ee5\u501f\u52a9JavaScript\u5bf9\u5e03\u5c40\u8fdb\u4e00\u6b65\u4f18\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">\/\/ \u7b80\u5355\u7684\u7011\u5e03\u6d41\u5e03\u5c40jQuery\u63d2\u4ef6\u5b9e\u4f8b<\/p>\n<p>var $container = $(&#039;.container&#039;);<\/p>\n<p>$container.imagesLoaded(function() {<\/p>\n<p>  $container.masonry({<\/p>\n<p>    itemSelector: &#039;.item&#039;,<\/p>\n<p>    columnWidth: 250,<\/p>\n<p>    gutter: 10<\/p>\n<p>  });<\/p>\n<p>});<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u7c7b\u4f3c\u4e8eMasonry\u8fd9\u6837\u7684\u5e93\u53ef\u4ee5\u7075\u6d3b\u5b9e\u73b0\u7011\u5e03\u6d41\u5e03\u5c40\uff0c\u5e76\u89e3\u51b3\u8bf8\u5982\u56fe\u7247\u52a0\u8f7d\u5bfc\u81f4\u5e03\u5c40\u53d8\u5316\u7b49\u95ee\u9898\u3002\u8fd9\u79cd\u65b9\u6cd5\u9700\u8981\u5f15\u5165\u4e00\u4e9b\u989d\u5916\u7684\u5e93\uff0c\u4f46\u80fd\u591f\u5728\u4e0d\u540c\u7684\u573a\u666f\u4e2d\u63d0\u4f9b\u7a33\u5b9a\u7684\u5e03\u5c40\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q: \u5982\u4f55\u4f7f\u7528CSS\u5b9e\u73b0\u4e00\u4e2a\u7011\u5e03\u6d41\u5e03\u5c40\uff1f<\/strong><\/p>\n<p>A: \u5b9e\u73b0\u4e00\u4e2a\u7011\u5e03\u6d41\u5e03\u5c40\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9bCSS\u6280\u5de7\u6765\u5b8c\u6210\u3002\u9996\u5148\uff0c\u9700\u8981\u4f7f\u7528CSS\u7684flexbox\u5e03\u5c40\u6765\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u591a\u5217\u7684\u5bb9\u5668\u3002\u7136\u540e\uff0c\u5bf9\u5bb9\u5668\u5185\u7684\u5143\u7d20\u8fdb\u884c\u5b9a\u4f4d\uff0c\u4f7f\u5176\u6309\u7167\u7011\u5e03\u6d41\u7684\u5f62\u5f0f\u6392\u5217\u3002\u53ef\u4ee5\u4f7f\u7528CSS\u7684column-count\u5c5e\u6027\u6765\u6307\u5b9a\u5217\u6570\uff0c\u540c\u65f6\u4e5f\u53ef\u4ee5\u4f7f\u7528column-gap\u5c5e\u6027\u6765\u8bbe\u7f6e\u5217\u4e0e\u5217\u4e4b\u95f4\u7684\u95f4\u8ddd\u3002\u8fd8\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u4f2a\u7c7b\u9009\u62e9\u5668\u548cnth-child()\u6765\u8bbe\u7f6e\u4e0d\u540c\u5217\u7684\u6837\u5f0f\u3002<\/p>\n<p><strong>Q: \u5728CSS\u7f16\u7a0b\u9879\u76ee\u4e2d\uff0c\u5982\u4f55\u786e\u4fdd\u7011\u5e03\u6d41\u5e03\u5c40\u7684\u54cd\u5e94\u5f0f\u8bbe\u8ba1\uff1f<\/strong><\/p>\n<p>A: \u8981\u786e\u4fdd\u7011\u5e03\u6d41\u5e03\u5c40\u5728\u4e0d\u540c\u5c4f\u5e55\u5c3a\u5bf8\u4e0a\u90fd\u80fd\u5448\u73b0\u826f\u597d\u7684\u6548\u679c\uff0c\u53ef\u4ee5\u5229\u7528CSS\u7684\u5a92\u4f53\u67e5\u8be2\u6765\u5b9e\u73b0\u54cd\u5e94\u5f0f\u8bbe\u8ba1\u3002\u5728\u5a92\u4f53\u67e5\u8be2\u4e2d\uff0c\u53ef\u4ee5\u6307\u5b9a\u4e0d\u540c\u7684\u5217\u6570\u548c\u5143\u7d20\u5bbd\u5ea6\uff0c\u4ee5\u9002\u5e94\u4e0d\u540c\u7684\u8bbe\u5907\u3002\u53ef\u4ee5\u6839\u636e\u8bbe\u5907\u7684\u5c4f\u5e55\u5bbd\u5ea6\u6765\u8c03\u6574\u5217\u6570\u548c\u5143\u7d20\u5bbd\u5ea6\uff0c\u4ece\u800c\u4fdd\u6301\u7011\u5e03\u6d41\u5e03\u5c40\u7684\u7f8e\u89c2\u548c\u5b8c\u6574\u6027\u3002<\/p>\n<p><strong>Q: \u5728CSS\u7f16\u7a0b\u9879\u76ee\u4e2d\uff0c\u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u7011\u5e03\u6d41\u5e03\u5c40\uff1f<\/strong><\/p>\n<p>A: \u9664\u4e86\u4f7f\u7528CSS\u7684flexbox\u5e03\u5c40\u548c\u5a92\u4f53\u67e5\u8be2\u6765\u5b9e\u73b0\u7011\u5e03\u6d41\u5e03\u5c40\u5916\uff0c\u8fd8\u53ef\u4ee5\u8003\u8651\u4f7f\u7528JavaScript\u5e93\u6216\u6846\u67b6\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528Masonry.js\u8fd9\u6837\u7684\u5e93\u6765\u81ea\u52a8\u5e03\u5c40\u7011\u5e03\u6d41\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u66f4\u52a0\u7075\u6d3b\u548c\u6613\u4e8e\u4f7f\u7528\uff0c\u53ea\u9700\u5f15\u5165\u5e93\u6587\u4ef6\u548c\u8bbe\u7f6e\u4e00\u4e9b\u914d\u7f6e\u5373\u53ef\u3002\u4f46\u662f\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4f7f\u7528JavaScript\u6765\u5b9e\u73b0\u7011\u5e03\u6d41\u5e03\u5c40\u53ef\u80fd\u4f1a\u589e\u52a0\u7f51\u9875\u52a0\u8f7d\u65f6\u95f4\uff0c\u56e0\u6b64\u5728\u6027\u80fd\u65b9\u9762\u9700\u8981\u8fdb\u884c\u8bc4\u4f30\u548c\u4f18\u5316\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"CSS \u7f16\u7a0b\u9879\u76ee\u4e2d\u5b9e\u73b0\u4e00\u4e2a\u7011\u5e03\u6d41\u5e03\u5c40\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528Flexbox\u3001CSS Grid\u3001\u5217\u5c5e\u6027\uff08col [&hellip;]","protected":false},"author":3,"featured_media":311542,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/311526"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=311526"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/311526\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/311542"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=311526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=311526"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=311526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}