美化表单元素

本文介绍了如何使用CSS的伪类如:focus和:checked来美化表单元素,特别是聚焦和选中状态的样式设置。同时,讲解了通过css属性resize控制textarea的调整尺寸以及如何设定文本框边缘到内容的距离,以及对单选和多选框样式的控制方法。

美化表单元素

新的伪类

  1. 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>

  1. 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>

常见用法

  1. 重置表单元素样式

  2. 设置textarea是否允许调整尺寸

css属性resize

  • both :默认值,两个方向都可以调整尺寸
  • none:不能调整尺寸
  • horizontal:水平可以调整
  • vertical:垂直方向可以调整尺寸
  1. 文本框边缘到内容的距离

<style>
        /*方式一:padding*/
        input{
            padding:0 10px;
        }
        /*方式2 :text-indent*/
        input{
            text-indent: 1em;
        }
</style>

  1. 控制单选和多选的样式

详见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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值