Videoheight

Master the videoHeight property for building responsive, dimension-aware video experiences on the web

What is videoHeight?

The videoHeight property is a read-only attribute of the HTMLVideoElement interface that returns the intrinsic height of a video resource in CSS pixels. In simple terms, this represents the natural height of the video media as it was originally encoded, before any CSS styling or HTML attribute modifications are applied.

When building modern web applications with video functionality, understanding how to access and work with video dimensions is essential for creating seamless user experiences.

Key Characteristics

  • Read-only: You cannot set videoHeight directly; it reflects the actual video dimensions
  • CSS pixels: The returned value is in CSS pixel units, not device pixels
  • Intrinsic value: Represents the natural size of the video file itself
  • Asynchronous availability: May return 0 until the video metadata is loaded

MDN Web Docs - HTMLVideoElement: videoHeight property

How videoHeight Differs from the height Attribute

It's crucial to distinguish between videoHeight and the HTML height attribute on the <video> element. The HTML height attribute specifies the display height of the video player in CSS pixels, which can be different from the video's intrinsic dimensions.

PropertyTypePurposeCan Be Set
videoHeightRead-onlyIntrinsic video heightNo
height attributeReflectedDisplay heightYes

This distinction allows developers to scale videos via CSS while preserving the original aspect ratio, enabling flexible responsive video layouts. For teams implementing custom video solutions, mastering this difference is fundamental to building professional-grade media experiences. W3Schools - HTML video height Attribute

Browser Support

The videoHeight property has been widely available across all major browsers since July 2015, making it a baseline web feature that can be used without polyfills in modern web development projects. MDN Web Docs - HTMLVideoElement

Practical Implementation

Accessing Video Dimensions

const video = document.getElementById('myVideo');

video.addEventListener('loadedmetadata', () => {
 console.log('Intrinsic dimensions:');
 console.log('Width:', video.videoWidth);
 console.log('Height:', video.videoHeight);
});

Dynamic Aspect Ratio Calculations

function getAspectRatio(video) {
 if (video.videoWidth && video.videoHeight) {
 return video.videoWidth / video.videoHeight;
 }
 return 16 / 9; // Default fallback
}

Responsive Video Sizing

const video = document.getElementById('myVideo');

video.addEventListener('resize', (ev) => {
 const w = video.videoWidth;
 const h = video.videoHeight;

 if (w && h) {
 // Match video dimensions exactly
 video.style.width = w + 'px';
 video.style.height = h + 'px';
 }
});

This approach is particularly useful for video editing interfaces or when precise pixel control is required. For complex video implementations, consider leveraging professional web development services to ensure optimal performance and cross-browser compatibility. MDN Web Docs - HTMLVideoElement: videoHeight property

Best Practices for Video Dimension Handling

Always Check for Valid Values

Since videoHeight may return 0 before metadata loads, always validate the value:

video.addEventListener('loadedmetadata', () => {
 if (video.videoHeight > 0 && video.videoWidth > 0) {
 // Safe to use dimensions
 initializeVideoPlayer(video);
 }
});

Use CSS Aspect-Ratio for Responsive Layouts

Modern CSS provides the aspect-ratio property for responsive video containers:

.video-container {
 aspect-ratio: 16 / 9;
 width: 100%;
}

Combine this with object-fit for proper video scaling within containers.

Defer Layout Calculations Until Metadata Loads

Perform dimension-dependent calculations in the loadedmetadata or resize event handlers rather than immediately after video creation.

Consider High-DPI Displays

Remember that videoHeight returns CSS pixels, not device pixels. For high-DPI displays, you may need to account for the device pixel ratio when calculating rendering dimensions.

Implementing these best practices ensures your video elements render correctly across all devices, from standard displays to high-resolution retina screens.

Common Use Cases

Video Lightbox Components

Calculate appropriate lightbox dimensions based on video aspect ratio:

function openVideoLightbox(video) {
 const aspectRatio = video.videoWidth / video.videoHeight;
 const maxWidth = window.innerWidth * 0.9;
 const maxHeight = window.innerHeight * 0.9;

 let lightboxWidth = maxWidth;
 let lightboxHeight = lightboxWidth / aspectRatio;

 if (lightboxHeight > maxHeight) {
 lightboxHeight = maxHeight;
 lightboxWidth = lightboxHeight * aspectRatio;
 }

 // Apply dimensions to lightbox
}

Adaptive Video Streaming

In adaptive streaming scenarios, videoHeight can be used to display quality selection controls:

function updateQualityDisplay(video, qualityLevels) {
 const currentQuality = qualityLevels.find(
 level => level.height === video.videoHeight
 );
 displayCurrentQuality(currentQuality.name);
}

Canvas Video Processing

When rendering video frames to canvas for processing or thumbnail generation:

function captureVideoFrame(video, canvas) {
 canvas.width = video.videoWidth;
 canvas.height = video.videoHeight;
 const ctx = canvas.getContext('2d');
 ctx.drawImage(video, 0, 0);
 return canvas.toDataURL('image/jpeg');
}

Frequently Asked Questions

Why does videoHeight return 0?

The videoHeight property returns 0 when the video's readyState is HAVE_NOTHING, meaning the metadata hasn't loaded yet. Access it within a 'loadedmetadata' or 'resize' event handler instead.

How is videoHeight different from the CSS height property?

videoHeight is read-only and reflects the intrinsic (natural) dimensions of the video file. CSS height controls the display size and can be set independently to scale the video.

Does videoHeight work on mobile browsers?

Yes, videoHeight is widely supported across all modern mobile browsers including Safari on iOS and Chrome on Android since July 2015.

Can I use videoHeight with video.js or other players?

Yes, but you'll need to access the underlying video element. In video.js, use player.el().querySelector('video') to get the native element, then access videoHeight.

Build Modern Video Experiences

Our team of web development experts can help you implement responsive video solutions that delight your users.