Creating a website that looks like YouTube in a single file is a complex task, as YouTube is a highly sophisticated platform with a lot of features. However, I can provide you with a simplified version of a YouTube-like layout using HTML, CSS, and JavaScript in a single file. This will include a basic header, sidebar, video grid, and a simple video player.
Here’s the code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>YouTube Clone</title>
<style>
/* General Reset */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
background-color: #f9f9f9;
color: #333;
}
/* Header */
header {
display: flex;
align-items: center;
justify-content: space-between;
background-color: #202020;
color: white;
padding: 10px 20px;
position: sticky;
top: 0;
z-index: 1000;
}
header .logo {
font-size: 24px;
font-weight: bold;
color: #ff0000;
}
header input[type="text"] {
width: 50%;
padding: 8px;
border: none;
border-radius: 2px;
}
header button {
padding: 8px 12px;
background-color: #ff0000;
color: white;
border: none;
border-radius: 2px;
cursor: pointer;
}
/* Sidebar */
.sidebar {
width: 200px;
background-color: #f1f1f1;
height: 100vh;
position: fixed;
top: 50px;
left: 0;
padding: 20px 10px;
overflow-y: auto;
}
.sidebar a {
display: block;
padding: 10px;
text-decoration: none;
color: #333;
margin-bottom: 10px;
border-radius: 4px;
}
.sidebar a:hover {
background-color: #ddd;
}
/* Main Content */
.main-content {
margin-left: 220px;
padding: 20px;
}
.video-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
gap: 20px;
}
.video-card {
background-color: white;
border-radius: 4px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
overflow: hidden;
cursor: pointer;
}
.video-card img {
width: 100%;
height: 180px;
object-fit: cover;
}
.video-card .info {
padding: 10px;
}
.video-card .info h4 {
font-size: 16px;
margin-bottom: 5px;
}
.video-card .info p {
font-size: 14px;
color: #666;
}
/* Video Player Modal */
.video-player {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.8);
justify-content: center;
align-items: center;
z-index: 2000;
}
.video-player video {
width: 80%;
max-height: 80%;
}
.video-player .close-btn {
position: absolute;
top: 20px;
right: 20px;
background-color: #ff0000;
color: white;
border: none;
padding: 10px;
cursor: pointer;
border-radius: 50%;
}
</style>
</head>
<body>
<!-- Header -->
<header>
<div class="logo">YouTube</div>
<input type="text" placeholder="Search">
<button>Search</button>
</header>
<!-- Sidebar -->
<div class="sidebar">
<a href="#">Home</a>
<a href="#">Trending</a>
<a href="#">Subscriptions</a>
<a href="#">Library</a>
<a href="#">History</a>
</div>
<!-- Main Content -->
<div class="main-content">
<div class="video-grid">
<!-- Video Card -->
<div class="video-card" onclick="playVideo('video1.mp4')">
<img src="https://via.placeholder.com/300x180" alt="Video Thumbnail">
<div class="info">
<h4>Sample Video 1</h4>
<p>Channel Name</p>
</div>
</div>
<div class="video-card" onclick="playVideo('video2.mp4')">
<img src="https://via.placeholder.com/300x180" alt="Video Thumbnail">
<div class="info">
<h4>Sample Video 2</h4>
<p>Channel Name</p>
</div>
</div>
<div class="video-card" onclick="playVideo('video3.mp4')">
<img src="https://via.placeholder.com/300x180" alt="Video Thumbnail">
<div class="info">
<h4>Sample Video 3</h4>
<p>Channel Name</p>
</div>
</div>
</div>
</div>
<!-- Video Player Modal -->
<div class="video-player" id="videoPlayer">
<button class="close-btn" onclick="closeVideo()">X</button>
<video id="videoElement" controls></video>
</div>
<script>
// JavaScript for Video Player
function playVideo(videoSrc) {
const videoPlayer = document.getElementById('videoPlayer');
const videoElement = document.getElementById('videoElement');
videoElement.src = videoSrc;
videoPlayer.style.display = 'flex';
}
function closeVideo() {
const videoPlayer = document.getElementById('videoPlayer');
const videoElement = document.getElementById('videoElement');
videoElement.pause();
videoPlayer.style.display = 'none';
}
</script>
</body>
</html>
Features:
- Header: Includes a logo, search bar, and search button.
- Sidebar: Contains navigation links.
- Main Content: Displays a grid of video cards.
- Video Player: A modal video player that opens when a video card is clicked.
Notes:
- Replace
video1.mp4, video2.mp4, etc., with actual video file paths or URLs.
- Replace placeholder images with actual video thumbnails.
- This is a basic layout and does not include advanced features like user accounts, comments, or dynamic content loading.
You can copy and paste this code into an .html file and open it in your browser to see the result.