I just want an HTML/CSS and JavaScript code with some examples of movies then I continue…
- User
I just want an HTML/CSS and JavaScript code with some examples of movies then I continue to add manually in the code
- Assistant
I just want an HTML/CSS and JavaScript code with some examples of movies then I continue to add manually in the code
Sure! Below is a simple example of an HTML page that displays a list of movies using HTML, CSS, and JavaScript. The page features a movie grid with posters, titles, and release years. You can easily add more movies by modifying the JavaScript array.
Create a single HTML file (e.g., index.html) and paste the following code into it:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Movie Collection</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
}
.container {
width: 90%;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
h1 {
text-align: center;
color: #333;
}
.movie-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
gap: 20px;
margin-top: 30px;
}
.movie-card {
background-color: #fff;
border-radius: 8px;
overflow: hidden;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
transition: transform 0.2s;
}
.movie-card:hover {
transform: scale(1.05);
}
.movie-poster {
width: 100%;
height: 270px;
object-fit: cover;
}
.movie-details {
padding: 15px;
text-align: center;
}
.movie-title {
font-size: 1.1em;
margin-bottom: 10px;
color: #555;
}
.movie-year {
font-size: 0.9em;
color: #999;
}
/* Responsive Design */
@media (max-width: 600px) {
.movie-poster {
height: 200px;
}
}
</style>
</head>
<body>
<div class="container">
<h1>My Movie Collection</h1>
<div id="movieGrid" class="movie-grid">
<!-- Movies will be injected here by JavaScript -->
</div>
</div>
<script>
// Array of movie objects
const movies = [
{
title: "The Shawshank Redemption",
year: 1994,
poster: "https://m.media-amazon.com/images/I/51NiGlapXlL._AC_.jpg"
},
{
title: "The Godfather",
year: 1972,
poster: "https://m.media-amazon.com/images/I/51rGGQlKhPL._AC_.jpg"
},
{
title: "Inception",
year: 2010,
poster: "https://m.media-amazon.com/images/I/51nbVEuw1HL._AC_.jpg"
},
{
title: "Pulp Fiction",
year: 1994,
poster: "https://m.media-amazon.com/images/I/71c05lTE03L._AC_SL1024_.jpg"
},
{
title: "The Matrix",
year: 1999,
poster: "https://m.media-amazon.com/images/I/51EG732BV3L.jpg"
}
];
// Function to display movies
function displayMovies(movieList) {
const movieGrid = document.getElementById('movieGrid');
movieGrid.innerHTML = ''; // Clear existing content
movieList.forEach(movie => {
// Create movie card elements
const card = document.createElement('div');
card.classList.add('movie-card');
const poster = document.createElement('img');
poster.classList.add('movie-poster');
poster.src = movie.poster;
poster.alt = `${movie.title} Poster`;
const details = document.createElement('div');
details.classList.add('movie-details');
const title = document.createElement('h3');
title.classList.add('movie-title');
title.textContent = movie.title;
const year = document.createElement('p');
year.classList.add('movie-year');
year.textContent = `Released: ${movie.year}`;
// Assemble the card
details.appendChild(title);
details.appendChild(year);
card.appendChild(poster);
card.appendChild(details);
movieGrid.appendChild(card);
});
}
// Initial display
displayMovies(movies);
// Example: Adding a new movie after 3 seconds (optional)
/*
setTimeout(() => {
const newMovie = {
title: "Interstellar",
year: 2014,
poster: "https://m.media-amazon.com/images/I/81k8kPjag2L._AC_SL1500_.jpg"
};
movies.push(newMovie);
displayMovies(movies);
}, 3000);
*/
</script>
</body>
</html>
HTML Structure: The body contains a container div which houses the page title and a div with the ID movieGrid. This movieGrid is where the movie cards will be dynamically injected using JavaScript.
CSS Styling: The styles define the layout and appearance of the page, including a responsive grid for the movie cards, hover effects, and basic typography. The grid adjusts based on screen size to ensure a good experience on both desktop and mobile devices.
JavaScript Functionality:
movies holds objects, each representing a movie with title, year, and poster (URL to the movie's poster image).movies array and generates HTML elements for each movie, inserting them into the movieGrid div.displayMovies(movies) is called to render the initial set of movies.movies array with the necessary properties.To add more movies to your collection, locate the movies array in the JavaScript section and add new movie objects. Here's how you can do it:
const movies = [
{
title: "The Shawshank Redemption",
year: 1994,
poster: "https://m.media-amazon.com/images/I/51NiGlapXlL._AC_.jpg"
},
// ... existing movies
{
title: "Interstellar",
year: 2014,
poster: "https://m.media-amazon.com/images/I/81k8kPjag2L._AC_SL1500_.jpg"
},
{
title: "Parasite",
year: 2019,
poster: "https://m.media-amazon.com/images/I/71cF5OuDkDL._AC_SL1187_.jpg"
}
];
Make sure to:
title of the movie as a string.year of release as a number.poster URL. You can find poster images from reliable sources or host them yourself.Here are a few ideas to enhance this basic setup:
localStorage so that the data persists across sessions.Feel free to customize and expand upon this foundation to suit your needs!