0

I have encountered some problem with my site. I want to add CSS to my button to be something like this which I found on the web. https://codepen.io/AllThingsSmitty/pen/WjZVjo I can't add an ID to it as I can't change the HTML code as it is auto generated by Wordpress. Any help would be appreciated.

<label class='grunion-field-label checkbox'>
    <input type='checkbox' name='g84-iagreetothetermsconditions' value='Yes' class='checkbox' required aria-required='true' />
    I agree to the Terms &amp; Conditions. <span>(required)</span>
</label>
6
  • You could use nth-child() Commented Apr 16, 2019 at 8:29
  • I can't change the HTML code as it is auto generated by Wordpress - why can't you? Commented Apr 16, 2019 at 8:30
  • How is it generated by Wordpress? Are you using a Plugin for that? If yes, which? Commented Apr 16, 2019 at 8:30
  • Can you try editing the css file and add label.checkbox .grunion-field-label{yourCss} ? (same idea for the rest of the classes) Commented Apr 16, 2019 at 8:32
  • You can't use that anyway as your input is inside your label - you need the input to be before the label if you want to use the styles in the codepen Commented Apr 16, 2019 at 8:33

2 Answers 2

1

With your current DOM structure you go with following snippet:

.grunion-field-label {
  position: relative;
  display:inline-block;
  padding-left:10px;
  line-height:30px;
}

.grunion-field-label::before {
  background-color: #fff;
  border: 1px solid #ccc;
  border-radius: 50%;
  cursor: pointer;
  height: 28px;
  left: 0;
  position: absolute;
  top: 0;
  width: 28px;
  content:"";
}

.grunion-field-label input[type="checkbox"] + span::after {
  border: 2px solid #fff;
  border-top: none;
  border-right: none;
  content: "";
  height: 6px;
  left: 7px;
  opacity: 0;
  position: absolute;
  top: 8px;
  transform: rotate(-45deg);
  width: 12px;
}

.grunion-field-label input[type="checkbox"] {
  visibility: hidden;
}

.grunion-field-label input[type="checkbox"]:checked + span::after {
  border-color: #f00;
  opacity: 1;
}
<label class='grunion-field-label checkbox'>
    <input type='checkbox' name='g84-iagreetothetermsconditions' value='Yes' class='checkbox' required aria-required='true' />
    I agree to the Terms &amp; Conditions. <span>(required)</span>
</label>

Sign up to request clarification or add additional context in comments.

Comments

0

You may add external css to re-style the checkbox and also you may need to get some help from javascript.

(function($) {

  $('input[type="checkbox"]').on('click', function() {
    if ($(this).is(':checked')) {
      $(this).closest('label').addClass('checked');
    } else {
      $(this).closest('label').removeClass('checked');
    }
  });

})(jQuery);
.grunion-field-label.checkbox {
  position: relative;
  padding-left: 24px;
}
.grunion-field-label.checkbox input[type="checkbox"] {
  display: none;
}
.grunion-field-label.checkbox:before {
  content: " ";
  position: absolute;
  left: 0;
  width: 20px;
  height: 20px;
  border-radius: 10px;
  background-color: #fff;
  border: 1px solid #ddd;
}
.grunion-field-label.checkbox.checked:after {
  content: " ";
  position: absolute;
  top: 4px;
  left: 8px;
  border: 2px solid #fff;
  border-left: none;
  border-top: none;
  width: 4px;
  height: 8px;
  transform: rotate(45deg);
}

.grunion-field-label.checkbox.checked:before {
  background-color: #0c0;
  border-color: #0c0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label class='grunion-field-label checkbox'>
    <input type='checkbox' name='g84-iagreetothetermsconditions' value='Yes' class='checkbox' required aria-required='true' />
    I agree to the Terms &amp; Conditions. <span>(required)</span>
</label>

3 Comments

Thanks for the information. However, I got this too high. Anyway to solve? i.imgur.com/d3gsjb6.jpg
Try to adjust the value of top in both before and after pseudo elements.
Works great! Thanks, do you know how to edit the font weight of "I agree to the Terms &amp; Conditions."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.