一.Accent-color
开发中不可避免要遇到使用原生input的控件,尤其还要按照UI 设计稿来给控件改变颜色。Accent-color是 CSS 2022 年推出的一个新属性,相比于其它新属性,它有不错的兼容性,它能修改 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>
.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>
看一下改变后的效果:
![](https://i-blog.csdnimg.cn/blog_migrate/183c0ec59d55344f072990cfc7918d5d.png)
复选框
<!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>
看一下改变后的效果:
![](https://i-blog.csdnimg.cn/blog_migrate/3233d42737b02d6c7006782d5343cb50.png)
进度条
<!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>
看一下改变后的效果:
![](https://i-blog.csdnimg.cn/blog_migrate/237f48e52ed2028e84de0910b446d855.png)
其他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>
看一下改变后的效果:
![](https://i-blog.csdnimg.cn/blog_migrate/924df3ffe89d97b2819ea50a290a1999.png)