×

CSS Radio Button

Radio Button

A radio button has been defined as an element of HTML. It can support us to take any input from the user. But it is challenging to design a radio button. The pseudo-components make it efficient to create the various radio buttons. These buttons are used when it is required to select some items from multiple items.

These HTML elements are generally applied in almost every website, although without designing them, they appear the same on all the sites. Thus, designing them will create our websites more attractive and different. Styling various radio buttons with the use of CSS will be a creative and exciting task that will give a new appearance to the radio button (default).

To design a radio button (custom), we need to address the CSS.

We need to address the HTML markup to create it.

Designing of any radio button can be precise with the use of some examples. Let’s take some examples regarding the same.

Example:

In the following illustration, we use the ‘~’ sign, which is a sibling combinator. This sign will select every element that is anticipated by any previous selector. Also, we have applied pseudo-class: hover to design any radio button if the user points the cursor on it.

<!DOCTYPE html> 
<html> 
<style> 
.container
{ 
 display: block; 
 position: relative; 
 padding-left: 40px; 
 margin-bottom: 20px; 
 cursor: pointer; 
 font-size: 25px; 
} 
/* Hide the radio button (default) */ 
.container input
{ 
 position: absolute; 
 opacity: 0; 
 cursor: pointer; 
} 
/* radio button (custom) */ 
.check
{ 
 position: absolute; 
 top: 0; 
 left: 0; 
 height: 30px; 
 width: 30px; 
 background-color: lightgray; 
 border-radius: 50%; 
} 
.container:hover input ~ .check
{ 
 background-color: gray; 
} 
.container input:checked ~ .check
{ 
 background-color: blue; 
} 
.check:after
{ 
 content: ""; 
 position: absolute; 
 display: none; 
} 
.container input:checked ~ .check:after
{ 
 display: block; 
} 
.container .check:after
{ 
 top: 8px; 
 left: 8px; 
 width: 15px; 
 height: 15px; 
 border-radius: 50%; 
 background: white; 
} 
</style> 
<body> 
<h1> Example of Radio Buttons (Custom)</h1> 
<h2> Choose your category </h2> 
<label class="container">GEN 
  <input type="radio" name="radio" checked> 
  <span class="check"></span> 
</label> 
<label class="container">OBC 
  <input type="radio" name="radio"> 
  <span class="check"></span> 
</label> 
<label class="container">SC 
  <input type="radio" name="radio"> 
  <span class="check"></span> 
</label> 
<label class="container">ST 
  <input type="radio" name="radio"> 
  <span class="check"></span> 
</label> 
</body> 
</html> 

Output:

CSS Radio Button

To explain it more efficiently, we will specify another example of designing any radio button. Creating any radio button with the use of CSS gives a professional appearance to any website.

Example:

<!DOCTYPE html>  
<html>  
<head>  
<style>  
.container
{ 
 float: left; 
 margin: 10px; 
} 
.right
{ 
 float: left; 
 margin: 10px; 
 color: blue; 
 font-size: 20px; 
 font-weight:bold; 
} 
.radio
{ 
 width: 20px; 
 position: relative; 
} 
.radio label
{ 
 width: 20px; 
 height: 20px; 
 cursor: pointer; 
 position: absolute; 
 top: 0; 
 left: 0; 
 background: white; 
 border-radius: 50px; 
 box-shadow: inset 0px 1px 1px white, 3px 3px 9px rgba(0,0,0,0.5); 
 border: 1px solid black; 
} 
.radio label:after
{ 
 content: ''; 
 position: absolute; 
 top: 4px; 
 left: 4px; 
 border: 6px solid blue; 
 border-radius: 50px; 
 opacity: 0; 
} 
.radio label:hover::after
{ 
 opacity: 0.3; 
} 
.radio input[type=radio]
{ 
 visibility: hidden; 
} 
.radio input[type=radio]:checked + label:after
{ 
 opacity: 1; 
}   
</style>  
</head>  
<body>  
<h1> Example of Radio Buttons of CSS </h1>  
<h2> Click any following radio buttons to look the effect </h2> 
<div class="container"> 
<div class="radio"> 
<input type="radio" value="Male" name='gender' id='male' /> 
<label for="male"></label> 
</div> 
</div> 
<div class="right">Male</div> 
<div class="container"> 
<div class="radio"> 
<input type="radio" value="Female" name='gender' id='female' /> 
<label for="female"></label> 
</div> 
</div> 
<div class="right">Female</div> 
</body>  
</html> 

