CSS:目標偽級示例
:target偽類用於樣式化URL片段標識符指向的元素,1. 當鏈接指向頁面中的ID時,對應元素成為目標並應用特殊樣式;2. 可用於高亮內容、創建選項卡或顯示/隱藏元素;3. 示例中通過section:target顯示目標區塊並隱藏其他;4. 實際應用包括FAQ、標籤頁和內容高亮;5. 支持動畫增強效果且無需JavaScript;6. 注意ID唯一性且一次僅一個元素為目標;7. 所有現代瀏覽器均支持該特性。
The :target
pseudo-class in CSS is used to style the element that is the target of a fragment identifier (the part of the URL after the #
). When a link points to an ID on the same page, the element with that ID becomes the "target", and :target
allows you to apply special styles to it.

This is useful for highlighting content, creating simple tabbed interfaces, or showing/hiding elements without JavaScript.
✅ Basic :target
Example
Here's a simple example showing how :target
works:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>:target Example</title> <style> /* Hide all sections by default */ section { display: none; padding: 20px; border: 1px solid #ccc; margin-top: 10px; } /* Show only the targeted section */ section:target { display: block; background-color: #f0f8ff; border-color: #007cba; } /* Style the navigation */ nav a { margin: 0 10px; text-decoration: none; color: #007cba; } nav a:hover { text-decoration: underline; } </style> </head> <body> <nav> <a href="#home">Home</a> <a href="#about">About</a> <a href="#contact">Contact</a> </nav> <section id="home"> <h2>Home</h2> <p>Welcome to the home section!</p> </section> <section id="about"> <h2>About</h2> <p>Learn more about us here.</p> </section> <section id="contact"> <h2>Contact</h2> <p>Get in touch with us.</p> </section> </body> </html>
? How It Works
- When you click a link like
<a href="#about">
, the browser scrolls to the element withid="about"
. - That element now matches the
:target
pseudo-class. - The CSS rule
section:target { display: block; }
makes only that section visible. - This lets you simulate a simple single-page navigation or accordion effect using only HTML and CSS.
? Practical Use Cases
- FAQ pages : Click a question to reveal its answer.
- Tabs or panels : Show one panel at a time.
- Highlighting referenced content : Add a yellow background or animation to the targeted section.
Example enhancement with animation:
section:target { display: block; animation: fadeIn 0.5s ease-in-out; } @keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
⚠️ Notes
- Only one element can be the target at a time.
- The
:target
selector works on any element with anid
that matches the URL hash. - Always ensure IDs are unique.
Basically, :target
gives you a lightweight way to create dynamic effects without JavaScript. It's well supported in all modern browsers.

以上是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)

Autoprefixer是一個根據目標瀏覽器範圍自動為CSS屬性添加廠商前綴的工具。 1.它解決了手動維護前綴易出錯的問題;2.通過PostCSS插件形式工作,解析CSS、分析需加前綴的屬性、依配置生成代碼;3.使用步驟包括安裝插件、設置browserslist、在構建流程中啟用;4.注意事項有不手動加前綴、保持配置更新、非所有屬性都加前綴、建議配合預處理器使用。

TocreatestickyheadersandfooterswithCSS,useposition:stickyforheaderswithtopvalueandz-index,ensuringparentcontainersdon’trestrictit.1.Forstickyheaders:setposition:sticky,top:0,z-index,andbackgroundcolor.2.Forstickyfooters,betteruseposition:fixedwithbot

theconic-Gradient()functionIncsscreatesCircularGradientsThatRotateColorStopSaroundAcentralPoint.1.IsidealForPieCharts,ProgressIndicators,colordichers,colorwheels和decorativeBackgrounds.2.itworksbysbysbysbydefindefingincolordefingincolorstopsatspecificains off.

創建CSS加載旋轉器的方法有三種:1.使用邊框的基本旋轉器,通過HTML和CSS實現簡單動畫;2.使用多個點的自定義旋轉器,通過不同延遲時間實現跳動效果;3.在按鈕中添加旋轉器,通過JavaScript切換類來顯示加載狀態。每種方法都強調了設計細節如顏色、大小、可訪問性和性能優化的重要性,以提升用戶體驗。

Mobile-firstCSSdesignrequiressettingtheviewportmetatag,usingrelativeunits,stylingfromsmallscreensup,optimizingtypographyandtouchtargets.First,addtocontrolscaling.Second,use%,em,orreminsteadofpixelsforflexiblelayouts.Third,writebasestylesformobile,the

要創建內在響應式網格佈局,核心方法是使用CSSGrid的repeat(auto-fit,minmax())模式;1.設置grid-template-columns:repeat(auto-fit,minmax(200px,1fr))讓瀏覽器自動調整列數並限制每列最小和最大寬度;2.使用gap控制格子間距;3.容器應設為相對單位如width:100%、配合box-sizing:border-box避免寬度計算錯誤並用margin:auto居中;4.可選設置行高與內容對齊方式提升視覺一致性,如row

要讓整個網格佈局在視口中居中顯示,可通過以下方法實現:1.使用margin:0auto實現水平居中,需設定容器固定寬度,適用於固定佈局;2.利用Flexbox在外層容器設置justify-content和align-items屬性,結合min-height:100vh可實現垂直和水平居中,適合全屏展示場景;3.直接使用CSSGrid的place-items屬性在父容器上快速居中,簡潔且現代瀏覽器支持良好,同時需確保父容器有足夠高度。每種方式均有適用場景和限制,根據實際需求選擇合適的方案即可。

prainuredetectionIncsssusissuse@supportScheckSifabRowsEsuppecifortSupecifortEfeatureBeforeApplyingReplyingStyles.1.itusesconditionalcsssssbasssbasedonproperty-valueperty-valuepairs,suessas@supports@supports@supports@supports(display:grid)
