×

CSS Checkbox style

CSS Checkbox style: It is an element of HTML, which is used to get input from various users. It is difficult to style any checkbox. However, some elements make it simple to design the checkbox, i.e., pseudo-elements.

Generally, these HTML elements are used on all the websites. However, without applying any style, these elements look the same on all the websites. Thus, styling them can create our website more attractive and different. We must hide the real checkbox to design the checkbox. It is a creative and interesting to style various checkboxes with the use of CSS. It will give a beautiful and a new view of any default checkbox.

Let’s consider some examples to clearly understand the checkbox stylish concept:

Example 1:

In the following example, we will use the '~’ symbol which is a sibling combinator. It selects every element that can be preceded by any previous selector. We should also apply a pseudo-class :hover for styling a checkbox if any user points the mouse over it.

<!DOCTYPE html> 
<html> 
<style> 
.container
{ 
  display: block; 
  position: relative; 
  padding-left: 35px; 
  margin-bottom: 20px; 
  cursor: pointer; 
  font-size: 25px; 
} 
/* Hide the default checkbox */ 
.container input
{ 
  visibility: hidden; 
  cursor: pointer; 
} 
/* Create a custom checkbox */ 
.mark
{ 
  position: absolute; 
  top: 0; 
  left: 0; 
  height: 25px; 
  width: 25px; 
  background-color: lightgray; 
} 
.container:hover input ~ .mark
{ 
  background-color: gray; 
} 
.container input:checked ~ .mark
{ 
  background-color: blue; 
} 
/* Create the mark/indicator (hidden when not checked) */ 
.mark:after
{ 
  content: ""; 
  position: absolute; 
  display: none; 
} 
/* Show the mark when checked */ 
.container input:checked ~ .mark:after
{ 
  display: block; 
} 
/* Style the mark/indicator */ 
.container .mark:after
{ 
  left: 9px; 
  top: 5px; 
  width: 5px; 
  height: 10px; 
  border: solid white; 
  border-width: 0 3px 3px 0; 
  transform: rotate(45deg); 
} 
</style> 
<body> 
<h1>Qualification</h1> 
<label class="container">M.tech 
  <input type="checkbox"> 
  <span class="mark"></span> 
</label> 
<label class="container">B.tech
  <input type="checkbox"> 
  <span class="mark"></span> 
</label> 
<label class="container">12th 
  <input type="checkbox"> 
  <span class="mark"></span> 
</label> 
<label class="container">10th 
  <input type="checkbox" checked="check"> 
  <span class="mark"></span> 
</label> 
</body> 
</html> 

Output:

CSS Checkbox style

Example 2:

In the following example, we will see a modified checkmark.

<!DOCTYPE html> 
<html> 
<style> 
.container
{ 
  display: block; 
  position: relative; 
  padding-left: 45px; 
  margin-bottom: 15px; 
  cursor: pointer; 
  font-size: 20px; 
} 
/* Hide the real checkbox */ 
input[type= checkbox]
{ 
  visibility: hidden; 
} 
/* Create a custom checkbox */ 
.mark
{ 
  position: absolute; 
  top: 0; 
  left: 0; 
  height: 25px; 
  width: 25px; 
  background-color: lightgray; 
} 
/*background color to be displayed if the checkbox is checked*/
.container input:checked ~ .mark
{ 
  background-color: blue; 
}  
/*checkmark to be displayed in the checkbox*/ 
/*It is bot displayed if unchecked*/
.mark:after
{ 
  content: ""; 
  position: absolute; 
  display: none; 
} 
/*display the checkmark when checked*/ 
.container input:checked ~ .mark:after
{ 
  display: block; 
} 
/*creating a square to be the sign of checkbox*/ 
.container .mark:after
{ 
  left: 6px; 
  bottom: 6px; 
  width: 6px; 
  height: 6px; 
  border: solid white; 
  border-width: 4px 4px 4px 4px; 
} 
</style> 
<body> 
<h1>Qualification</h1> 
<label class="container">M.tech 
  <input type="checkbox"> 
  <span class="mark"></span> 
