Pen Settings



CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource


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


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.


Auto Save

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.


                <div class="container">
  <div class="preview div"></div>
  <pre class="preview pre"></pre>
  <textarea class="textarea"></textarea>



                body {
  margin: 0;
    justify-content: center;
    align-items: center;
    padding: 0 10px;
    box-sizing: border-box;
    min-height: 100vh;
.container {
    display: flex;
    width: 100%;

.div {
  min-width: 220px;

.preview {
  flex: 1;
  margin-bottom: 20px;
  padding: 20px;
  background: #f5fcff;
  border: 1px solid #d3eeff;
  border-radius: 3px;
  margin: 0;

.textarea {
  flex: 1;
  min-width: 220px;
  margin-left: 20px;
  padding: 10px;
  font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, Courier, monospace;

.preview + .preview {
  margin-left: 10px;


                const isPlainObject = (v) => === "[object Object]"
const isString = (v) => === "[object String]"

 * 格式 JSON 字符串为对象
 * @author anran758
 * @param { any }
function formatJsonStrAsObj(sample) {
  let temp = sample;

  if (isString(temp)) {
    // 因为有解析失败的可能,使用 try catch 做相应处理
    try {
      temp = JSON.parse(temp);
    } catch (ex) {
      // parse error,return this sample
      return sample;

  if (isPlainObject(temp)) {
    temp = { ...temp };

    Object.keys(temp).forEach(key => {
      const item = temp[key];

      // 字符串或者对象进行递归确认
      if (isString(item) || isPlainObject(item)) {
        temp[key] = formatJsonStrAsObj(item);

  return temp;

 * 将 JSON 字符串转换为带缩进的字符串
 * @param {*} sample JSON 字符串
 * @param {number} [indnt=2] 缩进数
 * @returns
function formatJSONIndnt(sample, indnt = 2) {
  const newSample = formatJsonStrAsObj(sample);

  if (isString(newSample)) return newSample;

  try {
    return JSON.stringify(newSample, null, indnt);
  } catch (ex) {
    return newSample.toString();

const info = JSON.stringify({
  name: 'anran758',
  avatar: 'https://xxx',
  detail: JSON.stringify({
    desc: 'some description',
    level: 2,
const data = formatJSONIndnt(info);

const textarea = document.querySelector('.textarea');
const preview = document.querySelector('.pre');
const div = document.querySelector('.div');

textarea.addEventListener('paste', (e) => {
  const value = (e.clipboardData || window.clipboardData).getData('text'); =formatJSONIndnt(value, 2);
preview.innerHTML = data;
div.innerHTML = `