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

              
                header
main
  article.article
    h2 Dynamic Sticky Sidebar Component
    
    p Click on the elements below to add them to the sidebar component.
    
    ul.items.grid
      - for (let i = 0; i < 12; i++)
        li
          button.item
            .thumbnail
            .info
              .title
              .subtitle
    
    p Added this extra content for spacing purposes. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed sed porta arcu, vitae hendrerit magna. Nullam nec mi augue. Morbi non odio mauris. Vivamus accumsan lectus ac libero ornare lacinia. Etiam quis purus nec augue fermentum interdum. Aliquam sit amet dui rutrum, aliquam augue ut, iaculis arcu. Nunc euismod, magna sit amet gravida gravida, erat diam scelerisque massa, id dignissim sapien elit quis mauris. In interdum lectus id mattis aliquam. Ut pellentesque accumsan consectetur. Curabitur varius mollis lacus, nec rhoncus massa facilisis ac.

    p Maecenas fringilla consectetur blandit. Phasellus pulvinar, eros eleifend fermentum accumsan, leo massa rutrum lectus, nec cursus orci turpis at libero. Quisque dolor ex, suscipit id felis ac, bibendum aliquam enim. Curabitur mollis ligula a ipsum fermentum, in volutpat arcu mattis. Nulla facilisi. Duis mattis venenatis erat non sollicitudin. Phasellus nec augue eget magna eleifend laoreet. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Sed bibendum dolor euismod erat rhoncus, et mattis enim facilisis. Quisque arcu est, molestie sed massa nec, gravida feugiat massa. Donec id velit mi. Curabitur accumsan elit in magna mollis, quis pharetra felis faucibus.

    p Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. In molestie magna sit amet lobortis convallis. Etiam at magna eget risus venenatis luctus quis nec justo. Pellentesque elementum imperdiet mi. Vivamus nec fringilla est. Suspendisse tincidunt euismod turpis quis ornare. Donec et dui in massa porttitor malesuada. Aliquam erat volutpat.
  aside.sidebar
    .component 
      .header Selected Items
      .content
        .empty-text Your cart is currently empty
        ul.items.list
      .footer
        button.button Checkout
        button.button.empty-cart Empty Cart
footer
              
            
!

CSS

              
                :root {
  --space-xs: calc(var(--space) / 4);
  --space-sm: calc(var(--space) / 2);
  --space: 2rem;
  --space-md: calc(var(--space) * 2);
  
  --color-primary: lightgray;
  --color-accent: whitesmoke;
  --color-dark: black;
  --color-mid: gray;
  --color-light: white;
  --color-highlight: dodgerblue;
  
  --radius:  0.125rem;  
}

/* Here are the base styles for the main layout and sticky component */
* {
  box-sizing: border-box;
}

main {
  display: flex;
  flex-wrap: wrap;
}

.article {
  flex-basis: 0;
  flex-grow: 999;
  min-width: 40%;
}

.sidebar {
  --offset: var(--space);
  flex-grow: 1;
  flex-basis: 300px;
  align-self: start;
  position: sticky;
  top: var(--offset);
}

.component {
  display: grid;
  grid-template-rows: auto 1fr auto;
}

.component .content {
  max-height: 500px;
  overflow-y: auto;
}

.sidebar .component {
  max-height: calc(100vh - var(--offset) * 2);
}

/*
 * Other styles
**/
.visually-hidden {
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  height: 1px;
  overflow: hidden;
  position: absolute;
  white-space: nowrap;
  width: 1px;
}

html,
body {
  height: 100%;
}

[hidden] {
  display: none;
}

body {
  margin: 0 auto;
  padding: 0 var(--space);
  font-family: 'Helvetica Neue', sans-serif;
  font-size: 1.25rem;
  line-height: 1.4;
  max-width: 1400px;
}

h2 {
  font-size: clamp(2rem, 4vw, 3.5rem);
  font-weight: 600;
  line-height: 1.2;
}
  
p {
  max-width: 70ch;
  font-size: clamp(1rem, 4vw, 1.25rem);
}

header,
footer {
  display: flex;
  align-items: center;
  justify-content: center;
  margin: var(--space-md) 0;
  padding: var(--space);
  height: 40vh;
  background:
    repeating-linear-gradient(
      45deg,
      transparent,
      transparent 12px,
      var(--color-accent) 12px,
      var(--color-accent) 24px
  );
}

  /* Until flexbox gap is supported in all modern browsers, we use the negative margin trick to create space between the children elements */
