zoukankan      html  css  js  c++  java
  • css3更改input单选和多选的样式

    在项目开发中我们经常会遇到需要更改input单选和多选样式的情况,今天就给大家介绍一种简单改变input单选和多选样式的办法。

    在这之前先简单介绍一下:before伪类

    :before 选择器向选定的元素前插入内容。使用content 属性来指定要插入的内容(content是必须的哦)。

    相信这并不难以理解,接下来我们先理解思路:

    (1)首先在html用label为 input 元素定义标记,也就是当你点击label标签时相应的input也会选中或取消

    (2)接下来就是写css了,将input隐藏,只需要在label之前加入你的样式就好了,可以是图片也可以是自己画的圆圈,下面的这个例子是我写的圆,当然也可以替换成背景图。

    input[type="radio"]+label:before是未选中状态的样式
    input[type="radio"]:checked+label:before是选中状态的样式
    <input type="radio" id="nationality1"><label for="nationality1">中国</label></div>
    input[type="radio"]{
        display:none;
    }
    input[type="radio"]+label{
        position: relative;
    }
    input[type="radio"]+label:before{
        content: "";
        width:25px;
        height:25px;
        background: #ffffff;
        border:2px solid $gray;
        position: absolute;
        bottom:3px;
        left: -35px;
        border-radius: 50%;
    }                                                   
    input[type="radio"]:checked+label:before
    { content: ""; width: 25px; height: 25px; background: #34c0f5; position: absolute; bottom:3px; left: -35px; z-index: 99; border-radius: 50%; }

    把radio替换成checkbox就是多选的啦。

    注:隐藏和伪类定位是关键

  • 相关阅读:
    多线程(10) — Future模式
    Java的设计模式(7)— 生产者-消费者模式
    多线程(9) — 无锁
    多线程(8) — ThreadLocal
    Java的设计模式(6)— 模板模式
    多线程(7)— JDK对锁优化的努力
    多线程(6) — 提高锁性能的一些看法
    复位电路 解析
    C语言数据类型
    MSP下载方式
  • 原文地址:https://www.cnblogs.com/xmoomoo/p/6203027.html
Copyright © 2011-2022 走看看