Open In App

Foundation CSS JavaScript Programmatic Use

Last Updated : 10 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Foundation CSS is an open-source and responsive front-end framework created by ZURB in September 2011 that makes it simple to create stunning responsive websites, apps, and emails that operate on any device. Many companies, like Facebook, eBay, Mozilla, Adobe, and even Disney, use it. This framework is based on Bootstrap, which is similar to SaaS. It’s more complex, versatile, and configurable. It also comes with a command-line interface, making it simple to use with module bundlers. Email framework provides you with responsive HTML emails, which can be read on any device. Foundation for Apps allows you to build fully responsive web applications.

Foundation CSS and JavaScript can be used for programmatic use in an HTML document by including the necessary CSS and JavaScript files and initializing the plugins.

  • To include the necessary CSS and JavaScript files, you can either download them from the Foundation website and link to them locally, or you can link to them from a CDN (Content Delivery Network).
  • In order to use Foundation JavaScript, we can add the necessary HTML attributes to elements and initialize the relevant plugins. We can use the data-toggle attribute to toggle a dropdown menu, or the data-responsive-accordion-tabs attribute to create responsive tabs.
  • In order to use Foundation CSS, we can apply the various CSS classes to the HTML elements. We can use the button class to style a button, or the grid-x class to create a grid layout.
  • To initialize the Foundation plugins, we can call the foundation() function on the document object when the page is loaded.

Syntax:

<script>
    $(document).foundation();
</script>

 

Example 1: This code of Foundation CSS and JavaScript is for programmatic use in an HTML document.

  • This example uses Foundation’s dropdown JavaScript plugin to toggle a dropdown menu when the button is clicked.
  • The dropdown menu is created using the dropdown-pane class and the data-dropdown attribute. The data-toggle attribute on the button element is used to specify the ID of the dropdown element that should be toggled.

HTML




<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content=
"width=device-width, initial-scale=1.0">
  <title>Foundation CSS and JavaScript Programmatic Use</title>
  <!-- Link to Foundation CSS -->
  <script src=
        integrity=
"sha256-pvPw+upLPUjgMXY0G+8O0xUf+/Im1MZjXxxgOcBQBXU=" 
        crossorigin="anonymous">
    </script>
  
  <link rel="stylesheet" href=
        integrity=
"sha256-ogmFxjqiTMnZhxCqVmcqTvjfe1Y/ec4WaRj/aQPvn+I=" 
        crossorigin="anonymous">
  <!-- Link to Foundation JavaScript -->
  <script src=
            integrity=
"sha256-pRF3zifJRA9jXGv++b06qwtSqX1byFQOLjqa2PTEb2o=" 
            crossorigin="anonymous">
    </script>
</head>
<body>
    <center>
        <h1 style="color:green">GeeksforGeeks</h1>
        <h3>Foundation CSS JavaScript Programmatic Use</h3>
        <!-- Use Foundation's JavaScript to toggle a dropdown menu -->
        <button class="button" data-toggle="example-dropdown">
            Toggle Dropdown
        </button>
        <div class="dropdown-pane" id="example-dropdown" 
            data-dropdown>
            <p>This is a dropdown menu</p>
            <ul>
                <li><a href="#">Menu item 1</a></li>
                <li><a href="#">Menu item 2</a></li>     
            </ul>
        </div>
  
        <!-- Initialize Foundation JavaScript plugins -->
        <script>
            $(document).foundation();
        </script>
    </center>
</body>
</html>


Output:

 

Example 2: This code with Foundation CSS and JavaScript in an HTML document,  demonstrates the use of the responsive tabs plugin.

  • It creates a set of responsive tabs using the tabs and tabs-content classes, and data-responsive-accordion-tabs and data-tabs-content attributes. 
  • The id attribute on the ul element and the href attribute on the elements are used to link the tab titles to the corresponding tab panels. 
  • The is-active class is used to indicate which tab should be displayed by default.

HTML




<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content=
"width=device-width, initial-scale=1.0">
  <title>Foundation CSS and JavaScript Programmatic Use</title>
  <!-- Link to Foundation CSS -->
  <script src=
        integrity=
"sha256-pvPw+upLPUjgMXY0G+8O0xUf+/Im1MZjXxxgOcBQBXU=" 
        crossorigin="anonymous">
    </script>
  
  <link rel="stylesheet" href=
        integrity=
"sha256-ogmFxjqiTMnZhxCqVmcqTvjfe1Y/ec4WaRj/aQPvn+I=" 
        crossorigin="anonymous">
  <!-- Link to Foundation JavaScript -->
  <script src=
            integrity=
"sha256-pRF3zifJRA9jXGv++b06qwtSqX1byFQOLjqa2PTEb2o=" 
            crossorigin="anonymous">
    </script>
    <style>
        body{
           margin-left:10px;
           margin-right:10px;
        }
    </style>
</head>
<body>
    <center>
        <h1 style="color:green">GeeksforGeeks</h1>
        <h3>Foundation CSS JavaScript Programmatic Use</h3>
        <!-- Use Foundation's responsive tabs plugin -->
        <ul class="tabs" data-responsive-accordion-tabs=
            "tabs small-accordion medium-tabs" 
            id="example-tabs">
            <li class="tabs-title is-active">
                <a href="#panel1" aria-selected="true">
                    Tab 1</a>
            </li>
            <li class="tabs-title">
                <a href="#panel2">
                    Tab 2</a>
            </li>          
        </ul>
        <div class="tabs-content" 
            data-tabs-content="example-tabs">
            <div class="tabs-panel is-active" 
                id="panel1">
              <p>Content for tab 1</p>
            </div>
            <div class="tabs-panel" id="panel2">
              <p>Content for tab 2 </p>
            </div>            
        </div>
    </center>
    <!-- Initialize Foundation JavaScript plugins -->
    <script>
        $(document).foundation();
    </script>
</body>
</html>


Output:

 

Reference: https://get.foundation/sites/docs/javascript.html#programmatic-use



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

Similar Reads