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

              
                .image-container(style=`background-image: url(https://source.unsplash.com/random/400x400?bear&v=${Math.floor(20 * Math.random() + i)})`)
              
            
!

CSS

              
                *
  box-sizing border-box

body
  align-items center
  background radial-gradient(#e74c3c, black)
  display flex
  justify-content center
  height 100vh
  width 100vw
  overflow hidden
  margin 0
  padding 0

.image-container
  background-color #fafafa
  height 200px
  width 200px
  background-size cover
  background-position center

.rsizable
  position relative
  height calc(var(--height, 200) * 1px)
  width calc(var(--width, 200) * 1px)

  &__ghost
    border 5px dashed white
    height calc(var(--height) * 1px)
    width calc(var(--width) * 1px)
    position absolute
    left calc(var(--x) * 1px)
    top calc(var(--y) * 1px)

  $handleSize = 50px
  &__handle
    border-radius 100%
    height $handleSize
    width $handleSize
    display inline-block
    cursor var(--cursor, e)
    position absolute
    transform translate(var(--x, 0), var(--y, 0))

    &:after
      border-radius 100%
      content ''
      background-color white
      height $handleSize * 0.4
      width  $handleSize * 0.4
      position absolute
      top 50%
      left 50%
      transform translate(-50%, -50%)

    &--n
    &--s
      left 50%
      margin-left -($handleSize / 2)

    &--n
      top 0

    &--s
      bottom 0

    &--e
    &--w
      top 50%
      margin-top -($handleSize / 2)

    &--e
      right 0

    &--w
      left 0

    &--ne
      top 0
      right 0

    &--se
      bottom 0
      right 0

    &--nw
      top 0
      left 0

    &--sw
      bottom 0
      left 0

    &--nw
    &--n
    &--ne
      --y -50%

    &--nw
    &--w
    &--sw
      --x -50%

    &--ne
    &--e
    &--se
      --x 50%

    &--sw
    &--s
    &--se
      --y 50%


              
            
!

JS

              
                class Resizable {
  defaultOptions = {
    ghosting: true,
    handles: ['n', 's', 'e', 'w', 'se', 'sw', 'ne', 'nw']
  }
  constructor(element, options) {
    this.element = element
    this.options = Object.assign({}, this.defaultOptions, options)
    this.handles = this.options.handles
    this.ghosting = this.options.ghosting
    this.create()
  }

  create = () => {
    const {
      element,
      options,
    } = this
    element.classList.add('rsizable')
    for (let handle of options.handles) this.createHandle(handle)
  }

  start = (e) => {
    let {
      pageX: startX,
      pageY: startY,
      touches,
    } = e

    if (touches && touches.length === 1) {
      startX = touches[0].pageX
      startY = touches[0].pageY
    }

    const {
      height,
      width,
      x,
      y,
    } = this.element.getBoundingClientRect()


    const resize = (evt) => {
      evt.preventDefault()
      const direction = e.target.getAttribute('data-rsize-direction')
      let { pageX: X, pageY: Y, touches } = evt
      if (touches && touches.length === 1) {
        X = touches[0].pageX
        Y = touches[0].pageY
      }
      for (const d of direction.split('')) {
        switch(d) {
          case 'n':
            // If position absolute, set top to Y
            this.element.style.setProperty('--height', height + (startY - Y))
            break
          case 's':
            this.element.style.setProperty('--height', height + (Y - startY))
            break
          case 'e':
            this.element.style.setProperty('--width', width + (X - startX))
            break
          case 'w':
            // If position absolute, set left to X
            this.element.style.setProperty('--width', width + (startX - X))
            break
        }
      }
    }

    const end = () => {
      if (this.ghost) this.ghost.remove()
      document.body.removeEventListener('mousemove', resize)
      document.body.removeEventListener('touchmove', resize)
      document.body.removeEventListener('mouseup', end)
      document.body.removeEventListener('touchend', end)
    }

    document.body.addEventListener('mousemove', resize)
    document.body.addEventListener('mouseup', end)

    // Add touch support 😉
    document.body.addEventListener('touchmove', resize)
    document.body.addEventListener('touchend', end)

    if (this.options.ghosting) {
      const ghost = document.createElement('span')
      ghost.classList.add('rsizable__ghost')
      ghost.style.setProperty('--x', x + 1)
      ghost.style.setProperty('--y', y + 1)
      ghost.style.setProperty('--height', height - 2)
      ghost.style.setProperty('--width', width - 2)
      document.body.appendChild(ghost)
      this.ghost = ghost
    }
  }

  createHandle = (direction) => {
    const newHandle = document.createElement('span')
    newHandle.classList.add('rsizable__handle', `rsizable__handle--${direction}`)
    newHandle.setAttribute('data-rsize-direction', direction)
    newHandle.style.setProperty('--cursor', `${direction}-resize`)
    this.element.appendChild(newHandle)
    newHandle.addEventListener('mousedown', this.start)
    newHandle.addEventListener('touchstart', this.start)
  }
}

const imageToResize = document.querySelector('.image-container')
const resizableImage = new Resizable(imageToResize, {})

              
            
!
999px

Console