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

Details

Privacy

Go PRO Window blinds lowered to protect code. Code Editor with window blinds (raised) and a light blub turned on.

Keep it secret; keep it safe.

Private Pens are hidden everywhere on CodePen, except to you. You can still share them and other people can see them, they just can't find them through searching or browsing.

Upgrade to PRO

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.

Template

Make Template?

Templates are Pens that can be used to start other Pens quickly from the create menu. The new Pen will copy all the code and settings from the template and make a new Pen (that is not a fork). You can view all of your templates, or learn more in the documentation.

Template URL

Any Pen can act as a template (even if you don't flip the toggle above) with a special URL you can use yourself or share with others. Here's this Pen's template URL:

Screenshot

Screenshots of Pens are shown in mobile browsers, RSS feeds, to users who chose images instead of iframes, and in social media sharing.

This Pen is using the default Screenshot, generated by CodePen. Upgrade to PRO to upload your own thumbnail that will be displayed on previews of this pen throughout the site and when sharing to social media.

Upgrade to PRO

HTML

              
                <p>Would really love to figure out how to 1) crop into a square 2) THEN resize image. This resizes great, but will either squish or not be square.</p>

<p>
  <a target="_blank" href="https://cl.ly/M72U/not-square.jpg">Image to snag for testing</a>
</p>  

<p>
  <input id="file" type="file">
</p>

All three of these are undesireable:<br>

<div class="avatar-wrap">
<img src="" id="avatar-1">
</div>

<div class="avatar-wrap">
<img src="" id="avatar-2">
</div>

<div class="avatar-wrap">
<img src="" id="avatar-3">
</div>
              
            
!

CSS

              
                body {
  padding: 20px;
}

.avatar-wrap {
  width: 80px;
  height: 80px;
  border: 2px solid black;
  margin: 20px;
  float: left;
}
              
            
!

JS

              
                var Resample = (function (canvas) {

 // (C) WebReflection Mit Style License

 function Resample(img, width, height, onresample) {
  var

   load = typeof img == "string",
   i = load || img;
   
  // if string, a new Image is needed
  if (load) {
   i = new Image;
   i.onload = onload;
   i.onerror = onerror;
  }
  
  i._onresample = onresample;
  i._width = width;
  i._height = height;
  load ? (i.src = img) : onload.call(img);
 }
 
 function onerror() {
  throw ("not found: " + this.src);
 }
 
 function onload() {
  var
   img = this,
   width = img._width,
   height = img._height,
   onresample = img._onresample
  ;
  // if width and height are both specified
  // the resample uses these pixels
  // if width is specified but not the height
  // the resample respects proportions
  // accordingly with orginal size
  // same is if there is a height, but no width
  var minValue = Math.min(img.height, img.width);
  width == null && (width = round(img.width * height / img.height));
  height == null && (height = round(img.height * width / img.width));

  delete img._onresample;
  delete img._width;
  delete img._height;
   
  // when we reassign a canvas size
  // this clears automatically
  // the size should be exactly the same
  // of the final image
  // so that toDataURL ctx method
  // will return the whole canvas as png
  // without empty spaces or lines
  canvas.width = width;
  canvas.height = height;
  // drawImage has different overloads
  // in this case we need the following one ...
  context.drawImage(
   // original image
   img,
   // starting x point
   0,
   // starting y point
   0,
   // image width
   minValue,
   // image height
   minValue,
   // destination x point
   0,
   // destination y point
   0,
   // destination width
   width,
   // destination height
   height
  );
  // retrieve the canvas content as
  // base4 encoded PNG image
  // and pass the result to the callback
  onresample(canvas.toDataURL("image/png"));
 }
 
 var context = canvas.getContext("2d"),
  // local scope shortcut
  round = Math.round
 ;
 
 return Resample;
 
}(
 this.document.createElement("canvas"))
);

$("#file").on("change", function(event) {
  var file = event.target.files[0];
  
  if (file.type.match('image.*')) {
    
    fileTracker = new FileReader;
    fileTracker.onload = function() {
      Resample(
       this.result,
       80,
       80,
       doneResizeOne
     );
     Resample(
       this.result,
       null,
       80,
       doneResizeTwo
     );
     Resample(
       this.result,
       80,
       null,
       doneResizeThree
     );
    }
    fileTracker.readAsDataURL(file);
    
  }
});

function doneResizeOne(data) {
  $("#avatar-1").attr("src", data); 
}
function doneResizeTwo(data) {
  $("#avatar-2").attr("src", data); 
}
function doneResizeThree(data) {
  $("#avatar-3").attr("src", data); 
}
              
            
!
999px
💕 I'm the CSS to your HTML.

Console