Style Bootstrap 4自定义复选框

时间:2017-05-27 21:28:07

标签: html css twitter-bootstrap bootstrap-4

如何在Bootstrap 4中设置自定义复选框的样式?我已经尝试了一切,但我甚至无法改变它的背景颜色。

这是我的代码大纲:



<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<label class="custom-control custom-checkbox">
      <input type="checkbox" class="custom-control-input">
      <span class="custom-control-indicator"></span>
      <span class="custom-control-description">Check this custom checkbox</span>
</label>
&#13;
&#13;
&#13;

我的目标是什么?有人可以告诉我如何改变它的背景吗?

谢谢。

5 个答案:

答案 0 :(得分:7)

.custom-control{
  background-color: grey;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<label class="custom-control custom-checkbox">
      <input type="checkbox" class="custom-control-input">
      <span class="custom-control-indicator"></span>
      <span class="custom-control-description">Check this custom checkbox</span>
</label>

答案 1 :(得分:7)

对我有用
static string regRep(string inp)
{
    return inp.Replace("*", ".*").Replace('?', '.').Replace("-", @"\-");
}

static string regRep2(string inp)
{
    return inp.Replace("*", ".*").Replace('?', '.').Replace("-", ".");
}

static void Main()
{
    var patterns = new string[] { "8888*", "8889*", "8898*", "8899*", "8988*", "8989*", "8989*", "8999*", "7898*", "7899*", "8888?8", "888?-8", "*88*" };
    string ar = @"888?-*";

    List<string> res = new List<string>();

    bool mat1, mat2;
    Regex reg1, reg2;
    foreach (var reg in patterns)
    {
        reg1 = new Regex(regRep(reg));
        reg2 = new Regex(regRep(ar));

        mat1 = reg1.IsMatch(ar);
        mat2 = reg2.IsMatch(reg);

        // standard matching
        if (mat1 || mat2)
        {
            res.Add($"{ar} std matched with {reg}");
        }
        else
        {
            //forced matching
            reg1 = new Regex(regRep2(reg));
            reg2 = new Regex(regRep2(ar));
            mat1 = reg1.IsMatch(ar);
            mat2 = reg2.IsMatch(reg);

            if (mat1 || mat2)
            {
                res.Add($"{ar} frc matched with {reg}");
            }
        }
    }
}
//result
Count = 5
    [0]: "888?-* frc matched with 8888*"
    [1]: "888?-* frc matched with 8889*"
    [2]: "888?-* frc matched with 8888?8"
    [3]: "888?-* std matched with 888?-8"
    [4]: "888?-* std matched with *88*"

答案 2 :(得分:3)

您只需要覆盖custom-control-indicator样式。

尝试如下

&#13;
&#13;
<!DOCTYPE html>
<html>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">

<style>
.custom-control-input:checked~.custom-control-indicator{
color:white;
background-color:red;
}
</style>
<body>

<label class="custom-control custom-checkbox">
      <input type="checkbox" class="custom-control-input">
      <span class="custom-control-indicator"></span>
      <span class="custom-control-description">Check this custom checkbox</span>
</label>
</body>

</html>
&#13;
&#13;
&#13;

答案 3 :(得分:1)

我给你做了一个小demo

<style>
        .custom-checkbox {
            padding: 0;
        }

            .custom-checkbox .fa-toggle-on,
            .custom-checkbox .fa-toggle-off {
                font-size: 135%;
                /*this icon is relatively small*/
            }

            .custom-checkbox input[type=checkbox] {
                visibility: collapse;
                width: 0px;
                margin-left: -0.25em;
            }

                .custom-checkbox input[type=checkbox] ~ .custom-check-on {
                    display: none;
                }

                .custom-checkbox input[type=checkbox]:checked ~ .custom-check-on {
                    display: inline;
                }

                .custom-checkbox input[type=checkbox]:checked ~ .custom-check-off {
                    display: none;
                }

                .custom-checkbox input[type=checkbox]:disabled ~ * {
                    color: #b6b4b4;
                }

                .custom-checkbox input[type=checkbox].error ~ .custom-check-on,
                .custom-checkbox input[type=checkbox].error ~ .custom-check-off {
                    border: solid 2px red;
                }

            .custom-checkbox i.btn {
                overflow: hidden;
                color: transparent;
                position: relative;
                display: inline-block;
                width: 3em;
                padding: 0;
                font-style: normal;
            }

            .custom-checkbox .btn:after {
                content: "";
                font-style: normal;
                border: 7px solid white;
                position: absolute;
                top: 0;
                bottom: 0;
                border-radius: 5px;
            }

            .custom-checkbox .custom-check-on.btn:after {
                right: -4px;
            }

            .custom-checkbox .custom-check-off.btn:after {
                left: -4px;
            }

            .custom-checkbox .custom-check-on.btn:before {
                content: "On";
                color: white;
                margin-left: -10px;
            }

            .custom-checkbox .custom-check-off.btn:before {
                content: "Off";
                color: #333;
                margin-right: -15px;
            }

            .custom-checkbox input[type=checkbox]:checked ~ .btn.custom-check-on {
                display: inline-block;
            }
    </style>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css" rel="stylesheet"/>
<div class="container">
        <h3>Create custom checkbox with Bootstrap and Font Awesome</h3>
        <p>Here are some samples of customcheckboxes. You litterly can use any shape or color to make them the way you like it</p>
        <label class="custom-checkbox">
            <input name="check" type="checkbox" autocomplete="off" >
            <i class="custom-check-on btn btn-primary btn-sm">
            </i><i class="custom-check-off btn btn-light active btn-sm">
            </i><input name="check" type="hidden" value="false">
            <span class="ml-1">
                Check Box
            </span>
        </label>
        <br>
        <label class="custom-checkbox">
            <input name="check" type="checkbox" autocomplete="off" >
            <i class="custom-check-on fa fa fa-circle">
            </i><i class="custom-check-off fal fa-circle">
            </i><input name="check" type="hidden" value="false">
            <span class="ml-1">
                Check Box
            </span>
        </label>
        <br>
        <label class="custom-checkbox">
            <input name="check" type="checkbox" autocomplete="off" >
            <i class="custom-check-on far fa-check-square" style="color:black" >
            </i><i class="custom-check-off far fa-square" style="color:lightgray">
            </i><input name="check" type="hidden" value="false">
            <span class="ml-1">
                Check Box
            </span>
        </label>
        <br>
        <label class="custom-checkbox">
            <input name="check" type="checkbox" autocomplete="off" >
            <i class="custom-check-on text-info  fa fa-toggle-on">
            </i><i class="custom-check-off text-secondary fa fa-toggle-off">
            </i><input name="check" type="hidden" value="false">
            <span class="ml-1">
                Check Box
            </span>
        </label>
        <br>
        <label class="custom-checkbox">
            <input name="check" type="checkbox" autocomplete="off" >
            <i class="custom-check-on text-success far fa-smile">
            </i><i class="custom-check-off text-danger fas fa-angry">
            </i><input name="check" type="hidden" value="false">
            <span class="ml-1">
                Check Box
            </span>
        </label>
        <br>
        <label class="custom-checkbox">
            <input name="check" type="checkbox" autocomplete="off" >
            <i class="custom-check-on fas fa-arrow-alt-circle-up">
            </i><i class="custom-check-off far fa-arrow-alt-circle-down">
            </i><input name="check" type="hidden" value="false">
            <span class="ml-1">
                Check Box
            </span>
        </label>
        <br>


    </div>

<div class="container">
    <h3>Create custom checkbox with Bootstrap and Font Awesome</h3>
    <p>Here are some samples of customcheckboxes. You litterly can use any shape or color to make them the way you like it</p>
    <label class="custom-checkbox">
        <input name="check" type="checkbox" autocomplete="off" >
        <i class="custom-check-on btn btn-primary btn-sm">
        </i><i class="custom-check-off btn btn-light active btn-sm">
        </i><input name="check" type="hidden" value="false">
        <span class="ml-1">
            Check Box
        </span>
    </label>
    <br>
    <label class="custom-checkbox">
        <input name="check" type="checkbox" autocomplete="off" >
        <i class="custom-check-on fa fa fa-circle">
        </i><i class="custom-check-off fal fa-circle">
        </i><input name="check" type="hidden" value="false">
        <span class="ml-1">
            Check Box
        </span>
    </label>
    <br>
    <label class="custom-checkbox">
        <input name="check" type="checkbox" autocomplete="off" >
        <i class="custom-check-on far fa-check-square" style="color:black" >
        </i><i class="custom-check-off far fa-square" style="color:lightgray">
        </i><input name="check" type="hidden" value="false">
        <span class="ml-1">
            Check Box
        </span>
    </label>
    <br>
    <label class="custom-checkbox">
        <input name="check" type="checkbox" autocomplete="off" >
        <i class="custom-check-on text-info  fa fa-toggle-on">
        </i><i class="custom-check-off text-secondary fa fa-toggle-off">
        </i><input name="check" type="hidden" value="false">
        <span class="ml-1">
            Check Box
        </span>
    </label>
    <br>
    <label class="custom-checkbox">
        <input name="check" type="checkbox" autocomplete="off" >
        <i class="custom-check-on text-success far fa-smile">
        </i><i class="custom-check-off text-danger fas fa-angry">
        </i><input name="check" type="hidden" value="false">
        <span class="ml-1">
            Check Box
        </span>
    </label>
    <br>
    <label class="custom-checkbox">
        <input name="check" type="checkbox" autocomplete="off" >
        <i class="custom-check-on fas fa-arrow-alt-circle-up">
        </i><i class="custom-check-off far fa-arrow-alt-circle-down">
        </i><input name="check" type="hidden" value="false">
        <span class="ml-1">
            Check Box
        </span>
    </label>
    <br>


</div>

https://jsfiddle.net/eghbaly/t4zvshdu/4/ https://github.com/eghbaly/BS-CustomCheckbox

答案 4 :(得分:0)

我和olefrank有相同的问题。我想使用其他图像。例如,在我们的Intranet的表单页面上,为了指示喜欢的表单,我希望用户选择一个星星而不是一个复选框。我可以将其用于图像,但是我使用了Bootstrap 4文档中指定的标记:

<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="customCheck1">
<label class="custom-control-label" for="customCheck1">Check this custom checkbox</label>
</div>

然后是以下CSS:

.custom-checkbox .custom-control-label::before {border-color: transparent; background-color: transparent;}
.custom-checkbox .custom-control-label::after {background-image: url(five-pointed-star.svg); background-size: 100%;}
.custom-checkbox .custom-control-input:checked~.custom-control-label::before {border-color: transparent; background-color: transparent;}
.custom-checkbox .custom-control-input:checked~.custom-control-label::after {background-image: url(five-pointed-star-selected.svg); background-size: 100%;}

但是我很想知道如何使用网络字体指定替代图像,不幸的是,这超出了我的技能范围!