CSS Video Embedding
Learn CSS techniques for embedding and styling videos with responsive sizing, custom controls, and overlays.
CSS provides powerful tools for embedding and styling videos to create professional, responsive video players.
Responsive Video
.video-container {
position: relative;
padding-bottom: 56.25%; /* 16:9 aspect ratio */
height: 0;
overflow: hidden;
}
.video-container iframe,
.video-container video {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}Basic Video Styling
video {
width: 100%;
height: auto;
display: block;
border-radius: 8px;
}Custom Video Container
.video-wrapper {
max-width: 800px;
margin: 0 auto;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
border-radius: 12px;
overflow: hidden;
}Aspect Ratios
/* 16:9 (widescreen) */
.ratio-16-9 {
aspect-ratio: 16 / 9;
}
/* 4:3 (standard) */
.ratio-4-3 {
aspect-ratio: 4 / 3;
}
/* 1:1 (square) */
.ratio-1-1 {
aspect-ratio: 1 / 1;
}
/* 21:9 (ultra-wide) */
.ratio-21-9 {
aspect-ratio: 21 / 9;
}Video with Overlay
.video-overlay-container {
position: relative;
}
.video-overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
align-items: center;
justify-content: center;
opacity: 0;
transition: opacity 0.3s ease;
}
.video-overlay-container:hover .video-overlay {
opacity: 1;
}Play Button Overlay
.play-button {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 80px;
height: 80px;
background-color: rgba(0, 123, 255, 0.9);
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
transition: all 0.3s ease;
}
.play-button:hover {
transform: translate(-50%, -50%) scale(1.1);
background-color: rgba(0, 123, 255, 1);
}
.play-button::after {
content: '';
width: 0;
height: 0;
border-left: 20px solid white;
border-top: 12px solid transparent;
border-bottom: 12px solid transparent;
margin-left: 5px;
}Background Video
.hero-video {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
object-fit: cover;
z-index: -1;
}
.hero-content {
position: relative;
z-index: 1;
color: white;
text-align: center;
padding: 100px 20px;
}YouTube/Vimeo Responsive
.embed-container {
position: relative;
padding-bottom: 56.25%;
height: 0;
overflow: hidden;
max-width: 100%;
}
.embed-container iframe,
.embed-container object,
.embed-container embed {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border: 0;
}Video Grid
.video-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
gap: 20px;
}
.video-grid-item {
position: relative;
padding-bottom: 56.25%;
background-color: #000;
border-radius: 8px;
overflow: hidden;
}
.video-grid-item video {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
object-fit: cover;
}Thumbnail with Play Icon
.video-thumbnail {
position: relative;
cursor: pointer;
}
.video-thumbnail img {
width: 100%;
display: block;
border-radius: 8px;
}
.video-thumbnail::after {
content: '▶';
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 60px;
color: white;
text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
}Loading State
.video-loading {
position: relative;
background-color: #000;
}
.video-loading::before {
content: '';
position: absolute;
top: 50%;
left: 50%;
width: 40px;
height: 40px;
margin: -20px 0 0 -20px;
border: 4px solid rgba(255, 255, 255, 0.3);
border-top-color: white;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}Video Caption
.video-with-caption {
max-width: 800px;
margin: 0 auto;
}
.video-with-caption video {
width: 100%;
display: block;
border-radius: 8px 8px 0 0;
}
.video-caption {
background-color: #f8f9fa;
padding: 15px;
text-align: center;
border-radius: 0 0 8px 8px;
}Best Practices
Video Embedding Best Practices
- Always make videos responsive
- Use aspect-ratio for modern browsers
- Provide fallback for older browsers
- Include poster image for native videos
- Consider autoplay policies
- Provide captions/subtitles
- Optimize video file sizes
- Use lazy loading for below-fold videos
- Test on mobile devices
- Ensure controls are accessible
Accessibility
<video controls>
<source src="video.mp4" type="video/mp4">
<track kind="captions" src="captions.vtt" srclang="en" label="English">
Your browser doesn't support video.
</video>Performance
- Use lazy loading:
<video loading="lazy"> - Optimize video encoding
- Provide multiple formats
- Use poster images
- Consider adaptive bitrate streaming
Master video embedding to create professional, responsive video experiences!
How is this guide?
Sign in to share your feedback
Help us improve by sharing your thoughts on this guide.
Last updated on
CSS Navigation Bar
Learn to create beautiful, responsive navigation bars with CSS including dropdowns, mobile menus, and modern patterns.
CSS Display Property
Master the CSS display property to control how elements are rendered, including block, inline, inline-block, flex, grid, and none.
© 2026CoderrShyamAll Rights Reserved.