Foundation - Switch Basics



Description

  • To create a switch, you need to add .switch class to an element.

  • Within the switch add <input type = "checkbox"> with .switch-input class.

  • Next, you need to create a <label> with .switch-paddle class.

  • Provide a unique ID for <input> and point the <label> to <input> with for attribute, which makes the switch clickable.

  • Within the switch label uses .show-for-sr class, which is screen reader-only text to mask the text visually.

Example

The following example demonstrates how to use switch in Foundation.

<html>
   <head>
      <title>Switch</title>
      <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/foundation-sites@6.5.1/dist/css/foundation.min.css" integrity="sha256-1mcRjtAxlSjp6XJBgrBeeCORfBp/ppyX4tsvpQVCcpA= sha384-b5S5X654rX3Wo6z5/hnQ4GBmKuIJKMPwrJXn52ypjztlnDK2w9+9hSMBz/asy9Gw sha512-M1VveR2JGzpgWHb0elGqPTltHK3xbvu3Brgjfg4cg5ZNtyyApxw/45yHYsZ/rCVbfoO5MSZxB241wWq642jLtA==" crossorigin="anonymous">

      <!-- Compressed JavaScript -->
      <script src="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.0.1/js/vendor/jquery.min.js"></script>
      <script src="https://cdn.jsdelivr.net/npm/foundation-sites@6.5.1/dist/js/foundation.min.js" integrity="sha256-WUKHnLrIrx8dew//IpSEmPN/NT3DGAEmIePQYIEJLLs= sha384-53StQWuVbn6figscdDC3xV00aYCPEz3srBdV/QGSXw3f19og3Tq2wTRe0vJqRTEO sha512-X9O+2f1ty1rzBJOC8AXBnuNUdyJg0m8xMKmbt9I3Vu/UOWmSg5zG+dtnje4wAZrKtkopz/PEDClHZ1LXx5IeOw==" crossorigin="anonymous"></script>

   </head>

   <body>
      <div class = "switch">
         <input class = "switch-input" id = "demoSwitch" type = "checkbox" name = "demoSwitch">
         <label class = "switch-paddle" for = "demoSwitch">
            <span class = "show-for-sr">Switch</span>
         </label>
      </div>

      
      <script>
         $(document).ready(function() {
            $(document).foundation();
         })
      </script>
   </body>
</html>

Output

Let us carry out the following steps to see how the above given code works −

  • Save the above given html code switch_basics.html file.

  • Open this HTML file in a browser, an output is displayed as shown below.

foundation_basic_controls.htm
Advertisements