</label> 
<label class="container">B.tech
  <input type="checkbox"> 
  <span class="mark"></span> 
</label>
<label class="container">12th 
  <input type="checkbox"> 
  <span class="mark"></span> 
</label> 
<label class="container">10th 
  <input type="checkbox" checked="check"> 
  <span class="mark"></span> 
</label> 
</body> 
</html> 

Output:

CSS Checkbox style

Example 3:                  

We will see another illustration of checkbox styling. In the following example, we feel a ripple consequence, which makes any checkbox more interesting. It will provide us a unique view to our checkbox. We are using the ‘~’ symbol which is a sibling combinator, similar as above examples. Every element will be selected by using this symbol preceded by a former selector. We can also apply some of the pseudo-classes like :checked, :before, :after, etc.

With the use of CSS, we are creating a ripple effect inside a checkbox as follows:

<!DOCTYPE html>
<html>
<head>
<style>
body
{
 text-align: center;
}
.check
{
 width: 500px;
 margin: 50px auto;
 clear: both;
 display: block;
 background-color: #009BFF;
 border-radius: 4px;
}
.check::after
{
 clear: both;
 display: block;
 content: "";
}
.check .checkbox-container
{
 float: left;
 width: 50%;
 box-sizing: border-box;
 text-align:center;
 padding: 40px 0px;
}
/* Styling Checkbox Starts */
.checkbox-label
{
 color: white;
 display: block;
 position: relative;
 margin: auto;
 cursor: pointer;
 font-size: 22px;
 line-height: 24px;
 height: 50px;
 width: 24px;
 clear: both;
}
.checkbox-label input
{
 position: absolute;
 opacity: 0;
 cursor: pointer;
}
.checkbox-label .mark
{
 top:30px;
 position: absolute;
 height: 24px;
 width: 24px;
 background-color: transparent;
 border-radius: 5px;
 transition: all 0.3s ease-in;
 border: 2px solid white;
}
.checkbox-label input:checked ~ .mark
{
 background-color: white;
 border-radius: 5px;
 transform: rotate(0deg) scale(1);
 opacity:1;
 border: 2px solid white;
}
.checkbox-label .mark::after
{
 position: absolute;
 content: "";
 border-radius: 5px;
}
.checkbox-label input:checked ~ .mark::after
{
 transform: rotate(45deg) scale(1);
 left: 8px;
 top: 3px;
 width: 6px;
 height: 12px;
 border: solid red;
 border-width: 0 2px 2px 0;
 border-radius: 0;
}
/* For Ripple Effect */
.checkbox-label .mark::before
{
 position: absolute;
 content: "";
 border-radius: 10px;
 border: 5px solid yellow;
 transform: scale(0);   
}
.checkbox-label input:checked ~ .mark::before
{
 left: -3px;
 top: -3px;
 width: 24px;
 height: 24px;
 border-radius: 5px;
 transform: scale(3);
 opacity:0;  
 transition: all 0.3s ease-out;
}
</style>
</head>
<body>
<h1>Qualification</h1>
<div class="check">
<div class="checkbox-container">
<label class="checkbox-label">M.tech
  <input type="checkbox">
  <span class="mark"></span>
</label>
</div>
<div class="checkbox-container">
<label class="checkbox-label">B.tech
  <input type="checkbox">
  <span class="mark"></span>
</label>
</div>
<div class="checkbox-container">
<label class="checkbox-label">12th
  <input type="checkbox">
  <span class="mark"></span>
</label>
</div>
<div class="checkbox-container">
<label class="checkbox-label">10th
  <input type="checkbox" checked="check">
  <span class="mark"></span>
</label>
</div>
</div>
</body>
</html>

Output:

CSS Checkbox style

Related Topics

CSS Color Keywords

