{"id":602,"date":"2024-12-28T17:59:23","date_gmt":"2024-12-28T09:59:23","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=602"},"modified":"2024-12-28T17:59:23","modified_gmt":"2024-12-28T09:59:23","slug":"%e5%9c%a8-css-%e4%b8%ad%ef%bc%8c%e5%a6%82%e4%bd%95%e6%a0%b9%e6%8d%ae%e5%ae%b9%e5%99%a8%e7%9a%84%e5%a4%a7%e5%b0%8f%e6%8c%89%e6%af%94%e4%be%8b%e7%bc%a9%e6%94%be%e5%ad%97%e4%bd%93%e5%a4%a7%e5%b0%8f","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2024\/12\/28\/%e5%9c%a8-css-%e4%b8%ad%ef%bc%8c%e5%a6%82%e4%bd%95%e6%a0%b9%e6%8d%ae%e5%ae%b9%e5%99%a8%e7%9a%84%e5%a4%a7%e5%b0%8f%e6%8c%89%e6%af%94%e4%be%8b%e7%bc%a9%e6%94%be%e5%ad%97%e4%bd%93%e5%a4%a7%e5%b0%8f\/","title":{"rendered":"\u5728 CSS \u4e2d\uff0c\u5982\u4f55\u6839\u636e\u5bb9\u5668\u7684\u5927\u5c0f\u6309\u6bd4\u4f8b\u7f29\u653e\u5b57\u4f53\u5927\u5c0f"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u5728 CSS \u4e2d\uff0c\u8981\u6839\u636e\u5bb9\u5668\u7684\u5927\u5c0f\u6309\u6bd4\u4f8b\u7f29\u653e\u5b57\u4f53\u5927\u5c0f\uff0c\u53ef\u4ee5\u4f7f\u7528 <strong><code>viewport<\/code> \u5355\u4f4d<\/strong> \u6216\u8005\u7ed3\u5408 <strong><code>em<\/code>\u3001<code>rem<\/code> \u548c <code>@media<\/code> \u67e5\u8be2<\/strong> \u6765\u5b9e\u73b0\u54cd\u5e94\u5f0f\u5b57\u4f53\u3002\u5177\u4f53\u7684\u505a\u6cd5\u53d6\u51b3\u4e8e\u4f60\u5e0c\u671b\u5982\u4f55\u6839\u636e\u5bb9\u5668\u7684\u53d8\u5316\u6765\u8c03\u6574\u5b57\u4f53\u5927\u5c0f\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u89c1\u7684\u5b9e\u73b0\u65b9\u6cd5\uff1a<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u4f7f\u7528 <code>vw<\/code> \u548c <code>vh<\/code> \u5355\u4f4d\uff08\u89c6\u53e3\u5355\u4f4d\uff09<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>vw<\/code>\uff08\u89c6\u53e3\u5bbd\u5ea6\uff09\u548c <code>vh<\/code>\uff08\u89c6\u53e3\u9ad8\u5ea6\uff09\u662f\u76f8\u5bf9\u5355\u4f4d\uff0c\u5b83\u4eec\u7684\u5927\u5c0f\u4f1a\u968f\u89c6\u53e3\u7684\u5bbd\u5ea6\u6216\u9ad8\u5ea6\u800c\u53d8\u5316\uff0c\u9002\u5408\u7528\u4e8e\u54cd\u5e94\u5f0f\u5b57\u4f53\u7f29\u653e\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>.container {\n  width: 80%; \/* \u6216\u4efb\u610f\u5bbd\u5ea6 *\/\n  height: 400px; \/* \u6216\u4efb\u610f\u9ad8\u5ea6 *\/\n  background-color: #f0f0f0;\n  display: flex;\n  justify-content: center;\n  align-items: center;\n}\n\n.text {\n  font-size: 5vw; \/* \u5b57\u4f53\u5927\u5c0f\u662f\u89c6\u53e3\u5bbd\u5ea6\u7684 5% *\/\n  text-align: center;\n}<\/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>5vw<\/code> \u8868\u793a\u5b57\u4f53\u5927\u5c0f\u662f\u89c6\u53e3\u5bbd\u5ea6\u7684 5%\u3002\u5982\u679c\u4f60\u6539\u53d8\u6d4f\u89c8\u5668\u7684\u5bbd\u5ea6\uff0c\u5b57\u4f53\u5927\u5c0f\u4f1a\u76f8\u5e94\u8c03\u6574\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528\u89c6\u53e3\u5355\u4f4d\u7684\u4e00\u4e2a\u597d\u5904\u662f\uff0c\u5b83\u6839\u636e\u89c6\u53e3\u7684\u5bbd\u5ea6\u6216\u9ad8\u5ea6\u81ea\u52a8\u8c03\u6574\uff0c\u9002\u5e94\u4e0d\u540c\u8bbe\u5907\u548c\u5c4f\u5e55\u5927\u5c0f\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u4f7f\u7528 <code>em<\/code> \u6216 <code>rem<\/code> \u5355\u4f4d\uff08\u57fa\u4e8e\u5b57\u4f53\u5927\u5c0f\u7684\u76f8\u5bf9\u5355\u4f4d\uff09<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>em<\/code> \u548c <code>rem<\/code> \u90fd\u662f\u76f8\u5bf9\u5355\u4f4d\uff0c\u53ef\u4ee5\u6839\u636e\u7236\u5143\u7d20\u7684\u5b57\u4f53\u5927\u5c0f\u6765\u8fdb\u884c\u8c03\u6574\u3002<code>rem<\/code> \u57fa\u4e8e\u6839\u5143\u7d20\u7684\u5b57\u4f53\u5927\u5c0f\uff0c\u800c <code>em<\/code> \u662f\u76f8\u5bf9\u5f53\u524d\u5143\u7d20\u7684\u5b57\u4f53\u5927\u5c0f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u4e00\u4e2a\u6839\u5b57\u4f53\u5927\u5c0f\uff0c\u6216\u8005\u7236\u5bb9\u5668\u7684\u5b57\u4f53\u5927\u5c0f\uff0c\u7136\u540e\u57fa\u4e8e\u8fd9\u4e9b\u503c\u6765\u6309\u6bd4\u4f8b\u7f29\u653e\u5b57\u4f53\u5927\u5c0f\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>.container {\n  width: 80%;\n  height: 400px;\n  background-color: #f0f0f0;\n  display: flex;\n  justify-content: center;\n  align-items: center;\n  font-size: 2vw; \/* \u8bbe\u7f6e\u5bb9\u5668\u7684\u5b57\u4f53\u5927\u5c0f\u4e3a\u89c6\u53e3\u5bbd\u5ea6\u7684 2% *\/\n}\n\n.text {\n  font-size: 1em;  \/* \u5b57\u4f53\u5927\u5c0f\u76f8\u5bf9\u4e8e\u7236\u5bb9\u5668 *\/\n  text-align: center;\n}<\/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>\u5bb9\u5668\u7684\u5b57\u4f53\u5927\u5c0f\u901a\u8fc7 <code>font-size: 2vw<\/code> \u8bbe\u7f6e\u4e3a\u89c6\u53e3\u5bbd\u5ea6\u7684 2%\u3002<\/li>\n\n\n\n<li><code>.text<\/code> \u5143\u7d20\u7684\u5b57\u4f53\u5927\u5c0f\u901a\u8fc7 <code>font-size: 1em<\/code> \u76f8\u5bf9\u4e8e\u7236\u5bb9\u5668\uff08<code>.container<\/code>\uff09\u8fdb\u884c\u7f29\u653e\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u4f7f\u7528 CSS <code>clamp()<\/code> \u51fd\u6570<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>clamp()<\/code> \u51fd\u6570\u5141\u8bb8\u4f60\u4e3a\u5b57\u4f53\u5927\u5c0f\u8bbe\u7f6e\u4e00\u4e2a\u6700\u5c0f\u503c\u3001\u4e00\u4e2a\u6700\u5927\u503c\u548c\u4e00\u4e2a\u52a8\u6001\u503c\uff0c\u53ef\u4ee5\u5728\u4e00\u5b9a\u7684\u8303\u56f4\u5185\u6309\u6bd4\u4f8b\u7f29\u653e\u5b57\u4f53\u5927\u5c0f\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>.container {\n  width: 80%;\n  height: 400px;\n  background-color: #f0f0f0;\n  display: flex;\n  justify-content: center;\n  align-items: center;\n}\n\n.text {\n  font-size: clamp(16px, 5vw, 40px); \/* \u6700\u5c0f\u5b57\u4f53\u4e3a 16px\uff0c\u6700\u5927\u5b57\u4f53\u4e3a 40px\uff0c\u52a8\u6001\u5b57\u4f53\u5927\u5c0f\u4e3a\u89c6\u53e3\u5bbd\u5ea6\u7684 5% *\/\n  text-align: center;\n}<\/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>clamp(16px, 5vw, 40px)<\/code>\uff1a\u5b57\u4f53\u5927\u5c0f\u5c06\u5728 <code>16px<\/code> \u548c <code>40px<\/code> \u4e4b\u95f4\u7f29\u653e\uff0c\u57fa\u4e8e\u89c6\u53e3\u5bbd\u5ea6\u7684 <code>5vw<\/code> \u503c\u3002\u5b83\u53ef\u4ee5\u4fdd\u8bc1\u5b57\u4f53\u5927\u5c0f\u4e0d\u4f1a\u5c0f\u4e8e <code>16px<\/code> \u6216\u5927\u4e8e <code>40px<\/code>\uff0c\u540c\u65f6\u6839\u636e\u89c6\u53e3\u5bbd\u5ea6\u52a8\u6001\u8c03\u6574\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u4f7f\u7528 <code>@media<\/code> \u67e5\u8be2\u8fdb\u884c\u5b57\u4f53\u5927\u5c0f\u8c03\u6574<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u5e0c\u671b\u6839\u636e\u4e0d\u540c\u5c4f\u5e55\u5bbd\u5ea6\u6216\u8bbe\u5907\u8fdb\u884c\u66f4\u7cbe\u7ec6\u7684\u63a7\u5236\uff0c\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528 <code>@media<\/code> \u67e5\u8be2\u6765\u8c03\u6574\u5b57\u4f53\u5927\u5c0f\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>.container {\n  width: 80%;\n  height: 400px;\n  background-color: #f0f0f0;\n  display: flex;\n  justify-content: center;\n  align-items: center;\n}\n\n.text {\n  font-size: 2vw; \/* \u9ed8\u8ba4\u5b57\u4f53\u5927\u5c0f\u4e3a\u89c6\u53e3\u5bbd\u5ea6\u7684 2% *\/\n}\n\n@media (max-width: 600px) {\n  .text {\n    font-size: 4vw; \/* \u5c0f\u5c4f\u5e55\u4e0a\u5b57\u4f53\u53d8\u5927 *\/\n  }\n}\n\n@media (min-width: 1200px) {\n  .text {\n    font-size: 1.5vw; \/* \u5927\u5c4f\u5e55\u4e0a\u5b57\u4f53\u53d8\u5c0f *\/\n  }\n}<\/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>font-size: 2vw;<\/code> \u9ed8\u8ba4\u5b57\u4f53\u5927\u5c0f\u4e3a\u89c6\u53e3\u5bbd\u5ea6\u7684 2%\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>@media<\/code> \u67e5\u8be2\uff0c\u5728\u4e0d\u540c\u7684\u5c4f\u5e55\u5c3a\u5bf8\u4e0b\u8c03\u6574\u5b57\u4f53\u5927\u5c0f\u3002<\/li>\n\n\n\n<li>\u5c0f\u5c4f\u5e55\uff08<code>max-width: 600px<\/code>\uff09\u4e0a\u5b57\u4f53\u589e\u5927\u4e3a\u89c6\u53e3\u5bbd\u5ea6\u7684 4%\u3002<\/li>\n\n\n\n<li>\u5927\u5c4f\u5e55\uff08<code>min-width: 1200px<\/code>\uff09\u4e0a\u5b57\u4f53\u7f29\u5c0f\u4e3a\u89c6\u53e3\u5bbd\u5ea6\u7684 1.5%\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u4f7f\u7528 CSS Grid \u6216 Flexbox \u5e03\u5c40\u7684\u54cd\u5e94\u5f0f\u8c03\u6574<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u5bb9\u5668\u672c\u8eab\u5927\u5c0f\u4f1a\u53d1\u751f\u53d8\u5316\uff0c\u53ef\u4ee5\u7ed3\u5408 CSS Grid \u6216 Flexbox \u5e03\u5c40\u52a8\u6001\u8c03\u6574\u5b57\u4f53\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>.container {\n  display: grid;\n  place-items: center;\n  height: 100vh;  \/* \u9ad8\u5ea6\u5360\u6ee1\u89c6\u53e3 *\/\n  width: 100%;\n}\n\n.text {\n  font-size: calc(10px + 2vw); \/* \u5b57\u4f53\u5927\u5c0f\u968f\u7740\u89c6\u53e3\u5bbd\u5ea6\u52a8\u6001\u53d8\u5316 *\/\n  text-align: center;\n}<\/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>font-size: calc(10px + 2vw)<\/code>\uff1a\u4f7f\u7528 <code>calc()<\/code> \u51fd\u6570\uff0c\u7ed3\u5408\u4e86\u56fa\u5b9a\u7684\u6700\u5c0f\u5b57\u4f53\uff08<code>10px<\/code>\uff09\u548c\u89c6\u53e3\u5bbd\u5ea6\u7684 2%\uff08<code>2vw<\/code>\uff09\uff0c\u53ef\u4ee5\u8ba9\u5b57\u4f53\u5728\u4e0d\u540c\u5c4f\u5e55\u5c3a\u5bf8\u4e0b\u52a8\u6001\u8c03\u6574\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3\uff1a<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong><code>vw<\/code> \u548c <code>vh<\/code><\/strong>\uff1a\u6839\u636e\u89c6\u53e3\u5bbd\u5ea6\u6216\u9ad8\u5ea6\u52a8\u6001\u8c03\u6574\u5b57\u4f53\uff0c\u9002\u7528\u4e8e\u54cd\u5e94\u5f0f\u5e03\u5c40\u3002<\/li>\n\n\n\n<li><strong><code>em<\/code> \u548c <code>rem<\/code><\/strong>\uff1a\u6839\u636e\u7236\u5143\u7d20\u6216\u6839\u5143\u7d20\u7684\u5b57\u4f53\u5927\u5c0f\u8fdb\u884c\u76f8\u5bf9\u8c03\u6574\uff0c\u9002\u7528\u4e8e\u7ee7\u627f\u5173\u7cfb\u8f83\u5f3a\u7684\u5e03\u5c40\u3002<\/li>\n\n\n\n<li><strong><code>clamp()<\/code><\/strong>\uff1a\u8bbe\u7f6e\u5b57\u4f53\u5927\u5c0f\u7684\u6700\u5c0f\u503c\u3001\u6700\u5927\u503c\u548c\u52a8\u6001\u503c\uff0c\u786e\u4fdd\u5b57\u4f53\u5927\u5c0f\u5728\u6307\u5b9a\u8303\u56f4\u5185\u54cd\u5e94\u5f0f\u53d8\u5316\u3002<\/li>\n\n\n\n<li><strong><code>@media<\/code> \u67e5\u8be2<\/strong>\uff1a\u6839\u636e\u4e0d\u540c\u5c4f\u5e55\u5bbd\u5ea6\uff0c\u8c03\u6574\u5b57\u4f53\u5927\u5c0f\uff0c\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u5e03\u5c40\u9700\u6c42\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u6839\u636e\u5bb9\u5668\u7684\u5927\u5c0f\u5b9e\u73b0\u54cd\u5e94\u5f0f\u5b57\u4f53\u7f29\u653e\uff0c\u4f7f\u5f97\u9875\u9762\u5728\u4e0d\u540c\u5c4f\u5e55\u548c\u8bbe\u5907\u4e0a\u90fd\u6709\u826f\u597d\u7684\u53ef\u8bfb\u6027\u548c\u89c6\u89c9\u6548\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 CSS \u4e2d\uff0c\u8981\u6839\u636e\u5bb9\u5668\u7684\u5927\u5c0f\u6309\u6bd4\u4f8b\u7f29\u653e\u5b57\u4f53\u5927\u5c0f\uff0c\u53ef\u4ee5\u4f7f\u7528 viewport \u5355\u4f4d \u6216\u8005\u7ed3\u5408 em\u3001rem  [&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-602","post","type-post","status-publish","format-standard","hentry","category-css"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/602","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=602"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/602\/revisions"}],"predecessor-version":[{"id":603,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/602\/revisions\/603"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=602"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=602"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=602"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}