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="page-wrapper">
  <div class="form-wrapper column">
    <h2>Box Model Demo Tool</h2>
    <form>
      <div class="form-box-wrapper box-wrapper">
        <div class="form-content-box box">
          <p>content</p>
        </div>

        <div class="form-padding-box box">
          <p>padding</p>
          <input class="padding input-top" type="text" name="paddingTop" value="30">
          <input class="padding input-right" type="text" name="paddingRight" value="30">
          <input class="padding input-bottom" type="text" name="paddingBottom" value="30">
          <input class="padding input-left" type="text" name="paddingLeft" value="30">
        </div>

        <div class="form-border-box box">
          <p>border</p>
          <input class="border input-top" type="text" name="borderTop" value="30">
          <input class="border input-right" type="text" name="borderRight" value="30">
          <input class="border input-bottom" type="text" name="borderBottom" value="30">
          <input class="border input-left" type="text" name="borderLeft" value="30">
        </div>

        <div class="form-margin-box box">
          <p>margin</p>
          <input class="margin input-top" type="text" name="marginTop" value="30">
          <input class="margin input-right" type="text" name="marginRight" value="30">
          <input class="margin input-bottom" type="text" name="marginBottom" value="30">
          <input class="margin input-left" type="text" name="marginLeft" value="30">
        </div>
      </div>


      <div class="form-block">
        <p>Dimensions:</p>
        <label>H <input type="text" value="auto" name="boxHeight"></label>
        <label>W <input type="text" value="auto" name="boxWidth"></label>
      </div>
      <div class="form-block">
        <p>Box-Sizing:</p>
        <label><input type="radio" name="boxSizing" value="content-box" title="Default value. Width and height include content only (not padding, border, or margin)." checked> Content-Box</label>
        <label><input type="radio" name="boxSizing" value="border-box" title="Width and height include content, padding, and border (not margin)."> Border-Box</label>
      </div>
      <div class="form-block">
        <p>Lock Padding:</p>
        <label><input type="radio" name="lockPadding" value="none" checked title="Default value. Can set unique value for each side."> None</label>
        <label><input type="radio" name="lockPadding" value="parallel" title="Left-Right and Top-Bottom will always match."> Parallel</label>
        <label><input type="radio" name="lockPadding" value="all" title="All sides will match."> All</label>
      </div>
      <div class="form-block">
        <p>Lock Border:</p>
        <label><input type="radio" name="lockBorder" value="none" checked title="Default value. Can set unique value for each side."> None</label>
        <label><input type="radio" name="lockBorder" value="parallel" title="Left-Right and Top-Bottom will always match."> Parallel</label>
        <label><input type="radio" name="lockBorder" value="all" title="All sides will match."> All</label>
      </div>
      <div class="form-block">
        <p>Lock Margin:</p>
        <label><input type="radio" name="lockMargin" value="none" checked title="Default value. Can set unique value for each side."> None</label>
        <label><input type="radio" name="lockMargin" value="parallel" title="Left-Right and Top-Bottom will always match."> Parallel</label>
        <label><input type="radio" name="lockMargin" value="all" title="All sides will match."> All</label>
      </div>
    </form>


  </div>
  <!--form wrapper ends-->
  <div class="box-wrapper column">
    <div class="content-box box">
      <p>content</p>
    </div>

    <div class="padding-box box">
      <p>padding</p>
    </div>

    <div class="border-box box">
      <p>border</p>
    </div>

    <div class="margin-box box">
      <p>margin</p>
    </div>
  </div>
  <!--box wrapper ends-->
</div>
              
            
!

CSS

              
                /*layout*/

* {
  box-sizing: border-box;
}

body {
  font-family: sans-serif;
  font-size: 20px;
}

input[type="text"] {
  font-size: 1em;
  width: 4em;
  height: 1.5em;
  text-align: center;
}

input:read-only {
  background-color: #eee;
  color: #aaa;
}

.column {
  float: left;
  margin: 0 2em 2em 2em;
}


/*form*/

.form-block {
  display: block;
}

.form-block p {
  margin: 0;
  margin-top: 1em;
  font-weight: bold;
}

.form-box-wrapper input[type="text"] {
  width: 2em;
  height: 2em;
  border: 1px solid #aaa;
}


/*main box*/

.box-wrapper p {
  position: absolute;
  margin: 0;
  top: 5px;
  left: 5px;
  color: #555;
}

.content-box,
.form-content-box {
  background-color: #8bb5c0;
  background-color: rgba(139, 181, 192, 0.8);
  border: 1px solid #808080;
  width: 5em;
  height: 1.5em;
  z-index: 3;
}

.content-box p,
.form-content-box p {
  left: auto;
  width: 100%;
  text-align: center;
  top: 50%;
  transform: translateY(-50%);
}

.padding-box,
.form-padding-box {
  background-color: #c2cf8a;
  background-color: rgba(194, 207, 138, 0.8);
  border: 1px dashed #808080;
  z-index: 2;
}

.border-box,
.form-border-box {
  background-color: #fddd9b;
  background-color: rgba(253, 221, 155, 0.8);
  border: 1px solid #000;
  z-index: 1;
}

.margin-box,
.form-margin-box {
  background-color: #f9cc9d;
  background-color: rgba(249, 204, 157, 0.8);
  border: 1px dashed #222;
  z-index: 0;
}

.box,
.form-box {
  position: absolute;
}

.box-wrapper.column {
  top: 6em;
}

