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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <div class="panel">
  <fieldset id="source">
    <legend>border-image-source</legend>
    <div class="controls" data-type="0"><input type="radio" name="type" value="0" checked>
    url(<input id="source0" value="https://drafts.csswg.org/css-backgrounds-3/images/border.png" data-tpl="url($)">)</div>
    <div class="controls" data-type="1"><input type="radio" name="type" value="1">
    linear-gradient(<input id="source1" value="blue,red" data-tpl="linear-gradient($)">)</div>
    <div class="controls" data-type="2"><input type="radio" name="type" value="2">
    radial-gradient(<input id="source2" value="blue,yellow,red 69%,transparent 71%" data-tpl="radial-gradient($)">)</div>
    <div class="controls" data-type="3"><input type="radio" name="type" value="3"> <input type="file" id="source3" value="" data-tpl="url($)"></div>
  </fieldset>
  <fieldset>
    <legend>border-image-repeat</legend>
    <div class="repeat controls">
      <label>По горизонтали</label>
      <select class="repeat" id="repeatX" data-n="0">
        <option>stretch</option>
        <option>repeat</option>
        <option>round</option>
        <option>space</option>
      </select>
    </div>  
    <div class="repeat controls">
      <label>По вертикали</label>
      <select class="repeat" id="repeatY" data-n="1">
        <option>stretch</option>
        <option>repeat</option>
        <option>round</option>
        <option>space</option>
      </select>
    </div>
  </fieldset>
  <fieldset class="width">
    <legend>border-image-width</legend>
    <div class="controls">
      <label for="width-top">Сверху</label>
      <input type="range" min="0" max="10" value="1" id="width-top" data-prop="width" data-side="top" data-base-unit="px" data-base-max="200" data-ref="border-top-width">
      <output>1</output>
      <select>
        <option></option>
        <option>px</option>
        <option>em</option>
        <option>vh</option>
        <option>vw</option>
        <option>auto</option>
      </select>
    </div>
    <div class="controls">
      <label for="width-right">Справа</label>
      <input type="range" min="0" max="10" value="1" id="width-right" data-prop="width" data-side="right" data-base-unit="px" data-base-max="200" data-ref="border-right-width">
      <output>1</output>
      <select>
        <option></option>
        <option>px</option>
        <option>em</option>
        <option>vh</option>
        <option>vw</option>
        <option>auto</option>
      </select>
    </div>
    <div class="controls">
      <label for="width-bottom">Снизу</label>
      <input type="range" min="0" max="10" value="1" id="width-bottom" data-prop="width" data-side="bottom" data-base-unit="px" data-base-max="200" data-ref="border-bottom-width">
      <output>1</output>
      <select>
        <option></option>
        <option>px</option>
        <option>em</option>
        <option>vh</option>
        <option>vw</option>
        <option>auto</option>
      </select>
    </div>
    <div class="controls">
      <label for="width-left">Слева</label>
      <input type="range" min="0" max="10" value="1" id="width-left" data-prop="width" data-side="left" data-base-unit="px" data-base-max="200" data-ref="border-left-width">
      <output>1</output>
      <select>
        <option></option>
        <option>px</option>
        <option>em</option>
        <option>vh</option>
        <option>vw</option>
        <option>auto</option>
      </select>
    </div>
  </fieldset>
  <fieldset>
    <legend>border-image-outset</legend>
    <div class="controls">
      <label for="outset-top">Сверху</label>
      <input type="range" min="0" max="100" value="0" id="outset-top" data-prop="outset" data-side="top" data-base-unit="px" data-base-max="100">
      <output>0</output>
      <select>
        <option>px</option>
        <option>em</option>
        <option>vh</option>
        <option>vw</option>
      </select>
    </div>
    <div class="controls">
      <label for="outset-right">Справа</label>
      <input type="range" min="0" max="100" value="0" id="outset-right" data-prop="outset" data-side="right" data-base-unit="px" data-base-max="100">
      <output>0</output>
      <select>
        <option>px</option>
        <option>em</option>
        <option>vh</option>
        <option>vw</option>
      </select>
    </div>
    <div class="controls">
      <label for="outset-bottom">Снизу</label>
      <input type="range" min="0" max="100" value="0" id="outset-bottom" data-prop="outset" data-side="bottom" data-base-unit="px" data-base-max="100">
      <output>0</output>
      <select>
        <option>px</option>
        <option>em</option>
        <option>vh</option>
        <option>vw</option>
      </select>
    </div>
    <div class="controls">
      <label for="outset-left">Слева</label>
      <input type="range" min="0" max="100" value="0" id="outset-left" data-prop="outset" data-side="left" data-base-unit="px" data-base-max="100">
      <output>0</output>
      <select>
        <option>px</option>
        <option>em</option>
        <option>vh</option>
        <option>vw</option>
      </select>
    </div>
  </fieldset>
