• 改变Bootstrap4、Bootstrap5中复选框的颜色


    今天做练习,用到了Bootstrap4制作的开关式复选框,复选框默认的颜色是蓝色,跟整个网页颜色不搭,需要改成绿色。
    在这里插入图片描述

    想从网上找现成的答案,找了几个,试了试居然无效,只好自己来。经过几次尝试,改成功了。我用的版本是Bootstrap4,结果发现Bootstrap5和 Bootstrap4实现起来还不太一样,最后测试可以了。

    一、Bootstrap5中的复选框

    Bootstrap5中的复选框改变颜色实现起来很容易,只需要直接修改 .form-check-input选中时:checked的背景色和边框颜色就可以了。

      .form-check-input:checked {
          background-color: #28a745; 
          border-color:#28a745;
        }
    
    • 1
    • 2
    • 3
    • 4

    完整代码如下:

    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title>Bootstra5中复选框的颜色</title>
      <link href="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/css/bootstrap.min.css" rel="stylesheet">
    </head>
    <body>
      <style> -->
        .form-check-input:checked {
          background-color: #28a745;
          border-color:#28a745;
        }
      </style> 
      </head>
      <body>
        <div class="container mt-3">
          <div class="form-check">
            <input type="checkbox" class="form-check-input" id="option1" name="sports" value="something" checked>
            <label class="form-check-label" for="check1">羽毛球</label>
          </div>
          <div class="form-check">
            <input type="checkbox" class="form-check-input" id="option2" name="sports" value="something">
            <label class="form-check-label" for="check2">乒乓球</label>
          </div>
        </div>
        <!-- 开关样式的复选框 form-switch -->
        <div class="container mt-3 form-check form-switch">
          <input class="form-check-input" type="checkbox" id="mySwitch" value="yes" checked>
          <label class="form-check-label" for="mySwitch">已启用</label>
        </div>
      </body>
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    二、Bootstrap4中的复选框

    Bootstrap4中复选框颜色的修改要稍微复杂一点,需要使用~兄弟选择器,还要使用::before,所用的类名也不同。原本我参考的代码写的是:active,经测试无效,我抱着试试看的态度改成了:checked,实现了目标效果哦。直接复制下面代码,改改颜色值即可。代码如下:

        .custom-control-input:checked~.custom-control-label::before{
          background-color: #28a745;
          border-color: #28a745;
        } 
    
    • 1
    • 2
    • 3
    • 4

    完整代码如下:

    DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title>Bootstrap4中复选框的颜色title>
      <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
    head>
    <body>
      
      <style>
        .custom-control-input:checked~.custom-control-label::before{
          background-color: #28a745;
          border-color: #28a745;
        } 
      style>
    head>
      <body>
        <div class="container">
          <div class="custom-control custom-checkbox">
            <input type="checkbox" class="custom-control-input" id="customSwitch1" checked>
            <label class="custom-control-label" for="customSwitch1">网球label>
          div>
        <div class="custom-control  custom-switch">
          <input type="checkbox" class="custom-control-input" id="customSwitch2" checked>
          <label class="custom-control-label" for="customSwitch2">已启用label>
        div>
      div>
      body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
  • 相关阅读:
    2023年施工升降机司机(建筑特殊工种)证模拟考试题库及施工升降机司机(建筑特殊工种)理论考试试题
    使用亚马逊云科技人工智能内容审核服务,打造安全的图像生成和扩散模型
    SpringBoot自动装配源码解析
    C++初阶(八)类和对象
    【VPX302】基于3U VPX总线架构的高性能数据预处理平台
    iPhone或在2024开放第三方应用商店。
    设计模式之命令模式
    catia距离测量
    深度学习计算 - GPU
    redis的事务
  • 原文地址:https://blog.csdn.net/wangyining070205/article/details/126353319