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.

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

              
                .page
	.title CSS Calculator
	.separator
	- let n = 0
	while n < (16*2)
		if ((n % 16 === 0) && (n !== 0))
			br/
		input.num(type="radio" name="num_1" id=`add_${n}` value=`${n}` checked=((n === 5) ? "checked" : undefined))
		- n++
	- n = 0
	.space
	input.oper(type="radio" name="operation" id="oper_add" value="+" checked)
	input.oper(type="radio" name="operation" id="oper_minus" value="-")
	input.oper(type="radio" name="operation" id="oper_multi" value="*")
	.space
	while n < (16*2)
		if ((n % 16 === 0) && (n !== 0))
			br/
		input.num(type="radio" name="num_2" id=`add_${n}` value=`${n}` checked=((n === 12) ? "checked" : undefined))
		- n++
	.separator
	.result
              
            
!

CSS

              
                html, body {
	min-height: 100vh;
	margin: 0;
	padding: 0;
}
body {
	background-color: #246;
	display: flex;
	flex-direction: column;
	font-family: Helvetica;
}

.page {
	background: #fff;
	border-radius: 8px;
	box-shadow: 0 0 8px #0003;
	box-sizing: border-box;
	flex: 1;
	margin: 8px auto;
	padding: 8px;
	text-align: center;
	
	min-width: 320px;
	max-width: 960px;
}
.title {
	text-align: left;
	font-size: 3em;
}
.separator {
	background: #0001;
	height: 1px;
	margin: 16px 0;
}
.num, .oper {
	appearance: none;
	background: #eee;
	border: 2px solid #0003;
	border-radius: 8px;
	box-sizing: border-box;
	color: #666;
	cursor: pointer;
	display: inline-block;
	margin: .2em .05em;
	
	&:hover { background: #fff; }
	&:active { background: #ddd; }
	&:checked {
		background: #9ef;
		&[name="num_2"] { background: #fe9; }
	}
	&:before {
		content: attr(value);
		display: inline-flex;
		width: 1.8em;
		height: 1.6em;
		align-items: center;
		justify-content: center;
		text-align: center;
	}
}
.oper {
	font-size: 2em;
	font-weight: bold;
	line-height: .75em;
	margin: .5em 0em;
	
	&:checked {
		background: #bfb;
		&#oper_minus { background: #fbb; }
		&#oper_multi { background: #ffb; }
	}
}

/* The magic */
/*
	SCSS performs a for loop from 0 to 100 to create rulesets for each button when clicked. Whenever one of these buttons is clicked, the .result element would store a new value within the --num_1 or --num_2 variables based on what was clicked.

	For instance, if 1 and 5 were both clicked, a calculation (depending on the .oper button that was clicked) would be performed with those two numbers.

	The result would then be outputted in a string.
*/
@for $value from 0 through 100 {
	.num[value="#{$value}"] {
		&[name="num_1"]:checked ~ .result { --num_1: #{$value} }
		&[name="num_2"]:checked ~ .result { --num_2: #{$value} }
	}
}

/*
	The .result element stores the operation as well as a string counterpart within the --oper and --oper_string variables.
*/
#oper_add:checked ~ .result 	{ --oper: +; --oper_string: "+"; }
#oper_minus:checked ~ .result { --oper: -; --oper_string: "-"; }
#oper_multi:checked ~ .result { --oper: *; --oper_string: "*"; }

/* This is where the magic really happens. */
.result {
	font-family: Consolas, monospace;
	font-size: 3em;
	
	/* First we calculate the answer by using CSS' calc function to play with three variables. The result would then be stored inside the --total variable. */
	--total: calc(var(--num_1, 0) var(--oper, +) var(--num_2, 0));
	
	/* Next, we're going to do something weird. We're going to turn --num_1, --num_2, and --total into strings by using counter-reset to store the integers into counter variables. Since counters are just integers turned into strings, well... */
	counter-reset:
		total var(--total, 0)
		num_1 var(--num_1, 0)
		num_2 var(--num_2, 0);
	
	/* Now we can create the result string. I did a bunch of line-breaks so they can be somewhat easier to breakdown and read.
		
		It reads: <num_1> <--oper_string> <num_2> = <total>
	*/
	--string:
		counter(num_1)
		" "
		var(--oper_string, "+")
		" "
		counter(num_2)
		" = "
		counter(total);
}
.result:after {
	content: var(--string);
}

.footer {
	position: fixed;
	bottom: 0;
	left: 0;
	right: 0;
	padding: 1em;
}
.footer ul {
	list-style: none;
	padding: 0;
	margin: 0;
}
              
            
!

JS

              
                
              
            
!
999px

Console