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 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

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

              
                <table>
  <tr>
    <td>
      <img-placeholder type="random" width="200" height="300"></img-placeholder>
    </td>
    <td>
      <img-placeholder type="places" width="200" height="200"></img-placeholder>
    </td>
  </tr>
  <tr>
    <td>
      <img-placeholder type="kitten" width="200" height="200"></img-placeholder>
    </td>
    <td>
      <img-placeholder type="things" width="100" height="200"></img-placeholder>
    </td>
  </tr>
</table>
              
            
!

CSS

              
                
              
            
!

JS

              
                // create own class extending HTMLElement
class ImgPlaceHolder extends HTMLElement {
  // create needed html elements for this element
  // info: could/should use html templates
  constructor() {
    super();
    // info: no requirement to use shadow
    const shadow = this.attachShadow({ mode: "open" });

    const wrapper = document.createElement("span");
    wrapper.setAttribute("class", "wrapper");
    this.img = document.createElement("img");
    this.info = document.createElement("span");
    this.info.setAttribute("class", "info");
    this.info.setAttribute("part", "info");

    const style = document.createElement("style");
    style.textContent =
      " \
                .wrapper { \
                    position: relative; \
                } \
                .info { \
                    position: absolute; \
                    background-color: black; \
                    color: white; \
                }";

    shadow.appendChild(style);
    shadow.appendChild(wrapper);

    wrapper.appendChild(this.info);
    wrapper.appendChild(this.img);
  }
  
  // called when element can be used/manipulated
  connectedCallback() {
    this.update();
  }

  // define which attributes should be observerd
  static get observedAttributes() {
    return ["width", "height", "type"];
  }
  attributeChangedCallback(name, oldValue, newValue) {
    this.update();
  }
  
  // custom logic to fetch img based on attributes
  update() {
    const width = this.getAttribute("width");
    const height = this.getAttribute("height");
    const type = this.getAttribute("type");

    this.info.textContent = width + "x" + height + " " + type;

    let imgUrl = "https://placekitten.com/" + width + "/" + height;
    switch (type) {
      case "kitten":
        imgUrl = "https://placekitten.com/" + width + "/" + height;
        break;
      case "places":
      case "people":
      case "things":
        imgUrl =
          "https://placem.at/" +
          type +
          "/?w=" +
          width +
          "&h=" +
          height +
          "&random=1";
        break;
      case "random":
        imgUrl = "https://lorempixel.com/" + width + "/" + height;
        break;
      default:
        console.warn("unsupported type '" + type + "'.");
    }

    this.img.src = imgUrl;
  }
}

// define the new element in the browser, for it to be used
customElements.define("img-placeholder", ImgPlaceHolder);

              
            
!
999px

Console