Here, we will discuss the following keywords: transparent currentcolor Inherit Transparent keyword A color can generally be made transparent by using the transparent keyword. Often it is used to for all intents...

4 minutes read.

How to center a button in CSS

How to center a button in CSS? In the tutorial, we will learn how to center a button in CSS. CSS (Cascading Style Sheets) gives an HTML web page the greatest possible...

4 minutes read.

CSS Tutorial

CSS refers to the Cascading Style Sheet. CSS is a form of a programming language. In other words, CSS is a language of the style sheet that is utilised for...

9 minutes read.

CSS Functions

CSS functions are used for different CSS properties. Every front-end developer should have knowledge of all CSS functions. It makes the coding easier. FunctionsExplanationattr().It returns the value of an attribute of...

5 minutes read.

CSS Word Wrap

Word Wrap in CSS: The word-wrap property in CSS is referred to break any lengthy word and wrap towards the next or following line. It is employed for preventing overflow...

1 minute read.

CSS Pseudo-elements

Pseudo-elements The pseudo-classes in CSS can be represented as any keyword which is connected to the selector that represents any selected element’s unique state. On the other hand, pseudo-elements can be...

6 minutes read.

What is Bootstrap CSS?

Bootstrap: - Bootstrap is a CSS framework that is free to use and is used for the front-end development of the website in a faster and easier way. Bootstrap was released as...

3 minutes read.

How to change font size in CSS

How to change font size in CSS? Font Size: - Users may attract attention to content on a website page in various ways. One element in a sentence can be highlighted. You...

5 minutes read.

CSS Image Gallery

Image galleries are used to display and store collections of images. Example Let’s discuss an example. CSS was used to create the following image gallery: Code is as follows: <html> <head> <style> div.gallery {   margin: 6px;   border: 1px...

5 minutes read.

How to change the font in CSS

How to change the font in CSS? Professionals can't deny typefaces — how users organize and style text — if we want to completely customize the website's design. One may wish...

4 minutes read.

CSS Superscript and Subscript

Superscript and Subscript To specify the superscript and the subscript text, HTML provides us two tags that are <sup> and <sub>. The superscript content looks in the shorter font and half...

2 minutes read.

Radial-gradient() CSS function

Radial-gradient() CSS function It defines a CSS in-built function, which produces a smooth transition among multiple colors. It is used to set any radial-gradient that is same as a background-image. This...

4 minutes read.

How to resize an image in CSS

How to resize an image in CSS? The resize image attribute is used in responsive web design to resize images to fit within the div container automatically. The resize image attribute...

3 minutes read.

How to make smooth bounce animation using CSS

The smooth bouncing animation project is done with the help of HTML and CSS. This project gives so much fun and excitement to the users. What is HTML? The HTML or HyperText...

2 minutes read.

CSS Specificity

CSS Specificity: If one or more conflicting rules of CSS express a similar element, a browser will pursue a few rules to examine the specific one. Specificity can be specified...

3 minutes read.

CSS Box-shadow

CSS Box-shadow This property is referred to include effects as shadow-like around an element’s frame. Syntax box-shadow: h-offset v-offset blur spread color |inset|inherit|none; Property values Following are the various property values which can be used...

3 minutes read.

CSS Units

CSS Units In CSS, there are so many units available to show the length’s measurement. A single unit of CSS is applied to examine the size of the property, which the...

5 minutes read.

Create a 3D text effect using HTML and CSS

A3D text effect is a beautiful and attractive part of a webpage. As a web page developer, it is crucial to know how to create a 3D text effect with...

3 minutes read.

CSS Images

Images in CSS: Images are a critical part of a web application. It includes various types of images in the web application that cannot be recommended. However, it is essential...

4 minutes read.

CSS Content Property

Content Property The content attribute produces dynamic content. It can be applied with any pseudo-element ::after and ::before. These CSS properties are entirely supported inside every browser and applied to include...

5 minutes read.