Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

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.

            
              <div class="container">
	<button type="buton" id="add" title="Add an item">+</button>
	<h1 id="title">My Todo List</h1>

	<ul class="list" aria-describedby="title">
		<li>
			<input type="checkbox" name="todolist" id="item-1" aria-labelledby="item-label-1">
			<label for="item-1"></label>
			<span id="item-label-1" contenteditable="true">Your first task</span>
		</li>
		<li>
			<input type="checkbox" name="todolist" id="item-2" aria-labelledby="item-label-2">
			<label for="item-2"></label>
			<span id="item-label-2" contenteditable="true"></span>
		</li>
	</ul>

	<button type="button" id="reset">Reset the list</button>
</div>
            
          
!
            
              :root {
	--primary: #3d7e9a;
}

body {
	width: 370px;
	max-width: 100%;
	margin: 0 auto;
	font-family: Helvetica, Arial, sans-serif;
	background: #f5f9fa;
	color: #444;
}

h1 {
	margin-top: -40px;
	display: flex;
	align-items: center;
	color: var(--primary);
}

.container {
	margin: 40px 16px;
	padding: 32px 24px;
	background: white;
	box-shadow: 0 16px 16px -8px rgba(0, 0, 0, .1);
	border: 1px solid rgba(0,0,0,.025);
}

/*
 * Add button
 */
[id="add"] {
	position: sticky;
	top: 40px;
	width: 48px;
	height: 48px;
	margin-left: 100%;
	padding-bottom: 4px;
	border: 0;
	font-size: 1.5em;
	color: white;
	background: var(--primary);
	border-radius: 50%;
	transition: all .275s;
	cursor: pointer;
}

[id="add"]:hover,
[id="add"]:focus {
	background: #22586f;
	transform: translateY(-2px);
	box-shadow: 0 4px 8px -3px rgba(0,0,0,.6);
}

[id="add"]:active {
	transform: translateY(-1px);
	box-shadow: 0 2px 4px -3px rgba(0,0,0,.8);
}

/**
 * Reset button
 */
[id="reset"] {
	margin-top: 32px;
	background: none;
	padding: 0;
	border: 0;
	font-size: .875em;
	font-weight: bold;
	color: darksalmon;
	cursor: pointer;
	text-decoration: underline;
}

/**
 * THE LIST
 */
.list {
	display: flex;
	flex-direction: column;
	margin: 0;
	padding: 0;
	list-style: none;
}

.list li {
	position: relative;
	margin: 4px 0;
	padding: 13px 16px 5px;
	border-top: 1px solid rgba(0,0,0,.1);
	line-height: 1.55;
	background: white;
}

.list .done {
	padding: 4px 40px;
	margin: 0 -24px;
	border: 0;
	order: 100;
	background: #f5f9fa;
}

.list .done label::before,
.list .done label::after,
.list .done span {
	opacity: .5!important;
}

.list .first-done {
	margin-top: 40px;
}

.list .first-done:before {
	content: 'Things done';
	display: block;
	padding: 8px 0 16px;
	font-weight: bold;
	color: var(--primary);
}

.list span {
	padding: 5px 8px 3px;
	margin-top: -.1em;
	display: inline-block;
	vertical-align: top;
	width: calc(100% - 56px);
	min-height: 1.5em;
	transition: all .275s;
	border-radius: 8px;
}

.list span:empty {
	background: #EEE;
}

.list span:empty::before {
	content: 'Write something';
	opacity: .4;
}

.list span:focus {
	background: var(--primary);
	outline: none;
	color: #FFF;
}

/**
 * Checkbox styling
 * with feature support selector
 * @see: (EN) https://www.creativejuiz.fr/blog/en/tutorials/customize-checkbox-and-radio-button-with-css - (FR) 
 */
[type="checkbox"]:not(:checked), 
[type="checkbox"]:checked {
	position: absolute;
	left: 0;
	opacity: 0.01;
}

/* Preparing the label */
[type="checkbox"]:not(:checked) + label,
[type="checkbox"]:checked + label {
	position: relative;
	padding-left: 1.75em;
	font-size: 1.05em;
	line-height: 1.7;
	cursor: pointer; 
}

/* Box aspect */
[type="checkbox"]:not(:checked) + label:before,
[type="checkbox"]:checked + label:before {
	content: '';
	position: absolute;
	left: 0;
	top: -.25em;
	width: 1.4em;
	height: 1.4em;
	border: 1px solid #aaa;
	background: #FFF;
	border-radius: .2em;
	box-shadow: inset 0 1px 3px rgba(0,0,0, .1), 0 0 0 rgba(203, 34, 237, .2);
	transition: all .275s;
}

