{"id":26691,"date":"2016-06-27T21:01:30","date_gmt":"2016-06-27T13:01:30","guid":{"rendered":"https:\/\/www.hongkiat.com\/blog\/?p=26691"},"modified":"2025-04-24T17:03:41","modified_gmt":"2025-04-24T09:03:41","slug":"css-mask-switch","status":"publish","type":"post","link":"https:\/\/www.hongkiat.com\/blog\/css-mask-switch\/","title":{"rendered":"How to Create a Switch UI Using CSS Mask"},"content":{"rendered":"<p>In image processing, <em>masking<\/em> is a technique that allows you to <strong>hide an image<\/strong> with another. A mask is used to make a portion of an image <strong>see-through<\/strong>. You can perform masking using CSS with the help of <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.w3schools.com\/cssref\/#masking\">masking properties<\/a>.<\/p>\n<p>In today\u2019s post, we\u2019ll create a masked image using two PNG images and CSS masking techniques, and allow users to handle the two states of the image (<em>day<\/em> and <em>night<\/em>) with the help of a switch UI.<\/p>\n<p>Because of some browser-compatibility issues \u2014 not all masking properties are supported in every browser (as of June 2016) \u2014 I will show <strong>two techniques for adding masks<\/strong>, one for Webkit-based browsers and one for Firefox. The first two steps in this three-step tutorial are the same for every browser, but there will be a difference in the third step.<\/p>\n<p class=\"note\"><strong>Read more: <\/strong><a href=\"https:\/\/www.hongkiat.com\/blog\/css-masking\/\">Clipping and masking with CSS<\/a><\/p>\n<h2>Step 1. Create A Basic Switch<\/h2>\n<p>Since a typical switch has two states with <strong>only one <em>enabled<\/em><\/strong> at a time, you can use a <strong>radio button group of two<\/strong> to create the working components of the switch. Place each radio button at the left and right ends of their parent element.<\/p>\n<p>Radio button groups are created by giving each radio button the same <code>name<\/code> attribute. In a radio button group, <strong>only one radio button<\/strong> can be checked at a time.<\/p>\n<p>We start with the following HTML and CSS:<\/p>\n<p><strong>HTML<\/strong><\/p>\n<pre>\n&lt;div id=\"outerWrapper\"&gt;\n    &lt;div id=\"innerWrapper\"&gt;\n        &lt;input type=\"radio\" id=\"leftRadio\" class=\"radio\" name=\"radio\" \/&gt;\n        &lt;input type=\"radio\" id=\"rightRadio\" class=\"radio\" name=\"radio\" checked \/&gt;\n    &lt;\/div&gt;\n&lt;\/div&gt;\n<\/pre>\n<p><strong>CSS<\/strong><\/p>\n<p>In the CSS below, I used <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/css-tricks.com\/absolute-positioning-inside-relative-positioning\/\">absolute positioning<\/a> to place the radio buttons on the screen exactly where I want.<\/p>\n<pre>#outerWrapper {\n    width: 450px;\n    height: 90px;\n    padding: 10px;\n    margin: 100px auto 0 auto;\n    border-radius: 55px;\n    box-shadow: 0 0 10px 6px #EAEBED;\n    background: #fff;\n}\n\n#innerWrapper {\n    height: 100%;\n    border-radius: 45px;\n    overflow: hidden;\n    position: relative;\n}\n\n.radio {\n    width: 90px;\n    height: 100%;\n    position: absolute;\n    margin: 0;\n    opacity: 0;\n}\n\n#rightRadio {\n    right: 0;\n}\n\n.radio:not(:checked) {\n    cursor: pointer;\n}<\/pre>\n<p>I added the <code>opacity:0<\/code> rule to the <code>.radio<\/code> class to <strong>hide the radio buttons<\/strong>. The last rule in the code block below, <code>cursor: pointer;<\/code> shows the pointer cursor for the unchecked radio button, so users know which button to click to toggle the switch state.<\/p>\n<figure><img decoding=\"async\" alt=\"Switch UI radio buttons\" src=\"https:\/\/assets.hongkiat.com\/uploads\/css-mask-switch\/css-mask-switch-radio.jpg\"><figcaption>Screenshot of the switch UI with radio buttons in the Chrome browser<\/figcaption><\/figure>\n<h2>Step 2. Add Skins to the Switch<\/h2>\n<p>In this step, we\u2019ll add two <code>&lt;div&gt;<\/code> tags for the two skins below the radio buttons in our HTML file, and a background image to each skin in our CSS.<\/p>\n<p>I\u2019m using \u201cDay\u201d and \u201cNight\u201d as the two states of the switch, inspired by a <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/dribbble.com\/shots\/2691884-Day-Switch-Button\">Dribbble shot<\/a> by <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/dribbble.com\/minhkilly\">Minh Killy Le<\/a>.<\/p>\n<figure><img decoding=\"async\" alt=\"CSS mask switch day skin\" src=\"https:\/\/assets.hongkiat.com\/uploads\/css-mask-switch\/css-mask-switch-day-bg.jpg\"><figcaption>Day Skin<\/figcaption><\/figure>\n<figure><img decoding=\"async\" alt=\"CSS mask switch night skin\" src=\"https:\/\/assets.hongkiat.com\/uploads\/css-mask-switch\/css-mask-switch-night-bg.jpg\"><figcaption>Night Skin<\/figcaption><\/figure>\n<p><strong>HTML<\/strong><\/p>\n<pre>\n    &lt;div id=\"daySkin\" class=\"skin\"&gt;&lt;\/div&gt;\n    &lt;div id=\"nightSkin\" class=\"skin\"&gt;&lt;\/div&gt;\n<\/pre>\n<p><strong>CSS<\/strong><\/p>\n<pre>#daySkin {\n    background-image: url('day.png');\n}\n\n#nightSkin {\n    background-image: url('night.png');\n}\n\n.skin {\n    width: 100%;\n    height: 100%;\n    pointer-events: none;\n    position: absolute;\n    margin: 0;\n}<\/pre>\n<p>The <code>pointer-events: none;<\/code> rule is added to the skins so that click events on the switch can <strong>pass through them<\/strong> and <strong>reach the radio buttons<\/strong>.<\/p>\n<p>With the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/developer.mozilla.org\/en\/docs\/Web\/CSS\/pointer-events\">pointer-events<\/a> CSS property, you can set the circumstances under which a graphic element can be <strong>targeted by mouse events<\/strong>.<\/p>\n<p>As an alternative for the code above, two <code>&lt;img&gt;<\/code> tags (with source images) inside the <code>&lt;div&gt;<\/code> tags can also work. They will be the <strong>skins for the two switch states<\/strong>.<\/p>\n<figure><img decoding=\"async\" alt=\"CSS mask switch with background\" src=\"https:\/\/assets.hongkiat.com\/uploads\/css-mask-switch\/css-mask-switch-w-bg.jpg\"><figcaption>Screenshot of switch with skins in chrome<\/figcaption><\/figure>\n<h2>Step 3a. Add Mask (Webkit version)<\/h2>\n<p>For Chrome and other Webkit-based browsers, I will use the <code>mask-image<\/code> CSS property, which \u2014 as of the writing of this post \u2014 only works with the <code>-webkit<\/code> prefix in Webkit browsers. The <code><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/mask-image\">mask-image<\/a><\/code> property lets you <strong>specify the image<\/strong> to be used <strong>as the <em>mask<\/em><\/strong>.<\/p>\n<p>In general, there are two kinds of masking: <strong>luminance<\/strong> and <strong>alpha<\/strong>.<\/p>\n<ul>\n<li>In <strong>luminance masking<\/strong>, the dark portion of the mask image hides the image it is masking: the darker a portion is in the mask image, <strong>the more hidden that portion is<\/strong> in the image being masked.<\/li>\n<li>In <strong>alpha masking<\/strong>, the transparent portion of the mask image hides the image it is masking: the more transparent a portion is in the mask image, the more hidden that portion is in the image being masked.<\/li>\n<\/ul>\n<p>In Chrome (as of version 51.0.2704.103, Win10), only alpha seems to currently work.<\/p>\n<p>In CSS, <code>alpha<\/code> and <code>luminance<\/code> are the values of the <code><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/mask-type\">mask-type<\/a><\/code> property.<\/p>\n<p>Here is the CSS that <strong>adds a mask<\/strong> to background images in Webkit browsers:<\/p>\n<p><strong>CSS<\/strong><\/p>\n<pre>#nightSkin {\n    background-image: url('night.png');\n    mask-type: alpha;\n    \/* transparent circle with remaining portion opaque *\/\n    -webkit-mask-image: radial-gradient(circle at 45px 45px,\n        rgba(0, 0, 0, 0) 45px,\n        rgba(0, 0, 0, 1) 45px);\n}\n\n\/* When day skin selected *\/\n#leftRadio:checked ~ #nightSkin {\n    mask-type: alpha;\n    \/* opaque circle with remaining portion transparent *\/\n    -webkit-mask-image: radial-gradient(circle at 405px 45px,\n        rgba(0, 0, 0, 1) 45px,\n        rgba(0, 0, 0, 0) 45px);\n}<\/pre>\n<p>I used the <code>-webkit-mask-image<\/code> property to create the initial mask image. Its value uses the <code><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/radial-gradient\">radial-gradient()<\/a><\/code> CSS function that\u2019s used to create an image from a pre-defined shape, a radial gradient, and the center of the gradient.<\/p>\n<p>For the night skin, I created a transparent circle, and I made the remaining part of the container opaque. For the day skin, I did the opposite: created an opaque circle with the <code>radial-gradient()<\/code> function and made the remaining portion transparent.<\/p>\n<p>Although it\u2019s <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/mask-type#Browser_compatibility\">not yet supported<\/a> in Webkit browsers, I added the <code>mask-type<\/code> property to the CSS for future reference.<\/p>\n<figure><img decoding=\"async\" alt=\"CSS mask switch left mask\" src=\"https:\/\/assets.hongkiat.com\/uploads\/css-mask-switch\/css-mask-switch-w-left-mask.jpg\"><figcaption>Screenshot of switch with Night skin selected<\/figcaption><\/figure>\n<figure><img loading=\"lazy\" decoding=\"async\" alt=\"CSS mask switch right mask\" height=\"216\" src=\"https:\/\/assets.hongkiat.com\/uploads\/css-mask-switch\/css-mask-switch-w-right-mask.jpg\" width=\"637\"><figcaption>Screenshot of switch with Day skin selected<\/figcaption><\/figure>\n<p>As you can see above, the border of the circle isn\u2019t very smooth. To <strong>hide the rough edges<\/strong>, add a <code>&lt;div&gt;<\/code> after the skins in the shape of a circle (same size as the mask circle) with a box shadow. The shadow will hide the rough edges of the circle mask.<\/p>\n<p><strong>HTML<\/strong><\/p>\n<pre>&lt;div id=\"switchBtnOutline\"&gt;&lt;\/div&gt;<\/pre>\n<p><strong>CSS<\/strong><\/p>\n<pre>#switchBtnOutline {\n    width: 90px;\n    height: 100%;\n    border-radius: 45px;\n    box-shadow: 0 0 2px 2px grey inset, 0 0 10px grey;\n    pointer-events: none;\n    position: absolute;\n    margin: 0;\n}\n\n\/* Place #switchBtnOutline at the right end when day skin is selected *\/\n#leftRadio:checked ~ #switchBtnOutline {\n    right: 0;\n}<\/pre>\n<figure><img loading=\"lazy\" decoding=\"async\" alt=\"CSS mask switch button shadow\" height=\"240\" src=\"https:\/\/assets.hongkiat.com\/uploads\/css-mask-switch\/css-mask-switch-w-btn-shadow-outline.jpg\" width=\"700\"><figcaption>Screenshot of switch with mask circle\u2019s rough edges hidden<\/figcaption><\/figure>\n<h2>Step 3b. Add Mask (Firefox version)<\/h2>\n<p>The <code>mask-image<\/code> CSS property is actually a <strong>longhand property<\/strong>, and it\u2019s part of the shorthand property <code><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/mask\">mask<\/a><\/code> that lets you specify the image to be used as a mask as well. While <code>mask-image<\/code> isn\u2019t supported in Firefox yet, <code>mask<\/code> is.<\/p>\n<p>Although the <code>mask<\/code> property should accept an image created with the <code>radial-gradient()<\/code> CSS function as a value, just like the <code>mask-image<\/code> property did, there\u2019s no support for that in Firefox yet.<\/p>\n<p>So, instead of a <code>radial-gradient()<\/code> image, let\u2019s use an SVG image as the mask image with the mask type <code>luminance<\/code>.<\/p>\n<pre>\n&lt;svg&gt;\n    &lt;rect x=\"0\" y=\"0\" width=\"450\" height=\"90\" fill=\"rgb(255,255,255)\" \/&gt;\n    &lt;circle cx=\"45\" cy=\"45\" r=\"45\" \/&gt;\n&lt;\/svg&gt;\n<\/pre>\n<p>The SVG image above looks like a combination of a <strong>white rectangle<\/strong> and a <strong>black circle<\/strong>. Add this, and another one with a <strong>black rectangle<\/strong> and a <strong>white circle<\/strong> as masks to the HTML we used in the Webkit version.<\/p>\n<figure><img decoding=\"async\" alt=\"SVG mask for switch\" src=\"https:\/\/assets.hongkiat.com\/uploads\/css-mask-switch\/css-mask-switch-svg.jpg\"><figcaption>SVG image (white rectangle and black circle for the mask)<\/figcaption><\/figure>\n<p><strong>HTML<\/strong><\/p>\n<pre>&lt;svg&gt;\n    &lt;defs&gt;\n        &lt;mask id=\"leftSwitchMask\"&gt;\n            &lt;rect x=\"0\" y=\"0\" width=\"450\" height=\"90\" fill=\"rgb(255,255,255)\" \/&gt;\n            &lt;circle cx=\"45\" cy=\"45\" r=\"45\" \/&gt;\n        &lt;\/mask&gt;\n\n        &lt;mask id=\"rightSwitchMask\"&gt;\n            &lt;rect x=\"0\" y=\"0\" width=\"450\" height=\"90\" \/&gt;\n            &lt;circle cx=\"405\" cy=\"45\" r=\"45\" fill=\"rgb(255,255,255)\"\/&gt;\n        &lt;\/mask&gt;\n    &lt;\/defs&gt;\n&lt;\/svg&gt;<\/pre>\n<p>Replace (or combine with) the CSS code for <code>#nightSkin<\/code> we used in the Webkit version with the following code. And you\u2019re done.<\/p>\n<p>We now have two different mask images (CSS gradient & SVG), two different mask types (Alpha & Luminance), and support for both Webkit and Firefox.<\/p>\n<p><strong>CSS<\/strong><\/p>\n<pre>#nightSkin {\n    background-image: url('night.png');\n    mask-type: luminance;\n    mask: url(#leftSwitchMask);\n}\n\n#leftRadio:checked ~ #nightSkin {\n    mask-type: luminance;\n    mask: url(#rightSwitchMask);\n}<\/pre>\n<h2>Check Out the Demo<\/h2>\n<div class=\"button\">\n    <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/hongkiat.github.io\/css-mask-switch\/\">Demo<\/a>\n    <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/github.com\/hongkiat\/css-mask-switch\/\">Download Source<\/a>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>In image processing, masking is a technique that allows you to hide an image with another. A mask is used to make a portion of an image see-through. You can perform masking using CSS with the help of masking properties. In today\u2019s post, we\u2019ll create a masked image using two PNG images and CSS masking&hellip;<\/p>\n","protected":false},"author":145,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3392],"tags":[507,4501],"topic":[4520],"class_list":["entry-content","is-maxi"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v22.8 (Yoast SEO v27.5) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>How to Create a Switch UI Using CSS Mask - Hongkiat<\/title>\n<meta name=\"description\" content=\"In image processing, masking is a technique that allows you to hide an image with another. A mask is used to make a portion of an image see-through. You\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.hongkiat.com\/blog\/css-mask-switch\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Create a Switch UI Using CSS Mask\" \/>\n<meta property=\"og:description\" content=\"In image processing, masking is a technique that allows you to hide an image with another. A mask is used to make a portion of an image see-through. You\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.hongkiat.com\/blog\/css-mask-switch\/\" \/>\n<meta property=\"og:site_name\" content=\"Hongkiat\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/hongkiatcom\" \/>\n<meta property=\"article:published_time\" content=\"2016-06-27T13:01:30+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-04-24T09:03:41+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/assets.hongkiat.com\/uploads\/css-mask-switch\/css-mask-switch-radio.jpg\" \/>\n<meta name=\"author\" content=\"Preethi Ranjit\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:site\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Preethi Ranjit\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/css-mask-switch\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/css-mask-switch\\\/\"},\"author\":{\"name\":\"Preethi Ranjit\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/e981676afae36d1ff5feb75094950ab3\"},\"headline\":\"How to Create a Switch UI Using CSS Mask\",\"datePublished\":\"2016-06-27T13:01:30+00:00\",\"dateModified\":\"2025-04-24T09:03:41+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/css-mask-switch\\\/\"},\"wordCount\":984,\"commentCount\":7,\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/css-mask-switch\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/css-mask-switch\\\/css-mask-switch-radio.jpg\",\"keywords\":[\"CSS\",\"CSS Tutorials\"],\"articleSection\":[\"Coding\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/css-mask-switch\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/css-mask-switch\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/css-mask-switch\\\/\",\"name\":\"How to Create a Switch UI Using CSS Mask - Hongkiat\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/css-mask-switch\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/css-mask-switch\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/css-mask-switch\\\/css-mask-switch-radio.jpg\",\"datePublished\":\"2016-06-27T13:01:30+00:00\",\"dateModified\":\"2025-04-24T09:03:41+00:00\",\"description\":\"In image processing, masking is a technique that allows you to hide an image with another. A mask is used to make a portion of an image see-through. You\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/css-mask-switch\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/css-mask-switch\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/css-mask-switch\\\/#primaryimage\",\"url\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/css-mask-switch\\\/css-mask-switch-radio.jpg\",\"contentUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/css-mask-switch\\\/css-mask-switch-radio.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/css-mask-switch\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Create a Switch UI Using CSS Mask\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"name\":\"Hongkiat\",\"description\":\"Tech and Design Tips\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\",\"name\":\"Hongkiat.com\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"contentUrl\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"width\":1200,\"height\":799,\"caption\":\"Hongkiat.com\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/hongkiatcom\",\"https:\\\/\\\/x.com\\\/hongkiat\",\"https:\\\/\\\/www.pinterest.com\\\/hongkiat\\\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/e981676afae36d1ff5feb75094950ab3\",\"name\":\"Preethi Ranjit\",\"description\":\"A .NET developer with a JavaScript background, Preethi is expert in front-end coding, JavaScript, HTML, and CSS.\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/author\\\/preethi\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to Create a Switch UI Using CSS Mask - Hongkiat","description":"In image processing, masking is a technique that allows you to hide an image with another. A mask is used to make a portion of an image see-through. You","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.hongkiat.com\/blog\/css-mask-switch\/","og_locale":"en_US","og_type":"article","og_title":"How to Create a Switch UI Using CSS Mask","og_description":"In image processing, masking is a technique that allows you to hide an image with another. A mask is used to make a portion of an image see-through. You","og_url":"https:\/\/www.hongkiat.com\/blog\/css-mask-switch\/","og_site_name":"Hongkiat","article_publisher":"https:\/\/www.facebook.com\/hongkiatcom","article_published_time":"2016-06-27T13:01:30+00:00","article_modified_time":"2025-04-24T09:03:41+00:00","og_image":[{"url":"https:\/\/assets.hongkiat.com\/uploads\/css-mask-switch\/css-mask-switch-radio.jpg","type":"","width":"","height":""}],"author":"Preethi Ranjit","twitter_card":"summary_large_image","twitter_creator":"@hongkiat","twitter_site":"@hongkiat","twitter_misc":{"Written by":"Preethi Ranjit","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.hongkiat.com\/blog\/css-mask-switch\/#article","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/css-mask-switch\/"},"author":{"name":"Preethi Ranjit","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/e981676afae36d1ff5feb75094950ab3"},"headline":"How to Create a Switch UI Using CSS Mask","datePublished":"2016-06-27T13:01:30+00:00","dateModified":"2025-04-24T09:03:41+00:00","mainEntityOfPage":{"@id":"https:\/\/www.hongkiat.com\/blog\/css-mask-switch\/"},"wordCount":984,"commentCount":7,"publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/css-mask-switch\/#primaryimage"},"thumbnailUrl":"https:\/\/assets.hongkiat.com\/uploads\/css-mask-switch\/css-mask-switch-radio.jpg","keywords":["CSS","CSS Tutorials"],"articleSection":["Coding"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.hongkiat.com\/blog\/css-mask-switch\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.hongkiat.com\/blog\/css-mask-switch\/","url":"https:\/\/www.hongkiat.com\/blog\/css-mask-switch\/","name":"How to Create a Switch UI Using CSS Mask - Hongkiat","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.hongkiat.com\/blog\/css-mask-switch\/#primaryimage"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/css-mask-switch\/#primaryimage"},"thumbnailUrl":"https:\/\/assets.hongkiat.com\/uploads\/css-mask-switch\/css-mask-switch-radio.jpg","datePublished":"2016-06-27T13:01:30+00:00","dateModified":"2025-04-24T09:03:41+00:00","description":"In image processing, masking is a technique that allows you to hide an image with another. A mask is used to make a portion of an image see-through. You","breadcrumb":{"@id":"https:\/\/www.hongkiat.com\/blog\/css-mask-switch\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.hongkiat.com\/blog\/css-mask-switch\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.hongkiat.com\/blog\/css-mask-switch\/#primaryimage","url":"https:\/\/assets.hongkiat.com\/uploads\/css-mask-switch\/css-mask-switch-radio.jpg","contentUrl":"https:\/\/assets.hongkiat.com\/uploads\/css-mask-switch\/css-mask-switch-radio.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/www.hongkiat.com\/blog\/css-mask-switch\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.hongkiat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to Create a Switch UI Using CSS Mask"}]},{"@type":"WebSite","@id":"https:\/\/www.hongkiat.com\/blog\/#website","url":"https:\/\/www.hongkiat.com\/blog\/","name":"Hongkiat","description":"Tech and Design Tips","publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.hongkiat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.hongkiat.com\/blog\/#organization","name":"Hongkiat.com","url":"https:\/\/www.hongkiat.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","contentUrl":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","width":1200,"height":799,"caption":"Hongkiat.com"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/hongkiatcom","https:\/\/x.com\/hongkiat","https:\/\/www.pinterest.com\/hongkiat\/"]},{"@type":"Person","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/e981676afae36d1ff5feb75094950ab3","name":"Preethi Ranjit","description":"A .NET developer with a JavaScript background, Preethi is expert in front-end coding, JavaScript, HTML, and CSS.","url":"https:\/\/www.hongkiat.com\/blog\/author\/preethi\/"}]}},"jetpack_featured_media_url":"https:\/\/","jetpack_shortlink":"https:\/\/wp.me\/p4uxU-6Wv","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/26691","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/users\/145"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/comments?post=26691"}],"version-history":[{"count":4,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/26691\/revisions"}],"predecessor-version":[{"id":74091,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/26691\/revisions\/74091"}],"wp:attachment":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/media?parent=26691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/categories?post=26691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/tags?post=26691"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/topic?post=26691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}