HTML Exercises

CSS-Only Interactive Map

    HTML (index.html)
    <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>CSS-Only Interactive Map</title>
            <style>
                body {
                    font-family: 'Arial', sans-serif;
                    margin: 0;
                    display: flex;
                    justify-content: center;
                    align-items: center;
                    height: 100vh;
                    background-color: #f4f7fa;
                }
        
                /* Map Container */
                .map-container {
                    position: relative;
                    width: 600px;
                    height: 400px;
                    background-color: #eef2ff;
                    border: 2px solid #3b82f6;
                    border-radius: 10px;
                    overflow: hidden;
                }
        
                /* Map Image (You can replace this with a map SVG or PNG) */
                .map {
                    width: 100%;
                    height: 100%;
                    position: absolute;
                }
        
                /* Interactive Regions (For simplicity, we're using a grid to represent areas) */
                .region {
                    position: absolute;
                    cursor: pointer;
                    transition: background-color 0.3s ease, transform 0.3s ease;
                }
        
                /* Define regions using absolute positioning */
                .region-1 {
                    top: 50px;
                    left: 100px;
                    width: 150px;
                    height: 100px;
                    background-color: rgba(59, 130, 246, 0.5);
                }
        
                .region-2 {
                    top: 200px;
                    left: 250px;
                    width: 120px;
                    height: 80px;
                    background-color: rgba(59, 130, 246, 0.5);
                }
        
                .region-3 {
                    top: 100px;
                    left: 400px;
                    width: 130px;
                    height: 120px;
                    background-color: rgba(59, 130, 246, 0.5);
                }
        
                /* Hover effects to show information and scale region */
                .region:hover {
                    background-color: rgba(16, 185, 129, 0.7);
                    transform: scale(1.1);
                }
        
                /* Info Popups */
                .region::before {
                    content: attr(data-info);
                    position: absolute;
                    top: -40px;
                    left: 50%;
                    transform: translateX(-50%);
                    background-color: #3b82f6;
                    color: white;
                    padding: 5px 10px;
                    border-radius: 5px;
                    font-size: 12px;
                    opacity: 0;
                    pointer-events: none;
                    transition: opacity 0.3s ease, transform 0.3s ease;
                }
        
                /* Show popup on hover */
                .region:hover::before {
                    opacity: 1;
                    transform: translateX(-50%) translateY(-10px);
                }
        
                /* For more complexity, you can add animations or visual effects */
                @keyframes regionHighlight {
                    0% { background-color: rgba(59, 130, 246, 0.5); }
                    50% { background-color: rgba(96, 165, 250, 0.7); }
                    100% { background-color: rgba(59, 130, 246, 0.5); }
                }
        
                .region-1 {
                    animation: regionHighlight 3s infinite alternate;
                }
            </style>
        </head>
        <body>
        
            <div class="map-container">
                <!-- Map Image or Placeholder -->
                <img src="https://via.placeholder.com/600x400" alt="Map" class="map">
        
                <!-- Regions (Interactive areas) -->
                <div class="region region-1" data-info="Region 1: Northern Area"></div>
                <div class="region region-2" data-info="Region 2: Central Area"></div>
                <div class="region region-3" data-info="Region 3: Southern Area"></div>
            </div>
        
        </body>
        </html>
        
  1. Save and open index.html in your browser to view your website.

  2. Key Features of This Interactive Map:

    1. Map Container and Image:
      • The .map-container holds the map image and interactive regions.
      • You can replace the placeholder map with an SVG or an actual map image.
    2. Interactive Regions:
      • Each region (.region-1, .region-2, .region-3) is styled with absolute positioning, simulating different parts of the map.
      • You can adjust the top, left, width, and height values to fit your map layout.
    3. Hover Effects:
      • When hovering over a region, the background color changes, and the region grows slightly with transform: scale(1.1).
      • The regions transition smoothly due to the transition: background-color 0.3s ease, transform 0.3s ease.
    4. Popup Information:
      • When hovering over a region, a tooltip with detailed information (data-info) appears, floating above the region. The tooltip fades in and moves upward slightly with transform: translateY(-10px).
    5. Animation for Highlighting a Region:
      • The .region-1 area animates continuously with a color pulsing effect using the @keyframes animation.
    6. No JavaScript Required:
      • This interactive map is purely CSS-driven. The hover states, transitions, and keyframe animations are all handled with CSS.
    7. Customizability:
      • You can easily add more regions by defining new .region classes and positioning them appropriately on the map.
      • For an SVG map, each region can be mapped directly to SVG paths and styled similarly.

    This CSS-only interactive map is a great way to provide basic interactivity and a sleek design without using JavaScript. It can be further enhanced with additional CSS animations and effects.

Feel free to customize the content and structure as you like.