如何創建僅CSS的動畫模態窗口?
使用隱藏的複選框和:checked 偽類控制模態框狀態;2. 通過label 關聯實現點擊打開和關閉;3. 利用CSS transition 或@keyframes 實現淡入縮放動畫;4. 設置pointer-events 確保點擊遮罩可關閉;5. 無法純CSS 實現Esc 鍵關閉。該方法無需JavaScript,輕量且兼容現代瀏覽器,適合簡單項目或學習用途。
Creating a CSS-only animated modal window is totally doable using just HTML and CSS—no JavaScript required. The trick lies in using the :target
selector or checkboxes with :checked
to control visibility, combined with CSS transitions or animations for smooth effects.

Here's how to build one step by step:
✅ 1. Use a Checkbox Hack or :target
for Toggle Control
Since we can't use JavaScript, we need a way to "remember" the modal's state. Two common methods:

-
:target
– Uses URL fragment identifiers (eg,#modal
) - Checkbox hack – Hides a checkbox and styles its
:checked
state
We'll use the checkbox method because it allows smoother close-on-overlay-click behavior and better control.
HTML Structure:
<!-- Hidden checkbox to control modal state --> <input type="checkbox" id="modal-toggle" class="modal-toggle"> <label for="modal-toggle" class="modal-open-btn">Open Modal</label> <!-- Modal --> <div class="modal"> <label for="modal-toggle" class="modal-overlay"></label> <div class="modal-content"> <label for="modal-toggle" class="modal-close">×</label> <h2>Modal Title</h2> <p>This is your animated modal content.</p> </div> </div>
The checkbox acts as a toggle. Clicking the label (
modal-open-btn
) or close elements toggles the checkbox, which we style conditionally.
✅ 2. Style the Modal with CSS Animation
Now apply styles to make it animated and visually appealing.
Core CSS:
/* Hide the checkbox */ .modal-toggle { display: none; } /* Modal base: hidden by default */ .modal { opacity: 0; visibility: hidden; position: fixed; top: 0; left: 0; width: 100%; height: 100%; pointer-events: none; transition: opacity 0.3s ease; } /* Modal overlay (background dim) */ .modal-overlay { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); } /* Modal content box */ .modal-content { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%) scale(0.8); width: 90%; max-width: 500px; background: white; padding: 30px; border-radius: 10px; box-shadow: 0 4px 20px rgba(0,0,0,0.2); transition: transform 0.3s ease; } /* Close button */ .modal-close { position: absolute; top: 10px; right: 15px; font-size: 28px; cursor: pointer; color: #aaa; } .modal-close:hover { color: #000; } /* Show modal when checkbox is checked */ .modal-toggle:checked .modal { opacity: 1; visibility: visible; pointer-events: auto; } .modal-toggle:checked .modal .modal-content { transform: translate(-50%, -50%) scale(1); }
The
pointer-events: none
on the modal andpointer-events: auto
when active ensure clicks on the overlay close the modal (via the label).
✅ 3. Add Entrance and Exit Animations
Want more flair? Replace transition
with a full @keyframes
animation.
Example: Fade & Scale Animation
@keyframes modal-in { 0% { opacity: 0; transform: translate(-50%, -50%) scale(0.7); } 100% { opacity: 1; transform: translate(-50%, -50%) scale(1); } } @keyframes modal-out { 0% { opacity: 1; transform: translate(-50%, -50%) scale(1); } 100% { opacity: 0; transform: translate(-50%, -50%) scale(0.8); } } /* Apply animation only when opening */ .modal-toggle:checked .modal .modal-content { animation: modal-in 0.3s forwards; } /* Optional: animate out – tricky without JS, but works on close if you reverse */ .modal-content { animation: none; }
Note: Since CSS can't reverse animations easily on uncheck, using
transition
is often smoother than trying to animate the close.
✅ 4. Optional: Close on Overlay or Esc (Partial)
- Overlay click : Already handled via
<label for="modal-toggle" class="modal-overlay"></label>
- Esc key : Can't be handled in pure CSS. You'd need JS for keyboard support.
✅ Summary: Key Points
- Use a hidden checkbox and
:checked
to simulate state. - Use
label
elements to trigger open/close. - Animate with
transition
or@keyframes
. - Use
pointer-events
to control click behavior on the backdrop. - No keyboard support (Esc) without JavaScript.
This method is lightweight, accessible for basic use, and works across modern browsers. It's perfect for simple projects or learning CSS logic.
Basically just link the checkbox to labels and animate the appearance. Not magic—just clever CSS.
以上是如何創建僅CSS的動畫模態窗口?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undress AI Tool
免費脫衣圖片

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

backdrop-filter用於對元素背後的內容應用視覺效果,1.使用backdrop-filter:blur(10px)等語法實現毛玻璃效果;2.支持blur、brightness、contrast等多種濾鏡函數並可疊加;3.常用於玻璃態卡片設計,需確保元素與背景重疊;4.現代瀏覽器支持良好,可用@supports提供降級方案;5.避免過大模糊值和頻繁重繪以優化性能,該屬性僅在元素背後有內容時生效。

vw和vh單位通過將元素尺寸與視口寬度和高度關聯,實現響應式設計;1vw等於視口寬度的1%,1vh等於視口高度的1%;常用於全屏區域、響應式字體和彈性間距;1.全屏區域使用100vh或更優的100dvh避免移動瀏覽器地址欄影響;2.響應式字體可用5vw並結合clamp(1.5rem,3vw,3rem)限制最小和最大尺寸;3.彈性間距如width:80vw、margin:5vhauto、padding:2vh3vw可使佈局自適應;需注意移動設備兼容性、可訪問性及固定寬度內容衝突,建議優先使用dvh

Theaspect-ratioCSSpropertydefinesthewidth-to-heightratioofanelement,ensuringconsistentproportionsinresponsivedesigns.1.Itisapplieddirectlytoelementslikeimages,videos,orcontainersusingsyntaxsuchasaspect-ratio:16/9.2.Commonusecasesincludemaintainingres

:emptyPseudo-classSelectSelectsselemtswithnochildrenorcontent,包括pacesorcomments,sonlyTrulyEmpterementLikeMatchit; 1.ItcanhideEmptycontainersbousing:intume {note {note display:none;} toCleanuplayouts; 2.ItallowSaddingplacePlacePlacePlaceLanderStylingLingvia :: Forefore :: Forefor :: show offor :: show

Define@keyframesbouncewith0%,100%attranslateY(0)and50%attranslateY(-20px)tocreateabasicbounce.2.Applytheanimationtoanelementusinganimation:bounce0.6sease-in-outinfiniteforsmooth,continuousmotion.3.Forrealism,use@keyframesrealistic-bouncewithscale(1.1

使用帶邊框的div可快速創建垂直線,通過設置border-left和height定義樣式和高度;2.利用::before或::after偽元素可在無額外HTML標籤的情況下添加垂直線,適合裝飾性分隔;3.在Flexbox佈局中,通過設置divider類的寬度和背景色,可實現彈性容器間的自適應垂直分隔線;4.在CSSGrid中,將垂直線作為獨立列(如auto寬度列)插入網格佈局,適用於響應式設計;應根據具體佈局需求選擇最合適的方法,確保結構簡潔且易於維護。

CSS偽類是用於定義元素特殊狀態的關鍵字,可基於用戶交互或文檔位置動態應用樣式;1.:hover在鼠標懸停時觸發,如button:hover改變按鈕顏色;2.:focus在元素獲得焦點時生效,提升表單可訪問性;3.:nth-child()按位置選擇元素,支持odd、even或公式如2n 1;4.:first-child和:last-child分別選中首個和最後一個子元素;5.:not()排除匹配指定條件的元素;6.:visited和:link根據鏈接訪問狀態設置樣式,但:visited受隱私限制

使用隱藏的複選框和CSS的:checked偽類結合相鄰兄弟選擇器( )來控制內容顯示;2.HTML結構包含每個折疊項的input、label和內容div;3.通過設置max-height過渡實現平滑展開/收起動畫;4.可用偽元素添加打開/關閉狀態圖標;5.使用radio類型可實現單開模式,checkbox則允許多開。這是一種無需JavaScript、兼容現代瀏覽器的交互式折疊菜單實現方法。