main {
  margin-bottom: calc(-1 * var(--space-md));
  margin-left: calc(-1 * var(--space-md));
}
  
main > * {
  margin-bottom: var(--space-md);
  margin-left: var(--space-md);
}

article  > * + * {
  margin-top: var(--space);
}

.component {
  position: relative;
  border: 1px solid var(--color-primary);
  box-shadow: 
    0 1px 2px rgba(0,0,0,0.04), 
    0 2px 4px rgba(0,0,0,0.04), 
    0 4px 8px rgba(0,0,0,0.03), 
    0 8px 16px rgba(0,0,0,0.03),
    0 16px 32px rgba(0,0,0,0.02), 
    0 32px 64px rgba(0,0,0,0.02);
}

.component .header,
.component .footer {
  padding: var(--space-sm);
  text-align: center;
}

.component .header {
  border-bottom: inherit;
}

.component .footer {
  display: flex;
  justify-content: space-between;
  border-top: inherit;
}

.component .content {
  padding: var(--space-sm);
  max-height: 500px;
  overflow-y: auto;
  color: var(--color-dark);
}

.empty-text {
  padding: var(--space);
  text-align: center;
  color: var(--color-primary);
}

.item {
  position: relative;
  appearance: none;
  display: flex;
  align-items: center;
  padding: var(--space);
  outline: none;
  width: 100%;
  background-color: var(--color-accent);
  border: 1px solid var(--color-accent);
  border-radius: var(--radius);
  cursor: pointer;
}

.item:active {
  transform: translateY(1px);
}

.item:focus {
  outline: 2px solid var(--color-highlight);
}

.item > * {
  pointer-events: none;
}

.info {
  width: 100%;
}

.title,
.subtitle {
  width: 100%;
  height: 0.5rem;
  background-color: var(--color-primary);
  border-radius: var(--radius);
}

.title {
  margin-bottom: var(--space-xs);
  max-width: 15ch;
}

.subtitle {
  max-width: 12ch;
}

.thumbnail {
  flex-shrink: 0;
  width: var(--size);
  height: var(--size);
  background: var(--color-primary);
  border-radius: var(--radius);
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  grid-gap: var(--space-sm);
}

  
.grid .item {
  flex-direction: column;
  text-align: center;
}

.grid .info {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.grid .thumbnail {
  --size: 100px;
  margin-bottom: var(--space);
}

.list > * + * {
  margin-top: var(--space-xs);
}

.list .item {
  padding: var(--space-sm);
}

.list .item::before {
  content: "×";
  position: absolute;
  top: 0;
  right: 0;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-right: var(--space-sm);
  font-size: 2rem;
  text-align: center;
  text-transform: uppercase;
  letter-spacing: 0.05rem;
  color: var(--color-primary);
  pointer-events: none;
}

.list .thumbnail {
  --size: 50px;
  margin-right: var(--space-sm);
}

.button {
  appearance: none;
  padding: var(--space-xs) var(--space-sm);
  font-family: inherit;
  font-size: 1rem;
  color: white;
  background-color: var(--color-dark);
  border: 1px solid var(--color-dark);
  cursor: pointer;
  border-radius: var(--radius);
  box-shadow: rgba(black, 0.1) 4px 4px;
}

.button:active {
  transform: translateY(1px);
  box-shadow: none;
}

.empty-cart {
  color: var(--color-mid);
  border-color: var(--color-mid);
  background-color: var(--color-light);
}
              
            
!

JS

              
                const items = document.querySelector('.items');
const componentItems = document.querySelector('.component .items');
const emptyText = document.querySelector('.empty-text');
const emptyBtn = document.querySelector('.empty-cart');

items.addEventListener('click', (e) => {
  if (emptyText !== null) {
    emptyText.setAttribute('hidden', true);
  }
  
  if (e.target.matches('.item')) {
		const item = e.target.cloneNode(true);
    componentItems.append(item);
	}
});

componentItems.addEventListener('click', (e) => {
  if (e.target.matches('.item')) {
		e.target.remove();
	}
  if (componentItems.children.length < 1) {
    emptyText.removeAttribute('hidden');
  }
});

emptyBtn.addEventListener('click', (e) => {
  while (componentItems.lastElementChild) {
    componentItems.removeChild(componentItems.lastElementChild);
  }
  emptyText.removeAttribute('hidden');
});
              
            
!
999px

Console