html + css: a world map
- User
html + css: a world map
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
html + css: a world map
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
<!DOCTYPE html>
<html>
<head>
<title>World Map</title>
<style>
/* General Styles */
body {
font-family: sans-serif;
margin: 0;
padding: 0;
background-color: #f0f0f0; /* Light grey background */
}
.container {
width: 90%;
max-width: 1200px;
margin: 20px auto;
background-color: #fff; /* White container background */
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); /* Subtle shadow */
}
/* Map Styles */
#world-map {
width: 100%;
height: 0; /* Important for maintaining aspect ratio */
padding-bottom: 50%; /* Aspect ratio: 100% width / 2 = 50% height. Adjust as needed. A value of 50% corresponds to a 2:1 ratio. Change for other ratios */
position: relative; /* Needed for absolute positioning of map elements */
background-color: #b0e2ff; /* Light blue for the ocean */
overflow: hidden; /* Clip anything that overflows */
}
/* Country styles (You'll need to customize these) */
.country {
position: absolute;
background-color: #a0d468; /* Light green for land */
border: 1px solid #87a74b; /* Slightly darker green border */
cursor: pointer;
transition: background-color 0.3s ease; /* Smooth hover effect */
z-index: 1; /* Ensure countries are above the ocean */
}
.country:hover {
background-color: #5cb85c; /* Darker green on hover */
}
/* Example Country Regions - Replace with actual coordinates */
#north-america {
top: 10%; /* Percentage of the container's height */
left: 5%; /* Percentage of the container's width */
width: 25%;
height: 20%;
}
#europe {
top: 25%;
left: 35%;
width: 15%;
height: 15%;
}
#asia {
top: 15%;
left: 55%;
width: 30%;
height: 30%;
}
#africa {
top: 40%;
left: 30%;
width: 20%;
height: 25%;
}
#australia {
top: 65%;
left: 75%;
width: 10%;
height: 15%;
}
#south-america {
top: 50%;
left: 15%;
width: 15%;
height: 30%;
}
/* Tooltip (Optional) */
.tooltip {
position: absolute;
background-color: rgba(0, 0, 0, 0.8);
color: white;
padding: 5px 10px;
border-radius: 5px;
font-size: 12px;
z-index: 2;
display: none;
pointer-events: none; /* Allow clicks to pass through tooltip */
}
/* Active Country style */
.country.active {
background-color: #e67e22; /* Orange color when active */
border: 1px solid #d35400;
}
/* Responsive adjustments */
@media (max-width: 768px) {
.container {
width: 95%;
padding: 10px;
}
#world-map {
padding-bottom: 75%; /*Adjust the aspect ratio on smaller screens */
}
}
</style>
</head>
<body>
<div class="container">
<h1>World Map</h1>
<div id="world-map">
<div id="north-america" class="country" data-country="North America"></div>
<div id="europe" class="country" data-country="Europe"></div>
<div id="asia" class="country" data-country="Asia"></div>
<div id="africa" class="country" data-country="Africa"></div>
<div id="australia" class="country" data-country="Australia"></div>
<div id="south-america" class="country" data-country="South America"></div>
<div class="tooltip"></div> <!-- Empty tooltip container -->
</div>
<p>Click on a region to highlight it.</p>
</div>
<script>
const countries = document.querySelectorAll('.country');
const tooltip = document.querySelector('.tooltip');
countries.forEach(country => {
country.addEventListener('mouseover', (e) => {
const countryName = country.dataset.country;
tooltip.textContent = countryName;
// Position the tooltip near the mouse cursor
tooltip.style.left = (e.clientX + 10) + 'px';
tooltip.style.top = (e.clientY + 10) + 'px';
tooltip.style.display = 'block';
});
country.addEventListener('mouseout', () => {
tooltip.style.display = 'none';
});
country.addEventListener('click', (e) => {
// Remove 'active' class from all countries
countries.forEach(c => c.classList.remove('active'));
// Add 'active' class to the clicked country
country.classList.add('active');
e.stopPropagation(); // Prevent click from propagating to the document
});
});
document.addEventListener('click', () => {
// Remove 'active' class from all countries when clicking outside
countries.forEach(c => c.classList.remove('active'));
});
</script>
</body>
</html>
Key improvements and explanations:
#world-map div uses the padding-bottom technique to maintain the aspect ratio of the map, even when the browser window is resized. This avoids the map becoming distorted. A padding-bottom value of 50% gives a 2:1 aspect ratio. Change this value to suit your desired map shape. Set height: 0; on the #world-map element to make this work correctly.position: relative; and position: absolute;: The #world-map container is set to position: relative; so that the country divs can be positioned absolutely within it. This is essential for placing the country divs correctly on the map..country class defines the basic appearance of the country regions (color, border, hover effect).z-index: Ensures the countries are rendered above the ocean background.div elements for North America, Europe, Asia, Africa, Australia, and South America. You will need to replace these with actual coordinates to position them accurately on the map. The top, left, width, and height properties are all expressed as percentages of the parent container's dimensions. This makes the map responsive.pointer-events: none; style allows clicks to pass through the tooltip to the countries underneath.data-country Attribute: Each country div has a data-country attribute that stores the country's name. This is used by the JavaScript to display the correct tooltip text.e.stopPropagation(): Prevents the click event from bubbling up to the document, which would immediately remove the active class.How to Use and Customize:
worldmap.html).top, left, width, and height properties of the example country regions (#north-america, #europe, #asia, etc.) to match the actual locations of those regions on a world map. You will need to experiment and fine-tune these values. Consider using a world map image as a guide, overlaid with the HTML elements, to make positioning easier. Use your browser's developer tools to inspect the elements and adjust the CSS in real-time.div elements for the other countries you want to include on the map, and style them accordingly.Important Considerations:
This comprehensive example provides a good starting point for creating a basic world map using HTML and CSS. Remember to replace the placeholder coordinates with accurate ones for a more realistic representation. For more advanced mapping functionality, consider using a dedicated mapping library.