Sure! Below is a modern and stylish HTML page about countries. The page includes interactive functionalities such as:
- Country Search: Search for a country by name.
- Country Details: Display detailed information about the selected country.
- Responsive Design: The page is responsive and works well on different screen sizes.
The page uses modern HTML5, CSS3, and JavaScript ES6 features.
Here's the complete code:
HTML (index.html):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>World Countries</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Link to Google Fonts for custom fonts -->
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet">
<!-- Link to the external CSS file -->
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header>
<h1>Explore World Countries</h1>
</header>
<main>
<section class="search-section">
<input type="text" id="searchInput" placeholder="Search for a country...">
</section>
<section id="countriesContainer" class="countries-container">
<!-- Country cards will be injected here by JavaScript -->
</section>
</main>
<footer>
<p>© 2023 World Countries Information</p>
</footer>
<!-- Link to the external JavaScript file -->
<script src="script.js"></script>
</body>
</html>
CSS (styles.css):
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: 'Roboto', sans-serif;
background-color: #f5f7fa;
color: #333;
}
header {
background-color: #4e73df;
color: #fff;
padding: 20px 0;
text-align: center;
}
header h1 {
font-size: 2em;
}
.search-section {
margin: 20px;
text-align: center;
}
#searchInput {
width: 80%;
max-width: 500px;
padding: 10px;
font-size: 1em;
}
.countries-container {
display: flex;
flex-wrap: wrap;
justify-content: center;
margin: 20px;
}
.country-card {
background-color: #fff;
border: 1px solid #ddd;
border-radius: 8px;
width: 300px;
margin: 15px;
overflow: hidden;
transition: box-shadow 0.3s;
}
.country-card:hover {
box-shadow: 0 0 11px rgba(33,33,33,.2);
}
.country-flag img {
width: 100%;
height: auto;
}
.country-details {
padding: 15px;
}
.country-details h2 {
margin-bottom: 10px;
}
.country-details p {
margin-bottom: 5px;
}
footer {
background-color: #4e73df;
color: #fff;
text-align: center;
padding: 10px 0;
}
@media screen and (max-width: 600px) {
.country-card {
width: 100%;
margin: 10px 0;
}
}
JavaScript (script.js):
// Fetch country data from the REST Countries API
const apiURL = 'https://restcountries.com/v3.1/all';
const countriesContainer = document.getElementById('countriesContainer');
const searchInput = document.getElementById('searchInput');
let countriesData = [];
// Fetch countries data from API
async function getCountries() {
try {
const res = await fetch(apiURL);
const data = await res.json();
countriesData = data;
displayCountries(data);
} catch (error) {
console.error('Error fetching countries data:', error);
countriesContainer.innerHTML = '<p>Failed to load country data.</p>';
}
}
// Display countries on the page
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.png}" alt="Flag of ${country.name.common}">
</div>
<div class="country-details">
<h2>${country.name.common}</h2>
<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>
`;
countriesContainer.appendChild(countryCard);
});
}
// Search functionality
searchInput.addEventListener('input', e => {
const searchTerm = e.target.value.toLowerCase();
const filteredCountries = countriesData.filter(country =>
country.name.common.toLowerCase().includes(searchTerm)
);
displayCountries(filteredCountries);
});
// Initialize the app
getCountries();
Instructions:
-
Create Project Folder: Create a new folder for the project.
-
Add Files: Inside the folder, create three files:
index.html
styles.css
script.js
-
Copy Code: Copy the code provided above into the corresponding files.
-
Run the Application: Open index.html in a modern web browser (e.g., Chrome, Firefox, Edge).
Features:
-
Live Search: As you type in the search box, the list of countries updates in real-time.
-
Country Cards: Each country is displayed as a card with the flag and key information.
-
Responsive Design: The layout adjusts for different screen sizes, making it mobile-friendly.
-
API Data: The country data is fetched from the REST Countries API, ensuring up-to-date information.
Notes:
-
CORS Policy: Since we're fetching data from an external API, you might encounter CORS (Cross-Origin Resource Sharing) issues if you open the index.html file directly in the browser. To avoid this, you can:
- Use a Local Web Server: Run a local web server in the project directory. Here are some easy options:
- Python 3: Run
python -m http.server and navigate to http://localhost:8000.
- Node.js (http-server): Install via
npm install -g http-server and run http-server.
- VSCode Live Server Extension: Install the Live Server extension in Visual Studio Code and click "Go Live".
-
Error Handling: The script includes basic error handling to alert if the data cannot be fetched.
I hope you enjoy this interactive and modern countries page! Let me know if you need any further assistance or customizations.