{"id":295515,"date":"2024-05-20T15:37:42","date_gmt":"2024-05-20T07:37:42","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/295515.html"},"modified":"2024-05-20T15:37:55","modified_gmt":"2024-05-20T07:37:55","slug":"html-%e6%a0%87%e7%ad%be%e5%a6%82%e4%bd%95%e7%a6%81%e6%ad%a2%e5%9b%be%e7%89%87%e7%bc%a9%e6%94%be","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/295515.html","title":{"rendered":"html \u6807\u7b7e\u5982\u4f55\u7981\u6b62\u56fe\u7247\u7f29\u653e"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27142828\/9504b610-a181-47ca-9044-eaabb4ca7b08.webp\" alt=\"html \u6807\u7b7e\u5982\u4f55\u7981\u6b62\u56fe\u7247\u7f29\u653e\" \/><\/p>\n<p><p>HTML\u6807\u7b7e\u7981\u6b62\u56fe\u7247\u7f29\u653e\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6eimg\u5143\u7d20\u7684\u5c5e\u6027\u3001CSS\u6837\u5f0f\u63a7\u5236\u6765\u5b9e\u73b0\uff0c<strong>\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\u56fa\u5b9a\u56fe\u7247\u5927\u5c0f\u3001\u4f7f\u7528HTML\u5c5e\u6027\u3001\u63a7\u5236\u5305\u88f9\u5143\u7d20<\/strong>\u3002\u5176\u4e2d\uff0c\u4e00\u4e2a\u5e38\u7528\u4e14\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u5728img\u6807\u7b7e\u4e2d\u76f4\u63a5\u8bbe\u7f6e\u56fe\u7247\u7684\u5bbd\u5ea6\uff08width\uff09\u548c\u9ad8\u5ea6\uff08height\uff09\u5c5e\u6027\u4e3a\u56fe\u7247\u539f\u59cb\u5927\u5c0f\uff0c\u786e\u4fdd\u5728\u9875\u9762\u4e0a\u663e\u793a\u65f6\u4e0d\u4f1a\u88ab\u7f29\u653e\u3002<\/p>\n<\/p>\n<p><p><strong>\u56fa\u5b9a\u56fe\u7247\u5927\u5c0f<\/strong> \u662f\u6700\u76f4\u63a5\u7684\u7981\u6b62\u7f29\u653e\u7684\u529e\u6cd5\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0c\u5982\u679c\u4f60\u7684\u56fe\u7247\u539f\u59cb\u5c3a\u5bf8\u4e3a800&#215;600\u50cf\u7d20\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u5728img\u6807\u7b7e\u4e2d\u6307\u5b9a\u8fd9\u4e9b\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;img src=&quot;image.jpg&quot; width=&quot;800&quot; height=&quot;600&quot;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u5c31\u80fd\u786e\u4fdd\u56fe\u7247\u6309\u7167800&#215;600\u50cf\u7d20\u7684\u5927\u5c0f\u663e\u793a\uff0c\u4e0d\u4f1a\u56e0\u4e3a\u5bb9\u5668\u5927\u5c0f\u7684\u53d8\u5316\u800c\u88ab\u7f29\u653e\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001HTML\u5c5e\u6027\u7684\u4f7f\u7528<\/h3>\n<\/p>\n<p><p><strong>\u4f7f\u7528HTML\u5c5e\u6027\u76f4\u63a5\u8bbe\u7f6e\u5c3a\u5bf8<\/strong>\u80fd\u591f\u5728\u4e0d\u6dfb\u52a0CSS\u6837\u5f0f\u7684\u60c5\u51b5\u4e0b\u8fbe\u5230\u7981\u6b62\u56fe\u7247\u88ab\u7f29\u653e\u7684\u76ee\u7684\u3002\u5bf9\u4e8e\u56fe\u7247\uff0c\u6700\u57fa\u672c\u7684\u5c5e\u6027\u5c31\u662f<code>width<\/code>\u548c<code>height<\/code>\uff0c\u800c\u4e14\u8fd9\u4e9b\u5c5e\u6027\u5e94\u5f53\u8bbe\u7f6e\u4e3a\u56fe\u7247\u7684\u5b9e\u9645\u50cf\u7d20\u5c3a\u5bf8\u3002<\/p>\n<\/p>\n<p><pre><code>&lt;img src=&quot;path-to-image.jpg&quot; width=&quot;300&quot; height=&quot;200&quot; alt=&quot;\u63cf\u8ff0\u6587\u6848&quot;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u7b80\u5355\u76f4\u63a5\uff0c\u4e14\u517c\u5bb9\u6027\u597d\uff0c\u65e0\u8bba\u5728\u54ea\u4e2a\u6d4f\u89c8\u5668\u4e2d\uff0c\u56fe\u7247\u90fd\u5c06\u6309\u7167\u7ed9\u5b9a\u7684\u5bbd\u9ad8\u663e\u793a\u3002\u4f46\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u5177\u5907\u54cd\u5e94\u5f0f\u7279\u6027\uff0c\u53ef\u80fd\u5728\u79fb\u52a8\u8bbe\u5907\u4e0a\u663e\u793a\u4e0d\u4f73\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001CSS\u6837\u5f0f\u63a7\u5236<\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u4e2a\u65b9\u6cd5\u662f<strong>\u901a\u8fc7CSS\u6837\u5f0f\u6765\u7981\u6b62\u7f29\u653e<\/strong>\u3002\u4f60\u53ef\u4ee5\u4e3a\u56fe\u7247\u8bbe\u7f6eCSS\u6837\u5f0f\uff0c\u7981\u6b62\u5176\u5bbd\u5ea6\u6216\u9ad8\u5ea6\u8d85\u8fc7\u5176\u539f\u59cb\u5c3a\u5bf8\u3002<\/p>\n<\/p>\n<p><pre><code>img.no-resize {<\/p>\n<p>    max-width: 800px;<\/p>\n<p>    max-height: 600px;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5c06\u6b64\u7c7b\u5e94\u7528\u4e8eimg\u6807\u7b7e:<\/p>\n<\/p>\n<p><pre><code>&lt;img src=&quot;image.jpg&quot; class=&quot;no-resize&quot;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u597d\u5904\u662f\u53ef\u4ee5\u901a\u8fc7CSS\u63a7\u5236\u66f4\u591a\u7684\u6837\u5f0f\u5c5e\u6027\uff0c\u6bd4\u5982\u6700\u5927\u5bbd\u5ea6(max-width)\u548c\u6700\u5927\u9ad8\u5ea6(max-height)\u3002\u4f46\u5b83\u4f9d\u7136\u4e0d\u591f\u7075\u6d3b\uff0c\u65e0\u6cd5\u5b8c\u7f8e\u9002\u5e94\u6240\u6709\u5c4f\u5e55\u5c3a\u5bf8\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u54cd\u5e94\u5f0f\u56fe\u7247\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u73b0\u4ee3\u7f51\u9875\u8bbe\u8ba1\u4e2d\uff0c\u901a\u5e38\u8fd8\u9700\u8981\u4f7f\u56fe\u7247\u80fd\u591f\u9002\u5e94\u4e0d\u540c\u7684\u5c4f\u5e55\u5c3a\u5bf8\uff0c\u5373\u54cd\u5e94\u5f0f\u8bbe\u8ba1\u3002\u4e3a\u6b64\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u56fe\u7247\u5bbd\u5ea6\u4e3a100%\uff0c\u5e76\u9650\u5236\u5176\u6700\u5927\u5bbd\u5ea6\u4e3a\u56fe\u7247\u7684\u539f\u59cb\u5bbd\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code>img.responsive {<\/p>\n<p>    width: 100%;<\/p>\n<p>    max-width: 800px;<\/p>\n<p>    height: auto;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5c06\u8fd9\u4e2a\u7c7b\u5e94\u7528\u4e8eimg\u6807\u7b7e:<\/p>\n<\/p>\n<p><pre><code>&lt;img src=&quot;image.jpg&quot; class=&quot;responsive&quot;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u56fe\u7247\u5728\u5927\u5c4f\u5e55\u4e0a\u4e0d\u4f1a\u8d85\u8fc7\u5176\u539f\u59cb\u5c3a\u5bf8\uff0c\u800c\u5728\u5c0f\u5c4f\u5e55\u4e0a\u5219\u4f1a\u7f29\u653e\u4ee5\u9002\u5e94\u5c4f\u5e55\u5bbd\u5ea6\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7981\u7528\u7528\u6237\u7f29\u653e<\/h3>\n<\/p>\n<p><p>\u8981\u7981\u6b62\u7528\u6237\u624b\u52a8\u7f29\u653e\u56fe\u7247\uff0c\u53ef\u4ee5\u4f7f\u7528CSS\u7684<code>user-select<\/code>\u5c5e\u6027\uff0c<\/p>\n<\/p>\n<p><pre><code>img {<\/p>\n<p>    -webkit-user-drag: none;<\/p>\n<p>    user-select: none;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u8bbe\u7f6e\u540e\uff0c\u7528\u6237\u5c06\u65e0\u6cd5\u901a\u8fc7\u62d6\u52a8\u6216\u8005\u4ee5\u5176\u4ed6\u65b9\u5f0f\u6765\u7f29\u653e\u56fe\u7247\u3002\u8fd9\u4e2a\u6280\u5de7\u53ef\u4ee5\u7528\u4e8e\u907f\u514d\u7528\u6237\u5e72\u9884\u56fe\u7247\u5c55\u793a\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528HTML5\u7684srcset\u7279\u6027<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u54cd\u5e94\u5f0f\u56fe\u7247\uff0cHTML5\u5f15\u5165\u4e86<code>srcset<\/code>\u7279\u6027\uff0c\u5141\u8bb8\u6839\u636e\u4e0d\u540c\u5c4f\u5e55\u5c3a\u5bf8\u6307\u5b9a\u4e0d\u540c\u7684\u56fe\u7247\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code>&lt;img src=&quot;small.jpg&quot; srcset=&quot;medium.jpg 1000w, large.jpg 2000w&quot; alt=&quot;\u63cf\u8ff0&quot;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u544a\u8bc9\u6d4f\u89c8\u5668\u5728\u5c4f\u5e55\u5bbd\u5ea6\u5927\u4e8e1000px\u65f6\u4f7f\u7528medium.jpg\uff0c\u5927\u4e8e2000px\u65f6\u4f7f\u7528large.jpg\u3002\u4f7f\u7528srcset\u53ef\u4ee5\u66f4\u597d\u5730\u63a7\u5236\u5728\u4e0d\u540c\u8bbe\u5907\u4e0a\u7684\u56fe\u7247\u663e\u793a\u6548\u679c\uff0c\u907f\u514d\u65e0\u8c13\u7684\u7f29\u653e\u3002<\/p>\n<\/p>\n<p><p>\u7efc\u5408\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9879\u76ee\u9700\u8981\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u5f0f\u6765\u7981\u6b62HTML\u4e2d\u7684\u56fe\u7247\u88ab\u7f29\u653e\u3002\u8fd9\u662f\u786e\u4fdd\u56fe\u7247\u6309\u7167\u8bbe\u8ba1\u5e08\u7684\u539f\u610f\u5c55\u793a\u7684\u91cd\u8981\u6b65\u9aa4\uff0c\u4e5f\u6709\u52a9\u4e8e\u63d0\u5347\u7f51\u7ad9\u7684\u7528\u6237\u4f53\u9a8c\u548c\u89c6\u89c9\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u5728 HTML \u6807\u7b7e\u4e2d\u7981\u6b62\u56fe\u7247\u7f29\u653e\uff1f<\/strong><\/p>\n<p>\u5728 HTML \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 CSS \u6837\u5f0f\u6765\u7981\u6b62\u56fe\u7247\u7684\u7f29\u653e\u3002\u4f60\u53ef\u4ee5\u4e3a\u56fe\u7247\u6216\u56fe\u7247\u5bb9\u5668\u5143\u7d20\u8bbe\u7f6e\u4ee5\u4e0b CSS \u5c5e\u6027\u6765\u5b9e\u73b0\u7981\u6b62\u7f29\u653e\u7684\u6548\u679c\uff1a<\/p>\n<pre><code class=\"language-css\">img {\n  max-width: 100%;\n  height: auto;\n  object-fit: cover;\n  object-position: center;\n}\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>max-width: 100%<\/code> \u5c06\u56fe\u7247\u7684\u6700\u5927\u5bbd\u5ea6\u8bbe\u7f6e\u4e3a\u5bb9\u5668\u7684\u5bbd\u5ea6\uff0c<code>height: auto<\/code> \u5219\u81ea\u9002\u5e94\u56fe\u7247\u7684\u9ad8\u5ea6\u3002<code>object-fit: cover<\/code> \u548c <code>object-position: center<\/code> \u7528\u4e8e\u8c03\u6574\u56fe\u7247\u7684\u663e\u793a\u65b9\u5f0f\uff0c\u4f7f\u5176\u586b\u5145\u5bb9\u5668\u5e76\u5c45\u4e2d\u663e\u793a\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u4f7f\u7528 JavaScript \u7981\u6b62\u56fe\u7247\u7f29\u653e\uff1f<\/strong><\/p>\n<p>\u5982\u679c\u4f60\u60f3\u901a\u8fc7 JavaScript \u6765\u7981\u6b62\u56fe\u7247\u7f29\u653e\uff0c\u53ef\u4ee5\u901a\u8fc7\u76d1\u542c\u7528\u6237\u7684\u7f29\u653e\u64cd\u4f5c\uff0c\u5e76\u5728\u56fe\u7247\u4e0a\u963b\u6b62\u9ed8\u8ba4\u884c\u4e3a\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-javascript\">const imgElement = document.getElementById(&#039;myImage&#039;);\n\nimgElement.addEventListener(&#039;wheel&#039;, function(event) {\n  event.preventDefault();\n});\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>wheel<\/code> \u4e8b\u4ef6\u4f1a\u5728\u7528\u6237\u4f7f\u7528\u6eda\u8f6e\u8fdb\u884c\u7f29\u653e\u65f6\u89e6\u53d1\uff0c\u6211\u4eec\u901a\u8fc7\u8c03\u7528 <code>event.preventDefault()<\/code> \u65b9\u6cd5\u6765\u963b\u6b62\u9ed8\u8ba4\u7684\u7f29\u653e\u884c\u4e3a\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u5728\u79fb\u52a8\u8bbe\u5907\u4e0a\u7981\u6b62\u56fe\u7247\u7f29\u653e\uff1f<\/strong><\/p>\n<p>\u5728\u79fb\u52a8\u8bbe\u5907\u4e0a\u7981\u6b62\u56fe\u7247\u7f29\u653e\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 viewport meta \u6807\u7b7e\u6765\u5b9e\u73b0\u3002\u5728 HTML \u6587\u6863\u7684 <code>&lt;head&gt;<\/code> \u6807\u7b7e\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0&quot;&gt;\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>user-scalable=0<\/code> \u7684\u8bbe\u7f6e\u53ef\u4ee5\u7981\u6b62\u7528\u6237\u5bf9\u7f51\u9875\u8fdb\u884c\u7f29\u653e\u64cd\u4f5c\u3002<code>width=device-width, initial-scale=1, maximum-scale=1<\/code> \u5219\u8868\u793a\u7f51\u9875\u7684\u5bbd\u5ea6\u548c\u521d\u59cb\u7f29\u653e\u6bd4\u4f8b\u4e3a1\uff0c\u6700\u5927\u7f29\u653e\u6bd4\u4f8b\u4e3a1\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u56fe\u7247\u5728\u79fb\u52a8\u8bbe\u5907\u4e0a\u4e0d\u4f1a\u88ab\u7f29\u653e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"HTML\u6807\u7b7e\u7981\u6b62\u56fe\u7247\u7f29\u653e\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6eimg\u5143\u7d20\u7684\u5c5e\u6027\u3001CSS\u6837\u5f0f\u63a7\u5236\u6765\u5b9e\u73b0\uff0c\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\u56fa\u5b9a\u56fe\u7247\u5927\u5c0f\u3001\u4f7f\u7528HT [&hellip;]","protected":false},"author":3,"featured_media":295538,"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\/295515"}],"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=295515"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/295515\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/295538"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=295515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=295515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=295515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}