Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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

Save Automatically?

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

              
                <p class="note">Please see this in a bigger screen.</p>

<div class="block first">
	<div class="box">Go on, start scrolling...</div>
	<div class="sticky">
    <div class="sticky-content">I'm sticky</div>
		<div class="sticky-floating"></div>
		<div class="sticky-exclude" id="js-sticky-exclude">
      I'll be covered by the white box bellow
    </div>
	</div>
</div>

<div class="block" id="js-sticky-next">
  <div class="box full">
    I'll be above the <span class="highlight">red</span> box...
  </div>
</div>

<div class="block">
  <div class="box full last">
    That's all. Have a nice day!
  </div>
</div>

<svg width="0" height="0">
  <defs>
    <clipPath id="clipPath">
      <rect class="clipPathRect"></rect>
    </clipPath>
  </defs>
</svg>

              
            
!

CSS

              
                $highlight: #cc6060;

:root {
  // Clip Path to "mask" the element on scrolling.
  --js-sticky-exclude-path-height: 100%;
}

// Styles to make sticky work are at ".sticky-exclude"

body {
  background: #b3ced6;
  color: #394c53;
  font-family: "Trebuchet MS", Helvetica, sans-serif;
  line-height: 1.4;
  padding: 2rem 0;
  box-sizing: border-box;
  
  * {
    box-sizing: inherit;
  }
}

.block {
	position: relative;
	display: flex;
	box-sizing: border-box;
	align-items: flex-start;
	max-width: 35rem;
	margin: 0 auto;
  margin-top: 1rem;
  
  &.first {
    // optional: so .sticky-floating can be above of the next white box
  	z-index: 1;
  }
}

.sticky {
  position: -webkit-sticky;
  position: sticky;
  top: 10px;
  width: 33%;
  min-width: 11rem;
	margin: 0 1%;
  top: 10px;
}

.sticky-content {
  height: 150px;
  background: #fdfefe;
  box-shadow: 0px 1px 2px #4761757a;
  padding: 1rem;
}

.sticky-floating {
  position: absolute;
  top: 65px;
  left: 20px;
  width: 50px;
  height: 120px;
  background: #394c53;
  box-shadow: 0px 3px 3px #4761757a;
  z-index: 1;
}

.sticky-exclude {
  $height: 5.5rem; // It's hardcoded, but can be dynamic too
  $marginTop: 1rem; // Optional
	position: relative;
	height: $height;
  margin-top: $marginTop;
  margin-bottom: calc(#{-$height} + #{-$marginTop}); // Exclude it from ".sticky" height  
	clip-path: polygon(
    0% 0%, // top left corner
    100% 0%, // top right corner
    100% var(--js-sticky-exclude-path-height), // bottom right corner
    0% var(--js-sticky-exclude-path-height) // bottom left corner
  );

  background: $highlight;
  border-bottom: 1px solid #476175;
  padding: 2rem 1rem 1rem;
  color: #fdfefe;
  font-size: 0.9rem;
}

// Layout styling...

.box {
	width: 63%;
	margin: 0 1%;
	height: 70vh;
  padding: 1rem;
	background: #fdfefe;
  box-shadow: 0px 1px 2px #4761757a;
  
  &.full {
    width: 100%;
  }
  
  &.last {
    height: 50vh;
  }
}

.highlight {
  color: $highlight;
}

.note {
  text-align: center;
  font-size: 0.8rem;
  color: $highlight;
  margin-top: -1rem;
  
  @media screen and (min-height: 22rem) {
    display: none;
  }
}
              
            
!

JS

              
                let root = document.documentElement;
let hasClipPathSupport = false;

if ('CSS' in window) {
  hasClipPathSupport =
    CSS.supports('clip-path: polygon(0% 0%, 100% 0%, 100% 0px, 0% 0px)') ||
    CSS.supports('-webkit-clip-path: polygon(0% 0%, 100% 0%, 100% 0px, 0% 0px)');
}

const elStickyExclude = document.getElementById('js-sticky-exclude');
const elStickyNext = document.getElementById('js-sticky-next');
const stickyExcludeHeight = elStickyExclude.offsetHeight;

function lookForStickyExclude() {
  const extraTop = elStickyExclude.getBoundingClientRect().top + stickyExcludeHeight;
  const nextTop = elStickyNext.getBoundingClientRect().top;
  const diff = nextTop - extraTop;
  const pathHeight = stickyExcludeHeight + diff;
  
  const value = `${pathHeight}px`;

  if(hasClipPathSupport) {
    root.style.setProperty('--js-sticky-exclude-path-height', value);
  } else {
    // IE or Edge: ¯\_(ツ)_/¯
    elStickyExclude.style.maxHeight = value;
    elStickyExclude.style.overflow = 'hidden';
    // WIP: Working on another solution with SVG clip-path
    // elStickyExclude.style.clipPath = "url(#clipPath)";
  } 
}


window.addEventListener('scroll', lookForStickyExclude)
              
            
!
999px

Console