</div>
<div class="panel">
  <fieldset id="slice">
    <legend>border-image-slice</legend>
    <div class="controls">
      <label for="slice-top">Сверху</label>
      <input type="range" min="0" max="100" value="34" id="slice-top" data-prop="slice" data-side="top" data-base-unit="%" data-base-max="100" data-ref="imageHeight">
      <output>34</output>
      <select>
        <option>%</option>
        <option></option>
      </select>
    </div>
    <div class="controls">
      <label for="slice-right">Справа</label>
      <input type="range" min="0" max="100" value="34" id="slice-right" data-prop="slice" data-side="right" data-base-unit="%" data-base-max="100" data-ref="imageWidth">
      <output>34</output>
      <select>
        <option>%</option>
        <option></option>
      </select>
    </div>
    <div class="controls">
      <label for="slice-bottom">Снизу</label>
      <input type="range" min="0" max="100" value="34" id="slice-bottom" data-prop="slice" data-side="bottom" data-base-unit="%" data-base-max="100" data-ref="imageHeight">
      <output>34</output>
      <select>
        <option>%</option>
        <option></option>
      </select>
    </div>
    <div class="controls">
      <label for="slice-left">Слева</label>
      <input type="range" min="0" max="100" value="34" id="slice-left" data-prop="slice" data-side="left" data-base-unit="%" data-base-max="100" data-ref="imageWidth">
      <output>34</output>
      <select>
        <option>%</option>
        <option></option>
      </select>
    </div>
    <div class="controls"><label><input type="checkbox" id="fill" value="0"> использовать центр (<code>fill</code>)</label></div>
    <div class="stage">
      <div class="tiles" id="tile1">
        <div class="markers markers-t"></div>      
        <div class="markers markers-r"></div>
        <div class="markers markers-b"></div>
      </div>
      <div class="tiles" id="tile2">
        <div class="markers markers-r"></div>      
        <div class="markers markers-b"></div>
        <div class="markers markers-l"></div>
      </div>
      <div class="tiles" id="tile3">
        <div class="markers markers-r"></div>      
        <div class="markers markers-b"></div>
      </div>
      <div class="tiles" id="tile0">      
        <div class="markers markers-t"></div>      
        <div class="markers markers-r"></div>
        <div class="markers markers-b"></div>      
        <div class="markers markers-l"></div>
      </div>
    </div>
  </fieldset>
  <fieldset>
    <legend>Другие параметры (в <code>px</code>)</legend>
    <div class="controls">
      <label>Размер шрифта (база для <code>em</code>)</label>
        <input type="number" min="0" max="100" value="16" id="em2px" onchange="$('#result').css('font-size', this.value+'px');conversionMap.em = this.value;$('[type=range]').trigger('change')">
    </div>  
    <div class="controls">
      <label>border-width</label>
      <input type="number" min="0" max="100" value="20" id="border-top-width" onchange="updateBorderWidth(this,'top')">
      <input type="number" min="0" max="100" value="20" id="border-right-width" onchange="updateBorderWidth(this,'right')">
      <input type="number" min="0" max="100" value="20" id="border-bottom-width" onchange="updateBorderWidth(this,'bottom')">
      <input type="number" min="0" max="100" value="20" id="border-left-width" onchange="updateBorderWidth(this,'left')">
    </div>
  </fieldset>
</div>
<div class="panel last-panel">
  <fieldset>
    <legend>Результат</legend>
    <h2 id="output">border-image</h2>
    <div class="result-wrap">
    <div id="result">
      <div class="content" contenteditable>Текст блока можно редактировать. При наведении подсвечивается область обычного <code>border</code>-a. В заголовке отображается готовое для копирования значение.</div>
    </div>
    <div class="border" id="border-demo"></div>
    </div>
  </fieldset>
</div>

<div class="measurer"></div>
              
            
!

