Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="Widget-Base Widget-MiniFAQ" style="width:95%;">

  <h3>Mini FAQ - Wide view</h3>

  <dl>
    <dt>What is this?</dt>
    <dd> It is an attempt to make a mobile-first, responsive and accessible Mini FAQ.
    </dd> 

    <dt>How does it work?</dt>
    <dd><p>Poke around and see for yourself ... but mainly the following: </p>
      <ul>
        <li>Wide FAQs are split left and right</li>
        <li>Narrow ones up and down</li>
        <li>mobile (by window width) do not collapse any questions and show them all. </li>
      </ul>
      <p>   
        Try resizing the output window.</p>
    </dd> 

    <dt>Can I help?</dt>
    <dd><p>Sure, if you want to. I'm have pinged stack overflow at <a href="http://stackoverflow.com/questions/22764718/">http://stackoverflow.com/questions/22764718/</a> to see if anyone there has any ideas, but please fork away ... this is a work-in progress and probably has some issues still ;)</p>
      <p>
        Someone else may already have created something like this ... if you know about it then please say.
      </p>
    </dd> 

    <dt>What does the FAQ look like when there is a very very very very very very very very very very very very very very very very very very very very long question?</dt>
    <dd>
      You're looking at it :D
    </dd> 

    <dt>What issues are there currently?</dt>
    <dd>
      <p>There are a few issues I am working on at the mo ... and others I am aware of but may have to be left due to time constraints:</p>
      <ul>
        <li>js could be done better ... first attempt at namespacing</li>
        <li>mobile safari  $(window).width() 320px ?</li>
        <li>on transition from mobile to desktop modes, selected answers not restored.</li>
        <li>there may be a memory leak ... but not sure on that</li>
        <li>aria roles ... or is using dd and dt enough?</li>
        <li>cross browser testing (IE8+, modern ff, gc, op etc.)</li>
        <li>I have thought of making DD visible due to class on its DT, but the logic may me unclear when mixed between CSS and JS</li>
      </ul>
    </dd> 
  </dl>
</div>

<!-- ######################################### -->

<div class="Widget-Base Widget-MiniFAQ" style="width:300px;">


  <h3>Mini FAQ<br>Narrow view (greeked text)</h3>

  <p>The only difference between this FAQ and the one above is the size of the container.</p>
  
  <dl>
    <dt>Question 1</dt>
    <dd> 1 Etiam euismod diam vel ultricies consectetur. Nullam sodales mattis mauris at tempus. Proin feugiat mi vitae lacus aliquam sollicitudin. Morbi ullamcorper congue lorem, sit amet consequat nibh iaculis et. 
    </dd> 

    <dt>Question 2</dt>
    <dd> 2 Dat nibh iaculis et. Integer sollicitudin neque eget purus eleifend, quis pretium lectus adipiscing. Etiam sit amet mattis lorem. Curabitur molestie malesuada vestibulum. Nullam porttitor in ipsum id fringilla. 
    </dd> 

    <dt>Question 3 - long -  lectus adipiscing. Etiam sit amet mattis lorem. Curabitur molestie malesuada vestibulum. Nullam porttitor in ipsum id fringilla.</dt>
    <dd>
      <p>3 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean et iaculis nunc. Mauris blandit semper purus ac tempus.</p>
      <p>Curabitur porttitor metus sit amet sem hendrerit sagittis. Vestibulum bibendum lectus erat, vel auctor eros condimentum a. </p> 
    </dd> 
  </dl>

</div>
              
            
!

CSS

              
                
.Widget-Base {
  padding: 0.8em;
  padding-top: 1em;
  margin: 0;
  margin-bottom: 2em;

  border-top: #7DC243 9px solid;
  border-bottom: #7DC243 1px solid;
}

.Widget-MiniFAQ * {
  -webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
  -moz-box-sizing: border-box;    /* Firefox, other Gecko */
  box-sizing: border-box;         /* Opera/IE 8+ */

  font-family: sans-serif;
}

.Widget-MiniFAQ dt.active:before {
  content: "> ";
}

/* Widget css below this line ----------- */

