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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

              
                <h1>Multiplication Lookup Table</h1>
<div class="control">
	<div class="options">
		<div>Start X<input type="number" step="1" id="start-x" value="1" min="1"></div>
		<div>Start Y<input type="number" step="1" id="start-y" value="1" min="1"></div>
		<div>Rows<input type="number" step="1" id="rows" min="3" max="50" value="20"></div>
		<div>Columns<input type="number" step="1" id="columns" min="3" max="50" value="20"></div>
		<div>Highlight colour<input type="number" step="10" id="highlite-color" min="0" max="360" value="180"><div id="show-color">&nbsp;</div></div>
		<button id="reset" class="reset">Reset</button>
	</div>
	<div id= "sum" class="sum"></div>
</div>
<div class="container">
	<table id="table" class="table"></table>
</div>
<div id= "footer" class="footer">Click cell to copy value to clipboard. Value to copy: <input type="text" id="product"></div>
              
            
!

CSS

              
                * {
	font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Tahoma, Roboto, Ubuntu, "Helvetica Neue", sans-serif;
}
h1 { 
	color: #555;
	font-size: 1.25em;
}
.control {
	display: flex;
}
.options, .sum {
	background-color: #eee;
	border: 1px solid #ccc;
	font-size: 0.7em;
	padding: 0.5em 1em;
	margin-bottom: 1em;
	border-radius: 4px;
}
.options {
	width: 680px;
	display: flex;
	justify-content: space-between;
}
.options input {
	border-radius: 4px;
	border: 1px solid #aaa;
	width: 40px;
	text-align: right;
	padding: 2px;
	margin-left: 6px;
}
#show-color { 
	width: 28px;
	height: 16px;
	display: inline-block;
/* 	padding: 2px; */
	margin-left: 2px;
	border-radius: 4px;
}
.reset {
	padding: 2px 12px;
	background: #888;
	color: white;
	font-weight: bold;
	border: none;
	border-radius: 4px;
	box-shadow: 2px 2px 2px #aaa;
}
.reset:active {
	box-shadow: none;
	transform: translate(1px, 1px);
	outline: none;
}
input:focus, .reset:focus {
	outline: none;
	background: #888;
	color: white;
}
.sum {
	margin-left: 2em;
	padding-top: 8px;
	display: none;
}
.sum .product {
	font-weight: bold;
}
.table {
	border-collapse: collapse;
	font-size: 0.75em;
}
.table tr:nth-child(odd) {
	background-color: #f2f2f2;
}
.table tr:first-child {
	font-weight: bold;
	background-color: #cacaca;
}
.table tr td:first-child {
	font-weight: bold;
	background-color: #cacaca;
}

.table, th, td {
  border: 1px solid #aaa;
}
.table td {
	padding: 4px 6px;
	width: 28px;
	text-align: right;
}
.table .ch {
	width: 20px;
}

.footer {
	font-size: 0.7em;
	color: #888;
	margin-top: 0.5em;
	display: none;
}
.footer input {
	width: 80px;
	color: #888;
	border: none;
	font-weight: bold;
}
.footer input:focus{
    outline: none;
}
              
            
!

JS

              
                // get the input elements
const startXInput = document.querySelector('#start-x');
const startYInput = document.querySelector('#start-y');
const rowsInput = document.querySelector('#rows');
const colsInput = document.querySelector('#columns');
const highliteColorInput = document.querySelector('#highlite-color');
const resetBtn = document.querySelector('#reset');
// get the display elements
const table = document.querySelector('#table');
const showColor = document.querySelector('#show-color');
const sum = document.querySelector('#sum'); 
const footer = document.querySelector('#footer');
const product = document.querySelector('#product'); // used for copying value to clipboard

// initialise variables
const RowColMin = 1,
	  RowColMax = 50,
	  RowColDefault = 20;
// initialise options object
let options = {};

// get stored options from localStorage
const storedOptions = JSON.parse(localStorage.getItem('options'));
// console.log(storedOptions);

