实现思路
- 输入框获取焦点效果:使用
input:focus
伪类选择器,选中获取焦点的输入框,修改其边框颜色为橙色。
- 输入框内容为空且失去焦点效果:使用
input:empty:focus-within
伪类选择器,选中内容为空且失去焦点的输入框,修改其边框颜色为红色,并通过::after
伪元素显示提示文字“请输入内容”。
- 登录按钮禁用状态效果:使用
button:disabled
伪类选择器,选中禁用状态的按钮,设置透明度为0.5,并通过button:disabled:hover
伪类选择器,确保鼠标悬停时无效果。
主要CSS代码
/* 输入框获取焦点时,边框颜色变为橙色 */
input:focus {
border-color: orange;
}
/* 输入框内容为空且失去焦点时,边框颜色变为红色并显示提示文字 */
input:empty:focus-within {
border-color: red;
position: relative;
}
input:empty:focus-within::after {
content: '请输入内容';
position: absolute;
color: red;
font-size: 12px;
top: 100%;
left: 0;
}
/* 登录按钮禁用状态下,透明度为0.5且鼠标悬停时无任何效果 */
button:disabled {
opacity: 0.5;
}
button:disabled:hover {
cursor: default;
/* 这里可以不设置其他样式,确保悬停无效果 */
}