Creating a responsive Youtube video that is perfectly centered on a web page

Currently, I am working on embedding a responsive Youtube video. I have managed to make it responsive using code that I found online. However, I am facing an issue where the height of the video does not adjust to 100% when the width is small. I want the height to always be 100%, even if it means having black bars at the top and bottom when the width is reduced.

I came across a website where this functionality has been implemented successfully. You can see an example on this page. There, the video remains centered when the width is decreased instead of sticking to the top.

Here is the HTML code I am using:

<div id='wrap-player'>
    <div class="video-container">
        <div id="player"></div>
    </div> 
</div>

And here is the CSS:

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

.video-container iframe,
.video-container object,
.video-container embed {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    right: 0;
    left: 0;
}

#wrap-player {
    width: 100%;
    height: 100%;
    /*position: absolute;*/
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
}

I have also created a Fiddle for reference.

Answer №1

There's no need for excessive engineering here, just follow these simple steps:

COMPLETE CODE for FULLSCREEN

DEMO CLICK HERE

CSS

html,body{ margin:0; padding:0; height:100%; width:100%; }
#wrap-player{
    height:100%;
    width:100%;
    overflow:hidden;
}

HTML

<div id="wrap-player">
     <iframe width="100%" height="100%" src="//www.youtube.com/embed/d__QncYv3VU" frameborder="0" allowfullscreen></iframe>
</div>

Explanation

  • margin/padding: 0

Known as a global reset, this sets default values for margin and padding to remove browser-specific styles.

  • html: 100%

Setting the HTML element's height to 100% extends it to match the browser's visible area.

  • body: 100%

The body follows the same sizing rules as HTML, filling its container's height when set to 100%.

  • site div: 100%

Creating a master wrapping div allows for better control over layout without affecting body directly. Setting its height to 100% fills its parent's height - in this case, the body.

This cascading effect ensures block-level elements fill their containers' heights by default unless specified otherwise.

An element with height 100% within another div will only reach the height of its container, not the entire screen.

(Note: Fixed/absolute positioning removes elements from the document flow, affecting their height calculation based on nearest positioned parent element.)

  • overflow: hidden

Using overflow: hidden hides scrollbars even when content doesn't overflow, keeping design clean.

  • iframe: 100%

YouTube embeds videos using iframes, so setting iframe height to 100% adjusts video height to fit container.

It's best to set iframe height directly rather than through CSS for specificity reasons and to avoid conflicts with multiple iframes on a page.

(Further Note: Styling considerations may vary, but maintaining all parent containers at 100% up to HTML results in full window-height videos.)

Answer №2

I made some adjustments to the css in your configuration. I ensured that the video and all of its parent elements have a height of 100%, guaranteeing full height at all times. Check out the modified setup on jsFiddle

html, body, iframe, #wrap-player, .video-container, #player{
    width:100%;
    height:100%;
    margin:0px;
    overflow:hidden;
}

Answer №3

If you want to see a demo, click on this link http://jsfiddle.net/zLc2s/1/

It is important to calculate the height of the document and adjust the player's height accordingly.

$(document).ready(function () {
    $('.video-container').css('height', $(window).height());
});

Answer №4

Employing vh units makes it incredibly simple:

iframe {
    width: 100%;
    height: 100vh;
    display: block;
}

You can view the updated demo here.

(Sometimes, the most straightforward solution is the most effective)

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

Are there any tools available for selecting fonts to use in an input form for users?

I am currently seeking a font picker tool that can be integrated into my web project to allow users to select from a variety of fonts. I have implemented a basic code using radio buttons, which seems to be functioning as intended: <label style="font-fa ...

Connecting images - XHTML/CSS

Is it better to use XHTML or CSS for linking a small image? ...

Troubleshooting problem with displaying circular tag image below an image using Jquery

I'm looking to enhance my HTML DOM by adding circular tag images with dimensions of 25x25 pixels to all image tags. While my current method is functional, I've noticed that the position sometimes shifts and the tag often ends up displayed below t ...

HTML, PHP, and MySQLI registration form without any noticeable errors

For a personal project, I am currently working on coding a registration form for my webpage. Although the webpage loads, nothing seems to be happening. No data is being inserted into the database. Below is the HTML code for the form. Note that I am usin ...

