What is the best way to center align a YouTube embedded video using bootstrap?

I'm having trouble centering a YouTube embedded video on my Bootstrap page. The video size is always the same.

My html code is simple:

<div class="video">
    <iframe width="560" height="315" src="https://www.youtube.com/embed/ig3qHRVZRvM" frameborder="0" allowfullscreen=""></iframe>
</div>

I've tried various solutions from stackoverflow, but they only address centering a div, not a video. The best result I could achieve was with this fiddle.

I attempted solution1, solution2, and solution3 without success. Another somewhat successful solution was using:

width: 50%;
margin: 0 auto; 

While this worked on desktop, it failed on iPad or phone as the video extended beyond the screen. How can I properly center the video on desktop, tablet, and phone?

Answer №1

One important thing to remember is that "Bootstrap" consists of a collection of CSS rules

Check out this code snippet on JSFiddle

HTML

<div class="your-centered-div">
    <img src="http://placehold.it/1120x630&text=Pretend Video 560x315" alt="" />
</div>


CSS

/* Important styles */
.your-centered-div {
    width: 560px; /* Size is necessary for this method to work properly */
    height: 315px; /* Consider using max-width instead for better responsiveness */

    position: absolute; /* Positioned relative to the nearest parent element */
    top: 0; right: 0; /* Play with these values to adjust positioning */
    bottom: 0; left: 0;
    margin: auto; /* Center align horizontally and vertically */
}

View the complete example on JSFiddle here.

EDIT

This is my preferred method nowadays:

Pure CSS approach