CSS

              
                body {
  display: flex;
  flex-flow: row wrap;
  margin: 0;
  padding: .5em;
  box-sizing: border-box;
}

.panel {
  min-width: 230px;
  flex: 5 5 0px;
  display: flex;
  flex-flow: column;
}
.last-panel {
  min-width: 345px;
  flex: 7 7 0px;
}
fieldset {
  flex: 1 0 auto;
}
.extra {
  flex: none;
}
.result-wrap {
  width: 90%;
  margin: 5%;
  position: relative;
}
#result {
  border: 20px solid #888;
  background: #ccc;
  min-height: 120px;
  box-sizing: border-box;
  border-image: url(http://www.w3.org/TR/css3-background/border.png) 100% / 1 stretch;
  padding: 10px;
}

.controls {
  display: flex;
  flex-flow: row;
  align-items: baseline;
}
label[for] {
  width: 3em;
}
.repeat {
  margin: 6px 0;
}
.repeat label{
  width: 8em;
}
.repeat select {
  flex: 1;
}
[type="range"], [type="file"], [type="number"], input:not([type]) {
  flex: 1 0 0px;
  width: 30px;
  min-width: 30px;
  margin: 1px
}
[type="range"] {
  align-self: center;
}
[type="range"]:disabled {
  opacity: .5;
  pointer-events: none;
}
output + select {
    -webkit-appearance: none;
    -moz-appearance: none;
    text-indent: 1px;
    text-overflow: '';
}
output {
  width: 1.5em;
  text-align: right;
}

.stage {
  margin-top: 1em;
  position: relative;
  padding-bottom: 95%;
}

.tiles {
  background: url(http://www.w3.org/TR/css3-background/border.png);
  background-size: 100%;
  width: 45%;
  padding-top: 45%;
  position: absolute;
  border: 1px solid #840;
  transition: all ease-out .3s;
  opacity: 0;
}

.markers {
  position: absolute;
  border: dotted #800;
}
.markers-t {
  border-width: 0 0 1px;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
}
.markers-r {
  border-width: 0 0 0 1px;
  height: 100%;
  width: 100%;
  top: 0;
  right: 0;
}
.markers-b {
  border-width: 1px 0 0;
  bottom: 0;
  left: 0;
  height: 100%;
  width: 100%;
}
.markers-l {
  border-width: 0 1px 0 0;
  height: 100%;
  width: 100%;
  top: 0;
  left: 0;
}

#tile0 {
  opacity: 1;
}

#tile2 .markers-l, #tile2 .markers-r, #tile3 .markers-r {
  top: auto;
  bottom: 0;
}

#tile1 .markers-t, #tile1 .markers-b, #tile3 .markers-b {
  left: auto;
  right: 0;
}

.stage:not(.expanded-h) .markers-t, .stage:not(.expanded-h) .markers-b
{
  width: 100% !important;
}
.stage:not(.expanded-v) .markers-r, .stage:not(.expanded-v) .markers-l
{
  height: 100% !important;
}
.expanded-h #tile0 .markers-r, .expanded-h #tile2 .markers-r {
  display: none;
}
.expanded-v #tile0 .markers-b, .expanded-v #tile1 .markers-b {
  display: none;
}

.expanded-h #tile1 {
  transform: translate(105%, 0);
  opacity: 1;
}
.expanded-v #tile2 {
  transform: translate(0, 105%);
  opacity: 1;
}
.expanded-h.expanded-v #tile3 {
  transform: translate(105%, 105%);
  opacity: 1;
}

legend {
  white-space: nowrap;
}

.measurer {
  position: fixed;
  width: 1vw;
  height: 1vh;
  z-index: -1;
}

.border {
  position: absolute;
  top: 0; right: 0; bottom: 0; left: 0;
  border: 20px solid rgba(0,0,0,0);
  transition: border-color .2s;
}

.result-wrap:hover .border {
  border-color: rgba(0,0,0,.3);
}

.content {
  position: relative;
  z-index: 2;
}

#output {
  font: normal 16px/1.25 monospace;
}
.bi-src {
  display: inline-block;
  max-width: 200px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  vertical-align: bottom;
  color: #800;
}
              
            
!

