\/* Define CSS variables (custom properties) in the :root selector *\/\n :root {\n --primary-color: #3498db; \/* Main theme color *\/\n --secondary-color: #2ecc71; \/* Accent color *\/\n --text-color: #333; \/* Default text color *\/\n --font-size: 16px; \/* Base font size *\/\n --padding: 10px; \/* Base padding *\/\n }\n\n \/* General styles using variables *\/\n body {\n font-family: Arial, sans-serif;\n font-size: var(--font-size);\n color: var(--text-color);\n margin: 0;\n padding: 0;\n background-color: #f9f9f9;\n }\n\n header {\n background-color: var(--primary-color);\n color: white;\n text-align: center;\n padding: var(--padding);\n }\n\n .card {\n background-color: white;\n border: 1px solid #ddd;\n border-radius: 5px;\n margin: 20px;\n padding: var(--padding);\n box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);\n }\n\n .card h2 {\n color: var(--primary-color);\n }\n\n .card p {\n color: var(--text-color);\n }\n\n button {\n background-color: var(--secondary-color);\n color: white;\n border: none;\n border-radius: 5px;\n padding: calc(var(--padding) \/ 2) calc(var(--padding) * 2);\n cursor: pointer;\n font-size: var(--font-size);\n }\n\n button:hover {\n background-color: var(--primary-color);\n }\n\n \/* Dark mode example by overriding variables *\/\n body.dark-mode {\n --primary-color: #1abc9c;\n --secondary-color: #e74c3c;\n --text-color: #f9f9f9;\n background-color: #333;\n }\n<\/pre>\n\n\n\n\n \n \n 參考:\n<\/h3>\n\n<\/pre>\n
\n
- \nMDN Web 文件 - 使用 CSS 自訂屬性(變數(shù)) - 全面、適合初學(xué)者的解釋,包含定義、使用和更新 CSS 變數(shù)的範(fàn)例。 <\/li>\n
- \nW3Schools - CSS 變數(shù) - 透過(guò)即時(shí)程式碼範(fàn)例涵蓋 CSS 變數(shù)的基礎(chǔ)知識(shí),以便快速練習(xí)。 <\/li>\n
- \nCSS 技巧 - 自訂屬性完整指南 - 綜合指南,包含真實(shí)用例和進(jìn)階變數(shù)使用技巧。 <\/li>\n
- \nFreecodecamp - CSS 變數(shù)完整手冊(cè) - 探索強(qiáng)大的技術(shù),例如級(jí)聯(lián)效果、基於媒體查詢的變數(shù)和範(fàn)圍管理。 <\/li>\n<\/ul>\n\n\n
\n\n\n \n \n 動(dòng)畫(huà)和過(guò)渡\n<\/h2>\n\n
為您的網(wǎng)站添加動(dòng)感可創(chuàng)造引人入勝的使用者體驗(yàn)。 CSS 提供了兩種主要的動(dòng)畫(huà)製作方式:<\/p>\n\n
\n \n \n 過(guò)渡\n<\/h3>\n\n
非常適合簡(jiǎn)單的狀態(tài)變更:
\n<\/p>\n\/* Mobile-first approach *\/\n.container {\n width: 100%;\n padding: 10px;\n}\n\n\/* Tablet and larger *\/\n@media screen and (min-width: 768px) {\n .container {\n width: 750px;\n padding: 20px;\n }\n}\n\n\/* Desktop *\/\n@media screen and (min-width: 1024px) {\n .container {\n width: 960px;\n }\n}\n<\/pre>\n\n\n\n\n \n \n 關(guān)鍵影格動(dòng)畫(huà)\n<\/h3>\n\n
對(duì)於更複雜的多步驟動(dòng)畫(huà):
\n<\/p>\n\n\n\n\n\n CSS:
\n<\/p>\n\n\/* Mobile First Approach *\/\n.services {\n padding: 20px;\n max-width: 1200px;\n margin: 0 auto;\n}\n\nh2 {\n text-align: center;\n color: #333;\n margin-bottom: 30px;\n}\n\n.services-container {\n display: flex;\n flex-direction: column;\n gap: 20px;\n}\n\n.service-card {\n padding: 20px;\n background: white;\n border-radius: 8px;\n box-shadow: 0 2px 4px rgba(0,0,0,0.1);\n}\n\nbutton {\n width: 100%;\n padding: 10px;\n background: #007bff;\n color: white;\n border: none;\n border-radius: 4px;\n cursor: pointer;\n}\n\n\/* Tablet *\/\n@media (min-width: 768px) {\n .services-container {\n flex-direction: row;\n flex-wrap: wrap;\n }\n\n .service-card {\n flex: 0 1 calc(50% - 20px);\n }\n}\n\n\/* Desktop *\/\n@media (min-width: 1024px) {\n .service-card {\n flex: 1;\n }\n\n button {\n width: auto;\n padding: 10px 20px;\n }\n}\n<\/pre>\n\n\n\n\n \n \n 先進(jìn)的動(dòng)畫(huà)技術(shù)\n<\/h3>\n\n
動(dòng)畫(huà)中的 CSS 自訂屬性:
\n<\/p>\n\n\/* Base styles - Mobile First (320px and up) *\/\n.services {\n padding: 15px;\n max-width: 1200px;\n margin: 0 auto;\n overflow-x: hidden; \/* Prevent horizontal scroll *\/\n}\n\nh2 {\n text-align: center;\n color: #333;\n margin-bottom: 20px;\n font-size: clamp(1.5rem, 5vw, 2.5rem); \/* Fluid typography *\/\n}\n\n.services-container {\n display: flex;\n flex-direction: column;\n gap: 15px;\n}\n\n.service-card {\n padding: 15px;\n background: white;\n border-radius: 8px;\n box-shadow: 0 2px 4px rgba(0,0,0,0.1);\n transition: all 0.3s ease; \/* Smooth transitions for responsive changes *\/\n}\n\nbutton {\n width: 100%;\n padding: 8px;\n background: #007bff;\n color: white;\n border: none;\n border-radius: 4px;\n cursor: pointer;\n font-size: 14px;\n}\n\n\/* Small phones (375px and up) *\/\n@media (min-width: 375px) {\n .services {\n padding: 20px;\n }\n\n .service-card {\n padding: 20px;\n }\n}\n\n\/* Large phones (480px and up) *\/\n@media (min-width: 480px) {\n .services-container {\n gap: 20px;\n }\n\n button {\n padding: 10px;\n font-size: 16px;\n }\n}\n\n\/* Small tablets (600px and up) *\/\n@media (min-width: 600px) {\n .services-container {\n flex-direction: row;\n flex-wrap: wrap;\n }\n\n .service-card {\n flex: 0 1 calc(50% - 10px); \/* Two cards per row with gap consideration *\/\n }\n}\n\n\/* Tablets (768px and up) *\/\n@media (min-width: 768px) {\n .services {\n padding: 30px;\n }\n\n .service-card {\n padding: 25px; \/* Improved spacing for larger screens *\/\n }\n\n button: hover {\n \/* Add hover effect for non-touch devices *\/\n background: #0056b3;\n transform: translateY(-2px);\n }\n}\n\n\/* Small laptops (1024px and up) *\/\n@media (min-width: 1024px) {\n .service-card {\n flex: 1; \/* Three cards per row *\/\n transition: transform 0.3s ease, box-shadow 0.3s ease; \/* Add subtle hover effect *\/\n }\n\n .service-card:hover {\n transform: translateY(-5px);\n box-shadow: 0 4px 8px rgba(0,0,0,0.2);\n }\n\n button {\n \/* Change to inline button *\/\n width: auto;\n padding: 10px 20px;\n }\n}\n\n\/* Desktops (1200px and up) *\/\n@media (min-width: 1200px) {\n .services {\n padding: 40px;\n }\n\n .services-container {\n gap: 30px;\n }\n\n .service-card {\n padding: 30px;\n }\n}\n\n\/* Extra large screens (1440px and up) *\/\n@media (min-width: 1440px) {\n .services {\n max-width: 1400px; \/* Max width to maintain readability *\/\n }\n\n .service-card {\n padding: 35px; \/* Larger padding for extra large screens *\/\n }\n}\n\n\/* Print styles *\/\n@media print {\n .services {\n padding: 0;\n }\n\n .service-card {\n break-inside: avoid;\n box-shadow: none;\n border: 1px solid #ddd;\n }\n\n button {\n display: none;\n }\n}\n\n\/* Reduced motion preferences *\/\n@media (prefers-reduced-motion: reduce) {\n .service-card,\n button {\n transition: none;\n }\n}\n\n\/* Dark mode support *\/\n@media (prefers-color-scheme: dark) {\n .service-card {\n background: #2a2a2a;\n box-shadow: 0 2px 4px rgba(0,0,0,0.2);\n }\n\n h2 {\n color: #fff;\n }\n}\n<\/pre>\n\n\n\n\n \n \n 進(jìn)階關(guān)鍵影格動(dòng)畫(huà):\n<\/h3>\n\n\n\n
:root {\n --primary-color: #007bff;\n --secondary-color: #6c757d;\n --spacing-unit: 1rem;\n}\n\n.button {\n background-color: var(--primary-color);\n padding: var(--spacing-unit);\n}\n<\/pre>\n\n\n\n\n \n \n 實(shí)踐練習(xí):互動(dòng)卡\n<\/h2>\n\n
建立具有懸停效果的互動(dòng)式卡片:<\/p>\n\n
HTML:
\n<\/p>\/* Mobile-first approach *\/\n.container {\n width: 100%;\n padding: 10px;\n}\n\n\/* Tablet and larger *\/\n@media screen and (min-width: 768px) {\n .container {\n width: 750px;\n padding: 20px;\n }\n}\n\n\/* Desktop *\/\n@media screen and (min-width: 1024px) {\n .container {\n width: 960px;\n }\n}\n<\/pre>\n\n\n\n\n \n \n 參考:\n<\/h3>\n\n<\/pre>\n
\n
- \nMDN Web 文件 - CSS 過(guò)渡 - 對(duì) CSS 過(guò)渡的清晰介紹,解釋如何在更改樣式時(shí)創(chuàng)建平滑效果。 <\/li>\n
- \nMDN Web 文件 - CSS 動(dòng)畫(huà) - 關(guān)鍵影格、動(dòng)畫(huà)屬性和創(chuàng)建複雜動(dòng)畫(huà)的逐步指南。 <\/li>\n
- \nW3Schools - CSS 轉(zhuǎn)場(chǎng) - 適合初學(xué)者使用即時(shí)程式碼編輯器以互動(dòng)方式練習(xí)過(guò)渡和動(dòng)畫(huà)。 <\/li>\n
- \nW3Schools - CSS 動(dòng)畫(huà) - 關(guān)於使用關(guān)鍵影格和轉(zhuǎn)場(chǎng)為網(wǎng)站添加動(dòng)畫(huà)的簡(jiǎn)單易懂的指南。 <\/li>\n
- \nCSS 技巧 - 動(dòng)畫(huà) - 討論響應(yīng)式動(dòng)畫(huà)、減少可訪問(wèn)性的運(yùn)動(dòng)以及媒體查詢整合。 <\/li>\n
- \nAnimate.css - 一個(gè)流行的 CSS 庫(kù),提供預(yù)先建立的動(dòng)畫(huà),您可以輕鬆添加到您的專案中。 <\/li>\n<\/ul>\n\n\n
\n\n\n \n \n 最佳實(shí)踐和組織\n<\/h2>\n\n
\n \n \n CSS架構(gòu)\n<\/h3>\n\n
\n
- 使用一致的命名約定<\/li>\n
- 依組件\/功能組織 CSS 檔案<\/li>\n
- 盡可能保持較低的特異性<\/li>\n
- 有效註解你的程式碼\n<\/li>\n<\/ul>\n\n
\n\n\n\n CSS:
\n<\/p>\n\n\/* Mobile First Approach *\/\n.services {\n padding: 20px;\n max-width: 1200px;\n margin: 0 auto;\n}\n\nh2 {\n text-align: center;\n color: #333;\n margin-bottom: 30px;\n}\n\n.services-container {\n display: flex;\n flex-direction: column;\n gap: 20px;\n}\n\n.service-card {\n padding: 20px;\n background: white;\n border-radius: 8px;\n box-shadow: 0 2px 4px rgba(0,0,0,0.1);\n}\n\nbutton {\n width: 100%;\n padding: 10px;\n background: #007bff;\n color: white;\n border: none;\n border-radius: 4px;\n cursor: pointer;\n}\n\n\/* Tablet *\/\n@media (min-width: 768px) {\n .services-container {\n flex-direction: row;\n flex-wrap: wrap;\n }\n\n .service-card {\n flex: 0 1 calc(50% - 20px);\n }\n}\n\n\/* Desktop *\/\n@media (min-width: 1024px) {\n .service-card {\n flex: 1;\n }\n\n button {\n width: auto;\n padding: 10px 20px;\n }\n}\n<\/pre>\n\n\n\n\n \n \n 實(shí)踐練習(xí):CSS 架構(gòu)的最佳實(shí)踐\n<\/h2>\n\n\n\n
\n\n\n \n \nCSS 架構(gòu)練習(xí)<\/title>\n \n \n \n \n \n頭>\n\n \n\n\n\n \n \n \n 參考:\n<\/h3>\n\n<\/pre><\/pre>\n
\n
- \nBEM - 區(qū)塊元素修飾符 - 一種流行的命名 CSS 類別和建立樣式以提高可重複使用性和可維護(hù)性的方法。 <\/li>\n
- \nSMACSS - CSS 的可擴(kuò)展和模組化架構(gòu) - 將 CSS 組織為邏輯和可維護(hù)類別的詳細(xì)框架。 <\/li>\n
- \nHarry Roberts 的 CSS 指南 - 使用邏輯檔案結(jié)構(gòu)和命名約定編寫(xiě)可擴(kuò)展、可維護(hù)的 CSS 的高品質(zhì)指南。 <\/li>\n<\/ul>\n\n\n
\n\n\n \n \n 建造時(shí)間到了! ?\n<\/h2>\n\n
現(xiàn)在輪到你將所學(xué)付諸實(shí)踐了!這是你的挑戰(zhàn):<\/p>\n
\n
- 建立新的 CodePen(在 codepen.io 上免費(fèi))<\/li>\n
- 建立我們介紹的範(fàn)例和練習(xí)<\/li>\n
- \n分享您的創(chuàng)作! <\/strong>在下面的評(píng)論中加入您的 CodePen 連結(jié)<\/li>\n<\/ul>\n\n
獎(jiǎng)勵(lì)積分<\/strong>:在設(shè)計(jì)中加入自己的創(chuàng)意!我會(huì)親自審核並回覆評(píng)論中分享的每則 CodePen。 <\/p>\n\n
? 專業(yè)提示<\/strong>:請(qǐng)記得在 CSS 中加入註解來(lái)解釋您的想法。它可以幫助其他人從您的程式碼中學(xué)習(xí)! <\/p>\n\n\n
\n\n\n \n \n 接下來(lái)是什麼? ?\n<\/h2>\n\n
這是我們的 CSS 從零到英雄系列的第 2 部分。我們將在接下來(lái)的文章中更深入地探討更令人興奮的 CSS 概念。為了確保您不會(huì)錯(cuò)過(guò):<\/p>\n\n
\n
- ? 為這篇文章加書(shū)籤<\/strong>以便在編碼時(shí)快速參考<\/li>\n
- ?? 喜歡這篇文章<\/strong>如果您覺(jué)得它有幫助(它也可以幫助其他人找到它!)<\/li>\n
- ? 追蹤我<\/strong>觀看本系列的下一部分<\/li>\n<\/ol>\n\n
\n \n \n 讓我們聯(lián)絡(luò)吧! ?\n<\/h3>\n\n
你有嘗試過(guò)練習(xí)嗎?有疑問(wèn)嗎?在評(píng)論中分享您的經(jīng)驗(yàn)!我回覆每條評(píng)論並喜歡看到您的進(jìn)步。 <\/p>\n\n
第三部分見(jiàn)!快樂(lè)編碼! ???????<\/p>\n\n\n \n\n \n \n\n \n "}
国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂
目錄
No. Section Link 1 Responsive Design Principles Link 2 CSS Variables and Custom Properties Link 3 Animations and Transitions Link 4 Best Practices and Organization Link
響應(yīng)式設(shè)計(jì)原則
在當(dāng)今的多設(shè)備世界中,響應(yīng)式設(shè)計(jì)不是可選的,而是必不可少的。無(wú)論是在智慧型手機(jī)還是大型桌面顯示器上查看,您的網(wǎng)站都應(yīng)該看起來(lái)很棒。
媒體查詢
媒體查詢是您的響應(yīng)式設(shè)計(jì)超能力:
/* Mobile-first approach */ .container { width: 100%; padding: 10px; } /* Tablet and larger */ @media screen and (min-width: 768px) { .container { width: 750px; padding: 20px; } } /* Desktop */ @media screen and (min-width: 1024px) { .container { width: 960px; } }響應(yīng)單位
使用相對(duì)單位使您的設(shè)計(jì)自然響應(yīng):
- rem:相對(duì)於根元素的字體大小
- em:相對(duì)於父元素的字體大小
- vw/vh:相對(duì)於視口尺寸
- %:相對(duì)於父元素的大小
實(shí)踐練習(xí):回應(yīng)服務(wù)部分
創(chuàng)建一個(gè)響應(yīng)式服務(wù)部分,使用媒體查詢和靈活的單元無(wú)縫適應(yīng)不同的螢?zāi)怀叽纭?
HTML:
<section> <p>CSS:<br> </p> <pre class="brush:php;toolbar:false">/* Mobile First Approach */ .services { padding: 20px; max-width: 1200px; margin: 0 auto; } h2 { text-align: center; color: #333; margin-bottom: 30px; } .services-container { display: flex; flex-direction: column; gap: 20px; } .service-card { padding: 20px; background: white; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); } button { width: 100%; padding: 10px; background: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer; } /* Tablet */ @media (min-width: 768px) { .services-container { flex-direction: row; flex-wrap: wrap; } .service-card { flex: 0 1 calc(50% - 20px); } } /* Desktop */ @media (min-width: 1024px) { .service-card { flex: 1; } button { width: auto; padding: 10px 20px; } }CSS:讓我們來(lái)探索一下服務(wù)部分的更具體的斷點(diǎn)。
/* Base styles - Mobile First (320px and up) */ .services { padding: 15px; max-width: 1200px; margin: 0 auto; overflow-x: hidden; /* Prevent horizontal scroll */ } h2 { text-align: center; color: #333; margin-bottom: 20px; font-size: clamp(1.5rem, 5vw, 2.5rem); /* Fluid typography */ } .services-container { display: flex; flex-direction: column; gap: 15px; } .service-card { padding: 15px; background: white; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); transition: all 0.3s ease; /* Smooth transitions for responsive changes */ } button { width: 100%; padding: 8px; background: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 14px; } /* Small phones (375px and up) */ @media (min-width: 375px) { .services { padding: 20px; } .service-card { padding: 20px; } } /* Large phones (480px and up) */ @media (min-width: 480px) { .services-container { gap: 20px; } button { padding: 10px; font-size: 16px; } } /* Small tablets (600px and up) */ @media (min-width: 600px) { .services-container { flex-direction: row; flex-wrap: wrap; } .service-card { flex: 0 1 calc(50% - 10px); /* Two cards per row with gap consideration */ } } /* Tablets (768px and up) */ @media (min-width: 768px) { .services { padding: 30px; } .service-card { padding: 25px; /* Improved spacing for larger screens */ } button: hover { /* Add hover effect for non-touch devices */ background: #0056b3; transform: translateY(-2px); } } /* Small laptops (1024px and up) */ @media (min-width: 1024px) { .service-card { flex: 1; /* Three cards per row */ transition: transform 0.3s ease, box-shadow 0.3s ease; /* Add subtle hover effect */ } .service-card:hover { transform: translateY(-5px); box-shadow: 0 4px 8px rgba(0,0,0,0.2); } button { /* Change to inline button */ width: auto; padding: 10px 20px; } } /* Desktops (1200px and up) */ @media (min-width: 1200px) { .services { padding: 40px; } .services-container { gap: 30px; } .service-card { padding: 30px; } } /* Extra large screens (1440px and up) */ @media (min-width: 1440px) { .services { max-width: 1400px; /* Max width to maintain readability */ } .service-card { padding: 35px; /* Larger padding for extra large screens */ } } /* Print styles */ @media print { .services { padding: 0; } .service-card { break-inside: avoid; box-shadow: none; border: 1px solid #ddd; } button { display: none; } } /* Reduced motion preferences */ @media (prefers-reduced-motion: reduce) { .service-card, button { transition: none; } } /* Dark mode support */ @media (prefers-color-scheme: dark) { .service-card { background: #2a2a2a; box-shadow: 0 2px 4px rgba(0,0,0,0.2); } h2 { color: #fff; } }參考:
- MDN Web 文件 - 響應(yīng)式設(shè)計(jì)基礎(chǔ) - 響應(yīng)式設(shè)計(jì)概念的精彩介紹,涵蓋視窗、斷點(diǎn)和靈活佈局。
- FreeCodeCamp - 響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)認(rèn)證 - 涵蓋響應(yīng)式設(shè)計(jì)原則、網(wǎng)格、媒體查詢和可訪問(wèn)性的完整課程。
- 我可以使用 - 檢查瀏覽器相容性以取得響應(yīng)式設(shè)計(jì)功能,例如媒體查詢和 Flexbox。
- 響應(yīng)式設(shè)計(jì)備忘單 - 以易於理解的格式涵蓋關(guān)鍵的響應(yīng)式設(shè)計(jì)屬性和技術(shù)。
CSS 變數(shù)和自訂屬性
CSS 變數(shù)(自訂屬性)為您的樣式表帶來(lái)類似程式設(shè)計(jì)的彈性。它們使維護(hù)更容易並實(shí)現(xiàn)動(dòng)態(tài)造型。
:root { --primary-color: #007bff; --secondary-color: #6c757d; --spacing-unit: 1rem; } .button { background-color: var(--primary-color); padding: var(--spacing-unit); }實(shí)踐練習(xí):用於主題化和可重複使用性的 CSS 變量
<body> <header> <h1>CSS Variables & Custom Properties</h1> </header> <main> <section> <pre class="brush:php;toolbar:false">/* Define CSS variables (custom properties) in the :root selector */ :root { --primary-color: #3498db; /* Main theme color */ --secondary-color: #2ecc71; /* Accent color */ --text-color: #333; /* Default text color */ --font-size: 16px; /* Base font size */ --padding: 10px; /* Base padding */ } /* General styles using variables */ body { font-family: Arial, sans-serif; font-size: var(--font-size); color: var(--text-color); margin: 0; padding: 0; background-color: #f9f9f9; } header { background-color: var(--primary-color); color: white; text-align: center; padding: var(--padding); } .card { background-color: white; border: 1px solid #ddd; border-radius: 5px; margin: 20px; padding: var(--padding); box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); } .card h2 { color: var(--primary-color); } .card p { color: var(--text-color); } button { background-color: var(--secondary-color); color: white; border: none; border-radius: 5px; padding: calc(var(--padding) / 2) calc(var(--padding) * 2); cursor: pointer; font-size: var(--font-size); } button:hover { background-color: var(--primary-color); } /* Dark mode example by overriding variables */ body.dark-mode { --primary-color: #1abc9c; --secondary-color: #e74c3c; --text-color: #f9f9f9; background-color: #333; }參考:
- MDN Web 文件 - 使用 CSS 自訂屬性(變數(shù)) - 全面、適合初學(xué)者的解釋,包含定義、使用和更新 CSS 變數(shù)的範(fàn)例。
- W3Schools - CSS 變數(shù) - 透過(guò)即時(shí)程式碼範(fàn)例涵蓋 CSS 變數(shù)的基礎(chǔ)知識(shí),以便快速練習(xí)。
- CSS 技巧 - 自訂屬性完整指南 - 綜合指南,包含真實(shí)用例和進(jìn)階變數(shù)使用技巧。
- Freecodecamp - CSS 變數(shù)完整手冊(cè) - 探索強(qiáng)大的技術(shù),例如級(jí)聯(lián)效果、基於媒體查詢的變數(shù)和範(fàn)圍管理。
動(dòng)畫(huà)和過(guò)渡
為您的網(wǎng)站添加動(dòng)感可創(chuàng)造引人入勝的使用者體驗(yàn)。 CSS 提供了兩種主要的動(dòng)畫(huà)製作方式:
過(guò)渡
非常適合簡(jiǎn)單的狀態(tài)變更:
/* Mobile-first approach */ .container { width: 100%; padding: 10px; } /* Tablet and larger */ @media screen and (min-width: 768px) { .container { width: 750px; padding: 20px; } } /* Desktop */ @media screen and (min-width: 1024px) { .container { width: 960px; } }關(guān)鍵影格動(dòng)畫(huà)
對(duì)於更複雜的多步驟動(dòng)畫(huà):
<section> <p>CSS:<br> </p> <pre class="brush:php;toolbar:false">/* Mobile First Approach */ .services { padding: 20px; max-width: 1200px; margin: 0 auto; } h2 { text-align: center; color: #333; margin-bottom: 30px; } .services-container { display: flex; flex-direction: column; gap: 20px; } .service-card { padding: 20px; background: white; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); } button { width: 100%; padding: 10px; background: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer; } /* Tablet */ @media (min-width: 768px) { .services-container { flex-direction: row; flex-wrap: wrap; } .service-card { flex: 0 1 calc(50% - 20px); } } /* Desktop */ @media (min-width: 1024px) { .service-card { flex: 1; } button { width: auto; padding: 10px 20px; } }先進(jìn)的動(dòng)畫(huà)技術(shù)
動(dòng)畫(huà)中的 CSS 自訂屬性:
/* Base styles - Mobile First (320px and up) */ .services { padding: 15px; max-width: 1200px; margin: 0 auto; overflow-x: hidden; /* Prevent horizontal scroll */ } h2 { text-align: center; color: #333; margin-bottom: 20px; font-size: clamp(1.5rem, 5vw, 2.5rem); /* Fluid typography */ } .services-container { display: flex; flex-direction: column; gap: 15px; } .service-card { padding: 15px; background: white; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); transition: all 0.3s ease; /* Smooth transitions for responsive changes */ } button { width: 100%; padding: 8px; background: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 14px; } /* Small phones (375px and up) */ @media (min-width: 375px) { .services { padding: 20px; } .service-card { padding: 20px; } } /* Large phones (480px and up) */ @media (min-width: 480px) { .services-container { gap: 20px; } button { padding: 10px; font-size: 16px; } } /* Small tablets (600px and up) */ @media (min-width: 600px) { .services-container { flex-direction: row; flex-wrap: wrap; } .service-card { flex: 0 1 calc(50% - 10px); /* Two cards per row with gap consideration */ } } /* Tablets (768px and up) */ @media (min-width: 768px) { .services { padding: 30px; } .service-card { padding: 25px; /* Improved spacing for larger screens */ } button: hover { /* Add hover effect for non-touch devices */ background: #0056b3; transform: translateY(-2px); } } /* Small laptops (1024px and up) */ @media (min-width: 1024px) { .service-card { flex: 1; /* Three cards per row */ transition: transform 0.3s ease, box-shadow 0.3s ease; /* Add subtle hover effect */ } .service-card:hover { transform: translateY(-5px); box-shadow: 0 4px 8px rgba(0,0,0,0.2); } button { /* Change to inline button */ width: auto; padding: 10px 20px; } } /* Desktops (1200px and up) */ @media (min-width: 1200px) { .services { padding: 40px; } .services-container { gap: 30px; } .service-card { padding: 30px; } } /* Extra large screens (1440px and up) */ @media (min-width: 1440px) { .services { max-width: 1400px; /* Max width to maintain readability */ } .service-card { padding: 35px; /* Larger padding for extra large screens */ } } /* Print styles */ @media print { .services { padding: 0; } .service-card { break-inside: avoid; box-shadow: none; border: 1px solid #ddd; } button { display: none; } } /* Reduced motion preferences */ @media (prefers-reduced-motion: reduce) { .service-card, button { transition: none; } } /* Dark mode support */ @media (prefers-color-scheme: dark) { .service-card { background: #2a2a2a; box-shadow: 0 2px 4px rgba(0,0,0,0.2); } h2 { color: #fff; } }進(jìn)階關(guān)鍵影格動(dòng)畫(huà):
:root { --primary-color: #007bff; --secondary-color: #6c757d; --spacing-unit: 1rem; } .button { background-color: var(--primary-color); padding: var(--spacing-unit); }實(shí)踐練習(xí):互動(dòng)卡
建立具有懸停效果的互動(dòng)式卡片:
HTML:
/* Mobile-first approach */ .container { width: 100%; padding: 10px; } /* Tablet and larger */ @media screen and (min-width: 768px) { .container { width: 750px; padding: 20px; } } /* Desktop */ @media screen and (min-width: 1024px) { .container { width: 960px; } }參考:
- MDN Web 文件 - CSS 過(guò)渡 - 對(duì) CSS 過(guò)渡的清晰介紹,解釋如何在更改樣式時(shí)創(chuàng)建平滑效果。
- MDN Web 文件 - CSS 動(dòng)畫(huà) - 關(guān)鍵影格、動(dòng)畫(huà)屬性和創(chuàng)建複雜動(dòng)畫(huà)的逐步指南。
- W3Schools - CSS 轉(zhuǎn)場(chǎng) - 適合初學(xué)者使用即時(shí)程式碼編輯器以互動(dòng)方式練習(xí)過(guò)渡和動(dòng)畫(huà)。
- W3Schools - CSS 動(dòng)畫(huà) - 關(guān)於使用關(guān)鍵影格和轉(zhuǎn)場(chǎng)為網(wǎng)站添加動(dòng)畫(huà)的簡(jiǎn)單易懂的指南。
- CSS 技巧 - 動(dòng)畫(huà) - 討論響應(yīng)式動(dòng)畫(huà)、減少可訪問(wèn)性的運(yùn)動(dòng)以及媒體查詢整合。
- Animate.css - 一個(gè)流行的 CSS 庫(kù),提供預(yù)先建立的動(dòng)畫(huà),您可以輕鬆添加到您的專案中。
最佳實(shí)踐和組織
CSS架構(gòu)
- 使用一致的命名約定
- 依組件/功能組織 CSS 檔案
- 盡可能保持較低的特異性
- 有效註解你的程式碼
<section> <p>CSS:<br> </p> <pre class="brush:php;toolbar:false">/* Mobile First Approach */ .services { padding: 20px; max-width: 1200px; margin: 0 auto; } h2 { text-align: center; color: #333; margin-bottom: 30px; } .services-container { display: flex; flex-direction: column; gap: 20px; } .service-card { padding: 20px; background: white; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); } button { width: 100%; padding: 10px; background: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer; } /* Tablet */ @media (min-width: 768px) { .services-container { flex-direction: row; flex-wrap: wrap; } .service-card { flex: 0 1 calc(50% - 20px); } } /* Desktop */ @media (min-width: 1024px) { .service-card { flex: 1; } button { width: auto; padding: 10px 20px; } }實(shí)踐練習(xí):CSS 架構(gòu)的最佳實(shí)踐
<title>CSS 架構(gòu)練習(xí)</title> <link rel="stylesheet" href="styles/reset.css"> <!-- 重設(shè)預(yù)設(shè)瀏覽器樣式 --> <link rel="stylesheet" href="styles/layout.css"> <!-- 佈局相關(guān)的樣式 --> <!-- 標(biāo)題元件樣式 --> <link rel="stylesheet" href="styles/components/card.css"> <!-- 卡片組件樣式 --> <link rel="stylesheet" href="styles/utilities.css"> <!-- 用於快速修復(fù)的實(shí)用程式類別 --> 頭> <h3> 參考: </h3>
- BEM - 區(qū)塊元素修飾符 - 一種流行的命名 CSS 類別和建立樣式以提高可重複使用性和可維護(hù)性的方法。
- SMACSS - CSS 的可擴(kuò)展和模組化架構(gòu) - 將 CSS 組織為邏輯和可維護(hù)類別的詳細(xì)框架。
- Harry Roberts 的 CSS 指南 - 使用邏輯檔案結(jié)構(gòu)和命名約定編寫(xiě)可擴(kuò)展、可維護(hù)的 CSS 的高品質(zhì)指南。
建造時(shí)間到了! ?
現(xiàn)在輪到你將所學(xué)付諸實(shí)踐了!這是你的挑戰(zhàn):
- 建立新的 CodePen(在 codepen.io 上免費(fèi))
- 建立我們介紹的範(fàn)例和練習(xí)
- 分享您的創(chuàng)作! 在下面的評(píng)論中加入您的 CodePen 連結(jié)
獎(jiǎng)勵(lì)積分:在設(shè)計(jì)中加入自己的創(chuàng)意!我會(huì)親自審核並回覆評(píng)論中分享的每則 CodePen。
? 專業(yè)提示:請(qǐng)記得在 CSS 中加入註解來(lái)解釋您的想法。它可以幫助其他人從您的程式碼中學(xué)習(xí)!
接下來(lái)是什麼? ?
這是我們的 CSS 從零到英雄系列的第 2 部分。我們將在接下來(lái)的文章中更深入地探討更令人興奮的 CSS 概念。為了確保您不會(huì)錯(cuò)過(guò):
- ? 為這篇文章加書(shū)籤以便在編碼時(shí)快速參考
- ?? 喜歡這篇文章如果您覺(jué)得它有幫助(它也可以幫助其他人找到它!)
- ? 追蹤我觀看本系列的下一部分
讓我們聯(lián)絡(luò)吧! ?
你有嘗試過(guò)練習(xí)嗎?有疑問(wèn)嗎?在評(píng)論中分享您的經(jīng)驗(yàn)!我回覆每條評(píng)論並喜歡看到您的進(jìn)步。
第三部分見(jiàn)!快樂(lè)編碼! ???????
以上是《給所有人的權(quán)威 CSS 指南》中的掌握 CSS |第2部分的詳細(xì)內(nèi)容。更多資訊請(qǐng)關(guān)注PHP中文網(wǎng)其他相關(guān)文章!
本網(wǎng)站聲明本文內(nèi)容由網(wǎng)友自願(yuàn)投稿,版權(quán)歸原作者所有。本站不承擔(dān)相應(yīng)的法律責(zé)任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請(qǐng)聯(lián)絡(luò)admin@php.cn![]()
熱AI工具
![]()
Undress AI Tool
免費(fèi)脫衣圖片
![]()
Undresser.AI Undress
人工智慧驅(qū)動(dòng)的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片
![]()
AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。
![]()
Clothoff.io
AI脫衣器
![]()
Video Face Swap
使用我們完全免費(fèi)的人工智慧換臉工具,輕鬆在任何影片中換臉!
![]()
熱門(mén)文章
Agnes Tachyon Build Guide |漂亮的德比志2 週前 By Jack chenOguri Cap Build Guide |漂亮的德比志3 週前 By Jack chen峰:如何復(fù)興球員4 週前 By DDDGrass Wonder Build Guide |烏瑪媽媽漂亮的德比1 週前 By Jack chen峰如何表現(xiàn)3 週前 By Jack chen![]()
熱工具
![]()
記事本++7.3.1
好用且免費(fèi)的程式碼編輯器
![]()
SublimeText3漢化版
中文版,非常好用
![]()
禪工作室 13.0.1
強(qiáng)大的PHP整合開(kāi)發(fā)環(huán)境
![]()
Dreamweaver CS6
視覺(jué)化網(wǎng)頁(yè)開(kāi)發(fā)工具
![]()
SublimeText3 Mac版
神級(jí)程式碼編輯軟體(SublimeText3)
![]()
熱門(mén)話題
See all articles什麼是'渲染障礙CSS”? Jun 24, 2025 am 12:42 AM
CSS會(huì)阻塞頁(yè)面渲染是因?yàn)闉g覽器默認(rèn)將內(nèi)聯(lián)和外部CSS視為關(guān)鍵資源,尤其是使用引入的樣式表、頭部大量?jī)?nèi)聯(lián)CSS以及未優(yōu)化的媒體查詢樣式。 1.提取關(guān)鍵CSS並內(nèi)嵌至HTML;2.延遲加載非關(guān)鍵CSS通過(guò)JavaScript;3.使用media屬性優(yōu)化加載如打印樣式;4.壓縮合併CSS減少請(qǐng)求。建議使用工具提取關(guān)鍵CSS,結(jié)合rel="preload"異步加載,合理使用media延遲加載,避免過(guò)度拆分與復(fù)雜腳本控制。
什麼是AutoPrefixer,它如何工作? Jul 02, 2025 am 01:15 AM
Autoprefixer是一個(gè)根據(jù)目標(biāo)瀏覽器範(fàn)圍自動(dòng)為CSS屬性添加廠商前綴的工具。 1.它解決了手動(dòng)維護(hù)前綴易出錯(cuò)的問(wèn)題;2.通過(guò)PostCSS插件形式工作,解析CSS、分析需加前綴的屬性、依配置生成代碼;3.使用步驟包括安裝插件、設(shè)置browserslist、在構(gòu)建流程中啟用;4.注意事項(xiàng)有不手動(dòng)加前綴、保持配置更新、非所有屬性都加前綴、建議配合預(yù)處理器使用。
什麼是圓錐級(jí)函數(shù)? Jul 01, 2025 am 01:16 AM
theconic-Gradient()functionIncsscreatesCircularGradientsThatRotateColorStopSaroundAcentralPoint.1.IsidealForPieCharts,ProgressIndicators,colordichers,colorwheels和decorativeBackgrounds.2.itworksbysbysbysbydefindefingincolordefingincolorstopsatspecificains off.
CSS教程,用於創(chuàng)建粘性標(biāo)頭或頁(yè)腳 Jul 02, 2025 am 01:04 AM
TocreatestickyheadersandfooterswithCSS,useposition:stickyforheaderswithtopvalueandz-index,ensuringparentcontainersdon’trestrictit.1.Forstickyheaders:setposition:sticky,top:0,z-index,andbackgroundcolor.2.Forstickyfooters,betteruseposition:fixedwithbot
CSS自定義屬性的範(fàn)圍是什麼? Jun 25, 2025 am 12:16 AM
CSS自定義屬性的作用域取決於其聲明的上下文,全局變量通常定義在:root中,而局部變量則定義在特定選擇器內(nèi),以便組件化和隔離樣式。例如,定義在.card類中的變量?jī)H對(duì)匹配該類的元素及其子元素可用。最佳實(shí)踐包括:1.使用:root定義全局變量如主題色;2.在組件內(nèi)部定義局部變量以實(shí)現(xiàn)封裝;3.避免重複聲明同一變量;4.注意選擇器特異性可能引發(fā)的覆蓋問(wèn)題。此外,CSS變量區(qū)分大小寫(xiě),且應(yīng)在使用前定義以避免錯(cuò)誤。若變量未定義或引用失敗,則會(huì)採(cǎi)用回退值或默認(rèn)值initial。調(diào)試時(shí)可通過(guò)瀏覽器開(kāi)發(fā)者工
CSS網(wǎng)格中的FR單元是什麼? Jun 22, 2025 am 12:46 AM
ThefrunitinCSSGriddistributesavailablespaceproportionally.1.Itworksbydividingspacebasedonthesumoffrvalues,e.g.,1fr2frgivesone-thirdandtwo-thirds.2.Itenablesflexiblelayouts,avoidsmanualcalculations,andsupportsresponsivedesign.3.Commonusesincludeequal-
CSS教程專注於移動(dòng)優(yōu)先設(shè)計(jì) Jul 02, 2025 am 12:52 AM
Mobile-firstCSSdesignrequiressettingtheviewportmetatag,usingrelativeunits,stylingfromsmallscreensup,optimizingtypographyandtouchtargets.First,addtocontrolscaling.Second,use%,em,orreminsteadofpixelsforflexiblelayouts.Third,writebasestylesformobile,the
您可以在CSS網(wǎng)格項(xiàng)目中嵌套Flexbox容器嗎? Jun 22, 2025 am 12:40 AM
是的,可以在CSSGrid項(xiàng)中使用Flexbox。具體做法是先用Grid劃分頁(yè)面結(jié)構(gòu),在某個(gè)Grid單元格內(nèi)設(shè)置子容器為Flex容器,以實(shí)現(xiàn)更精細(xì)的對(duì)齊和排列;例如,在HTML中嵌套一個(gè)帶有display:flex樣式的div;這樣做的好處包括分層佈局、響應(yīng)式設(shè)計(jì)更容易、組件化開(kāi)發(fā)更友好;需要注意display屬性僅影響直接子元素、避免過(guò)度嵌套、考慮舊版瀏覽器兼容性問(wèn)題。
![]()