惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Spread Privacy
Spread Privacy
V
Visual Studio Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Recorded Future
Recorded Future
云风的 BLOG
云风的 BLOG
Microsoft Azure Blog
Microsoft Azure Blog
I
InfoQ
Apple Machine Learning Research
Apple Machine Learning Research
MyScale Blog
MyScale Blog
M
MIT News - Artificial intelligence
WordPress大学
WordPress大学
Recent Announcements
Recent Announcements
V
V2EX
The GitHub Blog
The GitHub Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Jina AI
Jina AI
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
V
Vulnerabilities – Threatpost
C
Check Point Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
AI
AI
宝玉的分享
宝玉的分享
P
Proofpoint News Feed
量子位
Attack and Defense Labs
Attack and Defense Labs
H
Hackread – Cybersecurity News, Data Breaches, AI and More
P
Privacy International News Feed
Google DeepMind News
Google DeepMind News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
CERT Recently Published Vulnerability Notes
腾讯CDC
Latest news
Latest news
Google DeepMind News
Google DeepMind News
The Register - Security
The Register - Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
G
GRAHAM CLULEY
Blog — PlanetScale
Blog — PlanetScale
博客园_首页
美团技术团队
The Cloudflare Blog
T
Tenable Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
J
Java Code Geeks
SecWiki News
SecWiki News
Webroot Blog
Webroot Blog
N
News | PayPal Newsroom
博客园 - 叶小钗
博客园 - Franky

CSS Articles by Temani Afif

Get Ready For the Powerful CSS border-shape Property! | CSS-Tricks Let’s Play With Gap Decorations! How to Control Infinite CSS Animations (Part 2 of 2) How to Control Infinite CSS Animations (Part 1 of 2) Two Circles, One Arrow, and Anchor Positioning Making a Responsive Pyramidal Grid With Modern CSS | CSS-Tricks How to Create a CSS-only Elastic Text Effect Making Complex CSS Shapes Using shape() | CSS-Tricks Responsive Hexagon Grid Using Modern CSS | CSS-Tricks Responsive List of Avatars Using Modern CSS (Part 2) | CSS-Tricks Responsive List of Avatars Using Modern CSS (Part 1) | CSS-Tricks Perfectly Pointed Tooltips: To The Corners Perfectly Pointed Tooltips: All Four Sides Perfectly Pointed Tooltips: A Foundation Sequential linear() Animation With N Elements | CSS-Tricks Infinite Marquee Animation using Modern CSS Better CSS Shapes Using shape() — Part 4: Close and Move | CSS-Tricks Drawing CSS Shapes using corner-shape Better CSS Shapes Using shape() — Part 3: Curves | CSS-Tricks Better CSS Shapes Using shape() — Part 2: More on Arcs | CSS-Tricks Better CSS Shapes Using shape() — Part 1: Lines and Arcs | CSS-Tricks Creating Blob Shapes using clip-path: shape() Creating Flower Shapes using clip-path: shape() Custom progress element using the attr() function A CSS-Only Star Rating Component and More! (Part 2) | CSS-Tricks A CSS-Only Star Rating Component and More! (Part 1) | CSS-Tricks How to Create Wavy Boxes Using CSS Full-Bleed Layout with Modern CSS Fancy Menu Navigation Using Anchor Positioning | CSS-Tricks How to Create a Zig-Zag Box Using CSS How to Create Zig-Zag CSS Loaders Using One Element Custom Progress Element Using Anchor Positioning & Scroll-Driven Animations How to Create Filling CSS Loaders Using One Element How to Create Curved-Edge and Rounded-Edge Shapes Using CSS CSS Tricks That Use Only One Gradient | CSS-Tricks How to create Shapes with Inner Curves using CSS Mask Custom Range Slider Using Anchor Positioning & Scroll-Driven Animations How to Get the Width/Height of Any Element in Only CSS How Keyboard Navigation Works in a CSS Game How To Create Cut-Out Shapes using The clip-path property The Modern Guide For Making CSS Shapes — Smashing Magazine css-shape.com: The Ultimate Collection of CSS-only Shapes ⚡️ Sliding 3D Image Frames In CSS — Smashing Magazine CSS Tricks To Master The clip-path Property Creating Wavy Circles with Fancy Animations in CSS Modern CSS Tooltips And Speech Bubbles (Part 2) — Smashing Magazine Do you need a Tooltip or a Speech Bubble? I have created 100 using CSS 😲 Modern CSS Tooltips And Speech Bubbles (Part 1) — Smashing Magazine Creating Flower Shapes using CSS Mask & Trigonometric Functions CSS Shapes: The Triangle The Complex But Awesome CSS border-image Property — Smashing Magazine CSS Tricks to add 3D Effects to your Text CSS Responsive Multi-Line Ribbon Shapes (Part 2) — Smashing Magazine CSS Responsive Multi-Line Ribbon Shapes (Part 1) — Smashing Magazine I have made 100+ CSS-only Ribbon Shapes | The Perfect Collection 🎀 How to create a CSS-only infinite scroll animation Re-Creating The Pop-Out Hover Effect With Modern CSS (Part 2) — Smashing Magazine Re-Creating The Pop-Out Hover Effect With Modern CSS (Part 1) — Smashing Magazine How to Create CSS Ribbon Shapes with a Single Element CSS Shapes: The Ribbon Revealing Images With CSS Mask Animations — Smashing Magazine css-loaders.com: The Biggest Collection of Loading Animations (more than 500 🤯) How To Define An Array Of Colors With CSS — Smashing Magazine CSS effects on images II Shines, Perspective, And Rotations: Fancy CSS 3D Effects For Images — Smashing Magazine What's your Lucky CSS Pattern? Check It Now! How to Add a CSS Reveal Animation to Your Images Modern Layouts using CSS Grid I created 100+ unique CSS patterns | The best collection 🤩 How to Create a Custom Range Slider Using CSS CSS Shapes: The Heart CSS Shapes: Polygon & Starburst A Text Reveal Animation using CSS CSS Tip: learn CSS the easy way! How to build a CSS-only accordion How to create Breadcrumb Navigation with CSS Different Ways to Get CSS Gradient Shadows | CSS-Tricks A CSS-only responsive Stepper component A Fancy Hover Effect For Your Avatar | CSS-Tricks How to make a zoom effect using CSS Creating a Custom Cursor using CSS CSS Infinite 3D Sliders | CSS-Tricks CSS Infinite Slider Flipping Through Polaroid Images | CSS-Tricks How to create a responsive sidebar menu using CSS CSS Infinite and Circular Rotating Image Slider | CSS-Tricks Making Static Noise From a Weird CSS Gradient Bug | CSS-Tricks CSS Grid and Custom Shapes, Part 3 | CSS-Tricks Fancy Image Decorations: Outlines and Complex Animations | CSS-Tricks Fancy Image Decorations: Masks and Advanced Hover Effects | CSS-Tricks Fancy Image Decorations: Single Element Magic | CSS-Tricks How to create an infinite image slider using CSS How to Create Wavy Shapes & Patterns in CSS | CSS-Tricks How I Made a Pure CSS Puzzle Game | CSS-Tricks How to create a Tooltip/Speech Bubble using CSS CSS Grid and Custom Shapes, Part 2 | CSS-Tricks CSS Grid and Custom Shapes, Part 1 | CSS-Tricks Zooming Images in a Grid Layout | CSS-Tricks How to create a CSS-only loader with one element Exploring CSS Grid’s Implicit Grid and Auto-Placement Powers | CSS-Tricks How to create background pattern using CSS & conic-gradient
How to style checkbox and radio buttons using CSS
2022-04-17 · via CSS Articles by Temani Afif

