How can I display NASA APOD images and videos in the same spot?

0
0
Asked By MellowPine42 On

I'm building an HTML page that displays NASA's Astronomy Picture of the Day. Most days the API returns an image, but occasionally it returns a video such as an MP4 or an embedded video page. What's the best way to detect the media type and render the correct element while keeping the media in the same layout container?

3 Answers

Answered By CloudyRook7 On

Use the API response’s media_type field instead of trying to guess from the URL. If it’s "image", render an img element; if it’s "video", render a video element or an iframe depending on the returned URL. Put either element inside the same wrapper div and style both consistently.

Answered By VividHarbor19 On

For direct MP4 files, use a video element with controls. If the video URL points to an external video page, use an iframe instead. You can also use the API’s thumbnail_url when you’d rather show a still image on video days.

Answered By NorthPixel53 On

Give the wrapper a consistent size and apply shared styles such as max-width, max-height, and object-fit to the image and video. That way the page doesn’t shift dramatically when the daily content changes from an image to a video.

MellowPine42 -

That makes sense. I’ll branch on media_type first and keep both elements inside one styled container.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.