HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<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>
.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;
}
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));
});
Also see: Tab Triggers