if (storedOptions) { 
	// set values in the options object
	options.startX = storedOptions.startX;
	options.startY = storedOptions.startY;
	options.cols = storedOptions.cols;
	options.rows = storedOptions.rows;
	options.highliteColor = storedOptions.highliteColor;
	// reset values of input elements
	startXInput.value = options.startX;
	startYInput.value = options.startY;
	colsInput.value = options.cols;
	rowsInput.value = options.rows;
	highliteColorInput.value = options.highliteColor;
	
	renderTable();
	
} else {
	// initialise values in the options object
	options.startX = parseInt(startXInput.value);
	options.startY = parseInt(startYInput.value);
	options.cols = parseInt(colsInput.value); 
	options.rows = parseInt(rowsInput.value); 
	options.highliteColor = highliteColorInput.value;
	renderTable();
	
}


// style display elements
showColor.style.backgroundColor = 'hsl(' + options.highliteColor + ', 50%, 50%)';
sum.style.backgroundColor = 'hsl(' + options.highliteColor + ', 50%, 90%)';
sum.style.borderColor = 'hsl(' + options.highliteColor + ', 50%, 50%)';

// add event listeners for the input elements
// event listener for input#start-x
startXInput.addEventListener('input', function(){
	options.startX = parseInt(startXInput.value);
	if (options.startX < 1) {
		startXInput.value = 1;
		options.startX = startXInput.value; 
	}
	localStorage.setItem('options', JSON.stringify(options));
	renderTable();
});
// event listener for input#start-y
startYInput.addEventListener('input', function(){
	options.startY = parseInt(startYInput.value);
	if (options.startY < 1) {
		startYInput.value = 1;
		options.startY = startYInput.value; 
	}
	localStorage.setItem('options', JSON.stringify(options));
	renderTable();
});
// event listener for input#columns
colsInput.addEventListener('input', function(){
	options.cols = parseInt(colsInput.value);
	if (options.cols < RowColMin ||options. cols > RowColMax) {
		colsInput.value = RowColDefault;
		options.cols = RowColDefault;
	}
	localStorage.setItem('options', JSON.stringify(options));
	renderTable();
});
// event listener for input#rows
rowsInput.addEventListener('input', function(){
	options.rows = parseInt(rowsInput.value);
	if (options.rows < RowColMin || options.rows > RowColMax) {
		rowsInput.value = RowColDefault;
		options.rows = RowColDefault;
	}
	localStorage.setItem('options', JSON.stringify(options));
	renderTable();
});
// event listener for input#highlite-color
highliteColorInput.addEventListener('input', function(){
	options.highliteColor = highliteColorInput.value;
	if (options.highliteColor < 0 || options.highliteColor > 360) {
		highliteColorInput.value = 180;
		options.highliteColor = 180;
	}
	showColor.style.backgroundColor = 'hsl(' + options.highliteColor + ', 50%, 50%)';
	sum.style.backgroundColor = 'hsl(' + options.highliteColor + ', 50%, 90%)';
	sum.style.borderColor = 'hsl(' + options.highliteColor + ', 50%, 50%)';
	localStorage.setItem('options', JSON.stringify(options));
	renderTable();
});
// event listener for button#reset
resetBtn.addEventListener('click', function(){
	// reset values in the options object to defaults
	options.startX = 1;
	options.startY = 1;
	options.cols = RowColDefault;
	options.rows = RowColDefault;
	options.highliteColor = 180;
	// reset values of input elements
	startXInput.value = options.startX;
	startYInput.value = options.startY;
	colsInput.value = options.cols;
	rowsInput.value = options.rows;
	highliteColorInput.value = options.highliteColor;
	showColor.style.backgroundColor = 'hsl(' + options.highliteColor + ', 50%, 50%)';
	sum.style.backgroundColor = 'hsl(' + options.highliteColor + ', 50%, 90%)';
	sum.style.borderColor = 'hsl(' + options.highliteColor + ', 50%, 50%)';
	// delete localStorage object
	localStorage.clear();
	renderTable();
});

