Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
CSS

CSS: Fix aspect-ratio Ignored on a Replaced Video Element

- 01.12.26 - ErcanOPAK

🎨 Fix aspect-ratio Ignored on a Replaced Video Element

The CSS aspect-ratio property only controls an element’s box sizing when at least one of width or height is left to resolve automatically, and a <video> element carries its own intrinsic aspect ratio from the media file itself — when both the element’s CSS width and height are set explicitly (common when a layout grid forces both dimensions), that explicit sizing wins outright, and the aspect-ratio declaration has nothing left to influence at all.

🔎 The Problem

video.player {
  width: 100%;
  height: 400px;       /* explicit, fixed height */
  aspect-ratio: 16 / 9; /* has no effect - both dimensions already set */
}
/* Video stretches or letterboxes inside the fixed 400px height
   instead of actually maintaining 16:9 */

✅ Fix: Leave One Dimension for aspect-ratio to Actually Control

  • Set only the width explicitly (width: 100%) and let height: auto combine with aspect-ratio to compute the height, since the property needs exactly one free dimension to have any effect.
  • If a fixed-height layout slot is unavoidable, wrap the video in a container sized to the aspect ratio instead, and have the video itself use width: 100%; height: 100%; object-fit: cover inside that container.
  • Check computed styles in DevTools when aspect-ratio ‘isn’t working’ — if both width and height show as explicit pixel values rather than one showing auto, that confirms this is the cause rather than a browser support issue.

Aspect-ratio doesn’t override an explicit size — it only steps in for the dimension you left for it to fill.

— UI Engineer

Related posts:

CSS: Motion Design with Animated Box Shadows

CSS: Motion Design with Animated Box Shadows

CSS: Fix a Grid Layout Where Columns Ignore the Widths You Set

CSS: Fix a Grid Layout Where Columns Ignore the Widths You Set

CSS: Fix Printed Pages Losing Every Background Color Because Browsers Disable Background Graphics in...

CSS: Fix Printed Pages Losing Every Background Color Because Browsers Disable Background Graphics in...

Views: 8
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

Visual Studio: Fix the Call Stack Window Losing Local Variables
Photoshop: Fix Layer Comps Resetting an Adjustment Layer’s Opacity

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Read

  1. 01How to make theater mode the default for Youtube 1,080 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 999 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 998 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 874 views
  5. 05How to select distinct rows in a datatable in C# 869 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 673 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 632 views
  8. 08Average of all values in a column that are not zero in SQL 586 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 563 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com