locked
Need help styling checkboxes/radio buttons RRS feed

  • Question

  • User-501297529 posted

    I'm trying to get checkboxes/radio buttons styled exactly like the image below

    I've tried this code in my css bu that's not exactly how it should be in the image. 

    /* Base for label styling */
    [type="checkbox"]:not(:checked),
    [type="checkbox"]:checked {
      ;
      left: -9999px;
    }
    [type="checkbox"]:not(:checked) + label,
    [type="checkbox"]:checked + label {
      ;
      padding-left: 1.95em;
      cursor: pointer;
    }
     
    /* checkbox aspect */
    [type="checkbox"]:not(:checked) + label:before,
    [type="checkbox"]:checked + label:before {
      content: '';
      ;
      left: 0; top: 0;
      width: 1.25em; height: 1.25em;
      border: 2px solid #ccc;
      background: #fff;
      border : 1px solid #512888;
      border-radius: 4px;
      box-shadow: inset 0 1px 3px rgba(0,0,0,.1);
    }
    /* checked mark aspect */
    [type="checkbox"]:not(:checked) + label:after,
    [type="checkbox"]:checked + label:after {
      content: '✔';
      ;
      top: .2em; left: .275em;
      font-size: 1.4em;
      line-height: 0.8;
      color: #512888;
      transition: all .2s;
      font-family: Helvetica, Arial, sans-serif;
    }
    /* checked mark aspect changes */
    [type="checkbox"]:not(:checked) + label:after {
      opacity: 0;
      transform: scale(0);
    }
    [type="checkbox"]:checked + label:after {
      opacity: 1;
      transform: scale(0.9);
    }
    /* disabled checkbox */
    [type="checkbox"]:disabled:not(:checked) + label:before,
    [type="checkbox"]:disabled:checked + label:before {
      box-shadow: none;
      border-color: #bbb;
      background-color: #ddd;
    }
    [type="checkbox"]:disabled:checked + label:after {
      color: #999;
    }
    [type="checkbox"]:disabled + label {
      color: #aaa;
    }
    
    
    
    
    /* Base for label styling */
    [type="radio"]:not(:checked),
    [type="radio"]:checked {
        ;
        left: -9999px;
    }
    
        [type="radio"]:not(:checked) + label,
        [type="radio"]:checked + label {
            ;
            padding-left: 1.95em;
            cursor: pointer;
        }
    
            /* checkbox aspect */
            [type="radio"]:not(:checked) + label:before,
            [type="radio"]:checked + label:before {
                content: '';
                ;
                left: 0;
                top: 0;
                width: 1.25em;
                height: 1.25em;
                border: 2px solid #ccc;
                background: #fff;
                border: 1px solid #512888;
                border-radius: 18px;
                box-shadow: inset 0 1px 3px rgba(0,0,0,.1);
            }
            /* checked mark aspect */
            [type="radio"]:not(:checked) + label:after,
            [type="radio"]:checked + label:after {
                content: "\2022";
                ;
                top: .2em;
                left: .275em;
                font-size: 1.4em;
                line-height: 0.8;
                color: #512888;
                transition: all .2s;
                font-family: Helvetica, Arial, sans-serif;
            }
            /* checked mark aspect changes */
            [type="radio"]:not(:checked) + label:after {
                opacity: 0;
                transform: scale(0);
            }
    
            [type="radio"]:checked + label:after {
                opacity: 1;
                transform: scale(2.1);
                vertical-align:middle;
            }
    /* disabled checkbox */
    [type="radio"]:disabled:not(:checked) + label:before,
    [type="radio"]:disabled:checked + label:before {
        box-shadow: none;
        border-color: #bbb;
        background-color: #ddd;
    }
    
    [type="radio"]:disabled:checked + label:after {
        color: #999;
    }
    
    [type="radio"]:disabled + label {
        color: #aaa;
    }

    Monday, June 4, 2018 4:26 PM

Answers

  • User283571144 posted

    Hi bootzilla,

    According to your description, I suggest you could try to use F12 develop tools to help you easily modify the check box and radio button's css style.

    Notice: I suggest you should pay attention for the browser compatibility issue.

    Besides, I suggest you could consider using some third party library to help you achieve your requirement.

    For example:

    iCheck's radio button and checkbox.

    You could choose the radio button and check box with different style.

    More details, you could refer to below demo site.

    http://icheck.fronteed.com/ 

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 5, 2018 1:10 PM

