How to style a checkbox using CSS ?
Last Updated :
04 Sep, 2024
Styling a checkbox using CSS involves customizing its appearance beyond the default browser styles. This can include changing the size, color, background, and border, and adding custom icons or animations. Techniques often involve hiding the default checkbox and styling a label or pseudo-elements for customization.
Using Pseudo-elements
To style checkboxes using pseudo-elements, target the input[type="checkbox"] element and apply styles to ::before or ::after pseudo-elements. This technique allows for custom designs using CSS properties like content, background, border, and size.
Example: In the example, a container div wraps content. Checkboxes are hidden with custom styles using pseudo-elements for various states. A span wraps each checkbox for custom styling, with a checkmark created using ::after.
html
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
display: flex;
justify-content: center;
}
h1 {
color: green;
}
.root{
padding: 20px;
}
.main {
display: block;
position: relative;
padding-left: 45px;
margin-bottom: 15px;
cursor: pointer;
font-size: 20px;
}
.main input[type=checkbox] {
visibility: hidden;
}
.checkbox-container {
position: absolute;
top: 0;
left: 0;
height: 25px;
width: 25px;
background-color: transparent;
border: 2px solid #000;
}
/* Hover effect */
.main:hover input~.checkbox-container {
background-color: yellow;
}
/* Active effect */
.main input:active~.checkbox-container {
background-color: red;
}
/* Checked effect */
.main input:checked~.checkbox-container {
background-color: green;
}
/* Checkmark */
.checkbox-container::after {
content: "";
position: absolute;
display: none;
left: 7px;
top: 3px;
width: 6px;
height: 12px;
border: solid white;
border-width: 0 2px 2px 0;
transform: rotate(45deg);
}
/* Display checkmark when checked */
.main input:checked~.checkbox-container::after {
display: block;
}
</style>
</head>
<body>
<div class="root">
<h1>Best Computer Science Platform</h1>
<label class="main">CodeX
<input type="checkbox">
<span class="checkbox-container"></span>
</label>
<label class="main">GeeksforGeeks
<input type="checkbox" checked="checked">
<span class="checkbox-container"></span>
</label>
<label class="main">CodeY
<input type="checkbox">
<span class="checkbox-container"></span>
</label>
</div>
</body>
</html>
Output:
Using Pseudo-elementsUsing Custom Design with CSS
In this approach, custom checkbox designs are created using CSS. Default checkboxes are hidden, and pseudo-elements are used to create custom checkbox styles, including hover, active, and checked states, enhancing the visual appearance and user interaction.
Example: In this example, checkboxes are styled with custom CSS to replace default appearance. Hovering or clicking changes colors, checked boxes show a green background, and a pseudo-element checkmark appears when selected.
html
<!DOCTYPE html>
<html>
<head>
<title>
Style a checkbox using CSS
</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
}
h1 {
color: green;
}
.script {
display: block;
position: relative;
padding-left: 45px;
margin-bottom: 15px;
cursor: pointer;
font-size: 20px;
}
.script input[type=checkbox] {
visibility: hidden;
}
.geekmark {
position: absolute;
top: 0;
left: 0;
height: 25px;
width: 25px;
background-color: green;
}
.script:hover input~.geekmark {
background-color: yellow;
}
.script input:active~.geekmark {
background-color: red;
}
.script input:checked~.geekmark {
background-color: green;
}
.geekmark:after {
content: "";
position: absolute;
display: none;
}
.script input:checked~.geekmark:after {
display: block;
}
.script .geekmark:after {
left: 6px;
bottom: 5px;
width: 6px;
height: 6px;
border: solid white;
border-width: 4px 4px 4px 4px;
}
</style>
</head>
<body>
<h1>Is GeeksforGeeks Useful?</h1>
<label class="script">
Yes
<input type="checkbox">
<span class="geekmark"></span>
</label>
<label class="script">
Absolutely Yes
<input type="checkbox" checked="checked">
<span class="geekmark"></span>
</label>
</body>
</html>
Output:

Similar Reads
How to Blur an Image using CSS? In CSS, the filter property is used to apply visual effects to images, such as blurring, grayscale, brightness adjustments, and more. One common use of the filter property is to apply a blur effect to an image, giving it a soft, out-of-focus appearance.Syntax:filter: blur(radius);radius: Defines the
2 min read
How to make an image center-aligned (vertically & horizontally) inside a bigger div using CSS ? We will know how to center-align the image in a div tag using CSS & will also understand its implementation through the example. Given an image, we need to set the image that align to the center (vertically and horizontally) inside a bigger div. But first let's create a basic structure, in which
2 min read
What does the CSS rule âclear: bothâ do? The clear property is used to specify which side of floating elements are not allowed to float. It sets or returns the position of the element about floating the objects. The "clear: both" means floating the elements are not allowed to float on both sides. It is used when no need for any element to
1 min read
How to Break Line Without using <br> Tag in HTML / CSS? Breaking lines in HTML without <br> tags can be achieved using block-level elements such as <div> or <p> combined with CSS properties like display: block; or display: inline-block; and others. These elements naturally wrap text or content, facilitating clean and structured layout d
2 min read
Set the size of background image using CSS ? Setting the size of a background image using CSS allows you to control how the image fits within an element. By using the background-size property, you can specify the width and height, ensuring the image scales appropriately for responsive design. Syntax: background-size: width height;Note: If the
2 min read
What is Greater-than Sign (>) Selector in CSS? In CSS, the greater than sign (>) is known as the child combinator selector. It is used to style elements that have a specific parent. Unlike other selectors, it only targets direct children of an element, meaning it only looks one level down in the HTML structure.How the Child Combinator Selecto
2 min read
How to Select all Child Elements Recursively using CSS? Here are three methods to achieve to Select all Child Elements Recursively using CSS:1. Using the Universal Selector (*)The universal selector applies styles to all child elements within a parent recursively.HTML<html> <head> <style> .parent * { color: blue; font-weight: bold; }
3 min read
How to style a checkbox using CSS ? Styling a checkbox using CSS involves customizing its appearance beyond the default browser styles. This can include changing the size, color, background, and border, and adding custom icons or animations. Techniques often involve hiding the default checkbox and styling a label or pseudo-elements fo
3 min read
CSS word-break vs word-wrap (Overflow Wrap) : What's the Difference ? In CSS, controlling how long words break at line endings is crucial for clean layouts and good readability. Two properties often confused are word-break and overflow-wrap also known as word-wrap. This guide compares how they handle text overflow and when to use each.word-break Vs. word-wrapPropertyw
3 min read
How to make div height expand with its content using CSS? When designing a webpage, it's important to make sure a div adjusts its height based on the content inside it. This is key to creating a flexible and responsive layout, especially when the content changes or is unpredictable. Using CSS, you can easily make a div adapt to different content sizes. One
3 min read