JS

              
                var parts = {
  source: {
    type: 'url($)', value: 'https://www.w3.org/TR/css3-background/border.png', width: 81, height: 81
  },
  slice: {
    top: '100%', right: '100%', bottom: '100%', left: '100%',
    fill: ''
  },
  repeat: ['stretch', 'stretch'],
  width: {
    top: '1', right: '1', bottom: '1', left: '1'
  },
  outset: {
    top: '0px', right: '0px', bottom: '0px', left: '0px'
  },
  border: {
    top: '20px', right: '20px', bottom: '20px', left: '20px'
  }
}

var localFile = '';

var controls = {
  sliceTop: document.querySelector('#slice-top'),
  sliceRight: document.querySelector('#slice-right'),
  sliceBottom: document.querySelector('#slice-bottom'),
  sliceLeft: document.querySelector('#slice-left'),
}

var conversionMap = {
  '%' : 1,
  'px' : 1,
  'em' : $('#em2px').val(),
  'vw' : $('.measurer').width(),
  'vh' : $('.measurer').height()
}

$(window).on('resizeend', function() {
  conversionMap.vw = $('.measurer').width();
  conversionMap.vh = $('.measurer').height();
});

$('.controls').on('click', function(e){
  if (!e.target.type || e.target.type != 'radio')
  $('[type="radio"]', $(this)).trigger('click');
});

var img = new Image();
img.onload = function() {
  parts.source.value = img.src;
  parts.source.type = 'url($)';
  var v = 'url('+img.src+')';
  parts.source.height = img.height;
  parts.source.width = img.width;
  if (img.height >= img.width) {
    tileWidth = 45 * img.width/img.height + '%';
    tilePadding = '';
  }
  else {
    tileWidth = '';
    tilePadding = 45 * img.height/img.width + '%';
  }
  makeStringOut();
  $('#result').attr( 'style', $('#output').text() );
  $('.tiles').css({'background-image': v,  'width':tileWidth, 'padding-top':tilePadding});
  $('#slice select').trigger('change');
}

$('[name="type"]').on('click', function(e) {
  $('#source'+$(this).val()).trigger('change');
});

$('#source0').on('change', function(e) {
  img.src = $(this).val();
});

$('#source1, #source2').on('change', function(e) {
  parts.source.value = $(this).val();
  parts.source.type = $(this).data('tpl');
  var v = $(this).data('tpl').replace('$', $(this).val());
  parts.source.height = 100;
  parts.source.width = 100;
  makeStringOut();
  $('#result').attr( 'style', $('#output').text() );
  $('.tiles').css({'background-image': v, 'width':'', 'padding-top':''});
  $('#slice select').trigger('change');
});

$('#source3').on('change', function(e) {
  var file    = document.querySelector('input[type=file]').files[0];
  var reader  = new FileReader();

  reader.addEventListener("load", function () {
    localFile = reader.result;
    img.src = localFile;
  }, false);

  if (file) {
    reader.readAsDataURL(file);
  }
});

$('.repeat').on('change', function(){
  var el = $(this);
  parts.repeat[el.data('n')] = el.val();
  update();
});

$('[type="range"]').on('change', function(e) {
  var el = $(this), unit = el.next().next().val();
  if (unit == 'auto') {
    el.attr('disabled',true);
    el.next().val('');
    parts[el.data('prop')][el.data('side')] = unit;
  }
  else {
    el.attr('disabled',false);
    el.data('abs-val', el.val() * getConversionFactor(unit, el));
    el.next().val(el.val());
    parts[el.data('prop')][el.data('side')] = el.val() + unit;
  }
  update();
});

$('#fill').on('change', function() {
  parts.slice.fill = this.checked ? 'fill' : '';
  update();
});

function update() {
  var t,r,b,l;
  t = 100 * controls.sliceTop.value / controls.sliceTop.max;
  r = 100 * controls.sliceRight.value / controls.sliceRight.max;
  b = 100 * controls.sliceBottom.value / controls.sliceBottom.max;
  l = 100 * controls.sliceLeft.value / controls.sliceLeft.max;
  
  if (t*1 + b*1 >= 100) $('.stage').addClass('expanded-v');
  else $('.stage').removeClass('expanded-v');
  if (r*1 + l*1 >= 100) $('.stage').addClass('expanded-h');
  else $('.stage').removeClass('expanded-h');
  $('.markers-t, #tile0 .markers-l, #tile0 .markers-r, #tile1 .markers-r').css({height: t + '%'});
  $('.markers-r, #tile1 .markers-t, #tile1 .markers-b, #tile3 .markers-b').css({width: r + '%'});
  $('.markers-b, #tile2 .markers-l, #tile2 .markers-r, #tile3 .markers-r').css({height: b + '%'});
  $('.markers-l, #tile0 .markers-t, #tile0 .markers-b, #tile2 .markers-b').css({width: l + '%'});
  
  makeStringOut();
  $('#result').css( 'border-image', $('#bi-value').text() );
}