All replies

  • User283571144 posted

    Hi bootzilla,

    According to your description, I suggest you could try to use F12 develop tools to help you easily modify the check box and radio button's css style.

    Notice: I suggest you should pay attention for the browser compatibility issue.

    Besides, I suggest you could consider using some third party library to help you achieve your requirement.

    For example:

    iCheck's radio button and checkbox.

    You could choose the radio button and check box with different style.

    More details, you could refer to below demo site.

    http://icheck.fronteed.com/ 

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 5, 2018 1:10 PM
  • User1359092739 posted
    1)Checkbox-html
    <div class="control-group">
    <h1>Checkboxes</h1>
    <label class="control control--checkbox">First checkbox
    <input type="checkbox" checked="checked"/>
    <div class="control__indicator"></div>
    </label>
    <label class="control control--checkbox">Second checkbox
    <input type="checkbox"/>
    <div class="control__indicator"></div>
    </label>
    <label class="control control--checkbox">Disabled
    <input type="checkbox" disabled="disabled"/>
    <div class="control__indicator"></div>
    </label>
    <label class="control control--checkbox">Disabled & checked
    <input type="checkbox" disabled="disabled" checked="checked"/>
    <div class="control__indicator"></div>
    </label>
    </div>
    2)radio -html
    <div class="control-group">
    <h1>Radio buttons</h1>
    <label class="control control--radio">First radio
    <input type="radio" name="radio" checked="checked"/>
    <div class="control__indicator"></div>
    </label>
    <label class="control control--radio">Second radio
    <input type="radio" name="radio"/>
    <div class="control__indicator"></div>
    </label>
    <label class="control control--radio">Disabled
    <input type="radio" name="radio2" disabled="disabled"/>
    <div class="control__indicator"></div>
    </label>
    <label class="control control--radio">Disabled & checked
    <input type="radio" name="radio2" disabled="disabled" checked="checked"/>
    <div class="control__indicator"></div>
    </label>
    </div>
    3)css
    body {
    background: #e6e6e6;
    }

    .control-group {
    display: inline-block;
    width: 200px;
    height: 210px;
    margin: 10px;
    padding: 30px;
    text-align: left;
    vertical-align: top;
    background: #fff;
    box-shadow: 0 1px 2px rgba(0,0,0,.1);
    }
    .control {
    font-size: 18px;
    ;
    display: block;
    margin-bottom: 15px;
    padding-left: 30px;
    cursor: pointer;
    }

    .control input {
    ;
    z-index: -1;
    opacity: 0;
    }
    .control__indicator {
    ;
    top: 2px;
    left: 0;
    width: 20px;
    height: 20px;
    background: #e6e6e6;
    }

    .control--radio .control__indicator {
    border-radius: 50%;
    }
    /* Hover and focus states */
    .control:hover input ~ .control__indicator,
    .control input:focus ~ .control__indicator {
    background: #ccc;
    }

    /* Checked state */
    .control input:checked ~ .control__indicator {
    background: #2aa1c0;
    }

    /* Hover state whilst checked */
    .control:hover input:not([disabled]):checked ~ .control__indicator,
    .control input:checked:focus ~ .control__indicator {
    background: #0e647d;
    }

    /* Disabled state */
    .control input:disabled ~ .control__indicator {
    pointer-events: none;
    opacity: .6;
    background: #e6e6e6;
    }

    /* Check mark */
    .control__indicator:after {
    ;
    display: none;
    content: '';
    }

    /* Show check mark */
    .control input:checked ~ .control__indicator:after {
    display: block;
    }

    /* Checkbox tick */
    .control--checkbox .control__indicator:after {
    top: 4px;
    left: 8px;
    width: 3px;
    height: 8px;
    transform: rotate(45deg);
    border: solid #fff;
    border-width: 0 2px 2px 0;
    }

    /* Disabled tick colour */
    .control--checkbox input:disabled ~ .control__indicator:after {
    border-color: #7b7b7b;
    }

    /* Radio button inner circle */
    .control--radio .control__indicator:after {
    top: 7px;
    left: 7px;
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: #fff;
    }

    /* Disabled circle colour */
    .control--radio input:disabled ~ .control__indicator:after {
    background: #7b7b7b;
    }
    Friday, June 8, 2018 11:33 AM