
- CSS 教程
- CSS - 首頁
- CSS - 路線圖
- CSS - 簡介
- CSS - 語法
- CSS - 選擇器
- CSS - 包含
- CSS - 測量單位
- CSS - 顏色
- CSS - 背景
- CSS - 字型
- CSS - 文字
- CSS - 圖片
- CSS - 連結
- CSS - 表格
- CSS - 邊框
- CSS - 塊級邊框
- CSS - 行內邊框
- CSS - 外邊距
- CSS - 列表
- CSS - 內邊距
- CSS - 游標
- CSS - 輪廓
- CSS - 尺寸
- CSS - 捲軸
- CSS - 行內塊
- CSS - 下拉選單
- CSS - 可見性
- CSS - 溢位
- CSS - 清除浮動
- CSS - 浮動
- CSS - 箭頭
- CSS - 調整大小
- CSS - 引號
- CSS - 順序
- CSS - 位置
- CSS - 連字元
- CSS - 懸停
- CSS - 顯示
- CSS - 聚焦
- CSS - 縮放
- CSS - 位移
- CSS - 高度
- CSS - 連字元字元
- CSS - 寬度
- CSS - 不透明度
- CSS - Z-Index
- CSS - 底部
- CSS - 導航欄
- CSS - 覆蓋層
- CSS - 表單
- CSS - 對齊
- CSS - 圖示
- CSS - 圖片庫
- CSS - 註釋
- CSS - 載入器
- CSS - 屬性選擇器
- CSS - 組合器
- CSS - 根元素
- CSS - 盒模型
- CSS - 計數器
- CSS - 剪裁
- CSS - 書寫模式
- CSS - Unicode-bidi
- CSS - min-content
- CSS - all
- CSS - inset
- CSS - isolation
- CSS - overscroll
- CSS - justify-items
- CSS - justify-self
- CSS - tab-size
- CSS - pointer-events
- CSS - place-content
- CSS - place-items
- CSS - place-self
- CSS - max-block-size
- CSS - min-block-size
- CSS - mix-blend-mode
- CSS - max-inline-size
- CSS - min-inline-size
- CSS - offset
- CSS - accent-color
- CSS - user-select
- CSS 高階
- CSS - 網格
- CSS - 網格佈局
- CSS - Flexbox
- CSS - 可見性
- CSS - 定位
- CSS - 層
- CSS - 偽類
- CSS - 偽元素
- CSS - @規則
- CSS - 文字效果
- CSS - 分頁媒體
- CSS - 列印
- CSS - 佈局
- CSS - 驗證
- CSS - 圖片精靈
- CSS - !important
- CSS - 資料型別
- CSS3 教程
- CSS3 - 教程
- CSS - 圓角
- CSS - 邊框圖片
- CSS - 多重背景
- CSS - 顏色
- CSS - 漸變
- CSS - 盒陰影
- CSS - box-decoration-break
- CSS - caret-color
- CSS - 文字陰影
- CSS - 文字
- CSS - 2D 變換
- CSS - 3D 變換
- CSS - 過渡
- CSS - 動畫
- CSS - 多列
- CSS - box-sizing
- CSS - 工具提示
- CSS - 按鈕
- CSS - 分頁
- CSS - 變數
- CSS - 媒體查詢
- CSS - 函式
- CSS - 數學函式
- CSS - 遮罩
- CSS - 形狀
- CSS - 樣式圖片
- CSS - 特效性
- CSS - 自定義屬性
- CSS 響應式
- CSS RWD - 簡介
- CSS RWD - 視窗
- CSS RWD - 網格檢視
- CSS RWD - 媒體查詢
- CSS RWD - 圖片
- CSS RWD - 影片
- CSS RWD - 框架
- CSS 工具
- CSS - PX 到 EM 轉換器
- CSS - 顏色選擇器和動畫
- CSS 資源
- CSS - 有用資源
- CSS - 討論
CSS - 箭頭
CSS 箭頭是使用 CSS 屬性建立的圖形符號,用於將檢視者的注意力指向特定方向。CSS 中的箭頭是使用邊框、剪裁路徑或圖示等技術建立的。本教程將探討使用 CSS 建立和設定箭頭樣式的所有方法。
目錄
如何在 CSS 中建立箭頭?
箭頭可以使用以下 CSS 屬性建立
- 使用 CSS 邊框:可以透過定義一個方形元素並將它相鄰的兩個邊框設定為透明,而另外兩個邊框設定為可見來建立 CSS 箭頭。
- 使用圖示:可以使用在 Font Awesome、Material Icons 和 Google Icons 等庫中定義的預定義箭頭圖示。它們通常帶有現成的類或實用程式類。
- 使用剪裁路徑:CSS 中的clip-path 屬性用於定義任何形狀的多邊形,您也可以用它來定義任何形狀的箭頭。
- 使用 Unicode 字元:要建立簡單的箭頭,可以在像 div 這樣的容器內指定Unicode 字元。例如,程式碼 '▶' 將建立一個右箭頭。
使用邊框建立 CSS 箭頭
邊框是建立箭頭的最常見方法。使用邊框,我們可以建立兩種型別的箭頭:
- 對於實心箭頭,將容器元素的高度和寬度設定為零。然後選擇任何邊框(例如,border-bottom),設定實心顏色和厚度以符合箭頭的尺寸。現在為其兩個相鄰的邊框(在本例中為 border-left 和 border-right)賦予相同的厚度,但顏色為透明。這將建立一個向上指向的箭頭。
- 對於線條箭頭,根據所需的箭頭大小設定元素的高度和寬度。現在為任意兩個相鄰的邊框(例如 border-left 和 border-top)設定厚度和顏色。使用 transform 屬性將元素旋轉到所需的方向。
示例
<!DOCTYPE html> <html> <head> <style> .arrow-up { width: 0; height: 0; border-left: 10px solid transparent; border-right: 10px solid transparent; border-bottom: 10px solid black; } .arrow-down{ width: 10px; height: 10px; border-left: 2px solid; border-top: 2px solid; transform: rotate(225deg) skew(10deg, 10deg); } .arrow-diagonal{ width: 10px; height: 10px; border-left: 2px solid; border-top: 2px solid; transform: rotate(90deg) skew(10deg, 10deg); } </style> </head> <body> <h3>Arrow Type 1</h3> <div class="arrow-up"></div> <h3>Arrow Type 2</h3> <div class="arrow-down"></div> <h3>Arrow Rotated</h3> <div class="arrow-diagonal"></div> </body> </html>
使用圖示建立 CSS 箭頭
我們還可以使用預定義的圖示庫(如 Font Awesome、Material Icons 和 Google Icons)建立箭頭。下面的示例使用 Google 圖示顯示箭頭。
示例
<!DOCTYPE html> <html> <head> <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons"> </head> <body> <h1> Google Fonts Icon </h1> <span class="material-icons" style="font-size:40px;"> arrow_forward </span> <span class="material-icons" style="font-size:40px;"> arrow_backward </span> </body> </html>
使用剪裁路徑屬性建立箭頭
我們可以使用 CSS 中的clip-path 屬性建立任何形狀的箭頭。
clip-path: polygon(x1 y1, x2 y2, x3 y3, ...);
- x1 y1, x2 y2, ... 是定義多邊形頂點的座標對。座標以百分比或絕對單位(例如,px)給出。
- 原點 (0, 0) 是元素的左上角,(100%, 100%) 是右下角。
示例
<!DOCTYPE html> <html> <head> <style> .arrow-right { width: 0; height: 0; margin: 29px; border-style: solid; border-width: 10px 0 10px 20px; clip-path: polygon(100% 50%, 0 100%, 0 0); } </style> </head> <body> <h3> Arrow Using Clip Path </h3> <div class="arrow-right" ></div> </body> </html>
CSS 箭頭樣式
以下示例演示如何透過設定顏色、transform-rotate、陰影和動畫等屬性來設定 CSS 箭頭的樣式。
示例
<!DOCTYPE html> <html lang="en"> <head> <style> .arrow { width: 0; height: 0; margin: 20px; } .arrow-color { border-left: 10px solid transparent; border-right: 10px solid transparent; border-bottom: 10px solid red; } .arrow-shadow { width: 20px; height: 20px; border-left: 2px solid; border-top: 2px solid; transform: rotate(135deg) skew(10deg, 10deg); box-shadow: -5px -5px 10px rgba(0, 0, 0, 0.5); } .arrow-rotated { border-left: 10px solid transparent; border-right: 10px solid transparent; border-bottom: 10px solid black; transform: rotate(45deg); } .arrow-animate { border-left: 10px solid transparent; border-right: 10px solid transparent; border-bottom: 10px solid aqua; transition: transform 0.3s ease-in-out; } .arrow-animate:hover { transform: translateY(-10px); } </style> </head> <body> <h3>Colored Arrow</h3> <div class="arrow arrow-color"></div> <h3> Arrow with shadow </h3> <div class="arrow arrow-shadow"></div> <h3> Rotated arrow </h3> <div class="arrow arrow-rotated"></div> <h3> Animated arrow </h3> <div class="arrow arrow-animate"></div> </body> </html>
CSS 工具提示箭頭
我們可以使用 CSS 邊框和 transform 屬性建立一個帶有向上指向的三角形箭頭的工具提示。我們使用了hover 偽類,以便在使用者將滑鼠懸停在內容上時顯示工具提示。
示例
<!DOCTYPE html> <html> <head> <style> .tooltip { position: relative; display: inline-block; cursor: pointer; } .tooltipContent { display: none; position: absolute; background-color: grey; color: #fff; padding: 8px; border-radius: 4px; z-index: 1; font-size: 14px; white-space: nowrap; } .tooltip:hover .tooltipContent { display: block; } .tooltipContent::before { content: ""; position: absolute; border-width: 6px; border-style: solid; border-color: transparent transparent grey transparent; top: -12px; left: 50%; transform: translateX(-50%); } </style> </head> <body> <h3 class="tooltip"> Hover Me!!! <span class="tooltipContent">CSS - Arrow</span> </h3> </body> </html>
廣告