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

              
                <h2>Aspect ratio calculator</h2>

<div class="container">
  <div class="box">
    <label for="initW">
      <span>Original width</span>
      <input type="number" min="1" id="initial-width">
    </label><label for="iniH">
      <span>Original height</span>
      <input type="number" min="1" id="initial-height">
    </label>
  </div>
  <div class="box">
    <label for="newW">
      <span>New width</span>
      <input type="number" min="1" id="new-width">
    </label><label for="newH">
      <span>New height</span>
      <input type="number" min="1" id="new-height">
    </label>
  </div>
</div>
              
            
!

CSS

              
                * {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

html, body {
  min-height: 100%;
  margin: 0;
  padding: 0;
}

body {
  background: rgba(204,105,224,1);
  background: -moz-linear-gradient(-45deg, rgba(204,105,224,1) 0%, rgba(78,27,219,1) 100%);
  background: -webkit-linear-gradient(-45deg, rgba(204,105,224,1) 0%, rgba(78,27,219,1) 100%);
  background: -o-linear-gradient(-45deg, rgba(204,105,224,1) 0%, rgba(78,27,219,1) 100%);
  background: linear-gradient(135deg, rgba(204,105,224,1) 0%, rgba(78,27,219,1) 100%);
  font-family: sans-serif;
}

h2 {
  margin: 80px auto 30px auto;
  padding: 0 10%;
  color: #EEEEEE;
  font-weight: normal;
  line-height: 1.4em;
  letter-spacing: .2em;
  text-align: center;
}

.container {
  width: 80%;
  max-width: 720px;
  margin: 50px auto;
  padding: 5%;
  background: rgba(255,255,255,.2);
  border-radius: 3px;
}

.box {
  background: #36BAAA;
  padding: 20px 5% 26px 5%;
  border-radius: 3px;
}

.box:first-child {
  margin-bottom: 30px;
}

.box label {
  display: inline-block;
  width: 50%;
}

.box label span {
  display: block;
  width: 90%;
  margin: 0 5%;
  padding: 10px 14px;
  font-size: .8em;
  letter-spacing: .15em;
  color: #FFC107;
}

.box label input {
  width: 90%;
  margin: 0 5%;
  padding: 10px 14px;
  border: 2px solid #FFC107;
  border-radius: 20px;
  outline: none;
  background: none;
  color: #FFFFFF;
  letter-spacing: .07em;
}

.box label input[type=number]::-webkit-inner-spin-button,
.box label input[type=number]::-webkit-outer-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

@media screen and (max-width: 520px){
  .container {
    padding: 45px 5%;
  }

  .box label {
    display: block;
    width: 100%;
  }
}
              
            
!

JS

              
                /*
===============================================================

Hi! Welcome to my little playground!

My name is Tobias Bogliolo. 'Open source' by default and always 'responsive',
I'm a publicist, visual designer and frontend developer based in Barcelona. 

Here you will find some of my personal experiments. Sometimes usefull,
sometimes simply for fun. You are free to use them for whatever you want 
but I would appreciate an attribution from my work. I hope you enjoy it.

===============================================================
*/

//Formulas:
//New height = new width / (original width / original height).
//New width = (original width / original height) * new height.

//Initial values:
var initialWidth = 1920,
    initialHeight = 1080,
    newWidth,
    newHeight;

$("#initial-width").val(initialWidth);
$("#initial-height").val(initialHeight);

//Get new values:
function getValues(){
  initialWidth = $("#initial-width").val();
  initialHeight = $("#initial-height").val();
  newWidth = $("#new-width").val();
  newHeight = $("#new-height").val();
};

//Aspect ratio:
function getAspectRatio(){
  //Formula: "Aspect Ratio = Width / Height".
  return aspectRatio = initialWidth/initialHeight;
};

//Get new height:
$("#new-width").on("change keyup", function(){
  //Refresh data.
  getValues();
  getAspectRatio();
  //Formula: "Height = Width / Aspect Ratio".
  newHeight = Math.round(newWidth/aspectRatio);
  //Output:
  $("#new-height").val(newHeight);
});

//Get new width:
$("#new-height").on("change keyup", function(){
  //Refresh data.
  getValues();
  getAspectRatio();
  //Formula: "Width = Aspect Ratio * Height".
  newWidth = Math.round(newHeight*aspectRatio);
  //Output:
  $("#new-width").val(newWidth);
});

//Reset:
$("#initial-width, #initial-height").on("change keyup", function(){
  //Output:
  $("#new-width").val("");
  $("#new-height").val("");
});
              
            
!
999px

Console