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就是多选的啦。

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

  • 相关阅读:
    哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(低年级)
    ACM_X章求和(数学)
    goj 扫雷(dfs)
    Sereja and Brackets(括号匹配)
    NOIP模拟赛 迷路
    NOIP模拟赛three(3)
    NOIP模拟赛2(two)
    NOIP模拟赛1(one)
    czy的后宫5
    [BZOJ3436]小K的农场
  • 原文地址:https://www.cnblogs.com/xmoomoo/p/6203027.html
Copyright © 2011-2022 走看看