<% @side_nav = "js" %>
<% @page_title = "Sections" %>

<%= @page_title %>

Sections are similar to tabs as a way to selectively show a single panel of content at a time.

Section 1

Section 1. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Section 2

Section 1

Section 1. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Section 2

Section 2. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Section 3

Section 3


Sections Replace Accordion, Tabs, Vertical Nav & Nav Bar

Sections replace a few things that you are used to from Foundation 3. We've taken the accordion, the tabs and the vertical nav and combined them into this really flexible plugin that can handle all of those. The single JS file handles all the interactions, but the classes you add to the element control how it gets rendered and styled across our breakpoint.


Build With Predefined HTML Classes

There are two ways to build sections in Foundation 4, with our predefined HTML classes or with our structure and mixins. Building a sections using our predefined classes is super-easy, you just need to create a <div class="section-container" data-section> as a wrapper for the sections themselves. Inside of this wrapper, you'll create either a <section class="section"> or <div class="section">. Within that you'll include some sort of .title (this can be headers or a paragraph) and a <div class="content">. Here is the markup you'll need:

If you want to use ZURB's default styles for section elements you can apply the auto, tabs, accordion, vertical-nav, or horizontal-nav classes. By default, section elements will be an accordion on mobile and tabs on desktops and tablets. If you want to adjust this behavior and force a particular format for your section, you will need to set data-section equal to the format you want to force as shown in the variations below.

<%= code_example '

Section 1

Content of section 1.

Section 2

Content of section 2.

', :html %>

Section 1

Content of section 1.

Section 2

Content of section 2.

This example will automatically switch between tabs and accordion based on the resolution of the device.

The class options:

  • auto
  • tabs
  • accordion
  • vertical-nav
  • horizontal-nav

For these styles to come across, make sure you have the default Foundation CSS package or that you've selected section from a custom package. These should be linked up following our default HTML page structure.


Variations

Accordion

Adding an accordion class to the section container will show an accordion on both small and large screens.

<%= code_example '

Section 1

Content of section 1.

Section 2

Content of section 2.

', :html %>

Section 1

Content of section 1.

Section 2

Content of section 2.

Tabs

Adding a tabs class to the section container will enable tabs on both small and large screens.

<%= code_example '

Section 1

Content of section 1.

Section 2

Content of section 2.

', :html %>

Section 1

Content of section 1.

Section 2

Content of section 2.

Vertical Tabs

Adding a vertical-tabs class to the section container and setting data-section="vertical-tabs" will enable tabs on both small and large screens.

<%= code_example '

Section 1

Content of section 1.

Section 2

Content of section 2.

Section 3

Content of section 3.

', :html %>

Section 1

Content of section 1.

Section 2

Content of section 2.

Section 3

Content of section 3.

Vertical Nav/Accordion

Adding a vertical-nav class to the section container will enable vertical navigation elements on large screens and show an accordion on small screens.

Vertical navigation elements usually appear within the grid so the width can be controlled on desktop.

Use the side-nav class on the list to apply the Foundation navigation styles

<%= code_example '

Content to the right of the navigation.

', :html %>

Content to the right of the navigation.

Horizontal Nav/Nav Bar

Adding a horizontal-nav class to the section container will enable horizontal navigation elements on large screens and show an accordion on small screens.

Horizontal navigation is a combination of the tab setting with vertical navigation drop downs.

Use the side-nav class on the list to apply the Foundation navigation styles

<%= code_example ' ', :html %>
Deep Linking