Adding a class to the wrapper of an input checkbox element and placing a class on the label/input field in CakePHP 3

To achieve a bootstrap checkbox layout in CakePHP 3 form helper, I want to include the custom checkbox class alongside the default "input checkbox" div and apply a class to the label as well. ...

Is there a way to extract all the `<select>` elements from a table and store them in an array?

I'm having some trouble with the order of my code and the array output. Everything seems to be a bit jumbled up right now and I can't figure out how to add all the rows correctly. $('#clicker').on('click', function(e) { v ...

displaying outcomes as 'Indefinite' rather than the anticipated result in the input field

I need to automatically populate values into 4 text fields based on the input value entered by the user. When the user exits the input field, a function called getcredentials() is triggered. This function, in turn, executes Python code that retrieves the r ...

Tips for arranging Bootstrap cards in ReactJS so they wrap to a new line after three have been displayed

In my personal portfolio, there is a dedicated page for showcasing various projects using Bootstrap cards. These projects are fetched from a CardData.js file and then mapped to a ProjectCard component. Ideally, I want to display 3 cards per row with consis ...

Center the cropped image within a div container

Is there a way to set a maximum height for a div containing an image and hide the parts of the image that exceed this height, while keeping it centered vertically inside the div? For example, if the browser width is 1200px and the image aspect ratio is 4:3 ...

The CSS infinite animation becomes erratic and sluggish after a short period of time

My website featured a banner with a series of thumbnail images that animated at different intervals and looped indefinitely. Initially, the animations ran smoothly, but after a few seconds, they began to slow down and lose consistency. To troubleshoot, I u ...

Updating Navigation Bar Font

I am a novice in the process of constructing my own navigation bar for my website, and I am encountering difficulties when trying to customize the font (color, font-family, etc). Currently, the links in my navigation bar (such as home, contact, store, etc ...

Struggling to position table correctly in Angular 2 - excess white space appearing on the right side

I am currently working on creating a table in Angular 2 with 4 columns. However, I have encountered an issue where unwanted white space appears to the right of the last column. Bootstrap is being utilized as well. Below is the HTML code snippet: < ...

What is the best way to apply custom styles in reactJs to toggle the visibility of Google Maps?

There are three radio buttons that correspond to school, restaurant, and store. Clicking on each button should display nearby locations of the selected type. Displaying Google Map and nearby places individually works fine without any issues. class Propert ...

Utilizing the asterisk selector in react/next.js

I'm currently in the process of building my first website using React. In my main.module.css file, I have the following CSS reset: * { padding: 0; margin: 0; box-sizing: border-box; } This is just a typical CSS styling reset. However, whe ...

Creating a Bootstrap 4 column form label with a maximum width limit

I am currently working on creating a form within a full-width fluid layout that consists of 2 columns - the label column and the inputs column. The issue I'm running into is that due to the wide screens on fluid layouts, the col-2 label appears too l ...

Tips for limiting a website to load exclusively within an iframe

I am managing two different websites: https://exampleiframe.com (a third-party website), https://example.com (my own website) My goal is to limit the loading of https://example.com so that it only opens within an iframe on https://exampleiframe.com To a ...

Incorporating User Input: Adding Paragraph Elements with HTML and Javascript to a Div Container

I am attempting to test my ability by creating a basic webpage. My goal is to take the text entered by the user in a textarea and add it to the page using a button. This button should generate a new paragraph element, place the user input inside of it, and ...

`Trying to conceal the tr elements on button click proves tricky with jquery`

I have a pair of buttons, #btn1 and #btn2, as well as two table rows, #tr1 and #tr2. The goal is to toggle the active state of the buttons when they are clicked by the user. The specific requirements are: When the button is set to active, the elements ...

"Troubleshooting a jittery React sticky header during scrolling: Dealing with IntersectionObserver and CSS

My current project involves implementing a sticky header in a React application using the Carbon framework. The goal is to have a sticky header with a bottom border when users scroll up, providing context for their viewing experience. However, when they sc ...

When a Mui Button is clicked, it changes the color of all tabs instead of just the active

My website footer contains 5 links represented by Mui Buttons with the component set to {Link} in order to transform them into clickable links. Currently, all the buttons are black and change to green when hovered over. However, I want the button color to ...