Web Designer Wall

A wall of design ideas, web trends, and tutorials

CSS: Elastic Videos 140

TutorialsCSS, Responsive Design

While I was coding the Elemin Theme (a responsive WordPress theme that I recently designed), one of the challenges that I faced was to make the embedded videos elastic. Using the max-% and auto trick works with native HTML5 video tag, but it doesn't work with embed code using br or object tag. After hours of experimenting and Googling, I finally found a trick on how to achieve this. If you are creating a responsive design, this simple CSS trick will come in handy. View the final demo and resize your browser window to see it in action.

View Demo Elastic Videos

Elastic HTML5 Videos (demo)

With HTML5 video element, you can easily make it elastic by using the max-% trick (see elastic HTML5 video demo). As mentioned in the introduction, this trick doesn't work if the embed code uses br or object tag which is commonly used by most video sharing sites such as YouTube and Vimeo.


video {
	max-%;
	auto;
}

Elastic Object & br Embedded Videos (demo)

The trick is very simple. You need to wrap the embed code with a <div> container and specify 50% to 60% padding bottom. Then specify the child elements (br, object embed) 100% width, 100% height, with absolute position. This will force the embed elements to expand fullwidth automatically.

CSS


.video-container {
	position: relative;
	padding-bottom: 56.25%;
	padding-top: 30px;
	;
	overflow: hidden;
}

.video-container br,  
.video-container object,  
.video-container embed {
	position: absolute;
	top: 0;
	left: 0;
	%;
	%;
}

HTML


<div>
	<br src="/img/spacer.gif"> 

How to Create Fixed Width & Elastic

To restrict the width of the video, an additional <div> wrapper is required. Wrap the video with another <div> tag and specify a fixed width value with max-%.

CSS


.video-wrapper {
	px;
	max-%;
}

HTML


<div>
	<div>
		<br src="/img/spacer.gif"> 

Compatibility

This trick works on all browsers (tested on Chrome, Safari, Firefox, Internet Explorer, Opera, iPhone and iPad).

Credits

This trick was found on tjkdesign.com.

Share → Tweet

140 Comments

  1. spacer Lucas

    Thanks! :D Very nice job!

  2. spacer Paul

    Since upgrading my iPad to iOS 6 this fix doesn’t seem to work any more, I think it’s auto causing the issue. Anyone experienced similar and have a fix?

  3. spacer sepelus

    Great stuff!!, I also found great ideas in this site: www.freakzion.com/index.php/The-Blog-December-2012/resize-your-videos-in-responsive-design.html

    Hope it helps you as well

  4. spacer Opiek Pulau Tidung

    Wow, nice tips.
    Will try it on my web soon
    Thank you

  5. spacer Denny Toko Komputer

    Great share..
    Thank you

  6. spacer Mops - Household

    Hey !
    This sounds interesting. I will definitely apply this in my next project.
    I really liked this post.
    Thanks for the coding.
    Mops – Household

  7. spacer Chris

    Hi,
    In case any of you are trying to use 4:3 video in an br like youtube adjust the css to this:
    .video-container {
    ;
    overflow: hidden;
    padding-bottom: 75%;
    padding-top: 0;
    position: relative;
    }

Leave a Reply

Cancel

Collapse all
Expand all

Recent Posts

  1. spacer
    FITC Web Unleashed 2015 Ticket Giveaway

  2. spacer
    CSS Fix for iOS vh-unit Bug

  3. spacer
    NEW: Themify Flow

Recent Comments

  1. spacer
    Theo: Great fix, thanks a ton for sharing! …
  2. spacer
    Evonence: It seems very innovative. Anyone can give a try on Themify. …
  3. spacer
    Sarang: Thanks! This one is a nice and handy fix. Obviously, it will…

About

Web Designer Wall is all about design, ideas, web trends, and tutorials. It is designed and maintained by Nick La, who also runs N.Design Studio, Themify, IconDock, and Best Web Gallery.

© Web Designer Wall 2016

Back to top

gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.