Css checkbox background color

How to Style a Checkbox with CSS

A checkbox is one of the HTML forms used on every website, but mostly they are not styled and look the same. If you want to make your site more attractive, you can style the checkboxes. If you don’t know how to do that, let’s create a sample together, step by step using only CSS.

Create HTML

html> html> head> title>Title of the document title> head> body> section> h2>How to style checkbox h2> div class="checkbox-example"> input type="checkbox" value="1" id="checkboxOneInput"/> label for="checkboxOneInput"> label> div> section> body> html>

Add CSS

  • Hide the checkboxes by setting the visibility property to its “hidden” value.
  • Use the :checked pseudo-class, which helps to see when the checkbox is checked.
  • Style the label with the width, height, background, margin, and border-radius properties. Set the position to «relative».
  • Style the «checkbox-example» class by setting the display to «block» and specifying the width and height properties. Then, specify the border-radius , transition, position, and other properties.
  • Now the slider is in the unchecked position. To move the slider button, we need to know whether the checkbox is checked. When it is checked, we must change the left property of the label element. We use the + combinator to select the label sibling, just next to it.
/* Start by hiding the checkboxes*/ input[type=checkbox] < visibility: hidden; > input[type="checkbox"]:checked < some code goes here > /* the slider bar */ .checkbox-example < width: 45px; height: 15px; background: #555; margin: 20px 80px; position: relative; border-radius: 5px; > /* the slider from the label */ .checkbox-example label < display: block; width: 18px; height: 18px; border-radius: 50%; transition: all .5s ease; cursor: pointer; position: absolute; top: -2px; left: -3px; background: #ccc; > /* Move the slider if the checkbox is clicked */ .checkbox-example input[type=checkbox]:checked+label < left: 27px; >

Now we will combine all these in one code and will have our checkbox ready.

Читайте также:  What is java and its advantages

Example of styling a checkbox with CSS:

html> html> head> title>Title of the document title> style> input[type=checkbox] < visibility: hidden; > .checkbox-example < width: 45px; height: 15px; background: #555; margin: 20px 10px; position: relative; border-radius: 5px; > .checkbox-example label < display: block; width: 18px; height: 18px; border-radius: 50%; transition: all .5s ease; cursor: pointer; position: absolute; top: -2px; left: -3px; background: #ccc; > .checkbox-example input[type=checkbox]:checked + label < left: 27px; > style> head> body> h2>How to style checkbox h2> section> div class="checkbox-example"> input type="checkbox" value="1" id="checkboxOneInput" /> label for="checkboxOneInput"> label> div> section> body> html>

Result

We can also use the CSS :after and :before pseudo selectors to add ticks or crosses or any other supplementary UI.

Example of using a styled checkbox:

html> html> head> style> .main < display: block; position: relative; padding-left: 45px; margin-bottom: 15px; cursor: pointer; font-size: 20px; > h1 < color: orange; > /* Hiding the initial checkbox */ input[type=checkbox] < visibility: hidden; > /* Creating a custom checkbox based on demand */ .w3docs < position: absolute; top: 0; left: 0; height: 25px; width: 25px; background-color: black; > /* Specify the background color to be shown when hovering over checkbox */ .main:hover input ~ .w3docs < background-color: gray; > /* Specify the background color to be shown when checkbox is active */ .main input:active ~ .w3docs < background-color: white; > /* Specify the background color to be shown when checkbox is checked */ .main input:checked ~ .w3docs < background-color: orange; > /* Checkmark to be shown in checkbox */ /* It is not be shown when not checked */ .w3docs:after < content: ""; position: absolute; display: none; > /* Display checkmark when checked */ .main input:checked ~ .w3docs:after < display: block; > /* Styling the checkmark using webkit */ /* Rotated the rectangle by 45 degree and showing only two border to make it look like a tickmark */ .main .w3docs:after < left: 8px; bottom: 5px; width: 6px; height: 12px; border: solid white; border-width: 0 4px 4px 0; -webkit-transform: rotate(45deg); -ms-transform: rotate(45deg); transform: rotate(45deg); > style> head> body> h1> Learn to design and build professional website h1> label class="main">CodeX input type="checkbox"> span class="w3docs"> span> label> label class="main">W3Docs input type="checkbox" checked="checked"> span class="w3docs"> span> label> label class="main">CodeY input type="checkbox"> span class="w3docs"> span> label> body> html>

Example of styling a checkbox using CSS properties:

html> html> head> title>Style a checkbox using CSS title> style> h1 < color: #8ebf42; > .script < display: block; position: relative; padding-left: 45px; margin-bottom: 15px; cursor: pointer; font-size: 20px; > /* Hide the default checkbox */ input[type=checkbox] < visibility: hidden; > /* creating a custom checkbox based on demand */ .w3docs < position: absolute; top: 0; left: 0; height: 25px; width: 25px; background-color: black; > /* specify the background color to be shown when hovering over checkbox */ .script:hover input ~ .w3docs < background-color: orange; > /* specify the background color to be shown when checkbox is active */ .script input:active ~ .w3docs < background-color: red; > /* specify the background color to be shown when checkbox is checked */ .script input:checked ~ .w3docs < background-color: black; > /* checkmark to be shown in checkbox */ /* It is not be shown when not checked */ .w3docs:after < content: ""; position: absolute; display: none; > /* display checkmark when checked */ .script input:checked ~ .w3docs:after < display: block; > /* styling the checkmark using webkit */ /* creating a square to be the sign of checkmark */ .script .w3docs:after < left: 6px; bottom: 5px; width: 6px; height: 6px; border: solid white; border-width: 4px 4px 4px 4px; > style> head> body> h1>Do you like W3Docs? h1> label class="script" style="color:black;"> Yes input type="checkbox"> span class="w3docs"> span> label> label class="script" style="color:black;"> Yes Of course input type="checkbox" checked="checked"> span class="w3docs"> span> label> body> html>