.centered-thing {
    position: absolute;
    margin: auto;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

If you use stylus/mixins (which I highly recommend)

center-center()
    absolute()
    margin auto
    top 50%
    left 50%
    transform translate(-50%,-50%)

This technique eliminates the need to specify the element's size, as the translate function is based on its own dimensions - Neat trick!

Answer №2

When embedding a Youtube video, iframe is commonly used. To center the iframe, you can use the following CSS:

iframe {
   display: block;
   margin: 0 auto;
}

Answer №3

<iframe style="display: flex; margin: auto;" width="760" height="415" src="https://www.youtube.com/embed/ig3qHRVZRvM" frameborder="0" allowfullscreen></iframe>

Answer №4

No need to nest the <iframe> within a parent div at all. You can easily style just the YouTube iframes using CSS/3:

iframe[src*="//youtube.com/"], iframe[src*="//www.youtube.com/"] {
   display: block;
   margin: 0 auto;
}

Answer №5

To ensure that my video always fits the screen width, I set the max width to 100%. This way, on mobile devices, the video automatically adjusts to fit the screen width. Since the video itself is only 560px wide, I added a 10% left-margin to the iframe in the desktop CSS. As for the mobile CSS, I removed the margin to allow for full-width viewing without the need for additional div elements around each video.

Desktop CSS:

iframe {
margin-left: 10%;
}

Mobile CSS:

iframe {
margin-left: 0;
}

This method worked perfectly for implementing videos on my blog (Botanical Amy).

Answer №6

To center content using Bootstrap's .center-block class, which adjusts left and right margin to auto:

<iframe class="center-block" width="560" height="315" src="https://www.youtube.com/embed/ig3qHRVZRvM" frameborder="0" allowfullscreen=""></iframe>

Alternatively, you can use the .text-center class to set text-align: center:

<div class="text-center">
  <iframe width="560" height="315" src="https://www.youtube.com/embed/ig3qHRVZRvM" frameborder="0" allowfullscreen=""></iframe>
</div>

Answer №7

<section>
    <iframe id="myFrame" src="https://player.vimeo.com/video/12345678?autoplay=1" width="640" height="360" frameborder="0" allowfullscreen> . 
    </iframe>
</section>

<script>
    function adjustFrame() {
        var screenWidth = window.innerWidth;
        var screenHeight = window.innerHeight;

        document.getElementById("myFrame").width = screenWidth ;
        document.getElementById("myFrame").height = screenHeight ;
    }

    adjustFrame();

    window.addEventListener('resize', function(){ adjustFrame(); });
</script>

this script dynamically resizes the Vimeo video by adding an id myFrame to the iframe

Answer №8

Create an iframe with the attribute "align=middle" and insert it into a paragraph with the style "text-align:center":

<p style="text-align:center;">
<iframe width="420" height="315" align="middle" src="https://www.youtube.com/embed/YOURVIDEO">
</iframe>
</p>

Answer №9

For an IFramed YouTube video that is fully responsive, consider using the following code:

<div class="video-container">
<iframe width="854" height="480" style="margin: auto;" src="https://www.youtube-nocookie.com/embed/h5ag-3nnenc" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>

.video-container {    
    overflow:hidden;
    padding-bottom:56.25%;
    position:relative;
    height:0;    
}

.video-container iframe {   
    left:10%; //centers for the 80% width - not necessary if width is 100%
    top:0;
    height:80%; //adjust to 100% for full width
    width:80%;
    position:absolute;
}

Answer №10

<center><div class="video">
<iframe width="560" height="315" src="https://www.youtube.com/embed/ig3qHRVZRvM" frameborder="0" allowfullscreen=""></iframe>
</div></center>

It appears to be functioning properly, is this all that you needed? It could be possible to explore more complex solutions, but for now, this seems like a straightforward option.

Answer №11

To center a video on your website, you can use the tag. Although the tag is not supported by HTML5 and may show an error in some code editors like VS Code, it still works fine with YouTube videos. Simply open the tag before the tag.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to center align tabs in a Bootstrap 4 list?

I am working with tabs structured like this: <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/> <ul class="nav nav-pills mb-3" id="pills-tab" role="tablist"> <li class="nav-item"> ...

Which is more optimal for importing CSS - using the link tag or style tag?

Is this a matter of personal preference or is there a specific advantage to using one method over the other? <link href="main.css" rel="stylesheet" type="text/css"> versus <style type="text/css> @import url('main.css'); </style& ...

Include a flexible div in the hero section and change the position when resizing the screen

Looking to achieve a specific layout with only two resolutions in mind (767px minimum and maximum). For screens larger than 767px, display two divs side by side: one with text on the left and the other with an image on the right. The text remains in a fi ...

Content within a div that is floated left

The scenario at hand is: HTML Structure: <div id="main"> <div id="a"></div> <div id="b">Some Text</div> </div> CSS Styles: #a{ float:left; width:800px; height:150px; background-color:#CCC; } ...

Ways to solve the issue of the mobile dropdown menu in Bootstrap

Check Out the Expected Look Here (Image link) See How it Appears on Mobile Devices The dropdown menu causes an increase in the size of the navbar on the mobile version. Below is the code snippet: <div style=" background-image: url(https://i.imgu ...

What is the process for incorporating a side group input with Bootstrap?

description of image here I am looking to add 4 small input boxes next to the third input box. Please refer to the image I have attached for reference. In the image, I have included 4 boxes. I am specifically looking for a similar layout. ...

"Enhance Your Design With CSS3 Styling for Labels and

HTML <div class="select"> <div> <label for="kitty" class="kitty-label kitty-label-1 l-center"> </label> <input type="checkbox" name="cats" value="1"> <label>Kitty One</label> ...

The tooltip malfunctions when I incorporate the PHP variable

When I use PHP variables to return an HTML span tag, I am encountering an issue. The span tag contains a title field where I bind my PHP data. However, it seems to only accept the first sentence of my PHP value, causing it to not work properly. Below is th ...

Issues with the background-image scroll feature not functioning as intended

Can anyone help me out with a CSS issue I'm having? I've set an image as my background on a webpage and I want it to scroll with the page. Even though I followed some online tutorials, it's still not working for me. It's been a while si ...

Utilize a CSS selector to target all deleted nodes using the MutationObserver in the HTML5 API

Is there a way to retrieve all removed nodes from the DOM that have specific classes using CSS selectors? Below is an example of how it can be done with some complexity, but is there a simpler and more general approach? new MutationObserver(mut =& ...

Encountered issues while attempting to submit a form using Twitter Bootstrap, PHP, MySQL, and

I am attempting to submit data using Bootstrap modal, but I am encountering issues. The page will have multiple modals to allow users to easily update their details. Below is the code snippet. <div class="modal fade" id="overview-modal" role="dialo ...

Issue with drop-down menu functionality on mobile-friendly website

After deciding to revamp my website for responsiveness, I encountered an issue with the menu not displaying on smaller screens. Everything else is functioning correctly except for the drop-down menu! Here is the HTML code: <!doctype html> <html ...

Is it possible to change the CSS of a parent element using a child element?

My current challenge involves altering the background-color of a parent <td> element, without being able to directly modify the HTML structure. The software system utilized at my workplace strictly relies on SQL queries for data manipulation and gene ...

The banner <div> appears in Chrome but is not visible in IE9

I'm facing an issue with a banner div under my navigation. It seems to show up in Google Chrome but doesn't appear in IE9. Below is the HTML and CSS code: HTML: <div class="content"> <div class="slide-banner"></div> ...

Tips for handling CSS loading delays with icons in OpenLayers markers

When using openlayers (v4.6.4) with font-awesome as marker icons, the icons do not display upon first load (even after clearing cache and hard reload). Instead, I see a rectangle resembling a broken character. It is only on the second load that they appear ...

Troubleshooting a problem with a personalized webkit scrollbar in Chrome

Using the CSS property scroll-snap-type: y mandatory; to customize my scrollbar with the following styles: ::-webkit-scrollbar { width: 12px; background: transparent; } ::-webkit-scrollbar-track { background-color: rgba(0, 0, 0, 0.5); -webkit-box-s ...

What could be causing the issue preventing me from applying a border in the code below?

There seems to be an issue in the code below where I am struggling to apply a border Can anyone help identify what the problem might be? <!doctype html> <html> <head> <title> My first Navigation Page</title& ...

Concealing the WordPress sidebar specifically for mobile phones, excluding iPads, tablets, and other devices

I currently have Google AdSense displayed in the sidebar of my website, but it appears distorted when viewed on a mobile phone. My objective is to eliminate the sidebar when the site is accessed via a mobile device, while maintaining it for all other devic ...

Issue with the height of content in Mobile Safari when using Bootstrap 4 columns

I have incorporated Bootstrap 4 into my website's design, specifically utilizing the bootstrap grid layout in flex mode. Within one of the columns, I have placed a button that I desire to fill the entire column width. I have achieved this by setting ...

Evolving fashion trends

I'm looking to dynamically change the style of my HTML element based on screen size, similar to this example: <p [ngStyle]="{'color': isMobile() ? 'red' : 'blue'}">Lorem Ipsum</p> The code above triggers a m ...