Aligning the iframe within a div element

When I was trying to code an iframe that would resize with the screen, I encountered a problem where I couldn't center it. Despite attempting all the suggestions provided in THIS thread, I still couldn't get it to work. Can someone please point out if I'm overlooking something obvious or if there's simply no way to achieve this?

Here is the HTML code:

<div class="videoWrap">
    <iframe src="http://www.youtube.com/embed/playlist?list=PLn0iVeY0xhgZvWDQ1K_6EChZe_4TL5zDZ"></iframe>
</div>

And here is the corresponding CSS:

.videoWrap {
    position: relative;
    padding-bottom: 56.25%; /* 16:9 aspect ratio */
    padding-top: 25px;
    height: 0;
}
.videoWrap iframe {
    position: absolute;
    top: 0;
    left: 0;
    width: 80%;
    height: 100%;
}

Answer №1

Upon reviewing the linked question...

.videoWrap {
  display: flex;
  align-items: center;
  justify-content: center;
}

.videoWrap iframe {
  width: 300px;
  height: 300px;
}

div, body, html {
  height: 100%;
  width: 100%;
}
<div class="videoWrap">
  <iframe src="http://www.youtube.com/embed/playlist?list=PLn0iVeY0xhgZvWDQ1K_6EChZe_4TL5zDZ"></iframe>
</div>

Answer №2

One solution is to use the margin: auto; CSS property on your div element.

Answer №3

For this demonstration, I have centered the wrapper element both horizontally and vertically using standard settings (position: absolute` is used in this case). Additionally, I have specified the width and height within this code snippet. The video itself occupies the central area of the wrapper.

html, body {
  height: 100%;
  margin: 0;
}

.videoWrap {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  /* 16:9 */
  width: 80vw;
  height: 45vw;
}

.videoWrap iframe {
  width: 100%;
  height: 100%;
}
<div class="videoWrap">
  <iframe src="http://www.youtube.com/embed/playlist?list=PLn0iVeY0xhgZvWDQ1K_6EChZe_4TL5zDZ"></iframe>
</div>

Answer №4

Implement the following CSS properties to center an iframe horizontally:

.videoContainer {
    position: relative;
    padding-bottom: 56.25%; /* 16:9 aspect ratio */
    padding-top: 25px;
    height: 0;
}
.videoContainer iframe {
    position: absolute;
    top: 0;
    left: 50%;
    transform: translateX(-50%);
    width: 80%;
    height: 100%;
}

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

Tips for accurately aligning a relative p tag within a td

Description: I am trying to prevent text overflow in a table cell and position the text underneath a header without wrapping it under an image. I have tried setting a static width for the image and adjusting the left property, but it is not working as expe ...

Adjust image source based on media query (CSS or JavaScript)

Is there a way to update the image src based on a media query with a maximum width of 515px? I'm considering using JavaScript, jQuery, or even AngularJS if there is an event equivalent to a CSS media query that can achieve this. I want to have differ ...

Discover the method for creating URLs that are relative to the specific domain or server on which they are hosted

I need to adapt my menu bar to cater for different server environments: <a href="http://staging.subdomain.site.co.uk/search/page">Menu</a> The main source site is hosted on an external subdomain from an API service, and I want the URLs in my ...

Navigational menu on website communicates with embedded Tableau iFrame

I have successfully integrated a Tableau dashboard/worksheet into my website using an iframe. I want to create navigation on the parent site that allows users to interact directly with the embedded dashboard by switching between different views, instead ...

Having issue with jQuery popup not functioning correctly on the right side with the contact form. Is there anyone who knows how

Looking for a side slide popup that only accepts paragraph content? Want to add a contact form to it? Check out this fiddle - link here For a working example, visit - $(function() { // Slide from right to left $('#test2').PopupLayer({ ...

The element in the HTML source code evades detection by Selenium

Given: Python 3.11.2 Selenium 4.8.2 Chrome as the browser engine I am searching for the following HTML code snippet: <input type="text" class="datepicker apex-item-text apex-item-datepicker hasDatepicker" size="32" value= ...

Flex mode allows for responsive row details in ngx-datatable

Having an issue with my ngx datatable row details. Everything works well initially, but when I adjust the browser width, the details don't scale properly with rows and columns. They seem to have a fixed width that was set when the page was first loade ...

What methods are available to modify styles for the before and after pseudo-elements in Angular?

I'm currently in the process of adding breadcrumb navigation with triangle shapes using before/after elements in CSS, following the method outlined in this tutorial: http://css-tricks.com/triangle-breadcrumbs/ These are the relevant code snippets: ...

Updating the styles of React Native components using stylesheets

I've created a unique React component with the following structure: import { StyleSheet } from 'react-native'; import { Input, Item } from 'native-base'; import Icon from 'react-native-vector-icons/FontAwesome'; import { ...

Unusual Occurrence: Unexpected Gap at the Beginning of HTML

There seems to be an issue with white space appearing unexpectedly at the top and right side of the file in a website I am working on. Despite adding margin: 0; padding: 0; to both <body> and <html>, the problem persists. After inspecting the ...

Twice the clicking actions triggered by the event

Whenever I try to trigger a function by clicking on the label text, the click event seems to be firing twice. HTML <label class="label_one"> Label One <input type="checkbox"/> </label> However, if I modify the HTML code as f ...

Hiding components in Angular 4/5 through routing decisions

Just getting started with routing in Angular 4/5, I am currently following the tutorial provided on the official Angular website. I have an Angular application and I want to create two separate pages. Currently, the main page is located at localhost:8870/d ...

What steps can I take to resolve the issue of my popup closing automatically upon opening and simultaneously refreshing my webpage?

After creating a popup in my HTML page, when I click on the element that triggers it, the popup appears for a brief moment before automatically closing and causing my page to refresh. Below is the code I am using: HTML Code : <a class="lv-footer" id= ...

Layout featuring center-aligned fixed-width design with elements stretching across the entire width of the page

How can I create a centered fixed-width layout for my page while also having headings with a background that extends over the whole page, without having to break up the single fixed-width+margin auto div into many separate divs? ...

Disabling font-awesome checkboxes

As someone who is new to front-end technologies, I am trying to achieve the following. Despite my efforts to find a solution, I have not been successful so far. Here is the issue I am facing-- I have two master checkboxes labeled 1) I do not like numbers ...

Vuetify vueJS dialog with elevated design

Is there a way to completely remove the elevation of a v-dialog so that it has no shadow at all? The elevation property in the v-dialog itself and using the class as Class = "elevation-0" have not worked for me. Here is the link to the component ...

Customize dynamically loaded data via AJAX

I have a webpage that is loading a table from another source. The CSS is working fine, but I am facing an issue editing the table using jQuery when it's loaded dynamically through a script. It seems like my changes are not getting applied because they ...

Unable to create a polygon on the Google Maps API using GPS coordinates inputted from a text field

I am currently developing an interactive map using the Google Maps API. The map includes a floating panel with two input fields where I can enter GPS coordinates. My goal is to create markers and connect them to form a polygon. While I can easily draw lin ...

Both position absolute and position relative are failing to work effectively

Whenever I click on a div called "test", another div named "outside" appears, along with a div named "inside" that has a higher z-index. The problem arises when I try to set the position of "inside" to absolute, as I am unable to assign a margin-bottom. A ...

Customize dropdown item colors in React using a color picker

I am currently utilizing a react color picker to create a personalized 16 color gradient. The process involves selecting a color from a dropdown menu and then using the color picker to make adjustments. This action updates an array that dictates the stylin ...