Updated July 13th, 2019
Did you know that having video on your site can help improve SEO? The logic is that videos are the most engaging content, and when people watch video on your site, that increases their “dwell time” (or time spent on page). Great, so let’s embed some videos! By default your video likely won’t fit on the page perfectly for all screen sizes (unless your theme has it built in). To illustrate, I clicked “Share” then “Embed” on a YouTube video, and here’s the code I got:<iframe width="560" height="315" src="https://www.youtube.com/embed/yvEzAx1Ij6M" frameborder="0" allowfullscreen></iframe>And the video with that code pasted here:
1. If you’re on a phone, it’s probably cut-off π€¨
2. If you’re on computer, you can see the whole video, but it’s a little small eh?The Solution
We can add some code to make the video grow/shrink with the screen size and maintain it’s original ratio, like this:BLAM! Drag your screen to resize and watch the video automatically form to the container size.
The Code Snippets
HTML
<div class="responsive-video"> <iframe width="560" height="315" src="YOUTUBE VIDEO LINK" frameborder="0" allowfullscreen></iframe> </div>
CSS
/* -- Responsive video embed -- */ .responsive-video { padding-bottom: 56.25%; /* for 16:9 ratio */ padding-top: 25px; position: relative; height: 0; } .responsive-video iframe, .responsive-video object, .responsive-video embed { position: absolute; top: 0; left: 0; width: 100%; height: 100%; }
Where to put the code
Let’s start with the CSS. Copy/paste the above CSS code to yourcustom.scss.liquid
file (or whatever you use to add custom CSS).
If you don’t have a custom file, I recommend creating one to keep separate from your theme default CSS. But for a quick workaround, you can just paste the CSS code snippet at the end of your styles.scss.liquid file (or whatever your theme’s stylesheet is called).
And the HTML: Now whenever you embed a Youtube video just copy/paste the HTML snippet above and place line 2 with your embed code. Easy.
That code will make the video grow responsively to fit the container it’s placed in. If you want to limit the size the maximum size of the video, you can add an outer wrapper like this:
<div style="max-width: 600px;"> <div class="responsive-video"> <iframe width="560" height="315" src="YOUTUBE VIDEO LINK" frameborder="0" allowfullscreen></iframe> </div> </div>
How to responsively embed Vimeo videos
Unfortunately it doesn’t work for these… nah just kidding π Here’s a Vimeo video with the same technique applied:Lazy Loading Video
If you have lazy loading enabled with the “lazysizes” library (this is what’s used on the videos on this page), you can lazy load your video by swapping indata-src
for src
, and adding class="lazyload"
to the iframe
element.
Now if your video is “under the fold”, it won’t contribute to initial page load, making your page faster and more Google friendly.
If you don’t know what lazy loading is, check out Lazy Loading for Shopify.
If you’d like us to set up lazy loading for images and videos on your site, checkout our Lazy Loading optimization service.
Hello Joe,
I do not have a lot of coding expertise. Most of which I have learned from forums and Youtube videos. I have a couple of pages where I would like to include a responsive embed. However, I need to have them centered on the page. Everything that I have tried does not seem to work. Any suggestions?
Hi, I was able to embed a youtube video on my Shopify product page but when my video is over it plays other videos. I want it to only play my youtube videos.
How can I fix this?
Thank you,
Josie
I don’t think that’s possible. If it is, I think it would be a configuration with the Youtube embed parameters.
hi there.
I am using a theme on shopify where in a section, you can add a youtube video (slideshow). But does not fit on phone screen. So i’m trying to do as you say but one question.
Where do I paste the HTML ? I’m using the Narative theme and can’t figure out where to put it since it’s in a section.
thanks for your help
If it’s in a section in Customize Theme, you might have to edit the HTML, but I would try just using CSS first to see if you can get it to work.
If it doesn’t work with pure CSS, head over to the Theme Editor and try to find the code that correlates to that section. The way I usually do it is start by inspecting the code in a live browser to find the ID or class of the section you want to edit. Once you find that identifier, then look for it in the theme editor (for example, if it’s on the home page, look in the templates section for Home or Index, etc).
This guide will help with that process: https://speedboostr.com/how-to-safely-edit-your-shopify-theme.
It might take some time, but if you can identify the element of your section (ID is best because it’s unique), then you can search through your theme to find that.
You can also download your theme file and search from your computer, that will be faster if you’re not used to how Shopify theme files are organized.
Hi Joe,
I am running into a similar problem as another commenter. I used your code and my mobile video is now perfect sizing but the desktop video sizing is too small. I see that more than likely there is a class containing my desktop videos. Would you be able to help my bypass that for my videos page?
Hey cool you got it integrated. For the desktop, ya you’re probably right, there’s likely another class or some CSS forcing the size.
What we do in that case is inspect the element, read the CSS code applied, then adjust it in the browser until it looks good. Then apply the CSS styles to your stylesheet in your theme editor.
If you’re not familiar with that process, this guide will help: How to Safely Edit Your Shopify Theme.
Can you have a YouTube or Vimeo video autoplay?
Yes, add the autoplay=1 parameter to the embed url. Reference: developers.google.com/youtube/player_parameters.
Note: Some browsers / devices ignore autoplay. For example on mobile devices where it’s not good to force users to download heavy videos because of data usage.
I dont know what Im doing wrong help!
Hey Tanya, send us a message with your url and someone will take a look for you: speedboostr.com/contact
Hi Joe,
This worked fantastically. I found this article when I was looking to fit my Video video seamlessly on the screen.for some reason, I get a thin white line, maybe a pixel or 2 wide running along the botton of the video and down the right hand side. Do you know how to remove this thin white line as I have a dark website and it sticks out like a sore thumb. Thank you ever so much.
Ben
Hey Ben, I’m guessing you can adjust the padding on the responsive-video class. Give that a shot and if it doesn’t solve it drop a message on our contact form with the page url and we can take a look.
Hi Joe,
This solution works great for the mobile device. But now the video is much too large on the desktop. Can that be fixed?
thanks
doug
That’s a great question Doug. You can wrap the video in an outer container and set the maximum size to whatever you’d like. I updated the post to share an example – look for the code block under the yellow box.
Hi Joe,
Thanks for this, super helpful!
I have worked to this and i have someone ended up with a black boarder/frame at the top and bottom of my video which i have no clue how to get rid of!
any help would be great!
Hi Hanna, it sounds like maybe the aspect ratio of the video could be different than 16:9. If that’s the case, you can experiment with adjusting the padding-bottom in the responsive-video CSS class (set to 56.25% in my example).
Here’s how I would approach it:
a) If you know the aspect ratio, you can use math to get the percentage (in my example, the video is 16:9, so I divided 9 by 16 to get .5625).
b) If you don’t know the aspect ratio, you can open your page in Chrome, then right click the video and select Inspect. That will open up Chrome Dev Tools console. From there, look for your html code wrapping the video: div class=”responsive-video”. click that, then adjust the padding-bottom and you’ll see the video container adjust on the fly. Once you find the right number, you can go back and paste that number in your CSS file.
Hello Joe,
Great information I can not seem to get my video right next to my image but rather below or above, do you know what I may try to fix this?
Hi Gina, the approach I would take is wrapping the code for the video in a div element so you can then set the width (example with CSS: style=”width: 49%;”). Then add that same CSS to the image. Then you can “float” one of them to put them on the same line (example: style=”float: left; margin-right: 2%;”).
Adding that CSS would put the video and image on the same line, with each being 49% width of the parent container and a 2% space between them.
I like to use Chrome dev tools to inspect the elements, then use classes and ID’s to identify the elements needing changed, and add that to a custom.scss.css file. (but adding them inline like the example above will work as well)
That fixed it. You are a bloody legend!
π€π»
G’day Joe. This was really helpful. My YouTube video now looks really good on a mobile device but is really small on the desktop site. Any suggestions on how to fix this if you have the time? I am unfortunately using the Debut theme. Any help would be gratefully appreciated. Cheers mate
Hey Matt, it’s most likely a CSS rule that is limiting the size. I looked at your home page and see a small video, so assuming that’s it I inspected the CSS code and see the parent container of the video has a class called “medium-up–one-quarter”, and a CSS rule for that class to make this container 25% on screens 750px and larger.
To fix you can either remove that class, or if the class is needed, you could add an ID to override that CSS, and set it to 100% width (or whatever width you want).