CSS :target 偽類
- 上一頁 :root
- 下一頁 :user-invalid
- 返回上一層 CSS 偽類參考手冊
定義和用法
CSS :target
偽類用于設置當前活動目標元素的樣式。
提示:帶有 # 的 URL 后跟錨點名稱,鏈接到文檔中的特定元素。被鏈接到的元素是目標元素。
實例
例子 1
設置當前活動目標元素的樣式:
:target { border: 2px solid darkorange; background-color: beige; }
例子 2
創建一個選項卡菜單:
.tab div { display: none; } .tab div:target { display: block; }
例子 3
創建一個模態框(對話框):
/* 模態框的背景 */ .modal { display: none; left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background-color: rgb(0, 0, 0); background-color: rgba(0, 0, 0, 0.4); } /* 當目標時顯示模態框 */ .modal:target { display: table; position: absolute; } /* 模態框 */ .modal-dialog { display: table-cell; vertical-align: middle; } /* 模態框的內容 */ .modal-dialog .modal-content { margin: auto; background-color: #f3f3f3; position: relative; padding: 0; outline: 0; border: 1px #777 solid; text-align: justify; width: 80%; box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19); }
CSS 語法
:target { css declarations; }
技術細節
版本: | CSS3 |
---|
瀏覽器支持
表格中的數字指定了完全支持該偽類的首個瀏覽器版本。
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
4 | 9 | 3.5 | 3.2 | 9.5 |
- 上一頁 :root
- 下一頁 :user-invalid
- 返回上一層 CSS 偽類參考手冊