美化表单元素
新的伪类
- focus
元素聚焦时的样式
通常用于设置选中框
<style>
input:focus{
outline: 2px #008c8c solid;
outline-offset: 0px;;
color: red;
}
</style>
... ...
<p>
<a tabindex="2" href="">Lorem.</a>
<input tabindex="1" type="text">
<button tabindex="3">提交</button>
</p>
- checked
单选或多选框被选中的样式
通常用于设置单(多)选框的兄弟元素
<style>
input:checked+label{
color:yellow;
background: #008c8c;
}
</style>
... ...
<input id='radioMale' type="radio" name = 'gender'>
<label for="radioMale">男</label>
<input id='radioFemale' type="radio" name = 'gender'>
<label for="radioFemale">女</label>
常见用法
-
重置表单元素样式
-
设置textarea是否允许调整尺寸
css属性resize
- both :默认值,两个方向都可以调整尺寸
- none:不能调整尺寸
- horizontal:水平可以调整
- vertical:垂直方向可以调整尺寸
- 文本框边缘到内容的距离
<style>
/*方式一:padding*/
input{
padding:0 10px;
}
/*方式2 :text-indent*/
input{
text-indent: 1em;
}
</style>
- 控制单选和多选的样式
详见test6
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.radio-item .radio{
width: 12px;
height: 12px;
border: 1px solid #ccc;
border-radius: 50%;
cursor: pointer;
display: inline-block;
}
.radio-item input:checked+.radio{
border-color: #008c8c;
}
.radio-item input:checked~span{
color: #008c8c;
}
.radio-item input:checked+.radio::after{
content: "";
display: block;
width: 5px;
height: 5px;
background: #008c8c;
margin-left: 3.5px;
margin-top: 3.5px;
border-radius: 50px;
}
.radio-item input[type="radio"]{
display: none;
}
</style>
</head>
<body>
<p>
请选择性别:
<label class="radio-item">
<input name='gender' type="radio">
<span class="radio"></span>
<span>男</span>
</label>
<label class="radio-item">
<input name='gender' type="radio">
<span class="radio"></span>
<span>女</span>
</label>
</p>
</body>
</html>
本文介绍了如何使用CSS的伪类如:focus和:checked来美化表单元素,特别是聚焦和选中状态的样式设置。同时,讲解了通过css属性resize控制textarea的调整尺寸以及如何设定文本框边缘到内容的距离,以及对单选和多选框样式的控制方法。
1261

被折叠的 条评论
为什么被折叠?



