{"id":309630,"date":"2024-05-20T21:30:54","date_gmt":"2024-05-20T13:30:54","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/309630.html"},"modified":"2024-05-20T21:31:05","modified_gmt":"2024-05-20T13:31:05","slug":"%e6%80%8e%e4%b9%88%e7%94%a8%e7%ba%af-css-%e5%ae%9e%e7%8e%b0%e5%af%bc%e8%88%aa%e8%8f%9c%e5%8d%95","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/309630.html","title":{"rendered":"\u600e\u4e48\u7528\u7eaf css \u5b9e\u73b0\u5bfc\u822a\u83dc\u5355"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26230117\/29bfa2c2-bb30-44a6-9c99-9a370b4b7c3e.webp\" alt=\"\u600e\u4e48\u7528\u7eaf css \u5b9e\u73b0\u5bfc\u822a\u83dc\u5355\" \/><\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u7eafCSS\u6765\u5b9e\u73b0\u5bfc\u822a\u83dc\u5355\uff0c\u53ef\u4ee5\u5728\u4e0d\u4f9d\u8d56JavaScript\u7684\u60c5\u51b5\u4e0b\uff0c\u8fbe\u5230\u4f18\u96c5\u7684\u5bfc\u822a\u6548\u679c\u3002\u8fd9\u79cd\u5b9e\u73b0\u65b9\u5f0f\u4e3b\u8981\u4f9d\u8d56\u4e8e\uff1a<strong>CSS\u9009\u62e9\u5668\u3001\u4f2a\u7c7b\u4ee5\u53ca\u8fc7\u6e21\u6548\u679c<\/strong>\u3002\u4f7f\u7528CSS\u9009\u62e9\u5668\u53ef\u4ee5\u7cbe\u786e\u5730\u9009\u62e9\u5230\u9700\u8981\u8bbe\u8ba1\u6837\u5f0f\u7684\u5143\u7d20\uff0c\u4f2a\u7c7b\u5982<code>:hover<\/code>\u5141\u8bb8\u6211\u4eec\u5728\u5149\u6807\u60ac\u505c\u65f6\u6539\u53d8\u5bfc\u822a\u83dc\u5355\u7684\u5916\u89c2\uff0c\u800c\u8fc7\u6e21\u6548\u679c\u5219\u4f7f\u5f97\u8fd9\u4e9b\u53d8\u5316\u66f4\u52a0\u5e73\u6ed1\u3002\u5176\u4e2d\uff0c<strong>\u4f2a\u7c7b\u7684\u4f7f\u7528<\/strong>\u662f\u7eafCSS\u5bfc\u822a\u83dc\u5355\u8bbe\u8ba1\u4e2d\u7279\u522b\u5173\u952e\u7684\u4e00\u70b9\uff0c\u5b83\u80fd\u591f\u4f7f\u5bfc\u822a\u83dc\u5355\u5728\u7528\u6237\u4ea4\u4e92\u65f6\u5448\u73b0\u52a8\u6001\u6548\u679c\u800c\u65e0\u9700\u4efb\u4f55JavaScript\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u57fa\u7840\u5bfc\u822a\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u5b9e\u73b0\u7eafCSS\u5bfc\u822a\u83dc\u5355\uff0c\u9996\u5148\u9700\u8981\u6784\u5efaHTML\u7ed3\u6784\u3002\u4f7f\u7528\u65e0\u5e8f\u5217\u8868<code>&lt;ul&gt;<\/code>\u6765\u521b\u5efa\u57fa\u7840\u7684\u5bfc\u822a\u7ed3\u6784\uff0c\u6bcf\u4e2a\u83dc\u5355\u9879\u4f5c\u4e3a\u5217\u8868\u9879<code>&lt;li&gt;<\/code>\u653e\u5165\u5176\u4e2d\u3002\u7136\u540e\uff0c\u901a\u8fc7\u94fe\u63a5<code>&lt;a&gt;<\/code>\u6807\u7b7e\u6765\u6307\u5b9a\u83dc\u5355\u9879\u7684\u6307\u5411\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;nav&gt;<\/p>\n<p>  &lt;ul&gt;<\/p>\n<p>    &lt;li&gt;&lt;a href=&quot;#home&quot;&gt;Home&lt;\/a&gt;&lt;\/li&gt;<\/p>\n<p>    &lt;li&gt;&lt;a href=&quot;#about&quot;&gt;About&lt;\/a&gt;&lt;\/li&gt;<\/p>\n<p>    &lt;li&gt;&lt;a href=&quot;#services&quot;&gt;Services&lt;\/a&gt;&lt;\/li&gt;<\/p>\n<p>    &lt;li&gt;&lt;a href=&quot;#contact&quot;&gt;Contact&lt;\/a&gt;&lt;\/li&gt;<\/p>\n<p>  &lt;\/ul&gt;<\/p>\n<p>&lt;\/nav&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u5728CSS\u4e2d\u8bbe\u5b9a\u57fa\u7840\u7684\u6837\u5f0f\u3002\u7ed9<code>&lt;nav&gt;<\/code>\u3001<code>&lt;ul&gt;<\/code>\u3001<code>&lt;li&gt;<\/code>\u4ee5\u53ca<code>&lt;a&gt;<\/code>\u8bbe\u5b9a\u57fa\u7840\u7684\u5e03\u5c40\u548c\u8bbe\u8ba1\uff0c\u5982\u79fb\u9664\u9ed8\u8ba4\u7684\u5217\u8868\u6837\u5f0f\u3001\u8bbe\u5b9a\u5b57\u4f53\u3001\u989c\u8272\u4ee5\u53ca\u95f4\u8ddd\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u6837\u5f0f\u8bbe\u8ba1<\/h3>\n<\/p>\n<p><p>\u5728\u57fa\u7840\u5bfc\u822a\u7ed3\u6784\u7684\u6837\u5f0f\u8bbe\u8ba1\u9636\u6bb5\uff0c\u521d\u6b65\u8bbe\u5b9a\u5bfc\u822a\u83dc\u5355\u7684\u5916\u5728\u7279\u5f81\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-css\">nav ul {<\/p>\n<p>  list-style-type: none;<\/p>\n<p>  margin: 0;<\/p>\n<p>  padding: 0;<\/p>\n<p>  overflow: hidden;<\/p>\n<p>  background-color: #333;<\/p>\n<p>}<\/p>\n<p>nav ul li {<\/p>\n<p>  float: left;<\/p>\n<p>}<\/p>\n<p>nav ul li a {<\/p>\n<p>  display: block;<\/p>\n<p>  color: white;<\/p>\n<p>  text-align: center;<\/p>\n<p>  padding: 14px 16px;<\/p>\n<p>  text-decoration: none;<\/p>\n<p>}<\/p>\n<p>nav ul li a:hover {<\/p>\n<p>  background-color: #111;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u54cd\u5e94\u5f0f\u8bbe\u8ba1<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u5bfc\u822a\u83dc\u5355\u5728\u4e0d\u540c\u8bbe\u5907\u4e0a\u90fd\u80fd\u4fdd\u6301\u826f\u597d\u7684\u663e\u793a\u6548\u679c\uff0c\u4f7f\u7528\u5a92\u4f53\u67e5\u8be2\u6765\u6dfb\u52a0\u54cd\u5e94\u5f0f\u8bbe\u8ba1\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u6839\u636e\u5c4f\u5e55\u5927\u5c0f\u8c03\u6574\u83dc\u5355\u9879\u7684\u5e03\u5c40\u548c\u6837\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-css\">@media screen and (max-width: 600px) {<\/p>\n<p>  nav ul li {<\/p>\n<p>    float: none;<\/p>\n<p>    display: block;<\/p>\n<p>    text-align: center;<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u9ad8\u7ea7\u6548\u679c\uff1a\u4e0b\u62c9\u83dc\u5355<\/h3>\n<\/p>\n<p><p>\u8981\u5728\u7eafCSS\u5bfc\u822a\u83dc\u5355\u4e2d\u5b9e\u73b0\u4e0b\u62c9\u83dc\u5355\uff0c\u53ef\u4ee5\u4f7f\u7528\u4f2a\u7c7b<code>:hover<\/code>\u4ee5\u53caHTML\u4e2d\u7684\u5d4c\u5957\u5217\u8868\u3002\u901a\u8fc7<code>:hover<\/code>\u9009\u62e9\u5668\u6539\u53d8\u5d4c\u5957\u5217\u8868\u7684\u663e\u793a\u5c5e\u6027\uff0c\u5b9e\u73b0\u9f20\u6807\u60ac\u505c\u65f6\u663e\u793a\u4e0b\u62c9\u83dc\u5355\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;li&gt;&lt;a href=&quot;#services&quot;&gt;Services&lt;\/a&gt;<\/p>\n<p>  &lt;ul&gt;<\/p>\n<p>    &lt;li&gt;&lt;a href=&quot;#webdesign&quot;&gt;Web Design&lt;\/a&gt;&lt;\/li&gt;<\/p>\n<p>    &lt;li&gt;&lt;a href=&quot;#webdevelopment&quot;&gt;Web Development&lt;\/a&gt;&lt;\/li&gt;<\/p>\n<p>    &lt;li&gt;&lt;a href=&quot;#seo&quot;&gt;SEO&lt;\/a&gt;&lt;\/li&gt;<\/p>\n<p>  &lt;\/ul&gt;<\/p>\n<p>&lt;\/li&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u76f8\u5e94\u7684CSS\u53ef\u80fd\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-css\">nav ul li ul {<\/p>\n<p>  display: none;<\/p>\n<p>}<\/p>\n<p>nav ul li:hover &gt; ul {<\/p>\n<p>  display: block;<\/p>\n<p>  position: absolute;<\/p>\n<p>  background-color: #333;<\/p>\n<p>}<\/p>\n<p>nav ul li ul li {<\/p>\n<p>  float: none;<\/p>\n<p>}<\/p>\n<p>nav ul li ul li a {<\/p>\n<p>  padding: 14px 20px;<\/p>\n<p>}<\/p>\n<p>nav ul li ul li a:hover {<\/p>\n<p>  background-color: #555;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u52a8\u6001\u89c6\u89c9\u6548\u679c<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u8fdb\u4e00\u6b65\u589e\u5f3a\u7528\u6237\u4f53\u9a8c\uff0c\u53ef\u4ee5\u5229\u7528CSS\u8fc7\u6e21\u6548\u679c\u7ed9\u5bfc\u822a\u83dc\u5355\u6dfb\u52a0\u52a8\u6001\u89c6\u89c9\u6548\u679c\u3002\u4f7f\u5f97\u989c\u8272\u53d8\u5316\u3001\u4e0b\u62c9\u663e\u793a\u7b49\u52a8\u4f5c\u66f4\u52a0\u5e73\u6ed1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-css\">nav ul li a {<\/p>\n<p>  transition: background-color 0.5s ease;<\/p>\n<p>}<\/p>\n<p>nav ul li ul {<\/p>\n<p>  transition: all 0.5s ease;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\u548c\u7ec6\u8282\u5904\u7406\uff0c\u53ef\u4ee5\u521b\u5efa\u51fa\u65e2\u7f8e\u89c2\u53c8\u529f\u80fd\u5168\u9762\u7684\u7eafCSS\u5bfc\u822a\u83dc\u5355\uff0c\u4e14\u5b8c\u5168\u4e0d\u4f9d\u8d56JavaScript\u3002\u8fd9\u6837\u4e0d\u4ec5\u53ef\u4ee5\u51cf\u5c11\u9875\u9762\u7684\u52a0\u8f7d\u65f6\u95f4\uff0c\u8fd8\u80fd\u63d0\u9ad8\u7f51\u7ad9\u7684\u53ef\u8bbf\u95ee\u6027\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u4f7f\u7528\u7eaf CSS \u521b\u5efa\u5bfc\u822a\u83dc\u5355\uff1f<\/strong><\/p>\n<p>\u521b\u5efa\u7eaf CSS \u5bfc\u822a\u83dc\u5355\u9700\u8981\u8003\u8651\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a<\/p>\n<ul>\n<li>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a HTML \u65e0\u5e8f\u5217\u8868 (ul) \u5143\u7d20\u4f5c\u4e3a\u5bfc\u822a\u6761\u7684\u5bb9\u5668\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u83dc\u5355\u9879\u521b\u5efa\u5217\u8868\u9879 (li) \u5143\u7d20\u3002<\/li>\n<li>\u7136\u540e\uff0c\u901a\u8fc7\u8bbe\u7f6e CSS \u6837\u5f0f\u6765\u5b9a\u4e49\u5bfc\u822a\u83dc\u5355\u7684\u5916\u89c2\uff0c\u5305\u62ec\u80cc\u666f\u989c\u8272\u3001\u5b57\u4f53\u6837\u5f0f\u3001\u95f4\u8ddd\u7b49\u3002<\/li>\n<li>\u6700\u540e\uff0c\u4e3a\u5bfc\u822a\u83dc\u5355\u7684\u6bcf\u4e2a\u83dc\u5355\u9879\u8bbe\u7f6e\u6837\u5f0f\uff0c\u5982\u60ac\u505c\u989c\u8272\u3001\u70b9\u51fb\u6548\u679c\u7b49\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u4f7f\u7528\u7eaf CSS \u5b9e\u73b0\u5bfc\u822a\u83dc\u5355\u4e86\u3002<\/p>\n<p><strong>2. \u6709\u54ea\u4e9b\u5e38\u89c1\u7684 CSS \u6280\u5de7\u53ef\u4ee5\u7528\u4e8e\u5bfc\u822a\u83dc\u5355\u7684\u8bbe\u8ba1\uff1f<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5e38\u89c1\u7684 CSS \u6280\u5de7\u53ef\u4ee5\u7528\u4e8e\u5bfc\u822a\u83dc\u5355\u7684\u8bbe\u8ba1\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528 <code>display: inline-block;<\/code> \u5c5e\u6027\u6765\u8ba9\u83dc\u5355\u9879\u6a2a\u5411\u6392\u5217\u3002<\/li>\n<li>\u901a\u8fc7\u8bbe\u7f6e <code>:hover<\/code> \u4f2a\u7c7b\u6765\u5b9e\u73b0\u60ac\u505c\u6548\u679c\uff0c\u5e76\u6539\u53d8\u83dc\u5355\u9879\u7684\u80cc\u666f\u989c\u8272\u3001\u5b57\u4f53\u989c\u8272\u7b49\u3002<\/li>\n<li>\u4f7f\u7528 <code>position: relative;<\/code> \u548c <code>position: absolute;<\/code> \u6765\u521b\u5efa\u4e0b\u62c9\u83dc\u5355\u6548\u679c\u3002<\/li>\n<li>\u4f7f\u7528 <code>transition<\/code> \u5c5e\u6027\u6765\u521b\u5efa\u5e73\u6ed1\u7684\u8fc7\u6e21\u6548\u679c\u3002<\/li>\n<li>\u4f7f\u7528 <code>box-shadow<\/code> \u5c5e\u6027\u6765\u6dfb\u52a0\u9634\u5f71\u6548\u679c\u3002<\/li>\n<li>\u4f7f\u7528 <code>transform<\/code> \u5c5e\u6027\u6765\u6dfb\u52a0\u52a8\u753b\u6548\u679c\uff0c\u5982\u65cb\u8f6c\u3001\u7f29\u653e\u3001\u6e10\u53d8\u7b49\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u4e9b\u6280\u5de7\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8bbe\u8ba1\u51fa\u72ec\u7279\u800c\u5438\u5f15\u4eba\u7684\u5bfc\u822a\u83dc\u5355\u3002<\/p>\n<p><strong>3. \u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u5bfc\u822a\u83dc\u5355\uff0c\u800c\u4e0d\u53ea\u662f\u7528\u7eaf CSS\uff1f<\/strong><\/p>\n<p>\u9664\u4e86\u4f7f\u7528\u7eaf CSS\uff0c\u8fd8\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u5bfc\u822a\u83dc\u5355\u7684\u8bbe\u8ba1\u3002\u5176\u4e2d\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 JavaScript \u6216 jQuery \u6765\u6539\u53d8\u5bfc\u822a\u83dc\u5355\u7684\u5916\u89c2\u548c\u884c\u4e3a\u3002<\/p>\n<p>\u901a\u8fc7\u4f7f\u7528 JavaScript \u6216 jQuery\uff0c\u4f60\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u5bfc\u822a\u83dc\u5355\u6548\u679c\uff0c\u5982\u54cd\u5e94\u5f0f\u5bfc\u822a\u83dc\u5355\u3001\u6298\u53e0\u83dc\u5355\u3001\u6eda\u52a8\u5bfc\u822a\u83dc\u5355\u7b49\u3002\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u589e\u52a0\u5bfc\u822a\u83dc\u5355\u7684\u4e92\u52a8\u6027\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<p>\u7136\u800c\uff0c\u4f7f\u7528 JavaScript \u6216 jQuery \u5b9e\u73b0\u5bfc\u822a\u83dc\u5355\u53ef\u80fd\u4f1a\u589e\u52a0\u7f51\u9875\u7684\u52a0\u8f7d\u65f6\u95f4\u548c\u590d\u6742\u5ea6\uff0c\u6240\u4ee5\u5728\u9009\u62e9\u65b9\u6cd5\u65f6\u9700\u8981\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u9879\u76ee\u8981\u6c42\u8fdb\u884c\u6743\u8861\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u901a\u8fc7\u4f7f\u7528\u7eafCSS\u6765\u5b9e\u73b0\u5bfc\u822a\u83dc\u5355\uff0c\u53ef\u4ee5\u5728\u4e0d\u4f9d\u8d56JavaScript\u7684\u60c5\u51b5\u4e0b\uff0c\u8fbe\u5230\u4f18\u96c5\u7684\u5bfc\u822a\u6548\u679c\u3002\u8fd9\u79cd\u5b9e\u73b0\u65b9\u5f0f\u4e3b\u8981 [&hellip;]","protected":false},"author":3,"featured_media":309643,"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\/309630"}],"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=309630"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/309630\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/309643"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=309630"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=309630"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=309630"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}