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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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 id="wrapper">
  <form>
    <h1>Temperature Converter</h1>
    <div id="input">
      <input type="number" min="-273" placeholder="Type here" required>
      <div id="inputUnit"><p>°C</p></div>
    </div>
    <div id="submit">
      <button>Convert</button>
    </div>
    <div id="toggle">
      <button id="toggleButton">°C / °F</button>
    </div>
    <div id="output">
      <div id="result"><p></p></div>
      <div id="resultUnit"><p>°F</p></div>
    </div>
  </form>
</div>
<div class="footer"><p>Created by <a href="https://www.rembertdesigns.co/" target="_blank">Richard Rembert</a></p>
    </div>
              
            
!

CSS

              
                *, *:before, *:after {
  box-sizing: border-box;
}
body{
  font-family: sans-serif;
  background-color: Linen;
}
#wrapper{
  width: 270px;
  height: 265px;
  position: absolute;
	top:0;
	bottom: 0;
	left: 0;
	right: 0;
	margin: auto;
  padding: 20px;
  background-color: rgba(0, 0, 0, 0.1);
}
form{
  display: grid;
  grid-template-columns: 140px 70px;
  grid-template-rows: 0.5fr 1fr 1fr;
  grid-template-areas:
    "title title"
    "input unit"
    "output button";
  grid-gap: 20px;
}
h1{
  grid-area: title;
  font-size: 18px;
  text-align: center;
}
#input{
  grid-area: input;
  display: flex;
  border: 3px inset;
}
input[type="number"]{
  width: 67%;
  height: stretch;
  border: none;
  text-align: center;
  font-size: 18px;
  opacity: 60%;
}
::placeholder{
  font-size: 12px;
  text-align: center;
}
#inputUnit{
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: Gainsboro;
  width: 33%;
  height: stretch;
  text-align: center;
  font-size: 18px;
  font-weight: 600;
  opacity: 60%;
}
#toggle{
  width: 100%;
  height: 100%;
}
#toggleButton{
  width: 100%;
  height: 100%;
}
#output{
  grid-area: output;
  display: flex;
  align-items: center;
  background-color: rgba(0, 0, 0, 0.7);
}
#result{
  position: relative;
  width: 67%;
  height: stretch;
  text-align: center;
  font-size: 18px;
  color: white;
  right: 7px;
}
#resultUnit{
  display: flex;
  align-items: center;
  justify-content: center;
  width: 33%;
  height: 100%;
  font-size: 18px;
  font-weight: 600;
  text-align: center;
  color: white;
  background-color: rgba(0, 0, 0, 0.2);
}
#submit{
  grid-area: button;
}
#submit button{
  width: 100%;
  height: 70px;
}
#toggleButton, #submit button{
  border: 3px outset;
  background-color: rgba(0, 0, 0, 0.4);
  color: white;
  font-size: 14px;
  font-weight: 600;
}
#toggleButton:focus, #submit button:focus, input[type="number"]:focus{
  outline: none;
}
#toggleButton:hover, #submit button:hover {
  background-color: rgba(0, 0, 0, 0.6);
}
.footer {
    position: absolute;
    right: 0;
    bottom: 0;
    left: 0;
    padding: 1rem;
    background-color: #000;
    text-align: center;
}
.footer p {
  text-decoration: none;
  color: #fff;
}
.footer a {
    text-decoration: none;
    color: red;
}
.footer a:hover {
    text-decoration: underline;
    color: gray;
}
              
            
!

JS

              
                document.addEventListener('DOMContentLoaded', () => {
  
  let temperature
  let unit = 'C';
  let result
  
  const form = document.querySelector('form')
  const tempInput = document.querySelector('input[type="number"]')
  const inputUnit = document.querySelector('#inputUnit p')
  const unitSwitch = document.querySelector('#toggleButton')
  const resultBox = document.querySelector('#result p')
  const resultUnitBox = document.querySelector('#resultUnit p')
  const convertCelsius = celsius => Math.round(celsius * 9 / 5 + 32);
  const convertFahrenheit = fahrenheit => Math.round((fahrenheit - 32) * 5 / 9);
  
  tempInput.addEventListener('focus', event => {
    resultBox.textContent = ''
  })  
/*
  tempInput.addEventListener('input', event => {
    temperature = tempInput.value
    resultBox.textContent = ''
  })
*/
  unitSwitch.addEventListener('click', (event) => {
    event.preventDefault();
    resultBox.textContent = ''
    resultUnitBox.textContent = `°${unit}`
    if (unit === 'C') {
      unit = 'F'
      tempInput.setAttribute("min", "-459")
    } else {
      unit = 'C'
      tempInput.setAttribute("min", "-273")
    }
    inputUnit.textContent = `°${unit}`
  })
  form.addEventListener('submit', (event) => {
    event.preventDefault();
    unit === 'C' ? result = convertCelsius(tempInput.value) : result = convertFahrenheit(tempInput.value)
    resultBox.textContent = result
  });
});
              
            
!
999px

Console