How to style checkbox and radio buttons using CSS

Written on by Temani AfifWeb Developer

Estimated read time 5 minutes

We all agree that the default styles applied to input elements like checkbox and radio aren’t that good. They are also different between browsers. For this reason, we always tend to update the default styles to create our design.

In this post, we will study CSS techniques that allow us to update the style of such elements without extra HTML code. Yes, we are going to give a fresh look to our <input> elements using only CSS!

In this post, I will focus on the CSS part so all the demos will include only the <input> elements for simplicity.

CSS Tricks for Styling Radio Buttons

Let’s start with a basic example:

As you can see in the demo, I am not adding any extra HTML code. Only a few lines of CSS are needed.

The first step is to disable the default browser styles using the following code:

input {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
}

Now, we have a “naked” radio button that we can style as we want. What I am doing next is pretty simple. I am giving the checkbox a dimension, a border, and some padding:

input {
  width: 40px;
  height: 40px;
  border: 5px solid grey;
  padding: 5px;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  cursor:pointer;   /* don't forget this as well */
}

Then, I am using the :checked pseudo-class to update the style when the element is checked

input:checked {
  background: green content-box;
  border-color: green;
}

I am adding a background that will cover only the content area (thanks to content-box) leaving a gap with the border. I am also giving the border a green color. That’s all!

This basic example illustrates how we can style a radio element without using any extra element. Of course, we will not stop there. We are going to add more CSS to have a better design.

Let’s update the previous example by introducing some CSS variables and transitions:

