Sure! Below is a complete, modern, and stylish HTML page about countries. It includes interactive features such as a search bar to filter countries, and clicking on a country card opens a modal with more detailed information. The design is responsive and uses CSS Flexbox for layout, along with some smooth transitions for interactivity.
Features:
- Search Functionality: Quickly find countries by name.
- Responsive Grid Layout: Displays country cards in a flexible grid that adapts to different screen sizes.
- Modal Popup: Click on a country card to view more detailed information in a modal.
- Dynamic Data Fetching: Uses the REST Countries API to fetch up-to-date information about countries.
- Modern Design: Clean and stylish UI with smooth animations.
Instructions:
- Create an HTML File: Save the following code into a file named
index.html.
- Open in Browser: Open
index.html in your web browser to see the page in action.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Explore Countries</title>
<!-- Google Fonts -->
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet">
<!-- Icons -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" integrity="sha512-Fo3rlrZj/k7ujTnHg4CGR2DhWN+e3vuFJh5QzT5y4cxy/hEIyZy6GKUE2zV+2cpKBk6iYVZ+V+5Xk6V+HC1wQ==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<style>
/* Reset */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
/* Typography */
body {
font-family: 'Roboto', sans-serif;
background-color: #f4f4f4;
color: #333;
}
/* Header */
header {
background-color: #4CAF50;
color: white;
padding: 1rem 2rem;
text-align: center;
position: sticky;
top: 0;
z-index: 1000;
}
header h1 {
margin-bottom: 0.5rem;
}
/* Search Bar */
.search-container {
margin: 1rem auto;
max-width: 600px;
padding: 0 1rem;
}
.search-container input {
width: 100%;
padding: 0.75rem 1rem;
border: none;
border-radius: 5px;
font-size: 1rem;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
transition: box-shadow 0.3s ease;
}
.search-container input:focus {
outline: none;
box-shadow: 0 4px 10px rgba(0,0,0,0.2);
}
/* Countries Grid */
.countries-container {
display: flex;
flex-wrap: wrap;
justify-content: center;
padding: 1rem;
gap: 1.5rem;
}
.country-card {
background-color: white;
border-radius: 10px;
overflow: hidden;
width: 250px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
cursor: pointer;
transition: transform 0.3s, box-shadow 0.3s;
}
.country-card:hover {
transform: translateY(-5px);
box-shadow: 0 4px 15px rgba(0,0,0,0.2);
}
.country-flag img {
width: 100%;
height: 150px;
object-fit: cover;
}
.country-info {
padding: 1rem;
}
.country-info h3 {
margin-bottom: 0.5rem;
font-size: 1.2rem;
color: #4CAF50;
}
.country-info p {
font-size: 0.95rem;
margin-bottom: 0.3rem;
}
/* Modal */
.modal {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0,0,0,0.6);
display: none;
align-items: center;
justify-content: center;
padding: 1rem;
z-index: 2000;
animation: fadeIn 0.3s ease;
}
.modal-content {
background-color: white;
border-radius: 10px;
max-width: 800px;
width: 100%;
max-height: 90vh;
overflow-y: auto;
position: relative;
padding: 2rem;
animation: slideIn 0.3s ease;
}
.close-button {
position: absolute;
top: 1rem;
right: 1rem;
background: none;
border: none;
font-size: 1.5rem;
cursor: pointer;
color: #aaa;
transition: color 0.3s;
}
.close-button:hover {
color: #000;
}
.modal-content img {
width: 100%;
height: auto;
border-radius: 5px;
margin-bottom: 1rem;
}
.modal-details {
display: flex;
flex-direction: column;
gap: 0.5rem;
}
.modal-details p {
font-size: 1rem;
}
/* Animations */
@keyframes fadeIn {
from {opacity: 0;}
to {opacity: 1;}
}
@keyframes slideIn {
from {transform: translateY(-50px); opacity: 0;}
to {transform: translateY(0); opacity: 1;}
}
/* Responsive */
@media (min-width: 600px) {
.modal-details {
flex-direction: row;
flex-wrap: wrap;
}
.modal-details div {
flex: 1 1 45%;
}
}
</style>
</head>
<body>
<header>
<h1><i class="fas fa-globe"></i> Explore Countries</h1>
<p>Discover information about countries around the world</p>
</header>
<div class="search-container">
<input type="text" id="searchInput" placeholder="Search for a country...">
</div>
<div class="countries-container" id="countriesContainer">
<!-- Country cards will be injected here -->
</div>
<!-- Modal -->
<div class="modal" id="countryModal">
<div class="modal-content">
<button class="close-button" id="closeModal">×</button>
<img src="" alt="Country Flag" id="modalFlag">
<div class="modal-details">
<div>
<p><strong>Name:</strong> <span id="modalName"></span></p>
<p><strong>Capital:</strong> <span id="modalCapital"></span></p>
<p><strong>Region:</strong> <span id="modalRegion"></span></p>
<p><strong>Subregion:</strong> <span id="modalSubregion"></span></p>
</div>
<div>
<p><strong>Population:</strong> <span id="modalPopulation"></span></p>
<p><strong>Languages:</strong> <span id="modalLanguages"></span></p>
<p><strong>Currencies:</strong> <span id="modalCurrencies"></span></p>
<p><strong>Timezones:</strong> <span id="modalTimezones"></span></p>
</div>
</div>
</div>
</div>
<!-- JavaScript -->
<script>
const countriesContainer = document.getElementById('countriesContainer');
const searchInput = document.getElementById('searchInput');
const countryModal = document.getElementById('countryModal');
const closeModal = document.getElementById('closeModal');
const modalFlag = document.getElementById('modalFlag');
const modalName = document.getElementById('modalName');
const modalCapital = document.getElementById('modalCapital');
const modalRegion = document.getElementById('modalRegion');
const modalSubregion = document.getElementById('modalSubregion');
const modalPopulation = document.getElementById('modalPopulation');
const modalLanguages = document.getElementById('modalLanguages');
const modalCurrencies = document.getElementById('modalCurrencies');
const modalTimezones = document.getElementById('modalTimezones');
let countriesData = [];
// Fetch countries data from REST Countries API
fetch('https://restcountries.com/v3.1/all')
.then(response => response.json())
.then(data => {
countriesData = data;
displayCountries(data);
})
.catch(error => {
countriesContainer.innerHTML = '<p>Error fetching countries data.</p>';
console.error('Error:', error);
});
// Display countries in the container
function displayCountries(countries) {
countriesContainer.innerHTML = '';
countries.forEach(country => {
const countryCard = document.createElement('div');
countryCard.classList.add('country-card');
countryCard.innerHTML = `
<div class="country-flag">
<img src="${country.flags.svg}" alt="Flag of ${country.name.common}">
</div>
<div class="country-info">
<h3>${country.name.common}</h3>
<p><strong>Capital:</strong> ${country.capital ? country.capital[0] : 'N/A'}</p>
<p><strong>Region:</strong> ${country.region}</p>
<p><strong>Population:</strong> ${country.population.toLocaleString()}</p>
</div>
`;
countryCard.addEventListener('click', () => openModal(country));
countriesContainer.appendChild(countryCard);
});
}
// Search functionality
searchInput.addEventListener('input', (e) => {
const query = e.target.value.toLowerCase();
const filteredCountries = countriesData.filter(country => country.name.common.toLowerCase().includes(query));
displayCountries(filteredCountries);
});
// Open modal with country details
function openModal(country) {
modalFlag.src = country.flags.svg;
modalFlag.alt = `Flag of ${country.name.common}`;
modalName.textContent = country.name.common;
modalCapital.textContent = country.capital ? country.capital[0] : 'N/A';
modalRegion.textContent = country.region;
modalSubregion.textContent = country.subregion || 'N/A';
modalPopulation.textContent = country.population.toLocaleString();
modalLanguages.textContent = country.languages ? Object.values(country.languages).join(', ') : 'N/A';
modalCurrencies.textContent = country.currencies ? Object.values(country.currencies).map(c => c.name).join(', ') : 'N/A';
modalTimezones.textContent = country.timezones.join(', ');
countryModal.style.display = 'flex';
}
// Close modal
closeModal.addEventListener('click', () => {
countryModal.style.display = 'none';
});
// Close modal when clicking outside the modal content
window.addEventListener('click', (e) => {
if (e.target === countryModal) {
countryModal.style.display = 'none';
}
});
// Optional: Close modal with 'Escape' key
window.addEventListener('keydown', (e) => {
if (e.key === 'Escape') {
countryModal.style.display = 'none';
}
});
</script>
</body>
</html>
Explanation of the Code:
-
HTML Structure:
- Header: Contains the title of the page with an icon and a subtitle.
- Search Container: An input field allowing users to search for countries by name.
- Countries Container: A flex container where country cards are dynamically injected.
- Modal: A hidden overlay that becomes visible when a country card is clicked, displaying detailed information about the selected country.
-
CSS Styling:
- Reset and Typography: Resets default margins and paddings, and sets a clean font.
- Header Styling: Sticky header with a background color, ensuring it stays on top during scroll.
- Search Bar: Styled input with shadow effects for better visibility and user experience.
- Country Cards: Clean, card-based layout with hover effects to enhance interactivity.
- Modal Styling: Centered modal with a semi-transparent background, smooth fade-in and slide-in animations, and responsive layout for detailed information.
- Responsive Design: Uses media queries to ensure the layout adapts gracefully to different screen sizes.
-
JavaScript Functionality:
- Data Fetching: Uses the Fetch API to retrieve data from the REST Countries API.
- Displaying Countries: Dynamically creates and inserts country cards into the DOM based on fetched data.
- Search Functionality: Filters displayed countries in real-time as the user types into the search bar.
- Modal Interaction: Opens a modal with detailed information when a country card is clicked and provides multiple ways to close the modal (clicking the close button, clicking outside the modal, or pressing the 'Escape' key).
-
External Resources:
- Google Fonts: Imports the 'Roboto' font for a modern look.
- Font Awesome: Adds an icon to the header for visual appeal.
Testing the Page:
- Open the HTML File: After saving the code in
index.html, open it using any modern web browser (e.g., Chrome, Firefox, Edge).
- Interact with the Features:
- Search: Type in the search bar to filter countries by name.
- View Details: Click on any country card to open the modal with more information.
- Close Modal: Click the close button (
×), click outside the modal content, or press the 'Escape' key to close the modal.
Feel free to customize the styling or add more features as per your requirements!