CSS :focus-within 伪类
cssweb developmentfront end technology更新于 2024/9/4 1:16:17
如果我们想要设置焦点元素父元素的样式,可以使用 CSS :focus-within 伪类。
以下示例演示了 CSS :focus-within 伪类。
示例
<!DOCTYPE html>
<html>
<head>
<style>
form {
margin: 2%;
padding: 2%;
display: flex;
flex-direction: column;
background: thistle;
}
input {
margin: 2%;
}
form:focus-within {
background-color: burlywood;
box-shadow: 0 0 12px rgba(0,0,0,0.6)
}
input:focus {
box-shadow: 0 0 12px rgba(0,0,0,0.6)
}
</style>
</head>
<body>
<form>
<input type="text" placeholder="name"/>
<input type="email" placeholder="email"/>
</form>
</body>
</html>
输出
这将产生以下结果 −

示例
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 40%;
margin: 2%;
padding: 2%;
display: flex;
flex-direction: column;
background: lavenderblush;
}
div > * {
margin: 2%;
}
div:focus-within {
background-color: aliceblue;
box-shadow: 0 0 12px rgba(0,0,0,0.6)
}
</style>
</head>
<body>
<div>
<textarea></textarea>
<button>Click</button>
</div>
</body>
</html>
输出
这将产生以下结果 −

相关文章
有用资源
css 参考教程 - 该教程包含有关 css 的更多信息:https://www.cainiaomax.com/css/

