Open In App

Foundation CSS Switch Basics

Last Updated : 10 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Foundation CSS is an open-source and responsive front-end framework built by ZURB foundation in September 2011, that makes it easy to design beautiful responsive websites, apps, and emails that look amazing and can be accessible to any device. It is used by many companies such as Facebook, eBay, Mozilla, Adobe, and even Disney. The framework is built on Saas-like bootstrap. It is more sophisticated, flexible, and easily customizable. It also comes with CLI, so it’s easy to use it with module bundlers. It offers the Fastclick.js tool for faster rendering on mobile devices. 

Switch in general gives the user an option to select one of the two possibilities present. So a basic switch is one in which we can toggle it on or off. In Foundation CSS, the switch is nothing but a check-box markup which we use to create this style.

Foundation CSS Switch Classes: 

  • switch: This class is given to the element which has to be made the switch.
  • switch-input: This class is given to the input element whose type must be a checkbox and this is responsible for toggling the switch on and off.
  • switch-paddle: This class is responsible for the style created on the original checkbox and this is also responsible for the box background around the switch.
  • show-for-sr: This class is used to mask the text.

Syntax:

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

Note:

  • The for the attribute of the label is the one that points to the input.
  • It is the for an attribute which makes the switch clickable.
  • The values of the for the attribute of the label, id of input, and name of input must match to ensure there is a link between these two elements.
  • The element with class show-for-sr contains text which is masked and is mainly helpful to describe that element’s functionality to visually impaired people.

Example 1: This is a basic example illustrating basic switches created using Foundation CSS.

HTML




<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Foundation CSS Switch</title>
        <!-- Compressed CSS -->
        <link rel="stylesheet" 
              href=
        <link rel="stylesheet" 
              href=
"//cdnjs.cloudflare.com/ajax/libs/foundicons/3.0.0/foundation-icons.min.css"/>
  
        <!-- Compressed JavaScript -->
        <script src=
        </script>
        <script src=
        </script>
    </head>
    <body>
        <center>
          <h1 style="color:green;">GeeksforGeeks</h1>
          <strong>Foundation CSS Switch</strong>
          <br/><br/>
          <div class="switch">
            <input class="switch-input"
                   id="exampleSwitch" 
                   type="checkbox" 
                   name="exampleSwitch">
            <label class="switch-paddle" 
                   for="exampleSwitch">
              <span class="show-for-sr">GeeksforGeeks</span>
            </label>
          </div>
        </center>
        <script>
            $(document).foundation();
        </script>
    </body>
</html>


Output:

Foundation CSS Switch Basics

Foundation CSS Switch Basics

Example 2: This is a basic example that illustrates a  survey made using Foundation CSS switch.

HTML




<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Foundation CSS Switch Basics</title>
        <!-- Compressed CSS -->
        <link rel="stylesheet" 
              href=
        <link rel="stylesheet"
              href=
"//cdnjs.cloudflare.com/ajax/libs/foundicons/3.0.0/foundation-icons.min.css"/>
        <!-- Compressed JavaScript -->
        <script src=
        </script>
        <script src=
        </script>
    </head>
    <body>
        <center>
        <h1 style="color:green;">GeeksforGeeks</h1>
        <strong>Foundation CSS Switch Basics</strong>
        <br>
        <br>
        <p>For the Following questions below answer Yes/No</p>
        <ol>
            <li>
                <span>Do you like day over night.</span>
                <div class="switch">
                    <input class="switch-input"
                           id="exampleSwitch1"
                           type="checkbox" 
                           name="exampleSwitch1" checked>
                    <label class="switch-paddle" 
                           for="exampleSwitch1">
                    <span class="show-for-sr">GeeksforGeeks</span>
                    </label>
                </div>
            </li>
            <li>
                <span>You are hardworking over intelligent.</span>
                <div class="switch">
                    <input class="switch-input" 
                           id="exampleSwitch2" 
                           type="checkbox" 
                           name="exampleSwitch2">
                    <label class="switch-paddle" 
                           for="exampleSwitch2">
                    <span class="show-for-sr">GeeksforGeeks</span>
                    </label>
                </div>
            </li>
             <li>
                <span>Your mental strength is good</span>
                <div class="switch">
                    <input class="switch-input"
                           id="exampleSwitch3" 
                           type="checkbox"
                           name="exampleSwitch3">
                    <label class="switch-paddle" 
                           for="exampleSwitch3">
                        <span class="show-for-sr">GeeksforGeeks</span>
                    </label>
                </div>
            </li>
        </ol>
        </center>
        <script>
            $(document).foundation();
        </script>
    </body>
</html>


Output:

Foundation CSS Switch Basics

Foundation CSS Switch Basics

Reference: https://get.foundation/sites/docs/switch.html



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads