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.
<header>
Header
</header>
<main>
<div class="center">
<div class="fullwidth">
Full Width Block
</div>
<div class="row">
<div class="left-sidebar">
<div class="content">
Sticky Sidebar
</div>
</div>
<div class="main-content">
Main Content
</div>
</div>
<div class="fullwidth two">
Full Width Block
</div>
</div>
</main>
<footer>
Footer
</footer>
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
body {
font-family: 'Roboto', sans-serif;
color: #ffffff;
font-size: 20px;
width: 100%;
margin: 0;
padding: 0;
}
html {
width: 100%;
margin: 0;
padding: 0;
}
header {
display: block;
text-align: center;
background-color: blue;
padding: 40px;
}
main {
margin: 40px 0;
}
.center {
max-width: 1080px;
margin: 0 auto;
position: relative;
}
.center:after { /*CLEARFIX*/
visibility: hidden;
display: block;
font-size: 0;
content: " ";
clear: both;
height: 0;
}
.left-sidebar {
float: left;
width: 430px;
margin-right: 10px;
text-align: center;
}
.row {
float: left;
width: 100%;
clear: both;
margin: 0;
}
.content {
padding: 80px 20px;
background-color: red;
}
.main-content {
float: left;
width: 620px;
margin-left: 20px;
background-color: blue;
height: 800px;
padding: 40px;
text-align: center;
}
.fullwidth {
float: left;
clear: both;
margin-bottom: 40px;
width: 100%;
padding: 40px;
text-align: center;
height: 300px;
background-color: blue;
}
.fullwidth.two {
margin-bottom: 0;
margin-top: 40px;
}
footer {
display: block;
text-align: center;
background-color: blue;
padding: 50px 20px;
}
$(function() {
$('.left-sidebar').StickySidebar({
// Settings
additionalMarginTop: 40
});
});
(function ($) {
$.fn.StickySidebar = function (options) {
var defaults = {
'containerSelector': '',
'additionalMarginTop': 0,
'additionalMarginBottom': 0,
'updateSidebarHeight': true,
'minWidth': 0,
'disableOnResponsiveLayouts': true,
'sidebarBehavior': 'modern'
};
options = $.extend(defaults, options);
// Validate options
options.additionalMarginTop = parseInt(options.additionalMarginTop) || 0;
options.additionalMarginBottom = parseInt(options.additionalMarginBottom) || 0;
tryInitOrHookIntoEvents(options, this);
// Try doing init, otherwise hook into window.resize and document.scroll and try again then.
function tryInitOrHookIntoEvents(options, $that) {
var success = tryInit(options, $that);
if (!success) {
console.log('TST: Body width smaller than options.minWidth. Init is delayed.');
$(document).scroll(function (options, $that) {
return function (evt) {
var success = tryInit(options, $that);
if (success) {
$(this).unbind(evt);
}
};
}(options, $that));
$(window).resize(function (options, $that) {
return function (evt) {
var success = tryInit(options, $that);
if (success) {
$(this).unbind(evt);
}
};
}(options, $that))
}
}
// Try doing init if proper conditions are met.
function tryInit(options, $that) {
if (options.initialized === true) {
return true;
}
if ($('body').width() < options.minWidth) {
return false;
}
init(options, $that);
return true;
}
// Init the sticky sidebar(s).
function init(options, $that) {
options.initialized = true;
// Add CSS
$('head').append($('<style>.StickySidebar:after {content: ""; display: table; clear: both;}</style>'));
$that.each(function () {
var o = {};
o.sidebar = $(this);
// Save options
o.options = options || {};
// Get container
o.container = $(o.options.containerSelector);
if (o.container.length == 0) {
o.container = o.sidebar.parent();
}
// Create sticky sidebar
o.sidebar.parents().css('-webkit-transform', 'none'); // Fix for WebKit bug - https://code.google.com/p/chromium/issues/detail?id=20574
o.sidebar.css({
'position': 'relative',
'overflow': 'visible',
// The "box-sizing" must be set to "content-box" because we set a fixed height to this element when the sticky sidebar has a fixed position.
'-webkit-box-sizing': 'border-box',
'-moz-box-sizing': 'border-box',
'box-sizing': 'border-box'
});
// Get the sticky sidebar element. If none has been found, then create one.
o.stickySidebar = o.sidebar.find('.StickySidebar');
if (o.stickySidebar.length == 0) {
o.sidebar.find('script').remove(); // Remove <script> tags, otherwise they will be run again on the next line.
o.stickySidebar = $('<div>').addClass('StickySidebar').append(o.sidebar.children());
o.sidebar.append(o.stickySidebar);
}
// Get existing top and bottom margins and paddings
o.marginTop = parseInt(o.sidebar.css('margin-top'));
o.marginBottom = parseInt(o.sidebar.css('margin-bottom'));
o.paddingTop = parseInt(o.sidebar.css('padding-top'));
o.paddingBottom = parseInt(o.sidebar.css('padding-bottom'));
// Add a temporary padding rule to check for collapsable margins.
var collapsedTopHeight = o.stickySidebar.offset().top;
var collapsedBottomHeight = o.stickySidebar.outerHeight();
o.stickySidebar.css('padding-top', 1);
o.stickySidebar.css('padding-bottom', 1);
collapsedTopHeight -= o.stickySidebar.offset().top;
collapsedBottomHeight = o.stickySidebar.outerHeight() - collapsedBottomHeight - collapsedTopHeight;
if (collapsedTopHeight == 0) {
o.stickySidebar.css('padding-top', 0);
o.stickySidebarPaddingTop = 0;
}
else {
o.stickySidebarPaddingTop = 1;
}
if (collapsedBottomHeight == 0) {
o.stickySidebar.css('padding-bottom', 0);
o.stickySidebarPaddingBottom = 0;
}
else {
o.stickySidebarPaddingBottom = 1;
}
// We use this to know whether the user is scrolling up or down.
o.previousScrollTop = null;
// Scroll top (value) when the sidebar has fixed position.
o.fixedScrollTop = 0;
// Set sidebar to default values.
resetSidebar();
o.onScroll = function (o) {
// Stop if the sidebar isn't visible.
if (!o.stickySidebar.is(":visible")) {
return;
}
// Stop if the window is too small.
if ($('body').width() < o.options.minWidth) {
resetSidebar();
return;
}
// Stop if the sidebar width is larger than the container width (e.g. the theme is responsive and the sidebar is now below the content)
if (o.options.disableOnResponsiveLayouts) {
var sidebarWidth = o.sidebar.outerWidth(o.sidebar.css('float') == 'none');
if (sidebarWidth + 50 > o.container.width()) {
resetSidebar();
return;
}
}
var scrollTop = $(document).scrollTop();
var position = 'static';
// If the user has scrolled down enough for the sidebar to be clipped at the top, then we can consider changing its position.
if (scrollTop >= o.container.offset().top + (o.paddingTop + o.marginTop - o.options.additionalMarginTop)) {
// The top and bottom offsets, used in various calculations.
var offsetTop = o.paddingTop + o.marginTop + options.additionalMarginTop;
var offsetBottom = o.paddingBottom + o.marginBottom + options.additionalMarginBottom;
// All top and bottom positions are relative to the window, not to the parent elemnts.
var containerTop = o.container.offset().top;
var containerBottom = o.container.offset().top + getClearedHeight(o.container);
// The top and bottom offsets relative to the window screen top (zero) and bottom (window height).
var windowOffsetTop = 0 + options.additionalMarginTop;
var windowOffsetBottom;
var sidebarSmallerThanWindow = (o.stickySidebar.outerHeight() + offsetTop + offsetBottom) < $(window).height();
if (sidebarSmallerThanWindow) {
windowOffsetBottom = windowOffsetTop + o.stickySidebar.outerHeight();
}
else {
windowOffsetBottom = $(window).height() - o.marginBottom - o.paddingBottom - options.additionalMarginBottom;
}
var staticLimitTop = containerTop - scrollTop + o.paddingTop + o.marginTop;
var staticLimitBottom = containerBottom - scrollTop - o.paddingBottom - o.marginBottom;
var top = o.stickySidebar.offset().top - scrollTop;
var scrollTopDiff = o.previousScrollTop - scrollTop;
// If the sidebar position is fixed, then it won't move up or down by itself. So, we manually adjust the top coordinate.
if (o.stickySidebar.css('position') == 'fixed') {
if (o.options.sidebarBehavior == 'modern') {
top += scrollTopDiff;
}
}
if (o.options.sidebarBehavior == 'stick-to-top') {
top = options.additionalMarginTop;
}
if (o.options.sidebarBehavior == 'stick-to-bottom') {
top = windowOffsetBottom - o.stickySidebar.outerHeight();
}
if (scrollTopDiff > 0) { // If the user is scrolling up.
top = Math.min(top, windowOffsetTop);
}
else { // If the user is scrolling down.
top = Math.max(top, windowOffsetBottom - o.stickySidebar.outerHeight());
}
top = Math.max(top, staticLimitTop);
top = Math.min(top, staticLimitBottom - o.stickySidebar.outerHeight());
// If the sidebar is the same height as the container, we won't use fixed positioning.
var sidebarSameHeightAsContainer = o.container.height() == o.stickySidebar.outerHeight();
if (!sidebarSameHeightAsContainer && top == windowOffsetTop) {
position = 'fixed';
}
else if (!sidebarSameHeightAsContainer && top == windowOffsetBottom - o.stickySidebar.outerHeight()) {
position = 'fixed';
}
else if (scrollTop + top - o.sidebar.offset().top - o.paddingTop <= options.additionalMarginTop) {
// Stuck to the top of the page. No special behavior.
position = 'static';
}
else {
// Stuck to the bottom of the page.
position = 'absolute';
}
}
/*
* Performance notice: It's OK to set these CSS values at each resize/scroll, even if they don't change.
* It's way slower to first check if the values have changed.
*/
if (position == 'fixed') {
o.stickySidebar.css({
'position': 'fixed',
'width': o.sidebar.width(),
'top': top,
'left': o.sidebar.offset().left + parseInt(o.sidebar.css('padding-left'))
});
}
else if (position == 'absolute') {
var css = {};
if (o.stickySidebar.css('position') != 'absolute') {
css.position = 'absolute';
css.top = scrollTop + top - o.sidebar.offset().top - o.stickySidebarPaddingTop - o.stickySidebarPaddingBottom;
}
css.width = o.sidebar.width();
css.left = '';
o.stickySidebar.css(css);
}
else if (position == 'static') {
resetSidebar();
}
if (position != 'static') {
if (o.options.updateSidebarHeight == true) {
o.sidebar.css({
'min-height': o.stickySidebar.outerHeight() + o.stickySidebar.offset().top - o.sidebar.offset().top + o.paddingBottom
});
}
}
o.previousScrollTop = scrollTop;
};
// Initialize the sidebar's position.
o.onScroll(o);
// Recalculate the sidebar's position on every scroll and resize.
$(document).scroll(function (o) {
return function () {
o.onScroll(o);
};
}(o));
$(window).resize(function (o) {
return function () {
o.stickySidebar.css({'position': 'static'});
o.onScroll(o);
};
}(o));
// Reset the sidebar to its default state
function resetSidebar() {
o.fixedScrollTop = 0;
o.sidebar.css({
'min-height': '1px'
});
o.stickySidebar.css({
'position': 'static',
'width': ''
});
}
// Get the height of a div as if its floated children were cleared. Note that this function fails if the floats are more than one level deep.
function getClearedHeight(e) {
var height = e.height();
e.children().each(function () {
height = Math.max(height, $(this).height());
});
return height;
}
});
}
}
})(jQuery);
Also see: Tab Triggers