Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

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.

+ 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

Auto Save

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

              
                <main>
  <section class="compare">
    <img class="compare-img-one" src="https://muffinman.io/img/image-comparison-slider/letters-01.jpg" />
    <div class="compare-mask">
      <img class="compare-img-two" src="https://muffinman.io/img/image-comparison-slider/letters-02.png" />
    </div>

    <div class="compare-separator">
      <svg class="compare-icon" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 16 16">
        <path d="M 6 2 L 1 8 L 6 14 M 10 2 L 15 8 L 10 14" stroke="currentColor"></path>
      </svg>
    </div>

    <input class="compare-input" type="range" min="0" step="0.5" max="100" />
  </section>

  <p>
    Drawings are from the book <a target="_blank" rel="noopener noreferrer" href="https://lettersfromsarajevo.com/en/about#the-drawings">Letters from
      Sarajevo</a>.
  </p>
</main>
              
            
!

CSS

              
                * {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

body {
  --bg-color: oklch(26.9% 0 0);
  background: var(--bg-color);
}

main {
  max-width: 50rem;
  height: 100vh;
  margin: 0 auto; /* 가로 중앙 정렬 */
  align-content: center; /* 세로 중앙 정렬(최신 문법) */
}

p {
  padding: 12px 0;
  color: white;
  font-size: 0.875rem;
  font-style: italic;
}

p > a {
  color: white;
}

.compare {
  --mask-width: 50%;
  --handle-size: 33px;

  position: relative;
}

.compare-img-one {
  /* 부모 요소 너비만큼 이미지 크기 제한 */
  width: 100%;
  /* 이미지 아래 생성되는 하단 여백을 없애기 위해 block으로 변경 */
  display: block;
}

.compare-mask {
  position: absolute;
  inset: 0;
  width: var(--mask-width);
  height: 100%;
  overflow: hidden;
}

.compare-img-two {
  height: 100%;
  /* width: auto 기본값 -> 원본 이미지 너비로 렌더링 */
}

.compare-separator {
  position: absolute;
  inset: 0;
  width: 2px;
  height: 100%;
  left: var(--mask-width); /* 마스크 너비만큼 오른쪽으로 이동 */
  translate: -50%; /* 자신 너비의 50% 만큼 왼쪽으로 이동(1px) */
  background: var(--bg-color);
  cursor: col-resize;
}

.compare-icon {
  position: absolute;
  top: 50%;
  left: var(--mask-width);
  width: var(--handle-size);
  height: var(--handle-size);
  translate: -50% -50%;
  padding: 6px;
  color: var(--bg-color);
  background: white;
  stroke-width: 2px;
  border: 2px solid currentColor;
  border-radius: 50%;
}

.compare-input {
  appearance: none; /* 슬라이더 기본 스타일 제거 */
  background: none; /* 슬라이더 배경 제거 */

  position: absolute;
  /* 네 방향의 여백을 모두 0으로 설정하면 부모 요소 크기만큼 채움 */
  inset: 0;
  cursor: col-resize;

  /* 핸들의 중심이 슬라이더 끝점에 위치할 수 있도록 조정 */
  width: calc(100% + var(--handle-size));
  left: calc(var(--handle-size) / -2);
}

.compare-input::-webkit-slider-thumb {
  appearance: none; /* Range Input 기본 핸들 스타일 제거 */
  width: var(--handle-size); /* width 지정안하면 트랙 꽉채움 */
}

              
            
!

JS

              
                const $compare = document.querySelector(".compare");
const $compareInput = document.querySelector(".compare-input");

$compareInput.addEventListener("input", (e) => {
  $compare.style.setProperty("--mask-width", `${e.target.value}%`);
});

              
            
!
999px

Console