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

              
                <div class="container-fluid">
  <div class="row justify-content-center">
    <div class="col-12 col-md-8">
      <h1 class="text-center">Guitar Fretboard</h1>
    </div>
    <div class="col-12">
      <hr>
    </div>
    <div class="col-11 col-md-10">
      <h4 class="">Select a Chord</h4>
      <div class="text-center">
        <button class="btn btn-secondary chord-selector" data-chord="C">C</button>
        <button class="btn btn-secondary chord-selector" data-chord="D">D</button>
        <button class="btn btn-secondary chord-selector" data-chord="E">E</button>
        <button class="btn btn-secondary chord-selector" data-chord="F">F</button>
        <button class="btn btn-secondary chord-selector" data-chord="G">G</button>
        <button class="btn btn-secondary chord-selector" data-chord="A">A</button>
        <button class="btn btn-secondary chord-selector" data-chord="B">B</button>
      </div>

      <!-- fretboar main -->
      <div class="mt-5 fretboard">

        <!-- strings wrap -->
        <div class="string-wrap">
          <div class="string"></div>
          <div class="string"></div>
          <div class="string"></div>
          <div class="string"></div>
          <div class="string"></div>

          <!-- notes wrap -->
          <div class="notes-wrap">
            <div class="note">E</div>
            <div class="note">B</div>
            <div class="note">G</div>
            <div class="note">D</div>
            <div class="note">A</div>
            <div class="note">E</div>
          </div>
        </div>
        <!-- frets wrap -->
        <div class="frets-wrap">
          <div class="fingers-wrap">
            <div class="finger-point"></div>
            <div class="finger-point"></div>
            <div class="finger-point"></div>
            <div class="finger-point"></div>
            <div class="finger-point"></div>
            <div class="finger-point"></div>
          </div>
          <div class="fret">
            <div class="fret-number">1</div>
          </div>
          <div class="fret">
            <div class="fret-number">2</div>
          </div>
          <div class="fret">
            <div class="fret-number">3</div>
          </div>
          <div class="fret">
            <div class="fret-number">4</div>
          </div>
          <div class="fret">
            <div class="fret-number">5</div>
          </div>
          <div class="fret">
            <div class="fret-number">6</div>
          </div>
          <div class="fret">
            <div class="fret-number">7</div>
          </div>
          <div class="fret">
            <div class="fret-number">8</div>
          </div>
          <div class="fret">
            <div class="fret-number">9</div>
          </div>
          <div class="fret">
            <div class="fret-number">10</div>
          </div>
          <div class="fret">
            <div class="fret-number">11</div>
          </div>
          <div class="fret">
            <div class="fret-number">12</div>
          </div>
        </div>

      </div>

    </div>

  </div>
</div>

              
            
!

CSS

              
                .fretboard {
    border-left: 5px solid #afafaf;
    border-right: 5px solid #afafaf;
    position: relative;
}
.string {
    border-bottom: 1px solid #afafaf;
    height: 30px;
}
.string:first-child {
    border-top: 1px solid #afafaf;
}
.notes-wrap,
.fingers-wrap {
    height: 150px;
    width: 30px;
    position: absolute;
    top: -15px;
    left: -35px;
}
.note {
    font-size: 25px;
    line-height: 1;
    height: 30px;
}

.fingers-wrap {
    left: 0;
}
.finger-point {
    background-color: #a10000;
    color: white;
    border-radius: 50px;
    height: 25px;
    width: 25px;
    margin-bottom: 5px;
    opacity: 0;
    font-size: 18px;
    text-align: center;
    line-height: 25px;
}

.frets-wrap {
    height: 150px;
    position: absolute;
    top: 0;
    left: 0;
    display: flex;
    width: 100%;
    justify-content: space-between;
}
.fret {
    height: 150px;
    flex: 1 1 100%;
    border-right: 2px solid #888888;
    display: flex;
    align-items: flex-end;
    justify-content: center;
}
.fret:last-child {
    border-right: none;
}