function makeStringOut() {
  var out = 'border-image: <span id="bi-value">';
  out += parts.source.type.replace('$', '<span class="bi-src">' + parts.source.value + '</span>') + ' ';
  if (parts.slice.left == parts.slice.right) {
    if (parts.slice.top == parts.slice.bottom) {
      if (parts.slice.top == parts.slice.left) {
        out += parts.slice.top;
      }
      else out += parts.slice.top + ' ' + parts.slice.right;
    }
    else out += parts.slice.top + ' ' + parts.slice.right + ' ' + parts.slice.bottom;
  }
  else out += parts.slice.top + ' ' + parts.slice.right + ' ' + parts.slice.bottom + ' ' + parts.slice.left;
  
  if (parts.slice.fill) out += ' ' + parts.slice.fill;
  
  var hasWidth = parts.width.top != '1' || parts.width.right != '1' || parts.width.bottom != '1' || parts.width.left != '1';
  if (hasWidth) {
    out += ' / ';
    if (parts.width.left == parts.width.right) {
      if (parts.width.top == parts.width.bottom) {
        if (parts.width.top == parts.width.left) {
          out += parts.width.top;
        }
        else out += parts.width.top + ' ' + parts.width.right;
      }
      else out += parts.width.top + ' ' + parts.width.right + ' ' + parts.width.bottom;
    }
    else out += parts.width.top + ' ' + parts.width.right + ' ' + parts.width.bottom + ' ' + parts.width.left;
  }
  
  var hasOutset = parts.outset.top != '0px' || parts.outset.right != '0px' || parts.outset.bottom != '0px' || parts.outset.left != '0px';
  if (hasOutset) {
    out += hasWidth ? ' / ' : ' / / ';
    if (parts.outset.left == parts.outset.right) {
      if (parts.outset.top == parts.outset.bottom) {
        if (parts.outset.top == parts.outset.left) {
          out += parts.outset.top;
        }
        else out += parts.outset.top + ' ' + parts.outset.right;
      }
      else out += parts.outset.top + ' ' + parts.outset.right + ' ' + parts.outset.bottom;
    }
    else out += parts.outset.top + ' ' + parts.outset.right + ' ' + parts.outset.bottom + ' ' + parts.outset.left;
  }
    
  if (parts.repeat[0] == parts.repeat[1]) out += ' ' + parts.repeat[0];
  else out += ' ' + parts.repeat.join(' ');
  
  out = out.replace(/ 0[a-z]+/g, ' 0');
  
  $('#output').html(out + '</span>');
}

function getConversionFactor(unit, ref) {
  var factor;
  if (typeof conversionMap[unit] != 'undefined') factor = conversionMap[unit];
  else if (ref && ref.data('ref')) {
    ref = ref.data('ref');
    if (ref == 'imageHeight') factor = 100 / parts.source.height;
    else
    if (ref == 'imageWidth') factor = 100 / parts.source.width;
    else
    if (ref = $('#'+ref)) factor = ref.val();
    else factor = 1;
  }
  else factor = 1;
  return factor;
}

$('select:not(.repeat)').on('change', function() {
  var units = $(this);
  var range = units.prev().prev();
  if (units.val == 'auto') {
    return;
  }
  else {
    range.attr('type','range');
    range.attr('disabled', false);
  }
  factor = getConversionFactor(units.val(), range);
  var newMax = Math.ceil(range.data('base-max') / factor);
  var newPos = Math.floor(range.data('abs-val') / factor);
  range.attr('max', newMax);
  range.val(newPos);
  range.trigger('change');
});

function updateBorderWidth(el, side) {
  var css = {}
  css['border-'+side+'-width'] = el.value + 'px';
  $('#result, #border-demo').css(css);
  $('#width [type="range"]').trigger('change');
}

$(function() {
  $('#source0, #em2px, #fill').trigger('change');
});
              
            
!
999px

Console