Bootstrap4 radio button background and fill color

前端 未结 2 533
野的像风
野的像风 2020-12-24 14:33

I was wondering how can I change Bootstraps radio button background color and fill color?

相关标签:
2条回答
  • 2020-12-24 15:23

    Use the custom-radio class like so:

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    
    <div class="custom-control custom-radio">
      <input type="radio" id="customRadio1" name="customRadio" class="custom-control-input">
      <label class="custom-control-label" for="customRadio1">Toggle this custom radio</label>
    </div>

    That gives you the flexibility to style the radio input in any way you want.

    The background color (i.e. what you see in the default, unchecked state) is controlled by this rule:

    .custom-control-label::before {
        background-color: darkorange;
    }

    I changed it from the default grey to orange there.

    And here are the css rules to control all states:

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    
    <style>
    /* This is the default state */
    .custom-radio .custom-control-label::before {
        background-color: darkorange;  /* orange */
    }
    
    /* This is the checked state */
    .custom-radio .custom-control-input:checked~.custom-control-label::before,
    .custom-radio .custom-control-input:checked~.custom-control-label::after {
        background-color: greenyellow;  /* green */
        /* this bg image SVG is just a white circle, you can replace it with any valid SVG code */
        background-image: url(data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='-4 -4 8 8'%3E%3Ccircle r='3' fill='%23fff'/%3E%3C/svg%3E); 
        border-radius: 50%;
    }
    
    /* active state i.e. displayed while the mouse is being pressed down */
    .custom-radio .custom-control-input:active ~ .custom-control-label::before {
        color: #fff;
        background-color: #ff0000; /* red */
    }
        
    /* the shadow; displayed while the element is in focus */
    .custom-radio .custom-control-input:focus ~ .custom-control-label::before {
        box-shadow: 0 0 0 1px #fff, 0 0 0 0.2rem rgba(255, 123, 255, 0.25); /* pink, 25% opacity */
    }
    </style>
    
    <div class="container">
        <div class="row mt-3">
            <div class="col">
                <div class="custom-control custom-radio">
                    <input type="radio" id="customRadio1" name="customRadio" class="custom-control-input">
                    <label class="custom-control-label" for="customRadio1">Toggle this custom radio</label>
                </div>
            </div>
        </div>
    </div>

    0 讨论(0)
  • 2020-12-24 15:26

    See the code:

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    
    <style>
    .custom-control-input:checked ~ .custom-control-label::before {
      color: #fff;
      border-color: #7B1FA2;
      background-color: #7B1FA2;
    }
    </style>
    
    <div class="custom-control custom-radio custom-control-inline">
      <input type="radio" id="customRadioInline1" name="customRadioInline1" class="custom-control-input">
      <label class="custom-control-label" for="customRadioInline1">Toggle this custom radio</label>
    </div>
    <div class="custom-control custom-radio custom-control-inline">
      <input type="radio" id="customRadioInline2" name="customRadioInline1" class="custom-control-input">
      <label class="custom-control-label" for="customRadioInline2">Or toggle this other custom radio</label>
    </div>

    0 讨论(0)
提交回复
热议问题