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

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 class="theme-switch-wrapper">
    <label class="theme-switch" for="checkbox">
        <input type="checkbox" id="checkbox" />
        <div class="slider round"></div>
  </label>
</div>
   
    <div class="container">
        
        <h4>A Message You would like to pass</h4>

        <!-- Where we will wite the message and submit it -->
        <form class="message-form">
            <input type="text" class="typedMessage" placeholder="Type Your Message..." autofocus>
            <button class="submit">Send</button>
        </form>

        <!-- Where message will be output for two seconds -->
        <h5>Last Message Delivered</h5>
        <div class="messages"></div>
    </div>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css2?family=Montserrat:[email protected]&display=swap');
@import url('https://fonts.googleapis.com/css2?family=Padauk&display=swap');

:root {
    --primary-color: #dcdc99;
    --secondary-color: #EAEAB4;
    --main-font: 'Montserrat', sans-serif;
    --text-color: black;
}

[data-theme="dark"] {
    --primary-color: #121212;
    --secondary-color: #2B2B2B;
    --main-font: 'Montserrat', sans-serif;
    --text-color: white;
} 

body{
    background-color: var(--secondary-color);
}

.container{
    display: flex;
    flex-direction: column;
    width: 400px;
    margin: 0 auto;
    background-color: var(--primary-color);
    padding: 10px;
    box-shadow: 0 5px 10px rgba(0, 0, 0, 0.4);
    border-radius: 5px;
}

h4{
    font-family: var(--main-font);
    text-align: center;
    padding: 10px;
    color: var(--text-color);
    
}

h5{
    font-family: 'Padauk', sans-serif;
    text-align: center;
    font-size: 17px;
    opacity: 0.5;
    color: var(--text-color);

}

input{
    width: 300px;
    padding: 10px;
    margin-bottom: 10px;
    border-radius: 4px;
    border-style: none;
    background-color: var(--secondary-color);
    font-size: 10px;
    font-weight: 600;
    color: var(--text-color);
}

.messages{
    font-family: var(--main-font);
    color: var(--text-color);
}

/* display: inline-block will help us to avoid the text from  */
.message-item span{
    display: inline-block;
    padding:10px;
    background-color: var(--secondary-color);
    border-radius: 10px;
}

.submit{
    padding: 8px;
    border-radius: 5px;
    font-family: var(--main-font);
    font-size: 15px;
    color: white;
    background-color: #3636C9;
    border-style: none;
}

.theme-switch-wrapper {
    display: flex;
    align-items: center;

  }

  .theme-switch {
    display: inline-block;
    height: 34px;
    position: relative;
    width: 60px;
  }
  
  .theme-switch input {
    display:none;
  }
  
  .slider {
    background-color: #c7c786;
    bottom: 0;
    cursor: pointer;
    left: 0;
    position: absolute;
    right: 0;
    top: 0;
    transition: .4s;
  }
  
  .slider:before {
    background-color: #fff;
    bottom: 4px;
    content: "";
    height: 26px;
    left: 4px;
    position: absolute;
    transition: .4s;
    width: 26px;
  }
  
  input:checked + .slider {
    background-color: #3636C9;
  }
  
  input:checked + .slider:before {
    transform: translateX(26px);
  }
  
  .slider.round {
    border-radius: 34px;
  }
  
  .slider.round:before {
    border-radius: 50%;
  }
  
              
            
!

JS

              
                //Create an array where the message along with it's ID will be stored.
let message = [];

// This fuction will enables us to add the message to the DOM
function addMessage(text){
    //Object where message will be stored
    const chat = {
        text,
        id: Date.now()
    }

    message.push(chat);
    
    //Render message to the screen
    const list = document.querySelector('.messages');
    list.insertAdjacentHTML('beforeend', 
        `<p class="message-item" data-key="${chat.id}">
            <span>${chat.text}</span>
        </p>`

    );
    
    // Delete the message from the screen after 2 seconds
    let token = setTimeout(() => {
        Array.from(list.children).forEach((child) => 
       list.removeChild(child))
       clearTimeout(token);
      },2000);

}



//Create event listener to detect when a message has been submitted
const form = document.querySelector('.message-form');
form.addEventListener('submit', event => {
    event.preventDefault();

    //input to save the message itself
    const input = document.querySelector('.typedMessage');

    //This helps us to detect empty messages and ignore them
    const text = input.value.trim();

    if(text !== ''){
        addMessage(text);
        input.value = '';
        input.focus();
        
    }
});

const toggleSwitch = document.querySelector('.theme-switch input[type="checkbox"]');

function switchTheme(e) {
    if (e.target.checked) {
        document.documentElement.setAttribute('data-theme', 'dark');
    }
    else {
        document.documentElement.setAttribute('data-theme', 'light');
    }    
}

toggleSwitch.addEventListener('change', switchTheme, false);

              
            
!
999px

Console