修改 input 默认控件的颜色

一.Accent-color

开发中不可避免要遇到使用原生input的控件,尤其还要按照UI 设计稿来给控件改变颜色。Accent-color是 CSS 2022 年推出的一个新属性,相比于其它新属性,它有不错的兼容性,它能修改 input 默认控件的颜色。

  1. 单选按钮

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .accented {
            accent-color: #6b41ff;
        }

        [color-scheme="dark"] .accented {
            accent-color: #6b41ff;
        }

        input {
            --size: 2rem;
            block-size: var(--size);
            inline-size: var(--size);
        }

        label {
            display: flex;
            align-items: center;
            justify-content: flex-end;
            gap: 1rem;
        }

        [color-scheme="light"] {
            color-scheme: light;
        }

        [color-scheme="dark"] {
            color-scheme: dark;
        }

        fieldset {
            border: 1px solid hsl(0 0% 50%);
            border-radius: 1rem;
            padding: 1rem;
            display: flex;
            flex-direction: column;
            gap: 1rem;
        }

        fieldset[color-scheme="dark"] {
            background: Canvas;
            color: white;
        }

        * {
            box-sizing: border-box;
            margin: 0;
        }

        html {
            block-size: 100%;
        }

        body {
            min-block-size: 100%;
            font-family: system-ui, sans-serif;

            display: grid;
            place-content: center;
            gap: 2rem;
        }

        @media (min-width: 540px) {
            body {
                grid-auto-flow: column;
            }
        }
    </style>
</head>

<body>
    <fieldset color-scheme="light">
        <label>
            Default
            <input type="radio" name="default-demo-light" checked>
            <input type="radio" name="default-demo-light">
            <input type="radio" name="default-demo-light">
        </label>

        <label class="accented">
            Tinted
            <input type="radio" name="accented-demo-light" checked>
            <input type="radio" name="accented-demo-light">
            <input type="radio" name="accented-demo-light">
        </label>
    </fieldset>

    <fieldset color-scheme="dark">
        <label>
            Default
            <input type="radio" name="default-demo-dark" checked>
            <input type="radio" name="default-demo-dark">
            <input type="radio" name="default-demo-dark">
        </label>

        <label class="accented">
            Tinted
            <input type="radio" name="accented-demo-dark" checked>
            <input type="radio" name="accented-demo-dark">
            <input type="radio" name="accented-demo-dark">
        </label>
    </fieldset>
</body>

</html>

看一下改变后的效果:

  1. 复选框

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .accented {
            accent-color: #6b41ff;
        }

        [color-scheme="dark"] .accented {
            accent-color: #6b41ff;
        }

        input {
            --size: 2rem;
            block-size: var(--size);
            inline-size: var(--size);
        }

        label {
            display: flex;
            align-items: center;
            justify-content: flex-end;
            gap: 1rem;
        }

        [color-scheme="light"] {
            color-scheme: light;
        }

        [color-scheme="dark"] {
            color-scheme: dark;
        }

        fieldset {
            border: 1px solid hsl(0 0% 50%);
            border-radius: 1rem;
            padding: 1rem;
            display: flex;
            flex-direction: column;
            gap: 1rem;
        }

        fieldset[color-scheme="dark"] {
            background: Canvas;
            color: white;
        }

        * {
            box-sizing: border-box;
            margin: 0;
        }

        html {
            block-size: 100%;
        }

        body {
            min-block-size: 100%;
            font-family: system-ui, sans-serif;

            display: grid;
            place-content: center;
            gap: 2rem;
        }

        @media (min-width: 480px) {
            body {
                grid-auto-flow: column;
            }
        }
    </style>
</head>

<body>
    <fieldset color-scheme="light">
        <label for="default-light">
            Default
            <input type="checkbox" id="default-light" checked>
        </label>

        <label for="accented-light">
            Tinted
            <input class="accented" type="checkbox" id="accented-light" checked>
        </label>
    </fieldset>

    <fieldset color-scheme="dark">
        <label for="default-dark">
            Default
            <input type="checkbox" id="default-dark" checked>
        </label>

        <label for="accented-dark">
            Tinted
            <input class="accented" type="checkbox" id="accented-dark" checked>
        </label>
    </fieldset>
</body>

</html>

看一下改变后的效果:

  1. 进度条

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .accented {
            accent-color: deeppink;
        }

        [color-scheme="dark"] .accented {
            accent-color: hsl(328 100% 80%);
        }

        progress {
            block-size: 1.5rem;
        }

        label {
            display: flex;
            align-items: center;
            justify-content: flex-end;
            gap: 1rem;
        }

        [color-scheme="light"] {
            color-scheme: light;
        }

        [color-scheme="dark"] {
            color-scheme: dark;
        }

        fieldset {
            border: 1px solid hsl(0 0% 50%);
            border-radius: 1rem;
            padding: 1rem;
            display: flex;
            flex-direction: column;
            gap: 1rem;
        }

        fieldset[color-scheme="dark"] {
            background: Canvas;
            color: white;
        }

        * {
            box-sizing: border-box;
            margin: 0;
        }

        html {
            block-size: 100%;
        }

        body {
            min-block-size: 100%;
            font-family: system-ui, sans-serif;

            display: grid;
            place-content: center;
            gap: 2rem;
        }

        @media (min-width: 680px) {
            body {
                grid-auto-flow: column;
            }
        }
    </style>
</head>

<body>

    <fieldset color-scheme="light">
        <label>
            Default
            <progress max="100" value="50">50%</progress>
        </label>

        <label class="accented">
            Tinted
            <progress max="100" value="50">50%</progress>
        </label>
    </fieldset>

    <fieldset color-scheme="dark">
        <label>
            Default
            <progress max="100" value="50">50%</progress>
        </label>

        <label class="accented">
            Tinted
            <progress max="100" value="50">50%</progress>
        </label>
    </fieldset>
</body>

</html>

看一下改变后的效果:

  1. 其他input类型

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        html {
            --brand: hotpink;
            scrollbar-color: hotpink Canvas;
        }

        :root {
            accent-color: var(--brand)
        }

        :focus-visible {
            outline-color: var(--brand)
        }

        ::selection {
            background-color: var(--brand)
        }

        ::marker {
            color: var(--brand)
        }

        ::-webkit-calendar-picker-indicator {
            color: var(--brand)
        }

        ::-webkit-clear-button {
            color: var(--brand);
        }

        ::-webkit-inner-spin-button,
        ::-webkit-outer-spin-button {
            color: var(--brand);
        }

        * {
            box-sizing: border-box;
            margin: 0;
        }

        html {
            block-size: 100%;
            color-scheme: dark light;
        }

        body {
            min-block-size: 100%;
            font-family: system-ui, sans-serif;

            display: grid;
            place-content: center;
            gap: 2ch;
        }
    </style>
</head>

<body>
    <input type="date">
    <input type="number" min=0 max=10 value=0>
    <input type="text" list="animals" placeholder="Animals">
    <input type="search" list="animals" placeholder="Search...">

    <details>
        <summary>Details</summary>
        <p>Peek-a-boo</p>
    </details>

    <ul>
        <li>This</li>
        <li>Is</li>
        <li>A</li>
        <li>List</li>
    </ul>

    <datalist id="animals">
        <option>tiger</option>
        <option>money</option>
        <option>Spider</option>
    </datalist>
</body>

</html>

看一下改变后的效果:

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值