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.
<p>status</p>
<div class="accordionContainer">
<ul>
<li>
<h1 class="title">Slide One</h1>
<div class="content">
Content forslide one is rendered here. Content for one is rendered from here. Content for slide one is from here. Content for one is rendered here. Content is here. Content for one is from here. Content for slide is from here. Content for one is rendered from here. Content forslide one is rendered here. Content for one is rendered from here. Content for slide one is from here. Content for one is rendered here. Content is here. Content for one is from here. Content for slide is from here. Content for one is rendered from here. Content forslide one is rendered here. Content for one is rendered from here. Content for slide one is from here. Content for one is rendered here. Content is here. Content for one is from here. Content for slide is from here. Content for one is rendered from here. Content forslide one is rendered here. Content for one is rendered from here. Content for slide one is from here. Content for one is rendered here. Content is here. Content for one is from here. Content for slide is from here. Content for one is rendered from here.
</div>
</li>
<li>
<h1 class="title">Slide Two</h1>
<div class="content">
Content for slide two is rendered from here.
</div>
</li>
<li>
<h1 class="title">Slide Three</h1>
<div class="content">
Content for slide three is rendered from here.
</div>
</li>
</ul>
</div>
.uberAccordion {
padding: 0;
}
.uberAccordion > li {
list-style: none;
margin: 0;
}
.uberAccordion > li > h1 {
background-color: #e3e3e3;
margin: 0;
font-size: 16px;
padding: 5px;
cursor: pointer;
}
.uberAccordion > li > div {
background-color: #aaa;
padding: 15px;
}
/* Default CSS for sub-accordion */
.uberAccordion .uberAccordion > li > h1 {
font-size: 12px;
padding: 5px;
}
.uberAccordion .uberAccordion > li > div {
background-color: #666666;
color: #FFF;
}
/* Default CSS for vertical orientation */
.uberAccordion.accordion-vertical {
height: 300px;
}
.uberAccordion .uberAccordion.accordion-vertical {
height: 260px;
}
(function($) {
var UberAccordion = function(parent, options) {
// Default settings
var defaults = {
verticalClass : 'accordion-vertical', // Class to apply when orientation is vertical
horizontalClass : 'accordion-horizontal', // Class to apply when orientation is horizontal
activeSlideClass : 'active', // Class to apply on active accordion slide
orientation : 'vertical', // Accordion orientation
orientationQuery : '(max-width: 700px)', // Media query which causes orientation change
startSlide : 1, // Starter slide
openMultiple : false, // Set to true for multiple open slides at a time
autoPlay : false, // Auto-play
autoPlaySpeed : 5000, // Auto-play interval
slideEvent : 'click', // Open slide event
animationSpeed : 333, // Animation Speed
headerItem : typeof options.headerClass === 'undefined' ? 'h1' : '.' + options.headerClass, // Header class
contentItem : typeof options.contentClass === 'undefined' ? 'div' : '.' + options.contentClass // Content class
};
var settings = $.extend(true, {}, defaults, options); // Add user settings with overwrite
var el = parent.children('ul'); // Accordion container
var slides = el.children().children(settings.contentItem); // Accordion slides
var headers = el.children('li').children(settings.headerItem); // Accordion headers
var state = {}; // Keeps current accordion state (ie. currentSlide, orientation, etc)
var showSlideCallback = function() {
el.trigger('ua-slide-changed');
}
this.toggleSlide = function(slideNumber, override) {
if(!settings.openMultiple) {
// If multiple is disabled, open current slide and close other
if(slideNumber !== state.currentSlide || override) {
this.hideSlide($(slides[el.children('li.' + settings.activeSlideClass).index()]));
this.showSlide($(slides[slideNumber]));
}
} else {
// If multiple is enabled, toggle clicked slide
if($(slides[slideNumber]).parent().hasClass(settings.activeSlideClass)) {
this.hideSlide($(slides[slideNumber]));
} else {
this.showSlide($(slides[slideNumber]));
}
}
state.currentSlide = slideNumber;
}
this.showSlide = function(slide) {
if(state.orientation === "horizontal") {
slide.slideDown(settings.animationSpeed, showSlideCallback);
} else {
slide.parent().animate({
width: slide.parent().attr('data-width')
}, settings.animationSpeed, showSlideCallback);
}
slide.parent().addClass(settings.activeSlideClass);
}
this.hideSlide = function(slide) {
if(state.orientation === "horizontal") {
slide.slideUp(settings.animationSpeed);
} else {
slide.parent().animate({
width: slide.parent().children(settings.headerItem).attr('data-width')
}, settings.animationSpeed);
}
slide.parent().removeClass(settings.activeSlideClass);
}
this.setOrientation = function(orientation) {
el.removeClass(settings.horizontalClass + ' ' + settings.verticalClass);
switch(orientation) {
case 'vertical':
el.addClass(settings.verticalClass);
$('p').html('switch vertical mode');
installVertical();
break;
case 'horizontal':
el.addClass(settings.horizontalClass);
// Don't wipe styles on first load
if(typeof state.orientation !== 'undefined') {
uninstallVertical();
}
$('p').html('switch horizontal mode');
applyHorizontalBase();
break;
}
$(slides[state.currentSlide])
.show()
.parent().addClass(settings.activeSlideClass);
state.orientation = orientation;
}
this.setAutoplay = function(enabled) {
var _this = this;
if(enabled) {
state.autoPlayInterval = setInterval(function() {
var totalSlides = el.children('li').children(settings.headerItem).length;
state.currentSlide = state.currentSlide < (totalSlides - 1) ? state.currentSlide + 1 : 0;
_this.toggleSlide(state.currentSlide, true);
}, settings.autoPlaySpeed);
} else {
if(typeof state.autoPlayInterval !== "undefined") {
clearInterval(state.autoPlayInterval);
}
}
}
// Installs styling and events for vertical slider
var installVertical = function() {
uninstallVertical();
$(window).bind('resize', resizeContainer);
applyVerticalBase();
calculateSizes();
}
var applyVerticalBase = function() {
slides.parent().css(CSSObject.SlideContainers);
CSSObject.Headers.functions.setTransforms(-$(headers[0]).outerHeight(), 0);
CSSObject.Slides.functions.setHeight(el.outerHeight(true));
CSSObject.Slides.functions.setLeft($(headers[0]).outerHeight(true));
headers.css(CSSObject.Headers.defaults);
headers.setOuterWidth(el.innerHeight());
slides.css(CSSObject.Slides.defaults);
}
var applyHorizontalBase = function() {
slides.css({ 'display': 'none' });
}
// Calculate sizes and save widths in data attributes
// for safe animating
var calculateSizes = function() {
var totalHeaderWidth = 0;
var currentContainer = el.children('li.' + settings.activeSlideClass);
var currentHeader = currentContainer.children(settings.headerItem);
var parentWidth = parent.innerWidth();
var slideWidth, headerWidth;
headers.each(function() {
headerWidth = $(this).outerHeight(true);
totalHeaderWidth += headerWidth;
$(this).parent().width(headerWidth);
$(this).attr('data-width', headerWidth);
});
slides.each(function() {
slideWidth = parentWidth - totalHeaderWidth;
headerWidth = $(this).parent().children(settings.headerItem).outerHeight(true);
$(this).setOuterWidth(slideWidth);
$(this).attr('data-width', slideWidth);
$(this).parent().attr('data-width', slideWidth + headerWidth - 1);
});
currentContainer.width(parentWidth - totalHeaderWidth + currentHeader.outerHeight(true) - 1);
}
// Called on resize event - adjusts accordion width according
// to parent to allow fluid design
var resizeContainer = function() {
if(state.orientation === "vertical") {
// If we don't hide the accordion before reading the parent's width value
// the parent will not auto-adjust in width
el.css('display', 'none');
el.width(parent.innerWidth());
el.css('display', 'block');
calculateSizes();
}
}
// Uninstalls vertical events and styles
var uninstallVertical = function() {
$(window).unbind('resize', resizeContainer);
el.removeAttr('style');
el.children('li').removeAttr('style');
slides.removeAttr('style');
headers.removeAttr('style');
}
// Constructor
var initialize = function(scope) {
if(typeof options.headerClass !== 'undefined' && typeof options.contentClass === 'undefined') {
throw("Content class must be defined along with header class.");
}
el.addClass('uberAccordion');
headers.on(settings.slideEvent, function(e) {
scope.toggleSlide(el.children('li').children(settings.headerItem).index(this));
scope.setAutoplay(false);
e.stopImmediatePropagation();
});
// If set to responsive, handle orientation changes
if(settings.orientationQuery) {
state.matchesQuery = window.matchMedia(settings.orientationQuery).matches;
// If media query match state changes, switch orientation
$(window).resize(function() {
if(window.matchMedia(settings.orientationQuery).matches !== state.matchesQuery) {
state.matchesQuery = window.matchMedia(settings.orientationQuery).matches;
scope.setOrientation(state.orientation === 'vertical' ? 'horizontal' : 'vertical');
}
});
if(state.matchesQuery) {
settings.orientation = settings.orientation === 'vertical' ? 'horizontal' : 'vertical';
}
}
state.currentSlide = settings.startSlide - 1;
$(slides[state.currentSlide]).parent().addClass(settings.activeSlideClass);
scope.setOrientation(settings.orientation);
// Set-up auto-play if enabled
if(settings.autoPlay && !settings.openMultiple) {
scope.setAutoplay(true);
}
el.attr('data-accordion-active', "true");
}
initialize(this);
}
$.fn.uberAccordion = function(options) {
// Avoid double instantiating
return $(this).children("ul").attr('data-accordion-active') === "true" ? false : new UberAccordion(this, options);
}
// Set total width with padding and borders (outerWidth)
$.fn.setOuterWidth = function(value) {
$(this).each(function() {
var paddingLeft = isNaN(parseInt($(this).css('padding-left'), 10)) || $(this).css('padding-left') === '100%' ? 0 : parseInt($(this).css('padding-left'), 10);
var paddingRight = isNaN(parseInt($(this).css('padding-right'), 10)) || $(this).css('padding-right') === '100%' ? 0 : parseInt($(this).css('padding-right'), 10);
var borderLeft = isNaN(parseInt($(this).css('border-left-width'), 10)) ? 0 : parseInt($(this).css('border-left-width'), 10);
var borderRight = isNaN(parseInt($(this).css('border-right-width'), 10)) ? 0 : parseInt($(this).css('border-right-width'), 10);
$(this).width(value - paddingLeft - paddingRight - borderLeft - borderRight);
});
}
// CSS Object for vertical accordion
// These base styles are necessary for all vertical accordions
var CSSObject = {
SlideContainers: {
'display': 'inline',
'overflow': 'hidden',
'float': 'left',
'height': '100%',
'position': 'relative'
},
Headers: {
defaults: {
'display': 'block',
'float': 'left',
'white-space': 'nowrap',
'position': 'absolute',
'top': '0',
'left': '0',
'transform-origin': '0 100% 0',
'-webkit-transform-origin': '0 100%',
'-moz-transform-origin': '0 100%',
'-ms-transform-origin': '0 100%'
},
functions: {
setTransforms: function(x, y) {
CSSObject.Headers.defaults['-webkit-transform'] = "rotate(90deg) translate(" + x + "px, " + y + "px)";
CSSObject.Headers.defaults['-moz-transform'] = "rotate(90deg) translate(" + x + "px, " + y + "px)";
CSSObject.Headers.defaults['-ms-transform'] = "rotate(90deg) translate(" + x + "px, " + y + "px)";
CSSObject.Headers.defaults['-sand-transform'] = "rotate(90deg) translate(" + x + "px, " + y + "px)";
CSSObject.Headers.defaults['transform'] = "rotate(90deg) translate(" + x + "px, " + y + "px)";
//CSSObject.Headers.defaults['filter'] = "progid:DXImageTransform.Microsoft.BasicImage(rotation=1)";
}
}
},
Slides: {
defaults: {
'float': 'left',
'height': 0,
'display': 'block',
'position': 'absolute',
'top': '0'
},
functions: {
setHeight: function(height) { CSSObject.Slides.defaults.height = height; },
setLeft: function(left) { CSSObject.Slides.defaults.left = left; }
}
}
}
$('.accordionContainer').uberAccordion({
headerClass: 'title',
contentClass: 'content'
});
}(jQuery));
Also see: Tab Triggers