.box-wrapper,
.form-box-wrapper {
  position: relative;
}


/*form box*/

.form-box-wrapper {
  width: 19em;
  height: 15.5em;
}

.form-content-box,
.form-padding-box,
.form-border-box {
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.form-content-box {
  width: 7em;
  height: 3.5em;
}

.form-padding-box {
  width: 11em;
  height: 7.5em;
}

.form-border-box {
  width: 15em;
  height: 11.5em;
}

.form-margin-box {
  width: 100%;
  height: 100%;
}

.form-box-wrapper input {
  position: absolute;
}

.input-bottom,
.input-top {
  left: 50%;
  transform: translateX(-50%);
}

.input-left,
.input-right {
  top: 50%;
  transform: translateY(-50%);
}

.input-bottom {
  bottom: 0;
}

.input-top {
  top: 0;
}

.input-left {
  left: 0;
}

.input-right {
  right: 0;
}
              
            
!

JS

              
                //on document ready
$(document).ready(function() {

  function lockBox(sides, boxName) {
    //set variables
    $top = $("." + boxName + ".input-top");
    $right = $("." + boxName + ".input-right");
    $bottom = $("." + boxName + ".input-bottom");
    $left = $("." + boxName + ".input-left");

    //reset fields
    $right.prop('readonly', false);
    $bottom.prop('readonly', false);
    $left.prop('readonly', false);

    if (sides === "none") {

    } else if (sides === "parallel") {
      $bottom.prop('readonly', true);
      $right.prop('readonly', true);
      $right.val(parseInt($left.val()));
      $bottom.val(parseInt($top.val()));
    } else if (sides === "all") {
      $right.prop('readonly', true);
      $bottom.prop('readonly', true);
      $left.prop('readonly', true);
      $right.val(parseInt($top.val()));
      $bottom.val(parseInt($top.val()));
      $left.val(parseInt($top.val()));
    }
  }

  function serializeForm(box) {
    $.each($('form').serializeArray(), function() {
      if ($.isNumeric(this.value)) {
        box[this.name] = parseInt(this.value);
      } else {
        box[this.name] = this.value;
      }
    });
  }

  function setBox() {
    //array to contain proprty names and values from form
    var box = {};

    serializeForm(box);

    lockBox(box.lockPadding, "padding");
    lockBox(box.lockBorder, "border");
    lockBox(box.lockMargin, "margin");

    serializeForm(box);

    //content-box || border-box
    //contentBox size 
    $(".content-box").removeAttr('style');

    //for content-box box-sizing
    if (box.boxSizing == "content-box") {
      //dimensions determines content dimensions
      if ($.isNumeric(box.boxHeight)) {
        $(".content-box").height(box.boxHeight);
      }
      if ($.isNumeric(box.boxWidth)) {
        $(".content-box").width(box.boxWidth);
      }
      var $paddingBoxHeight = $(".content-box").height() + box.paddingTop + box.paddingBottom;
      var $paddingBoxWidth = $(".content-box").width() + box.paddingLeft + box.paddingRight;
      var $borderBoxHeight = $paddingBoxHeight + box.borderTop + box.borderBottom;
      var $borderBoxWidth = $paddingBoxWidth + box.borderLeft + box.borderRight;
    }

    //for border-box box-sizing
    else if (box.boxSizing == "border-box") {
      //dimensions determine border/padding/content combined dimensions
      if ($.isNumeric(box.boxHeight)) {
        $borderBoxHeight = box.boxHeight;
        $paddingBoxHeight = $borderBoxHeight - box.borderTop - box.borderBottom;
        $(".content-box").height($paddingBoxHeight - box.paddingTop - box.paddingBottom);
      }

      if ($.isNumeric(box.boxWidth)) {
        $borderBoxWidth = box.boxWidth;
        $paddingBoxWidth = $borderBoxWidth - box.borderRight - box.borderLeft;
        $(".content-box").width($paddingBoxWidth - box.paddingRight - box.paddingLeft);
      }
    }

    //contentBox position
    var $contentBoxTop = box.paddingTop + box.borderTop + box.marginTop;
    $(".content-box").css('top', $contentBoxTop);

    var $contentBoxLeft = box.paddingLeft + box.borderLeft + box.marginLeft;
    $(".content-box").css('left', $contentBoxLeft);

    //paddingBox size and position
    var $paddingBoxTop = box.borderTop + box.marginTop;
    $(".padding-box").height($paddingBoxHeight);
    $(".padding-box").css('top', $paddingBoxTop);

    var $paddingBoxLeft = box.marginLeft + box.borderLeft;
    $(".padding-box").width($paddingBoxWidth);
    $(".padding-box").css('left', $paddingBoxLeft);

    //borderBox size and position
    var $borderBoxTop = box.marginTop;
    $(".border-box").height($borderBoxHeight);
    $(".border-box").css('top', $borderBoxTop);

    var $borderBoxLeft = box.marginLeft;
    $(".border-box").width($borderBoxWidth);
    $(".border-box").css('left', $borderBoxLeft);

    //marginBox size and position
    var $marginBoxHeight = $borderBoxHeight + box.marginTop + box.marginBottom;
    var $marginBoxWidth = $borderBoxWidth + box.marginLeft + box.marginRight;
    $(".margin-box").height($marginBoxHeight);
    $(".margin-box").width($marginBoxWidth);
  }

  setBox();

  $("form").change(function() {
    setBox();
  });

});
              
            
!
999px

Console