// the function to display the table
function renderTable() {
	
	let startX = options.startX - 1, // subtract 1 to account for header column
		startY = options.startY - 1, // subtract 1 to account for header row
		rows = options.rows,
		cols = options.cols;
	
	table.innerHTML = ''
	
	// create the table cells
	for (let y = startY; y <= startY+rows; y++) { // the rows

		let row = document.createElement('tr');

		for (let x = startX; x <= startX+cols; x++) { // the columns
			
			let cellContent;
			
			let cell = document.createElement('td');
			if (x == startX) { // header column
				cell.setAttribute('class', 'ch');
				cell.setAttribute('data-hy', y);
				cellContent = document.createTextNode(y);
			}
			if (y == startY) { // header row
				cell.setAttribute('data-hx', x);
				cellContent = document.createTextNode(x);
			}
			if (x==startX && y==startY) {
				cellContent = document.createTextNode(''); // blank cell in top left corner
			}
			if (x>startX && y>startY) {
				cell.setAttribute('class', 'cv');
				cell.setAttribute('data-x', x);
				cell.setAttribute('data-y', y);
				cellContent = document.createTextNode(x * y);
			} 
			cell.appendChild(cellContent);  
			row.appendChild(cell);
		}

		table.appendChild(row);

	}

	// get all the cells
	const cells = document.querySelectorAll('.cv')
	cells.forEach(function(cell){
		
		let bg; // stores the cells original background colour
		
		// add a mouseover event listener to each cell
		cell.addEventListener('mouseover', function(){
			let highliteColor = options.highliteColor;
			// show the hidden elements
			sum.style.display = 'block';
			footer.style.display = 'block';
			// style the current cell
			// this.style.fontWeight = 'bold';
			bg = this.style.backgroundColor;
			this.style.backgroundColor = 'hsl(' + highliteColor + ', 50%, 50%)';
			this.style.color = '#fff';
			this.style.cursor = 'none';
			// find the column header cell and style it
			let x = this.dataset.x;
			let hx = document.querySelector('[data-hx="' + x + '"]');
			hx.style.backgroundColor = 'hsl(' + highliteColor + ', 20%, 40%)';
			hx.style.color = '#ccc';
			// find the row header cell and style it
			let y = this.dataset.y;
			let hy = document.querySelector('[data-hy="' + y + '"]');
			hy.style.backgroundColor = 'hsl(' + highliteColor + ', 20%, 40%)';
			hy.style.color = '#ccc';
			// find and style the column cells between the current cell and the header cell
			let xCells = document.querySelectorAll('[data-y="' + y + '"]');
			xCells.forEach(function(xCell){
				if (parseInt(xCell.dataset.x) < x) {
					xCell.style.backgroundColor = 'hsl(' + highliteColor + ', 50%, 90%)';
				}
			});
			// find and style the row cells between the current cell and the header cell
			let yCells = document.querySelectorAll('[data-x="' + x + '"]');
			yCells.forEach(function(yCell){
				if (parseInt(yCell.dataset.y) < y) {
					yCell.style.backgroundColor = 'hsl(' + highliteColor + ', 50%, 90%)';
				}
			});
			// add content to the sum display and product display
			sum.innerHTML = x + ' x ' + y + ' = <span class="product">' + (x * y) + '</span>';
			product.value = x * y;
		});
		
		// add a mouseout event listener to each cell
		cell.addEventListener('mouseout', function(){
			// revert the current cell to original styling
			// this.style.fontWeight = 'normal';
			this.style.backgroundColor = bg;
			this.style.color = '#000';
			// find and revert the column header cell to original styling
			let x = this.dataset.x;
			let hx = document.querySelector('[data-hx="' + x + '"]');
			hx.style.backgroundColor = '#cacaca';
			hx.style.color = '#000';
			// find and revert the row header cell to original styling
			let y = this.dataset.y;
			let hy = document.querySelector('[data-hy="' + y + '"]');
			hy.style.backgroundColor = '#cacaca';
			hy.style.color = '#000';
			// find and revert the column cells between the current cell and the header cell to the original styling
			let xCells = document.querySelectorAll('[data-y="' + y + '"]');
			xCells.forEach(function(xCell){
				if (parseInt(xCell.dataset.x) < x) {
					xCell.style.backgroundColor = bg;
				}
			});
			// find and revert the row cells between the current cell and the header cell to the original styling
			let yCells = document.querySelectorAll('[data-x="' + x + '"]');
			yCells.forEach(function(yCell){
				if (parseInt(yCell.dataset.y) < y) {
					yCell.style.backgroundColor = bg;
				}
			});
			// hide the display elements
			sum.style.display = 'none';
			footer.style.display = 'none';
			product.value = '';
		});
		// add a click event listener to each cell to copy the value to the clipboard
		cell.addEventListener('click', function(){
			product.select();
			document.execCommand("copy");
			product.blur();
		});
		
	});
	
}


              
            
!
999px

Console