Set deep-linking to true to enable deep linking to sections of content. Deep linking allows visitors to visit a predefined URL with a hash that points to a particular section of the content. Deep linking also requires a matching data-slug on the content section that the hash should point to, without the pound (#) sign.

<%= code_example '

Section 1

Content of section 1.

Section 2

Content of section 2.

', :html %>
Nested Sections

You can infinitely nest Section elements to create more complicated Section layouts.

ROOT: Section 1

Content 1 Summary Paragraphs

DETAIL: #1

Detail awesome stuff!

DETAIL: #2

Detail awesome stuff numeroe duo.

ROOT: Section 2

Content 2

<%= code_example '

ROOT: Section 1

Content 1 Summary Paragraphs

DETAIL: #1

Detail awesome stuff!

DETAIL: #2

Detail awesome stuff numeroe duo.

ROOT: Section 2

Content 2

', :html %>

Note: The section element can be replaced with a div.section element if you do not want to use HTML5 elements.


Building with Mixins

If you are keen on SCSS and use it for your Foundation projects, you have access to mixins that will let you create any type of section you want and change it within a media query too! Building a section with our mixins requires a bit of predefined structure, but you can choose the classes or ID's to style them with. Start with some sort of block-level container, we use a <div>. This container needs to have data-section="type-of-section". The section types are explained above. Inside this you create "sections", which can be an HTML5 section element or a <div class="section">, that will hold content.

Title and Content Elements

Inside of these "sections" you created you'll need to add a title and content. To create the title element, use something like a <p>, <h5>, etc and give it an attribute of data-section-title. After this, you add <div data-section-content> and fill it with your content.

Here's an example structure you might use with the section mixin:

<%= code_example '

Section 1

Content of section 1.

Section 2

Content of section 2.

', :html %>

Here's a look at the available options for the mixins you need to create sections:

<%= code_example ' @include section-container($base-style, $section-type) { ... } /* Container Options */ /* This controls whether or not you include default styles when chaining classes, set to true or false */ $base-style: true /* This controls the style for the section you are using: accordion, tabs, vertical-tabs, vertical-nav, horizontal-nav */ $section-type: accordion @include section($section-type, $title-selector, $content-selector, $title-padding, $title-color, $title-font-size, $title-bg, $title-bg-hover, $title-bg-active, $title-color-active, $content-bg, $content-padding, $section-border-size, $section-border-style, $section-border-color) { ... } /* Section Options */ /* Control which section type styles come across: accordion, tabs, vertical-tabs, vertical-nav, horizontal-nav */ $section-type: accordion /* You can set the class that is used for the title elements */ $title-selector: ".title" /* You can set the class that is used for the content elements */ $content-selector: ".content" /* Give the titles some padding, can be any px or emCalc() value */ $title-padding: $section-padding /* give the titles a color, any color value */ $title-color: $section-title-color /* Set the font size for titles */ $title-font-size: $section-font-size /* Set the background color for titles */ $title-bg: $section-title-bg /* Change title background for hover */ $title-bg-hover: $section-title-bg-hover /* Change title background for .active */ $title-bg-active: $section-title-bg-active /* Change title color for .active */ $title-color-active: $section-title-color /* Content area background color */ $content-bg: $section-content-bg /* Content area padding */ $content-padding: $section-content-padding /* Border size in pixels */ $section-border-size: $section-border-size /* Border style */ $section-border-style: $section-border-style /* Border color */ $section-border-color: $section-border-color', :css %>
Auto Configuration

The default use case for our section plugin is to create accordions on small screens and tabs on large screens.

<%= code_example '

Section 1

Content of section 1.

Section 2

Content of section 2.

', :html %> <%= code_example ' .section-auto-sample { @include section-container($section-type:accordion); & > section { @include section($section-type:accordion, $title-selector:".title-sample", $content-selector:".content-sample"); } @media #{$small} { @include section-container(false,$section-type:tabs); & > section { @include section($section-type:tabs, $title-selector:".title-sample", $content-selector:".content-sample"); } } }', :css %>

Section 1

Content of section 1.

Section 2

Content of section 2.


Accordion Configuration

You can have your section act like an accordion the entire time, like so:

<%= code_example '

Section 1

Content of section 1.

Section 2

Content of section 2.

', :html %> <%= code_example ' .section-auto-sample-accordion { @include section-container($section-type:accordion); & > section { @include section($section-type:accordion, $title-selector:".title-sample", $content-selector:".content-sample"); } }', :css %>

Section 1

Content of section 1.

Section 2

Content of section 2.


Tabs Configuration

You can have your section act like tabs the entire time, like so:

<%= code_example '

Section 1

Content of section 1.

Section 2

Content of section 2.

', :html %> <%= code_example ' .section-auto-sample-tabs { @include section-container($section-type:tabs); & > section { @include section($section-type:tabs, $title-selector:".title-sample", $content-selector:".content-sample"); } }', :css %>

Section 1

Content of section 1.

Section 2

Content of section 2.


Vertical Tabs Configuration

You can have your section act like vertical tabs on large screen and go back to accordion on small screens, like so:

<%= code_example '

Section 1

Content of section 1.

Section 2

Content of section 2.

', :html %> <%= code_example ' .section-auto-sample-vtabs { @include section-container($section-type:accordion); & > section { @include section($section-type:accordion, $title-selector:".title-sample", $content-selector:".content-sample"); } } @media #{$small} { .section-auto-sample-vtabs { @include section-container($section-type:vertical-tabs); & > section { @include section($section-type:vertical-tabs, $title-selector:".title-sample", $content-selector:".content-sample"); } } }', :css %>

Section 1

Content of section 1.

Section 2

Content of section 2.


Vertical Nav Configuration

You can have your section act like vertical navigation the entire time, like so:

<%= code_example ' ', :html %> <%= code_example ' .nav-container { @include grid-row($behavior:nest); > nav { @include grid-column($columns:4); } } .section-auto-sample-vnav { @include section-container($section-type:accordion); & > section { @include section($section-type:accordion, $title-selector:".title-sample", $content-selector:".content-sample"); } } @media #{$small} { .section-auto-sample-vnav { @include section-container($section-type:vertical-nav); & > section { @include section($section-type:vertical-nav, $title-selector:".title-sample", $content-selector:".content-sample"); } } }', :css %>
Horizontal Nav Configuration

You can have your section act like horizontal navigation the entire time, like so:

<%= code_example '

Section 1

Content of section 1.

Section 2

Content of section 2.

', :html %> <%= code_example ' .section-auto-sample-hnav { @include section-container($section-type:accordion); & > section { @include section($section-type:accordion, $title-selector:".title-sample", $content-selector:".content-sample"); } } @media #{$small} { .section-auto-sample-hnav { @include section-container($section-type:horizontal-nav); & > section { @include section($section-type:horizontal-nav, $title-selector:".title-sample", $content-selector:".content-sample"); } } }', :css %>

Section 1

Content of section 1.

Section 2

Content of section 2.


Custom Styles Using the Mixin

You have access to a few options that will make styling the sections really easy and you can add onto it as you want. You'll still need the data-section attribute to match the style you give the mixin.

<%= code_example '

Section 1

Content of section 1.

Section 2

Content of section 2.

', :html %> <%= code_example ' .section-auto-sample-custom { @include section-container($section-type:tabs); & > section { @include section($section-type:tabs, $title-selector:".title-sample", $content-selector:".content-sample", $title-padding: 10px 50px, $title-color:#000, $title-bg:pink, $title-bg-hover:darken(pink,5%), $title-bg-active: #fff, $title-color-active: darken(pink,10%)); } }', :css %>

Section 1

Content of section 1.

Section 2

Content of section 2.


Default SCSS Variables
<%= code_example ' $include-html-section-classes: $include-html-classes !default; /* We use these to set padding and hover factor */ $section-title-padding: emCalc(15px); $section-content-padding: emCalc(15px); $section-function-factor: 10%; /* These style the titles */ $section-title-color: #333; $section-title-bg: #efefef; $section-title-bg-active: darken($section-title-bg, $section-function-factor); $section-title-bg-active-tabs: #fff; $section-title-bg-hover: darken($section-title-bg, $section-function-factor/2); /* Want to control border size, here ya go! */ $section-border-size: 1px; $section-border-style: solid; $section-border-color: #ccc; /* Font controls */ $section-font-size: emCalc(14px); /* Control the color of the background and some size options */ $section-content-bg: #fff; $section-vertical-nav-min-width: emCalc(200px); $section-vertical-tabs-title-width: emCalc(200px); $section-bottom-margin: emCalc(20px); $title-selector: ".title"; $content-selector: ".content"; ', :css %>

Note: emCalc(); is a function we wrote to convert px to em.


Using the JavaScript

You'll need to include zepto.js and foundation.js above the sections plugin. Above your closing </body> tag include the following line of code and make sure you have the JS in your directory.

Read how to install Foundation JavaScript

Required Foundation Library: foundation.section.js

Then, you'll need to add a data-attribute to make the JS work properly on that element. That looks like:

<%= code_example ' ', :html %>

Note: If you are using Sections in a Modal, or they are being loaded via AJAX, or they are hidden when Foundation is initialized, you will need to reflow the sections to get tabs to display properly:

<%= code_example " $('#myModal').on('opened', function () { $(this).foundation('section', 'reflow'); }); ", :json %>
Optional JavaScript Configuration

You can either set these options in a data-options attribute in the markup, data-options="option: value; option2: value syntax", or pass in on initialization.

<%= code_example " { deep_linking: false, one_up: true, rtl: false, callback: function (){} }", :js %>
<%= render "_sidebar-components.html.erb" %>