Auto Generated from HTML

.js-example

Add a class of js-example to any HTML element and the code for that element and all of its contents will be output as a code example immediately following the element.

title

content

title

content

Additional Options

data-ex-no-content

Add the data-ex-no-content='true' attribute to ignore the contents of the element and replace them with an ellipsis in the output code example.

title

content

title

content

data-ex-only

Add the data-ex-only='true' attribute to not render the element and only output the code example.

title

content

title

content

code only AND no content

The above attributes can both be applied to not render the element AND ignore its contents.

title

content

title

content

Other Languages

You can specify the language you want to highlight as by setting the data-ex-language attribute. In this current implementation, accepted values are 'markup', 'css', 'scss', and 'js'.

When not doing markup examples, use a <pre> tag.

CSS

.c-card {
  border: 1px solid dodgerblue;
  border-radius: 0.2rem;
  box-shadow: 0 1rem 1rem -1rem;
  margin-bottom: 1rem;
}

.c-card__title {
  background: dodgerblue;
  color: white;
  padding: 1rem;
  margin: 0;
}

.c-card__content {
  margin: 0;
  padding: 1rem;
}
//line-break
.c-card {
  border: 1px solid dodgerblue;
  border-radius: 0.2rem;
  box-shadow: 0 1rem 1rem -1rem;
  margin-bottom: 1rem;
}

.c-card__title {
  background: dodgerblue;
  color: white;
  padding: 1rem;
  margin: 0;
}

.c-card__content {
  margin: 0;
  padding: 1rem;
}
//line-break

SCSS

.c-card {
  border: 1px solid dodgerblue;
  border-radius: 0.2rem;
  box-shadow: 0 1rem 1rem -1rem;
  margin-bottom: 1rem;
  
  &__title {
    background: dodgerblue;
    color: white;
    padding: 1rem;
    margin: 0;
  }

  &__content {
    margin: 0;
    padding: 1rem;
  }
}
//line-break
.c-card {
  border: 1px solid dodgerblue;
  border-radius: 0.2rem;
  box-shadow: 0 1rem 1rem -1rem;
  margin-bottom: 1rem;
  
  &__title {
    background: dodgerblue;
    color: white;
    padding: 1rem;
    margin: 0;
  }

  &__content {
    margin: 0;
    padding: 1rem;
  }
}
//line-break

JavaScript

const examples = document.querySelectorAll('.js-example')
examples.forEach((ex) => {
  ex.classList.remove('js-example')
})
//line-break
const examples = document.querySelectorAll('.js-example')
examples.forEach((ex) => {
  ex.classList.remove('js-example')
})

Additional Weirdness

Because I built this with pug, some of the preformatted text was a little weird. I countered this by adding //line-break in some of the code examples and then replacing each instance of that with '' in the javascript. This seems to force a line break within the pre tags.