Pen Settings

HTML

CSS

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

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

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.

HTML

              
                <h2>Sortable Colors Name Chart</h2>
<div class="table"></div>
              
            
!

CSS

              
                * {
  color: #333;
  font-size: 14px;
  font-family: "Helvetica Neue", "Helvetica", Helvetica, Arial, sans-serif;

}

h2 {
  text-align: center;
  text-transform: uppercase;
  padding: 1em;
  font-size: 1.5em;
  font-weight: normal;
  
}

table {
  min-width: 1000px;
  border: 1px solid #d9d9d9;
  border-collapse: collapse;
  margin: 0 auto;
}

td,
th {
  border: 1px solid #d9d9d9;
  padding: .5em;
  min-width: 3em;
}
th span{font-size:150%;}
tr.header {
  border-bottom-style: double;
}

.Hex:before {
  content: "#"
}

th span {
  cursor: pointer;
}

th:nth-child(1),
t1:nth-child(1) {
  width: 18px;
}


/*
table col:nth-child(4n), table th:nth-child(4n), table td:nth-child(4n){display:none;}
*/

table col:nth-child(even) {
  background-color: #f9f9f9;
}

span {
  display: inline-block;
}

table col:hover{background-color: #f9f9f9;}
/*
span{
  width: 0;
  height: .6em;
  border: .3em solid transparent; 
  position: relative;
  
  
}
.asc{border-bottom: .6em solid #999;
  border-top-width: 0;
top:0em;}

.desc{border-top: .6em solid #999;
  border-bottom-width: 0;
top:.6em;}*/
              
            
!

JS

              
                //Colors array: 
//https://codepen.io/enxaneta/pen/1a2e636e0025176339d49eac2f233bb1

var headerRy = Object.keys(ry[0]);

var parent = document.querySelector(".table");
var table = document.createElement("table");
var colgroup = document.createElement("colgroup");
var thead = document.createElement("thead");
var tbody = document.createElement("tbody");

table.appendChild(colgroup);
table.appendChild(thead);
table.appendChild(tbody);
parent.appendChild(table);

tableColgroup();

tableHead();
tableBody(ry);

function tableColgroup(){
  var html = "";
  for (var i = 0; i < headerRy.length; i++) {
    html += "<col></col>"
  }
  colgroup.innerHTML = html;
}

function tableHead() {
  var html = "<tr class='header'><th></th>";
  for (var i = 0; i < headerRy.length; i++) {
    html += "<th id = '";
    html += headerRy[i];
    html += "'>";
    html += headerRy[i];
    
    if( headerRy[i] != "RGB" && headerRy[i] != "HSL"){
    html += " <span class='asc'>+</span>";
    html += " <span class='desc'>-</span>";
    }
    html += "</th>";
  }
  html += "</tr>";
  thead.innerHTML = html;
}

function tableBody(ry) {
  var html = "";
  for (var i = 0; i < ry.length; i++) {
    html += "<tr><td style='background-color:" + ry[i][headerRy[0]] + ";'></td>";
    for (var j = 0; j < headerRy.length; j++) {
      html += "<td class='" + headerRy[j] + "'>" + ry[i][headerRy[j]] + "</td>";
    }
    html += "</tr>";
  }
  tbody.innerHTML = html;
}

function sortByName(name, ad) {
  var byName = ry.slice(0);
  var flag = true;
  byName.sort(function(a, b) {
    var x = a[name];
    var y = b[name];
    if (ad == "desc") {
      return x < y ? 1 : x > y ? -1 : 0;  
    } else if (ad == "asc")  {
      return x < y ? -1 : x > y ? 1 : 0;
    }else{flag = false}
  });
  if(flag){tableBody(byName);}
}

function sortByNum(val, ad) {
  var byNum = ry.slice(0);
  var flag = true;
  byNum.sort(function(a, b) {
    if (ad == "desc") {
      return b[val] - a[val];
    } else if (ad == "asc") {
      return a[val] - b[val];
     }else{flag = false}
  });
  if(flag){tableBody(byNum);}
}

function sortByHex(hex, ad) {
  var byHex= ry.slice(0);
  var flag = true;
  byHex.sort(function(a, b) {
    var x = parseInt(a[hex], 16);
    var y = parseInt(b[hex], 16);
    if (ad == "desc") {
      return y - x;
    } else if (ad == "asc") {
      return x - y;
     }else{flag = false}
  });
  if(flag){tableBody(byHex);}
}// do I need this?


// events

Color.addEventListener("click", function(e) {
sortByName(e.target.offsetParent.id,e.target.className);
}, false);

Hex.addEventListener("click", function(e) {
sortByHex(e.target.offsetParent.id, e.target.className);
}, false);

var idry = ["R","G","B","H","S","L"]       

for(i=0; i< idry.length; i++) {  
(function(i) {
document.getElementById(idry[i]).addEventListener("click", function(e) {
sortByNum(e.target.offsetParent.id, e.target.className);
}, false); 
    })(i);
}
              
            
!
999px

Console