Skip to main content

Modal with Vanilla JS: Full Code

### CSS Effects **Tags:** Modal, Vanilla JS, Inter Font, Rounded Corners, Soft Shadows **Summary:** A fully functional modal component implemented using vanilla JavaScript with a modern UI design. --- ### ✨ Overview This example demonstrates how to create a modal popup using pure vanilla JavaScript. The modal features rounded corners, soft shadows, and a modern typography style. ### ๐Ÿงฑ Full Implementation
LIVE PREVIEW
Interactive
HTML
<style>
  @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap');
  
  body { font-family: 'Inter', system-ui, sans-serif; background: #f3f4f6; margin: 0; padding: 0; display: flex; justify-content: center; align-items: center; min-height: 100vh; }
  
  .modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    justify-content: center;
    align-items: center;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s, visibility 0.3s;
  }
  
  .modal-overlay.active {
    opacity: 1;
    visibility: visible;
  }
  
  .modal {
    background: #fff;
    border-radius: 12px;
    box-shadow: 0 4px 20px rgba(0,0,0,0.08);
    padding: 30px;
    max-width: 500px;
    width: 90%;
    text-align: center;
    position: relative;
  }
  
  .modal-header {
    margin-bottom: 20px;
  }
  
  .modal-header h2 {
    font-size: 24px;
    color: #1d3557;
  }
  
  .modal-body p {
    font-size: 16px;
    color: #6b7280;
  }
  
  .close-btn {
    position: absolute;
    top: 10px;
    right: 10px;
    cursor: pointer;
    font-size: 24px;
    color: #9ca3af;
  }
</style>

<div id="app">
  <button id="openModalBtn">Open Modal</button>
  <div class="modal-overlay" id="modalOverlay">
    <div class="modal" id="modal">
      <div class="modal-header">
        <h2>Modal Title</h2>
        <span class="close-btn" id="closeModalBtn">&times;</span>
      </div>
      <div class="modal-body">
        <p>This is a modal dialog box.</p>
      </div>
    </div>
  </div>
</div>

<script>
  document.getElementById('openModalBtn').addEventListener('click', function() {
    document.getElementById('modalOverlay').classList.add('active');
  });

  document.getElementById('closeModalBtn').addEventListener('click', function() {
    document.getElementById('modalOverlay').classList.remove('active');
  });
</script>
### ๐Ÿ’ก Explanation 1. **HTML Structure:** - The modal is encapsulated within a `.modal-overlay` div, which acts as the backdrop for the modal. - Inside the overlay, there's a `.modal` div that contains the content of the modal. 2. **CSS Styling:** - The `@import` statement includes the Inter font from Google Fonts. - Basic styling is applied to the body to center its contents. - The `.modal-overlay` has a semi-transparent background and uses flexbox for centering. - The `.modal` div has rounded corners, soft shadows, and padding. 3. **JavaScript Functionality:** - Event listeners are added to the "Open Modal" button to toggle the `active` class on the modal overlay, which controls its visibility. - Similarly, a click event listener is attached to the close button within the modal to hide it again.
โ„น️ Note: Code snippets are ready to copy-paste.

๐Ÿ“š More to Read

Comments

Popular posts from this blog

Accordion with Vanilla JS: Full Code

Overview This guide demonstrates how to build a responsive accordion component using purely Vanilla JavaScript, HTML, and CSS. An accordion allows users to toggle the visibility of content sections, efficiently managing screen space by displaying only the necessary information. We'll focus on a clean, maintainable structure and dynamic interaction without external libraries. Implementation Here is the complete code for the accordion, combining HTML, CSS, and JavaScript into a single, cohesive block. ● LIVE PREVIEW Interactive What is an Accordion? An accordion is a graphical control element comprising a vertically stacked list of items such as labels or thumbnails. Each item can be "expanded" or "collapsed...

Top 5 Gradient Buttons CSS Styles

### CATEGORY: CSS Effects **TAGS**: gradient buttons, CSS styles, user interface, frontend development **SUMMARY**: Discover the top 5 gradient button styles using pure CSS. These styles enhance the visual appeal of your web applications with dynamic and appealing UI elements. --- ## Introduction Gradient buttons are a popular choice in modern web design to draw attention and improve user experience. They create a stunning visual effect by transitioning between two or more colors. In this article, we will explore 5 unique gradient button styles that you can implement using only CSS. ## 1. Neon Gradient Button **Description**: This style creates a vibrant neon effect with a glowing border around the button. ● LIVE PREVIEW Interactive ...

5 Creative Loading Spinners Designs

Introduction Loading spinners are more than just a visual cue; they're critical elements in user experience design. A well-crafted spinner can mitigate perceived wait times, reassure users that the system is active, and even delight them with subtle animations. As frontend developers and UI engineers, our goal is to integrate these seamlessly and efficiently. Here, we'll explore five distinct, creative loading spinner designs, each implemented with concise HTML and CSS, focusing on performance and visual appeal. 1. Orbiting Dots Loader This design features multiple small dots that gracefully orbit a central point, creating a fluid and engaging animation. It's a classic pattern made elegant through synchronized but offset movements. ● LIVE PREVIEW Interactive ...