Источник

Читайте также:  Как питоны едят курицу

Checkbox CSS Styling a checkbox with only CSS

This seems like an obvious statement; styling should be done with CSS. But unfortunately in the case of checkboxes, there’s basically no support for directly styling the checkbox. Because of that, I’ve seen several JavaScript implementations to change between displaying an unchecked box and a checked box based on state in order to fake a styled checkbox. But let’s not go so quickly to JS to solve a fundamentally styling problem! It can be done with just CSS, we just need to shift our thinking a little.

The goals

  1. Style the checkbox
  2. Style the label
  3. Require no additional DOM nodes or configuration (including icons/images)
  4. Require 0 JavaScript

The results

The strategy

Since we cannot style the checkbox input directly with something like checkbox-style: awesome; , we’ll need to take a different approach:

  1. Hide the actual checkbox input
  2. Show a styled element that looks like an empty checkbox when the input is unchecked
  3. Show a styled element that looks like a checked checkbox when the input is checked

How to get there

The CSS selectors used

  1. Type selector type — selects all elements of the given type (e.g. input will select all nodes)
  2. Attribute selector [attribute=»value»] — selects an element with attribute where its value is exactly value
  3. Psuedo-class :checked — selects checkbox/radio input types or option s in a select that are selected/checked/on/active
  4. Psuedo-element ::before — styleable element that doesn’t actually exist in the DOM; considered the first child of the selected element
  5. Universal selector * — selects anything/everything
  6. Child combinator > — combines two selectors; narrowing the selection on the right-hand side to only those elements that are direct descendants of elements selected by the left-hand side.
  7. Adjacent sibling combinator + — combines two selectors; narrowing the selection on the right-hand side to only those elements that are the siblings immediately after the elements on the left-hand side
Читайте также:  Javascript form serialize object

Important CSS styles used

  1. content — used in the ::before psuedo-element to set its content
  2. display — specifically none to hide elements and inline-block to make our otherwise inline checkbox able to have a consistent width and height
  3. width / height — does what you think: sets width and height of the element
  4. color — sets the color of the text
  5. text-align / vertical-align — used for adjusting the position of our check/checkbox to its label
  6. border styles — How we’ll form and color the checkbox
  7. background — sets the background color (used to fill in the checkbox when it is checked)

Starting out: the HTML

Let’s set up our checkbox as a child of its label element with a sibling of a span of the label text:

  type="checkbox" name="key" value="value" /> I am a checkbox  

This structure allows clicking on the label text ( I am a checkbox ) to toggle the checkbox without needing for or unique id attributes. Placing the text in a span directly after the input will allow us to select it in CSS.

First step: hide the unstyleable checkbox

Going back to our strategy: since we can’t do anything with the native checkbox, we’ll have to hide it and do our own thing.

label > input[type="checkbox"]  display: none; > 

We’ll select the checkbox ( input[type=»checkbox»] ) and make sure it’s labelled the way we need it to be ( label > ). Then just display: none to get it off our screens.

Second step: make our own checkbox

Making an empty square is easy with CSS, just put a border around an element with no content and set its width and height. Let’s also put it on the ::before psuedo-element to avoid adding unnecessary HTML.

label > input[type="checkbox"] + *::before  content: ""; display: inline-block; vertical-align: bottom; width: 1rem; height: 1rem; border-radius: 10%; border-style: solid; border-width: 0.1rem; border-color: gray; > 

Building from the selection in the previous section; we add + * to select any element as long as it is the direct subsequent sibling to the checkbox of interest and then ::before to select the said psuedo-element for making the box.

Last step: make our checkbox change when checked

So far, even though the checkbox has been working, it doesn’t look like it’s working. Let’s change that by making the checkbox checked when it is :checked . Also, for the proof-of-concept, let’s change styles on the label text itself (while I’ll only change the text color, you can imagine changing any style to build any sort of check/uncheck select/unselect interface).

label > input[type="checkbox"]:checked + *::before  content: "✓"; color: white; text-align: center; background: teal; border-color: teal; > label > input[type="checkbox"]:checked + *  color: teal; > 

The important part here being :checked is placed after the input[type=»checkbox»] (since that is the element that is checked or not). And fortunately for us, there’s a checkmark character that we can set as the content of our checkbox: ✓ .

Bonus points: extend this to radio buttons

As a challenge to you, reader, this same strategy applies to making a styled radio button. Using this as a guide, are you able to transform this for the type=»radio» input? (or just follow me as that will be the next installment in this series)

Wrapping up and side notes

Thanks for reading! I like sharing and finding things like this where common web design patterns can be done without a massive JavaScript library or framework bogging the page down. Give me some suggestions below on patterns you’d like to see me break-down in CSS/HTML-only for this series.

While this example had «no unnecessary DOM»; it is also perfectly valid to include an additional span (or two) to hold svg/font-awesome icons for more precise/exotic checkbox designs. Then :checked should be used to alternatively display: none and display: inline-block the icons and + will need to become ~ to select all the siblings.

Источник

Оцените статью