如何用純CSS繪製三角形--03

最小生成树發表於2024-08-20

下拉選單中的箭頭通常用於提示使用者點選以展開選單。CSS三角形實現這個箭頭:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS三角形應用示例 - 下拉選單箭頭</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            background-color: #f9f9f9;
            margin: 0;
        }
        .dropdown {
            position: relative;
            display: inline-block;
        }
        .dropdown-button {
            padding: 10px 20px;
            background-color: #3498db;
            color: white;
            font-size: 16px;
            border: none;
            cursor: pointer;
            display: flex;
            align-items: center;
        }
        .dropdown-arrow {
            margin-left: 10px;
            width: 0;
            height: 0;
            border-left: 5px solid transparent;
            border-right: 5px solid transparent;
            border-top: 7px solid white;
        }
        .dropdown-content {
            display: none;
            position: absolute;
            background-color: white;
            min-width: 160px;
            box-shadow: 0px 8px 16px rgba(0, 0, 0, 0.2);
            z-index: 1;
            margin-top: 10px;
        }
        .dropdown-content a {
            color: black;
            padding: 12px 16px;
            text-decoration: none;
            display: block;
        }
        .dropdown-content a:hover {
            background-color: #f1f1f1;
        }
        .dropdown:hover .dropdown-content {
            display: block;
        }
    </style>
</head>
<body>
    <div class="dropdown">
        <button class="dropdown-button">
            下拉選單
            <div class="dropdown-arrow"></div>
        </button>
        <div class="dropdown-content">
            <a href="#">選項 1</a>
            <a href="#">選項 2</a>
            <a href="#">選項 3</a>
        </div>
    </div>
</body>
</html>

相關文章