input {
  --s: 40px;
  
  width: var(--s);
  aspect-ratio: 1; /* OR height: var(--s) for old browsers */
  border: calc(var(--s)/8) solid var(--_c,grey);
  padding: calc(var(--s)/8);
  background: var(--_c,#0000) content-box;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  cursor: pointer;
  transition: .3s;
}
input:checked {
  --_c: green;
}

Instead of setting each property individually, I define one variable (--s) that I use to set the dimension, padding, border, etc. By only adjusting that variable, we can easily control the overall size.

I am doing the same with the coloration. Since the border and background need to be green on checked, I am defining one variable for that purpose.

Let’s consider a more fancy animation

Things start to get more funny! Instead of simply changing the colors, we are having a size animation of the inner part.

To do this, I am using a gradient instead of background color and I am changing the background-size on :checked

input {
  --s: 40px;
  --c: green;
  
  width: var(--s);
  aspect-ratio: 1;
  border: calc(var(--s)/8) solid var(--_c,grey);
  padding: calc(var(--s)/8);
  background: linear-gradient(var(--c) 0 0) 50%/0 0 no-repeat content-box;
  transition:.3s;
}
input:checked {
  --_c: var(--c);
  background-size: 100% 100%;
}

Notice the usage of another variable that allows me to control the color as well.

For the rounded radio button, we need a radial-gradient instead of a linear one

.round {
  border-radius: 50%;
  background: radial-gradient(farthest-side,var(--c) 96%,#0000 ) 50%/0 0 no-repeat content-box;
}

Let’s add an effect to the border:

See the Pen by Temani Afif () on .

In addition to the gradient, I am using an outline:

input {
  --s: 40px;
  --c: green;
  
  border: calc(var(--s)/8) solid grey;
  outline: calc(var(--s)/8) solid var(--_c,#0000);
  outline-offset: calc(var(--s)/4);
}
input:checked {
  --_c: var(--c);
  background-size: 100% 100%;
  outline-offset: calc(var(--s)/-8);
  border-color: #0000
}

I am defining an outline equal to the border width. Initially, the outline color is transparent and the offset is a positive value expressed with the variable --s. On :checked, I change the color of the outline, I make the offset negative to create an overlap with the border and I change the color of the border to transparent.

We have a cool radio button with only CSS a stated in the introduction. Not only this, but we can easily adjust things using CSS variables.

Below two more examples to show a full code including custom radio buttons

90%

💰 90% OFF YOUR FIRST MONTH WITH ALL VERPEX SHARED HOSTING PLANS

with the discount code

AWESOME

CSS Tricks for Styling Checkbox

Styling a checkbox is the same as styling a radio button. The only difference between both is their functional part which is out of the scope of this post.

For this one, I will focus on my favorite design which is the toggle (also called switch)

input {
  --s:40px;
  
  height: var(--s);
  padding: calc(var(--s)/10);
  box-sizing: content-box;
  aspect-ratio: 2;
  border-radius: var(--s);
  background:
    radial-gradient(farthest-side,#fff 97%,#0000) 
     left/var(--s) 100% content-box no-repeat,
    grey;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  cursor: pointer;
  transition: .3s;
}
input:checked {
  background-position: right;
  background-color: green;
}

Like all the previous examples, we are going to use a CSS variable to control the size and then a gradient for the main trick.

At first glance, it may look difficult but the trick is pretty simple. We use a radial-gradient to create a circle placed on the left side within the content area (thanks to content-box). The padding will define the gap between the circle and the edge. On :checked we update the position to the right which will create the sliding effect thanks to the transition. As simple as that!

Let’s not forget about changing the background-color to green or whatever color you want to show the checked state.

Let’s consider another idea of a checkbox

input {
  --s:80px; /* adjust this to control the size*/
  
  height: var(--s);
  aspect-ratio: 2;
  padding: calc(var(--s)/5);
  border-radius: var(--s);
  background:
    radial-gradient(farthest-side,#fafafa 85%,#0005,#0000) left/var(--s) 100% no-repeat,
    linear-gradient(90deg,#20b68f 50%,#939393 0) right/200% 100% content-box;
  transition: 0.5s;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  cursor: pointer;
}
input:checked {
  background-position: right,left;
}

For this one, we are going to rely on two gradients. Like the previous one, a radial-gradient will create the circle. For the main background, we will consider a linear-gradient.

Below is an illustration to see how the bottom gradient behaves.

The linear-gradient has a width equal to twice the element width (the 200%) defined with two colors (the grey and green one) then we slide it from left to right as we do with the circle. Since it’s a background, we won’t see its overflowing part.

As you can see, using the same technique (background and gradients) we made 2 different checkbox using only the input element. And this is only a small overview of the possibilities. We can push the limit of CSS and create more fancy checkbox.

Here are a few more examples I recently made:

50%

💰 EXTRA 50% OFF ALL VERPEX YEARLY MANAGED CLOUD SERVER PLANS

with the discount code

SERVERS-SALE

Conclusion

Thanks to modern CSS techniques we no more need to rely on hacks or a ton of HTML code to create fancy checkbox and radio buttons. All that is needed is the native input element and our creativity to get almost any design we want.

Check out our hosting options. We offer plans for WordPress, cloud hosting, managed cloud servers, and more, all of which can help you ensure your website is built on a strong foundation.

Frequently Asked Questions

Temani Afif

Temani Afif is an expert web developer, a content creator, and a CSS addict. He is the mastermind behind CSS Loaders, CSS Generators, CSS Tip and many other CSS-related websites.

View more posts by Temani Afif