.fret-number {
    font-size: 1.5rem;
    transform: translateY(150%);
}
              
            
!

JS

              
                console.clear();

(function() {
  var chords = {
    A: {
      name: "A",
      strings: [
        null,
        { finger: 3, fret: 2 },
        { finger: 2, fret: 2 },
        { finger: 1, fret: 2 },
        null,
        null
      ]
    },
    B: {
      name: "B",
      strings: [
        { finger: 1, fret: 2 },
        { finger: 4, fret: 4 },
        { finger: 3, fret: 4 },
        { finger: 2, fret: 4 },
        { finger: 1, fret: 2 },
        null,
      ]
    },
    C: {
      name: "C",
      strings: [
        null,
        { finger: 1, fret: 1 },
        null,
        { finger: 2, fret: 2 },
        { finger: 3, fret: 3 },
        null,
      ]
    },
    D: {
      name: "D",
      strings: [
        { finger: 2, fret: 2 },
        { finger: 3, fret: 3 },
        { finger: 1, fret: 2 },
        null,
        null,
        null,
      ]
    },
    E: {
      name: "E",
      strings: [
        null,
        null,
        { finger: 1, fret: 1 },
        { finger: 3, fret: 2 },
        { finger: 2, fret: 2 },
        null,
      ]
    },
    F: {
      name: "F",
      strings: [
        { finger: 1, fret: 1 },
        { finger: 1, fret: 1 },
        { finger: 2, fret: 2 },
        { finger: 3, fret: 3 },
        { finger: 4, fret: 3 },
        { finger: 1, fret: 1 }
      ]
    },
    G: {
      name: "G",
      strings: [
        { finger: 3, fret: 3 },
        null,
        null,
        null,
        { finger: 1, fret: 2 },
        { finger: 2, fret: 3 }
      ]
    }
  };

  var frets = document.querySelectorAll(".fret");
  // has all the positions for each fret
  // must be re-calculated on resize event
  var fretboardData = [];

  /** Reset Fretboard Data
   * Gets and stores the data for each fret in the array.
   * Gets the width and left position of each fret.
   */
  function resetFretboardData() {
    frets.forEach(function(fret, index) {
      fretboardData[index] = {
        width: fret.clientWidth,
        x: fret.offsetLeft
      };
    });
  }
  resetFretboardData();

  // finger points
  var fingerPoints = document.querySelectorAll(".finger-point");

  // selected note
  var selectedChord = null;
  var activeButton = null;
  /** Set Selected Chord
   * @param {String} chord the selected chord
   */
  function setSelectedChord(e, chord) {
    activeButton && activeButton.classList.toggle("btn-info");
    activeButton = e.target;
    activeButton.classList.toggle("btn-info");
    selectedChord = activeButton.getAttribute("data-chord");
    // now draw the note
    drawChord();
  }
  var chordSelectors = document.querySelectorAll(".chord-selector");
  chordSelectors.forEach(function(selector) {
    selector.addEventListener("click", setSelectedChord, false);
  });

  /** Draw Chord
   * Creates the finger points for the selected chord.
   * Uses the frets data and the chords data to position
   * each finger on the correct fret
   */
  function drawChord() {
    chords[selectedChord].strings.forEach(function(string, index) {
      if (string === null) {
        TweenLite.to(fingerPoints[index], 0.25, { autoAlpha: 0 });
      } else {
        fingerPoints[index].innerHTML = string.finger;
        // if the string is not null
        // select the finger point with the current index
        // and set it's position based on the frets data
        TweenLite.to(fingerPoints[index], 0.25, {
          x:
            fretboardData[string.fret - 1].x +
            fretboardData[string.fret - 1].width / 2 -
            12.5,
          autoAlpha: 1
        });
      }
    });
  } // draw chord

  window.addEventListener(
    "resize",
    function() {
      resetFretboardData();
      selectedChord && drawChord();
    },
    false
  );
})();

              
            
!
999px

Console