Output:

CSS Radio Button

Related Topics

CSS Flex-Box

Introduction The flex-box property of CSS stands for Flexible Box Layout Module. It is specifically used to design a flexible responsive layout. Flex-box is a one dimensional layout model, i.e. it works...

6 minutes read.

CSS Font

CSS Font: CSS has font property, which makes the text more attractive. We can alter the entire look by changing font color, font size, font style, and much more. In...

5 minutes read.

CSS 2d Transform

Transform property in CSS is used mainly for enhancing the effects and to animate the elements. Transform is an effect that changes the shape, size and position of an element. It helps...

6 minutes read.

CSS Reference

The CSS Reference lists all of the basic standard CSS properties, pseudo-classes, pseudo-elements, and data types, for all intents and purposes functional notations, and at-rules alphabetically.  The site also includes...

5 minutes read.

CSS Outline

CSS Outline: This CSS property is quite similar to the border property of CSS. It eases us to design an additional border around the element to obtain visual attention. The...

4 minutes read.

CSS z-index Property

CSS z-index: CSS z-index permits us to represent any visual hierarchy over a 3D plane that is the z-axis. This index can be applied to describe the positioned element’s stacking...

2 minutes read.

Grid Vs Flexbox in CSS

Grid: - CSS Grid Layout is a 2D grid-based matrix-based layout method with columns and rows that simplifies web pages created by eliminating the need for floats and placement. A grid layout, like...

4 minutes read.

CSS Important

CSS Important: CSS !important property is used to provide more importance, as compared to other CSS properties. The meaning of this property is “This is important.” This property enables CSS...

2 minutes read.

What is a CSS File?

What is a CSS File and How to use it in Html? CSS: - CSS stands for Cascading Style Sheet. CSS is a file of style sheets that describe the presentation of...

4 minutes read.

CSS translate() Function

Translate() CSS function This function in CSS is a CSS in-built function. It relocates the element in the horizontal direction or in the vertical direction. It can move the items from...

2 minutes read.

CSS Justify Content

Justify Content: These properties are applied for the alignment of items of a flexible box container if any item does not cover every available space over the main-axis, i.e., horizontally....

4 minutes read.

CSS Font-Size

A Cascading Style Sheet (CSS) is a simple design scripting language intended to simplify the process of making web pages more visually appealing with the kind of high-level formatting. Cascading...

3 minutes 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.

CSS Syntax

The CSS syntax contains the selector and the declaration block, which will be clearer with the following example: Selector: A CSS selector specifies the HTML tag we wish to style. A selector can be...

2 minutes read.

Divi in CSS

Divi is popular WordPress themes due to its versatility and user-friendly interface. It includes a built-in page builder for sophisticated design options and an assortment of customization features to help...

9 minutes read.

CSS Word-spacing

Word-spacing The work-spacing property of CSS is applied to manage the space among the words. We can decrease or increase the space among the words by the use of this property. It...

3 minutes read.

CSS White Space Property

White Space in CSS The white-space CSS property describes how to show the content inside an element. This property is applied to manage a white space within an element. Values of White-space...

2 minutes read.

CSS Order

CSS Order CSS order property in CSS describes the flex item’s order inside the flex or any grid container. It is applied to order any flex item. Note: If any component is...

3 minutes read.

CSS Filter

CSS Filter CSS filter is used to apply visual effects on the images, text, and other factors of the webpage. This filter property in CSS allows us to admit the effects like...

7 minutes read.

CSS Width

CSS Width The width property in CSS sets the content width of area of the element. These properties do not provide margin and border features. It is used to set the...

1 minute read.