About this snippet

Create a perfectly responsive video gallery using the CSS `aspect-ratio` property. This ensures your video thumbnails always maintain their 16:9 shape, preventing layout shifts during loading.

Size
1.9 KB
Uses
HTML, CSS
Loads
placehold.co
Views
553
<div class="video-grid">
  <div class="video-card">
    <div class="thumbnail">
      <img src="https://placehold.co/600x338/1e293b/ffffff?text=Video+1" alt="Thumb">
      <div class="play-icon">▶</div>
    </div>
    <div class="meta">
      <h3>Intro to CSS</h3>
      <span>10:24</span>
    </div>
  </div>
  <div class="video-card">
    <div class="thumbnail">
      <img src="https://placehold.co/600x338/334155/ffffff?text=Video+2" alt="Thumb">
      <div class="play-icon">▶</div>
    </div>
    <div class="meta">
      <h3>Advanced Grids</h3>
      <span>15:02</span>
    </div>
  </div>
</div>
* { box-sizing: border-box; }
body {
  font-family: system-ui, "Segoe UI", sans-serif;
  background: #0f172a;
  padding: 20px;
  display: flex;
  justify-content: center;
}

.video-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 20px;
  width: 100%;
  max-width: 900px;
}

.video-card {
  background: #1e293b;
  border-radius: 12px;
  overflow: hidden;
  transition: transform 0.2s;
}

.video-card:hover { transform: translateY(-4px); }

.thumbnail {
  width: 100%;
  /* The Magic: Force 16/9 ratio */
  aspect-ratio: 16 / 9;
  position: relative;
  background: #000;
}

.thumbnail img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  opacity: 0.8;
  transition: opacity 0.2s;
}

.video-card:hover img { opacity: 1; }

.play-icon {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 40px;
  height: 40px;
  background: rgba(0,0,0,0.6);
  color: white;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 14px;
  backdrop-filter: blur(4px);
}

.meta {
  padding: 12px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: white;
}

h3 { margin: 0; font-size: 1rem; font-weight: 500; }
span { font-size: 0.85rem; color: #94a3b8; }
Esc
↑↓ moveEnter select

Start from a template

Open an .html file, or HTML, CSS, JS, Markdown, Less or TypeScript files. You can also drop them here. Styles and scripts go into their panes.

Share

Source

Keyboard shortcuts

Save