{"id":593,"date":"2024-12-28T17:51:51","date_gmt":"2024-12-28T09:51:51","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=593"},"modified":"2024-12-28T17:51:51","modified_gmt":"2024-12-28T09:51:51","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-css-%e5%88%9b%e5%bb%ba%e4%b8%80%e4%b8%aa%e5%93%8d%e5%ba%94%e5%bc%8f%e7%9a%84-%e4%b8%8d%e5%8f%af%e5%8f%98%e6%96%b9%e5%bd%a2%e5%85%83%e7%b4%a0-%e7%9a%84","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2024\/12\/28\/%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-css-%e5%88%9b%e5%bb%ba%e4%b8%80%e4%b8%aa%e5%93%8d%e5%ba%94%e5%bc%8f%e7%9a%84-%e4%b8%8d%e5%8f%af%e5%8f%98%e6%96%b9%e5%bd%a2%e5%85%83%e7%b4%a0-%e7%9a%84\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 CSS \u521b\u5efa\u4e00\u4e2a\u54cd\u5e94\u5f0f\u7684\u00a0\u4e0d\u53ef\u53d8\u65b9\u5f62\u5143\u7d20\u00a0\u7684\u7f51\u683c\uff1f"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u8981\u4f7f\u7528 CSS \u521b\u5efa\u4e00\u4e2a\u54cd\u5e94\u5f0f\u7684 <strong>\u4e0d\u53ef\u53d8\u65b9\u5f62\u5143\u7d20<\/strong> \u7684\u7f51\u683c\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <strong>CSS Grid<\/strong> \u6216 <strong>Flexbox<\/strong> \u5e03\u5c40\uff0c\u5e76\u7ed3\u5408 <strong><code>padding<\/code><\/strong> \u6280\u5de7\u6765\u786e\u4fdd\u5143\u7d20\u59cb\u7ec8\u4fdd\u6301\u65b9\u5f62\u3002\u5173\u952e\u662f\u4f7f\u7528\u6bd4\u4f8b\u6765\u786e\u4fdd\u5143\u7d20\u7684\u9ad8\u5ea6\u4e0e\u5bbd\u5ea6\u59cb\u7ec8\u4e00\u81f4\uff0c\u5e76\u4e14\u968f\u7740\u5c4f\u5e55\u5927\u5c0f\u7684\u53d8\u5316\u4fdd\u6301\u54cd\u5e94\u5f0f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0b\u662f\u4e24\u79cd\u5e38\u89c1\u65b9\u6cd5\u7684\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u4f7f\u7528 CSS Grid \u521b\u5efa\u54cd\u5e94\u5f0f\u65b9\u5f62\u7f51\u683c<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5229\u7528 CSS Grid \u5e03\u5c40\uff0c\u4f60\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u521b\u5efa\u4e00\u4e2a\u54cd\u5e94\u5f0f\u7684\u7f51\u683c\uff0c\u5e76\u901a\u8fc7 <code>padding-top<\/code> \u6216 <code>padding-bottom<\/code> \u4fdd\u8bc1\u5143\u7d20\u7684\u9ad8\u5ea6\u4e0e\u5bbd\u5ea6\u59cb\u7ec8\u4fdd\u6301\u4e00\u81f4\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n&lt;head&gt;\n  &lt;meta charset=\"UTF-8\"&gt;\n  &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"&gt;\n  &lt;title&gt;Responsive Square Grid&lt;\/title&gt;\n  &lt;style&gt;\n    \/* \u8bbe\u7f6e\u7f51\u683c\u5bb9\u5668 *\/\n    .grid {\n      display: grid;\n      grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); \/* \u81ea\u9002\u5e94\u5217\u6570 *\/\n      gap: 20px;\n      padding: 20px;\n    }\n\n    \/* \u521b\u5efa\u65b9\u5f62\u5143\u7d20 *\/\n    .square {\n      position: relative;\n      width: 100%;  \/* \u5bbd\u5ea6\u5360\u6ee1\u6574\u4e2a\u5355\u5143\u683c *\/\n      padding-top: 100%; \/* \u4f7f\u7528 padding-top \u6765\u521b\u5efa\u65b9\u5f62 (\u9ad8\u5ea6\u662f\u5bbd\u5ea6\u7684100%) *\/\n      background-color: #3498db;\n      border-radius: 8px; \/* \u53ef\u9009\uff0c\u5706\u89d2 *\/\n      display: flex;\n      justify-content: center;\n      align-items: center;\n      color: white;\n      font-size: 16px;\n      text-align: center;\n    }\n  &lt;\/style&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n\n  &lt;div class=\"grid\"&gt;\n    &lt;div class=\"square\"&gt;1&lt;\/div&gt;\n    &lt;div class=\"square\"&gt;2&lt;\/div&gt;\n    &lt;div class=\"square\"&gt;3&lt;\/div&gt;\n    &lt;div class=\"square\"&gt;4&lt;\/div&gt;\n    &lt;div class=\"square\"&gt;5&lt;\/div&gt;\n    &lt;div class=\"square\"&gt;6&lt;\/div&gt;\n  &lt;\/div&gt;\n\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u91ca\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>.grid<\/code><\/strong>\uff1a\u4f7f\u7528 <code>grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));<\/code> \u6765\u521b\u5efa\u54cd\u5e94\u5f0f\u5217\u6570\u3002\u5f53\u5c4f\u5e55\u5bbd\u5ea6\u53d8\u5316\u65f6\uff0c\u7f51\u683c\u5217\u4f1a\u81ea\u52a8\u8c03\u6574\u3002<\/li>\n\n\n\n<li><strong><code>.square<\/code><\/strong>\uff1a<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>width: 100%<\/code> \u4f7f\u65b9\u5f62\u5143\u7d20\u7684\u5bbd\u5ea6\u5360\u6ee1\u7f51\u683c\u5355\u5143\u683c\u3002<\/li>\n\n\n\n<li><code>padding-top: 100%<\/code> \u6280\u5de7\u4f7f\u5f97\u9ad8\u5ea6\u59cb\u7ec8\u662f\u5bbd\u5ea6\u7684 100%\uff0c\u4ece\u800c\u786e\u4fdd\u5143\u7d20\u4fdd\u6301\u65b9\u5f62\u3002<\/li>\n\n\n\n<li><code>position: relative<\/code> \u548c <code>display: flex<\/code> \u4f7f\u5f97\u5176\u4e2d\u7684\u5185\u5bb9\u5c45\u4e2d\u5bf9\u9f50\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u4f7f\u7528 Flexbox \u521b\u5efa\u54cd\u5e94\u5f0f\u65b9\u5f62\u7f51\u683c<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528 <strong>Flexbox<\/strong> \u5e03\u5c40\u6765\u5b9e\u73b0\u7c7b\u4f3c\u7684\u6548\u679c\u3002\u901a\u8fc7 <code>padding-top<\/code> \u6216 <code>padding-bottom<\/code> \u6280\u5de7\uff0c\u540c\u6837\u53ef\u4ee5\u786e\u4fdd\u5143\u7d20\u59cb\u7ec8\u4fdd\u6301\u65b9\u5f62\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n&lt;head&gt;\n  &lt;meta charset=\"UTF-8\"&gt;\n  &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"&gt;\n  &lt;title&gt;Responsive Square Flexbox&lt;\/title&gt;\n  &lt;style&gt;\n    \/* \u8bbe\u7f6e\u5bb9\u5668\u4e3a Flexbox *\/\n    .flex-container {\n      display: flex;\n      flex-wrap: wrap;\n      gap: 20px;\n      padding: 20px;\n    }\n\n    \/* \u521b\u5efa\u65b9\u5f62\u5143\u7d20 *\/\n    .square {\n      position: relative;\n      width: 30%;  \/* \u5b9a\u4e49\u5bbd\u5ea6\u7684\u767e\u5206\u6bd4 *\/\n      padding-top: 30%; \/* \u4f7f\u7528 padding-top \u6765\u521b\u5efa\u65b9\u5f62 (\u9ad8\u5ea6\u662f\u5bbd\u5ea6\u768430%) *\/\n      background-color: #9b59b6;\n      border-radius: 8px; \/* \u53ef\u9009\uff0c\u5706\u89d2 *\/\n      display: flex;\n      justify-content: center;\n      align-items: center;\n      color: white;\n      font-size: 16px;\n      text-align: center;\n    }\n  &lt;\/style&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n\n  &lt;div class=\"flex-container\"&gt;\n    &lt;div class=\"square\"&gt;1&lt;\/div&gt;\n    &lt;div class=\"square\"&gt;2&lt;\/div&gt;\n    &lt;div class=\"square\"&gt;3&lt;\/div&gt;\n    &lt;div class=\"square\"&gt;4&lt;\/div&gt;\n    &lt;div class=\"square\"&gt;5&lt;\/div&gt;\n    &lt;div class=\"square\"&gt;6&lt;\/div&gt;\n  &lt;\/div&gt;\n\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u91ca\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>.flex-container<\/code><\/strong>\uff1a\u4f7f\u7528 <code>display: flex<\/code> \u548c <code>flex-wrap: wrap<\/code> \u4f7f\u5bb9\u5668\u652f\u6301\u591a\u884c\uff0c\u5e76\u4e14\u5b50\u5143\u7d20\u80fd\u591f\u81ea\u52a8\u6362\u884c\u3002<\/li>\n\n\n\n<li><strong><code>.square<\/code><\/strong>\uff1a<\/li>\n\n\n\n<li><code>width: 30%<\/code> \u8bbe\u7f6e\u6bcf\u4e2a\u65b9\u5f62\u5143\u7d20\u7684\u5bbd\u5ea6\u4e3a\u7236\u5bb9\u5668\u7684 30%\u3002<\/li>\n\n\n\n<li><code>padding-top: 30%<\/code> \u786e\u4fdd\u6bcf\u4e2a\u65b9\u5f62\u5143\u7d20\u7684\u9ad8\u5ea6\u4e0e\u5bbd\u5ea6\u76f8\u7b49\uff0c\u4fdd\u6301\u65b9\u5f62\u7684\u6bd4\u4f8b\u3002<\/li>\n\n\n\n<li>\u5176\u4ed6\u6837\u5f0f\u5982\u80cc\u666f\u8272\u3001\u5c45\u4e2d\u5bf9\u9f50\u7b49\u548c\u4e4b\u524d\u7684 Grid \u793a\u4f8b\u76f8\u540c\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u4f7f\u7528 CSS Grid \u548c <code>auto-fit<\/code> \u521b\u5efa\u54cd\u5e94\u5f0f\u7f51\u683c<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u60f3\u8981\u6839\u636e\u5bb9\u5668\u5bbd\u5ea6\u81ea\u9002\u5e94\u5730\u663e\u793a\u65b9\u5f62\u5143\u7d20\u7684\u6570\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>auto-fit<\/code> \u548c <code>minmax<\/code> \u6765\u81ea\u52a8\u586b\u5145\u7f51\u683c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n&lt;head&gt;\n  &lt;meta charset=\"UTF-8\"&gt;\n  &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"&gt;\n  &lt;title&gt;Responsive Grid with Auto-Fit&lt;\/title&gt;\n  &lt;style&gt;\n    \/* \u8bbe\u7f6e\u7f51\u683c\u5bb9\u5668 *\/\n    .responsive-grid {\n      display: grid;\n      grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); \/* \u81ea\u9002\u5e94\u5217\u6570 *\/\n      gap: 20px;\n      padding: 20px;\n    }\n\n    \/* \u521b\u5efa\u65b9\u5f62\u5143\u7d20 *\/\n    .square {\n      position: relative;\n      width: 100%;  \/* \u5360\u6ee1\u5355\u5143\u683c\u5bbd\u5ea6 *\/\n      padding-top: 100%; \/* \u901a\u8fc7 padding-top \u4fdd\u8bc1\u9ad8\u5ea6\u7b49\u4e8e\u5bbd\u5ea6 *\/\n      background-color: #2ecc71;\n      border-radius: 8px; \/* \u5706\u89d2 *\/\n      display: flex;\n      justify-content: center;\n      align-items: center;\n      color: white;\n      font-size: 16px;\n      text-align: center;\n    }\n  &lt;\/style&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n\n  &lt;div class=\"responsive-grid\"&gt;\n    &lt;div class=\"square\"&gt;1&lt;\/div&gt;\n    &lt;div class=\"square\"&gt;2&lt;\/div&gt;\n    &lt;div class=\"square\"&gt;3&lt;\/div&gt;\n    &lt;div class=\"square\"&gt;4&lt;\/div&gt;\n    &lt;div class=\"square\"&gt;5&lt;\/div&gt;\n    &lt;div class=\"square\"&gt;6&lt;\/div&gt;\n  &lt;\/div&gt;\n\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u91ca\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));<\/code> \u81ea\u52a8\u8ba1\u7b97\u6bcf\u884c\u5bb9\u7eb3\u7684\u65b9\u5f62\u5143\u7d20\u6570\u91cf\uff0c\u5e76\u6839\u636e\u5c4f\u5e55\u5927\u5c0f\u81ea\u52a8\u8c03\u6574\u3002<\/li>\n\n\n\n<li><code>minmax(200px, 1fr)<\/code> \u786e\u4fdd\u6bcf\u4e2a\u65b9\u5f62\u5143\u7d20\u7684\u6700\u5c0f\u5bbd\u5ea6\u4e3a 200px\uff0c\u540c\u65f6\u5b83\u4f1a\u968f\u7740\u5c4f\u5e55\u5bbd\u5ea6\u7684\u589e\u52a0\u6269\u5c55\u5230\u53ef\u7528\u7684\u7a7a\u95f4\u3002<\/li>\n\n\n\n<li>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u65b9\u5f62\u7f51\u683c\u4f1a\u81ea\u52a8\u54cd\u5e94\u4e0d\u540c\u7684\u5c4f\u5e55\u5bbd\u5ea6\uff0c\u786e\u4fdd\u9875\u9762\u59cb\u7ec8\u770b\u8d77\u6765\u662f\u54cd\u5e94\u5f0f\u7684\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528 CSS Grid<\/strong>\uff1a\u901a\u8fc7 <code>padding-top<\/code> \u6280\u5de7\u786e\u4fdd\u5143\u7d20\u59cb\u7ec8\u4fdd\u6301\u65b9\u5f62\uff0c<code>grid-template-columns<\/code> \u53ef\u4ee5\u4f7f\u7f51\u683c\u81ea\u9002\u5e94\u5c4f\u5e55\u5927\u5c0f\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528 Flexbox<\/strong>\uff1a\u7ed3\u5408 <code>padding-top<\/code> \u548c\u5bbd\u5ea6\u767e\u5206\u6bd4\uff0c\u4e5f\u80fd\u521b\u5efa\u54cd\u5e94\u5f0f\u7684\u65b9\u5f62\u5143\u7d20\u7f51\u683c\u3002<\/li>\n\n\n\n<li><strong><code>auto-fit<\/code> \u6216 <code>auto-fill<\/code><\/strong>\uff1a\u4e0e <code>minmax()<\/code> \u914d\u5408\u4f7f\u7528\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u52a0\u7075\u6d3b\u7684\u54cd\u5e94\u5f0f\u7f51\u683c\u5e03\u5c40\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0a\u4e24\u79cd\u65b9\u6cd5\u90fd\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5b9e\u73b0\u54cd\u5e94\u5f0f\u4e14\u4e0d\u53ef\u53d8\u7684\u65b9\u5f62\u5143\u7d20\u7f51\u683c\uff0c\u9009\u62e9\u9002\u5408\u7684\u5e03\u5c40\u65b9\u5f0f\u5373\u53ef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8981\u4f7f\u7528 CSS \u521b\u5efa\u4e00\u4e2a\u54cd\u5e94\u5f0f\u7684 \u4e0d\u53ef\u53d8\u65b9\u5f62\u5143\u7d20 \u7684\u7f51\u683c\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 CSS Grid \u6216 Flexbox \u5e03 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[55],"tags":[],"class_list":["post-593","post","type-post","status-publish","format-standard","hentry","category-css"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/593","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=593"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/593\/revisions"}],"predecessor-version":[{"id":594,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/593\/revisions\/594"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}