.Widget-MiniFAQ dl a {color: #339;}
.Widget-MiniFAQ dl a:hover {color: #993;}
.Widget-MiniFAQ dt {font-weight: bold; margin: 0.4em 0;}

.Widget-MiniFAQ dt.active,
.Widget-MiniFAQ dt.active a {color: #7DC243;}

.Widget-MiniFAQ dt.active + dd {display: block !important;}

.Widget-MiniFAQ.TwoCol {
  position: relative;
  min-height:400px;
}

.Widget-MiniFAQ.TwoCol dl,
.Widget-MiniFAQ.TwoCol dd {
  width: 48%;
}

.Widget-MiniFAQ.TwoCol dd
{
  position: absolute;
  top: 0;
  right: 0;
  background: #7DC243;
  color:#FFF;
  padding: 0 0.8em 0.4em;
  overflow: auto;
  border: 0px solid #FFF;
  border-width: 15px 0 0
}

.Widget-MiniFAQ.TwoCol dd h3 {
  color: #004687;
  padding-bottom: 1em;
  border-bottom: 1px solid #FFF;
  border-color: rgba(255,255,255,0.5);
}

.Widget-MiniFAQ .AnswerIntro {
  margin: auto;
  text-align: center;
}

              
            
!

JS

              
                
var MiniFAQ = MiniFAQ || {
  settings: {
    mobileRendermodeBreakpoint: 599, // px (make fair size bigger than one col b/p due to bug)
    oneColumnBreakpoint: 400,  //px
    widgetClass: ".Widget-MiniFAQ",
    twoColumnClass: "TwoCol",
    renderMode: "unknown",
    mobile: "m",
    desktop: "d"
  },

  init: function () {

    console.log("init fired");

    // Check if need to trigger mobile view based on viewport width
    this.settings.renderMode = this.getRenderMode();

    if (this.settings.renderMode == this.settings.mobile) {
      console.log("Viewport was too small, mobile view");
      return;
    }

    // Init each instance of MiniFAQ
    var faqs = $(this.settings.widgetClass);
    faqs.each(function (i, faq) { MiniFAQ.initFaq($(faq)); });

  },

  getRenderMode: function () {
    if ($(window).width() <= this.settings.mobileRendermodeBreakpoint) {
      return this.settings.mobile;
    }
    return this.settings.desktop;
  },

  initFaq: function (faq) {
    // hide all answers
    $(this.settings.widgetClass + " dd").hide();
    
    if (faq.width() > this.settings.oneColumnBreakpoint) {
      this.initTwoCol(faq);
    }
    else {
      this.initOneCol(faq);
    }
  },

  initOneCol: function (faq) {
    
    //faq.find(".status").remove();
    //faq.prepend("<p class=status>One Column</p>");
    
    // add links
    faq.find("dt").wrapInner("<a href='#void1'></a>");
    // add handlers
    faq.find('dl').on("click", "dt a", function (e) {
      $(this).parent().next("dd").toggle("fast");
      e.preventDefault();
    });
  },

  initTwoCol: function (faq) {
    
    //faq.find(".status").remove();
    //faq.prepend("<p class=status>Two Columns</p>");
    
    // apply styling
    faq.addClass(this.settings.twoColumnClass);
    
    var qs = faq.find("dt");
    // add question to top of each answer
    qs.each(function (i, e) {
      var q = $(e);
      q.next("dd").prepend("<h3 class='MiniFAQ-QuestionClone'>" + q.text() + "</h3>");
    });
    // add links after setting titles on answers
    qs.wrapInner("<a href='#void2'></a>");
    
    // apply delegated handlers to entire faq
    faq.find('dl').on("click", "dt a", function (e) {
      // hide all answers
      faq.find('dd').hide();
      faq.find('dt').removeClass("active");
      // show current answer
      var dt = $(this).parent();
      dt.addClass("active");
      dt.next("dd").show();
      e.preventDefault();
    });
    // set height of all absolutely positioned answers to match container
    faq.find('dd').height(faq.height()-5); 
    
    // if none are active, make first active
    if (faq.find('.active').length == 0) {
      faq.find("dt").first().addClass("active");
    } 
  },

  resize: function () {
    console.log("resize fired, width " + $(window).width());
    var newRenderMode = this.getRenderMode();
    if (this.settings.renderMode != newRenderMode) {
      console.warn("renderMode was '" + this.settings.renderMode + "' changed to '" + newRenderMode + "'");
      if (newRenderMode == this.settings.mobile) {
        this.reset();
      }
      this.init();
    }
  },

  // Reset funtion for when going from desktop to mobile render mode
  reset: function () {
    console.log("reset fired");
    var w = $(this.settings.widgetClass);
    w.removeClass(this.settings.twoColumnClass);
    // remove clones of question in answer for two column items
    w.find(".MiniFAQ-QuestionClone").remove();
    // reset height and show
    w.find("dd").height('auto').show();
    // remove links
    w.find("dt").each(function (i, e) {
      var dt = $(e);
      var text = dt.text();
      dt.empty();
      dt.append(text);
    });
    // remove handlers
    w.find("dl").off();
  }
};

$(window).ready(function () {
  MiniFAQ.init();
});

$(window).resize(function () {
  clearTimeout($(this).data('timer'));
  $(this).data('timer', setTimeout(function() {MiniFAQ.resize();}, 300));
});

              
            
!
999px

Console