/* Check aspect */
[type="checkbox"]:not(:checked) + label:after,
[type="checkbox"]:checked + label:after {
	content: '✕';
	position: absolute;
	top: .35em;
	left: .18em;
	font-size: 1.6em;
	color: var(--primary);
	line-height: 0;
	transition: all .2s;
}

/* Unchecked aspect */
[type="checkbox"]:not(:checked) + label:after {
	opacity: 0;
	transform: scale(0) rotate(45deg);
}

/* Checked aspect */
[type="checkbox"]:checked + label:after {
	opacity: 1;
	transform: scale(1) rotate(0);
}

/* Accessibility focus */
[type="checkbox"]:checked:focus + label:before,
[type="checkbox"]:not(:checked):focus + label:before {
	box-shadow: inset 0 1px 3px rgba(0,0,0, .1), 0 0 0 6px rgba(34, 170, 247, .2);
}
            
          
!
            
              /**
 * CodePen is buggy and doesn't get the LocalStorage data at the first load of JS :/
 */
const add   = document.getElementById('add'),
		reset = document.getElementById('reset');

var saveListData = function(event){
	if ( window.localStorage ) {
		var lis  = document.querySelectorAll('.list li'),
			data = {},
			i    = 0;

		lis.forEach(function(el){
			var item = {};
			item['status'] = el.querySelector('input').checked;
			item['content'] = el.querySelector('span').innerHTML;
			data[i] = item;
			i++;
		});

		localStorage.setItem('my-awesome-todolist', JSON.stringify(data) );
	}
}

/**
 * Add an Item into the list
 */
add.addEventListener('click', function(){
	var last = document.querySelector('.list li:last-child'),
		 num = last.querySelector('input').id.split('-')[1];

	var clone = last.cloneNode(true);

	num = parseInt(num) + 1;
	clone.querySelector('input').setAttribute('id', 'item-' + num );
	clone.querySelector('input').setAttribute('aria-labelledby', 'item-label-' + num );
	clone.querySelector('input').removeAttribute('checked');
	clone.querySelector('input').checked = false;

	clone.querySelector('label').setAttribute('for', 'item-' + num );
	clone.querySelector('span').setAttribute('id', 'item-label-' + num );
	clone.querySelector('span').innerHTML = '';

	clone.className = '';

	last.insertAdjacentElement('afterend', clone);

	document.getElementById( 'item-' + num ).scrollIntoView({
		behavior: 'smooth'
	});

	document.getElementById( 'item-label-' + num ).focus();
});

/**
 * Reset the list
 */
reset.addEventListener('click', function(){
	var clone = document.querySelector('.list li:last-child').cloneNode(true);

	clone.className = '';
	clone.querySelector('span').innerHTML = '';
	clone.querySelector('input').removeAttribute('checked');

	document.querySelector('.list').innerHTML = '';
	document.querySelector('.list').appendChild( clone );

	saveListData();
});

/**
 * Save the data into localStorage
 */
if ( window.localStorage ) {
	var changeItemStatus = function(event){
		var parent = event.target.parentNode,
			lis    = document.querySelectorAll('.list li'),
			fdone  = document.querySelector('.first-done');

		if ( fdone ) {
			fdone.classList.remove('first-done');
		}

		if ( parent.classList.contains('done') ) {
			parent.classList.remove('done');
		} else {
			parent.classList.add('done');
		}

		for ( let el of document.querySelectorAll('.done') ) {
			el.classList.add('first-done');
			break;
		};
	},
	firstDone = false;

	// If data exists, build the list
	if ( localStorage.getItem('my-awesome-todolist') ) {

		var data = JSON.parse( localStorage.getItem('my-awesome-todolist') ),
			 html = '';

		Object.keys(data).forEach(function(k){

			if ( data[k].status === true && firstDone ==! 'already' ) {
				firstDone = true;
			}

			console.log(firstDone);

			html += '<li' + ( data[k].status ? ' class="done' + ( firstDone === true ? ' first-done' : '' ) + '"' : '' ) + '><input type="checkbox" name="todolist" id="item-' + k + '" aria-labelledby="item-label-' + k + '"' + ( data[k].status ? ' checked="checked"' : '' ) + '><label for="item-' + k + '"></label><span id="item-label-' + k + '" contenteditable="true">' + data[k].content + '</span></li>';

			if ( firstDone !== 'already' ) {
				firstDone = firstDone === true ? 'already' : false;
			}
		});

		// Write the existing list.
		document.querySelector('.list').innerHTML = html;
	}

	// Delegation of blur and change events
	document.querySelector('.list').addEventListener('blur', saveListData, true);
	document.querySelector('.list').addEventListener('change', saveListData, true);

	// Delegation of change for ordering items
	document.querySelector('.list').addEventListener('change', changeItemStatus, true);
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console