Css

Example of creating modal windows using CSS and JavaScript

Below is a simple example of how to create a modal window using HTML, CSS, and JavaScript. This example includes a button to open the modal and a close button inside the modal.

HTML Structure (index.html)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <title>Modal Example</title>
</head>
<body>
    <button id="openModal">Open Modal</button>

    <!-- The Modal -->
    <div id="myModal" class="modal">
        <!-- Modal content -->
        <div class="modal-content">
            <span class="close">&times;</span>
            <h2>Modal Header</h2>
            <p>This is a simple modal window.</p>
        </div>
    </div>

    <script src="script.js"></script>
</body>
</html>

CSS Styles (style.css)

Add some styles to make the modal look good.

body {
    font-family: Arial, sans-serif;
}

button {
    padding: 10px 20px;
    background-color: #007bff;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
}

button:hover {
    background-color: #0056b3;
}

/* The Modal (background) */.modal {
    display: none; /* Hidden by default */    position: fixed; /* Stay in place */    z-index: 1; /* Sit on top */    left: 0;
    top: 0;
    width: 100%; /* Full width */    height: 100%; /* Full height */    overflow: auto; /* Enable scroll if needed */    background-color: rgb(0,0,0); /* Fallback color */    background-color: rgba(0,0,0,0.4); /* Black w/ opacity */}

/* Modal Content */.modal-content {
    background-color: #fefefe;
    margin: 15% auto; /* 15% from the top and centered */    padding: 20px;
    border: 1px solid #888;
    width: 80%; /* Could be more or less, depending on screen size */    border-radius: 5px;
}

/* The Close Button */.close {
    color: #aaa;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

.close:hover,
.close:focus {
    color: black;
    text-decoration: none;
    cursor: pointer;
}

JavaScript Logic (script.js)

Implement the JavaScript to handle the opening and closing of the modal.

// Get the modal
var modal = document.getElementById("myModal");

// Get the button that opens the modal
var btn = document.getElementById("openModal");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks the button, open the modal 
btn.onclick = function() {
    modal.style.display = "block";
}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
    modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
}

How It Works:

  1. HTML Structure: We have a button that, when clicked, will show the modal. The modal itself contains a close button (the “×” symbol) and some content.
  2. CSS Styles: We define styles for the modal, including the background, content area, and close button.
  3. JavaScript Logic:
    • We select the modal, the button to open it, and the close button.
    • When the “Open Modal” button is clicked, we change the display style of the modal to “block” to make it visible.
    • When the close button or the area outside the modal is clicked, we hide the modal by setting its display style back to “none”.

Conclusion:

This example provides the basic structure for creating a modal window. You can customize the styling as needed, and add additional content or interactivity to the modal as required for your application.

Victoria

Im just a girl who hanging around with her friends ;)

Recent Posts

Building Your Next Project with wp-scripts: A Comprehensive Guide

WordPress development has evolved significantly, and modern tooling plays a crucial role in creating efficient…

6 days ago

Script for automatically informing search engines about new content on website

I. Project Overview The goal is to automate the process of notifying search engines (like…

2 weeks ago

Creating an XML sitemap script with PHP, designed for automated updates via CRON

1. Database Structure (MySQL) We'll need a database table to store information about our website's…

2 weeks ago

Comprehensive guide on building a URL shortening script

This explanation aims to provide a solid foundation for understanding the process and implementing your…

2 weeks ago

Guide on building a real-time website chat script

Okay, here's a comprehensive guide on building a real-time website chat script using PHP, HTML,…

2 weeks ago

Comprehensive guide on creating a simple website analytics system

Comprehensive guide on creating a simple website analytics system using PHP, HTML, CSS, JavaScript, and…

2 weeks ago