How To Make Youtube Embeded Videos Responsive

So you have hit the issue where you have a responsive website and everything looks great, but then you notice that embeded videos do not correctly fit within your responsive design. Fear not this can be quickly fixed by adding a div around all of your embeded videos and a little bit of css to go along with it. If you are using something like TinyMCE to do this, then it might seem like a lot of work to have to manually add a div to all of the embed videos that you add. Well fear not it’s possible to do this automatically!

Start by going to the plugins folder of your TinyMCE install directory. I am using TinyMCE 4 so the plugin file contents may differ if you are using an older version. Either way the concept will remain the same. Search through the file and look for he following string. “Paste your embed code below”. This may differ for each version, the best way to test is to use TinyMCE and see what message pops up when you are adding the embed code. Using minified code makes this quite hard to find, but it’s possible.

You will see an onclick method just past this. This is the function that you need to modify. Inside of this function you will see the following code

a.insertContent(g(this.toJSON()))

Replace this line of code with the following.

a.insertContent("<div class='videoiframe'>"+g(this.toJSON())+"</div>")

If you save the file and test this you will see that the iframe is now being added with the div wrapped around of it. (you might need to have the tools that allow you to view the source code to check this). From now on any embeded iframe from youtube or any other video website, will have a div wrapped around it. This allows you to add some custom css that will make the video responsive.

Open up your css file and add the following code.

.videoiframe {
float: none;
clear: both;
width: 100%;
position: relative;
padding-bottom: 56.25%;
padding-top: 25px;
height: 0;
}
.videoiframe iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}

Save this and check your website again, any videos that have the “videoiframe” div wrapped around it will be responsive. This of course wont effect existing videos that were added before you made this change to TinyMCE, but at least now you can go back and edit the posts to add the div to all of the embeded videos and they will now be responsive without having to do any work modifying the source code.

Related Articles

Related Questions

Which component should I upgrade first for my PC?

Hey everyone! I'm trying to figure out which part of my PC I should upgrade first and could use your advice. Here's what I'm...

Will my motherboard support an upgrade to a Ryzen 9600X and GeForce 5070?

I'm looking to upgrade my setup with a GeForce 5070 GPU and a Ryzen 9600X CPU. My current motherboard is an ASUS ROG STRIX...

Frustrated with Frame Drops on My New CPU

I just got back my PC from Micro Center after upgrading to a new motherboard, but it seems like all my BIOS settings got...

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.

Latest Tools

OpenAI Token Calculator

This tool is a simple OpenAI token calculator, web-based utility designed to help you quickly estimate the number of tokens in your text when...

List Sorting Tool

Welcome to our innovative list ordering and management tool. This next-level platform enables you to sort a list of items in ascending or descending...

Sudoku Solver

Welcome to our free online Sudoku solving tool, an interactive platform for puzzle enthusiasts seeking a break from a Sudoku conundrum. This advanced platform...

Apply Image Filters To Image

Digital imagery in the modern world is all about reinforcing emotions and stories behind each photo we take. To amplify this storytelling, we are...

Add Watermark To Image

As the world is increasingly consumed by digital media, protecting your original images is paramount. We are thrilled to introduce you to our innovative...

CSV To Xml Converter

Welcome to our CSV to XML converter tool, a convenient and user-friendly solution for all your data conversion needs. This versatile tool on our...

Latest Posts

Latest Questions