Place an element relative to another under a very specific circumstance - css

I want to place an element above the top right corner of a Youtube Video (an iframe).
Something like this:
Note: notice the white cross on the top right corner.
The iframe is within a container and it expands until a max-width (in this example it's 20rem) and when the container is narrower, it shrinks with an aspect ratio of 16:9 (big shout out to the old-schoolers who also were forced to do this with the 52% padding trick when aspect-ratio wasn't a thing yet in CSS!)
You can see it in action here:
.container {
position: relative;
border: 0.1rem dashed tomato;
}
.video {
display: block;
aspect-ratio: 16 / 9;
width: 100%;
max-width: 20rem;
margin: 0 auto;
}
<div class="container">
<iframe class="video" src="https://www.youtube.com/embed/8o0Qao60T1Y" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
The question is: how do I do this?
Did I try something?:
Yes! My happpy me said, "ha, easy, just use :before". And I just learned... it won't work for an iframe.
So yeah, I'm not seeing the obvious right now, and I'll appreciate your help.

If you copy those aspect-ratio and max-width rules to an inner container, you can then have the X float relative to the video's width.
.container {
position: relative;
border: 0.1rem dashed tomato;
}
.video {
display: block;
width: 100%;
aspect-ratio: 16 / 9;
margin: 0 auto;
}
.inner {
aspect-ratio: 16 / 9;
max-width: 20rem;
border: 1px solid blue;
width: auto;
margin: 0 auto;
}
.inner:before {
content: "X";
display: block;
position: relative;
background: red;
text-align: right;
}
<div class="container">
<div class="inner">
<iframe class="video" src="https://www.youtube.com/embed/8o0Qao60T1Y" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
</div>

Use This
max-width:100%;
You need to change your max-width to see video in full screen.

Related

How to keep video and lesson menu same height

I'm trying to do a video layout like the following:
The video embed will be with an iframe. I'd like to keep the height of lesson items on the left to be same height as the video and to have different container on the bottom that comes after the video and lesson container.
html
<div>
<div class='container'>
<div class='video-container'>
<iframe class='iframe' src="https://www.youtube.com/embed/I2UBjN5ER4s" width="640" height="360" frameBorder="0"
allow="autoplay; fullscreen" allowFullScreen title="https://player.vimeo.com/video/459785420">
</iframe>
</div>
<div class='menu-items'>
<div class='lesson'>
lesson menu
</div>
...
</div>
</div>
</div>
css:
.container {
display: flex;
height: 100px;
}
.video-container {
overflow: hidden;
padding-bottom: 56.25%;
height: 0;
position: relative;
outline: 1px solid black;
flex: 0 0 75%;
}
.iframe {
left:0;
top:0;
height:100%;
width:100%;
position:absolute;
}
.menu-items {
padding: 10px;
flex: 0 0 25;
}
.lesson {
padding-bottom: 20px;
}
I've created a codepen: https://codepen.io/cagaroos/pen/XWdoROw?editors=1100
try this
.video-container {
height: auto;
}
instead of
.video-container {
padding-bottom: 56.25%;
height: 0;
}
add bottom and right
.iframe {
bottom:0;
right:0;
}
remove padding-bottom of last lesson div
.lesson:last-child {
padding-bottom:0;
}
also remove width and height <iframe> tag in html
Codepen demo

How to display a grid keeping 16:9 aspect ratio?

I'm trying to display a grid of live cameras.
I need two responsive columns keeping 16:9 aspect ratio for the items.
My code is:
<div class="grid">
<div class="item" *ngFor="let i of cameras">
<iframe [src]="this.sanitizer.bypassSecurityTrustResourceUrl(i.url)"
scrolling="no"
frameBorder="0"
allow="autoplay; encrypted-media">
</iframe>
</div>
</div>
And the CSS
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
grid-template-rows: 1fr;
grid-gap: 1px;
.item {
background: grey;
display: flex;
}
.item:before {
content: "";
display: block;
height: 0;
width: 0;
padding-bottom: calc(9/16 * 50%);
}
iframe{
width:100%;
height:100%;
background:#F2F2F2;
overflow:auto !important;
-webkit-overflow-scrolling:touch !important;
}
}
The problem is how to keep the 16:9 aspect ratio.
Any idea on how to do that?
CSS-Tricks has a cool trick that has helped me with a similar issue before:
.videoWrapper {
position: relative;
padding-bottom: 56.25%; /* 16:9 */
padding-top: 25px;
height: 0;
}
.videoWrapper iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
<div class="videoWrapper">
<iframe width="560" height="349" src="#" frameborder="0" allowfullscreen></iframe>
</div>
Source: https://css-tricks.com/NetMag/FluidWidthVideo/Article-FluidWidthVideo.php

Don't want iframe to take up 100% width after 759px

I have a problem with youtube iframe videos. I want the iframe to take up the 100% width of the page and maintain aspect ratio on mobile devices. I do not want the video to get bigger than its 560 width or 315 height. I then want the 2 videos to be displayed inline-block on a tablet and for the desktop. I want the iframes side by side on a tablet and desktop. I can't seem to figure this out. I have exhaustively searched this and can't find an answer that works. Here is my code.
HTML
<div class="video_responsive">
<div class="video-container">
<iframe width="560" height="315" src="https://www.youtube.com/embed/9_zGXEmNKPo" frameborder="0" allowfullscreen></iframe>
</div>
<div class="video-container">
<iframe width="560" height="315" src="https://www.youtube.com/embed/zEI8CT7cElQ" frameborder="0" allowfullscreen></iframe>
</div>
</div>
CSS
.video_responsive{
padding-left: 1em;
padding-right: 1em;
}
.video-container{
position: relative;
padding-bottom: 56.25%;
padding-top: 35px;
margin-top: 20px;
height: 0;
overflow: hidden;
}
.video-container iframe{
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
Any help would be appreciated. I am doing this for a class. Professor has no idea how to fix this problem. Thanks
Update 2
OP mentioned an interest in tables. Although they can make a layout simple, it's not meant for layout, they're meant to represent data. Fortunately, there's are several display values that allow an element to behave like a table. On both demos, when in desktop mode, the posters (images displayed when video is idle) overlap and are distorted, but the video isn't.
The most important ones are:
table element will behave like <table>
table-row element will behave like <tr>
table-cell element will behave like <td>
Here's an article with tips on how to use display: table
This Plunker is using display: table/table-cell
Update 1
The code editor on this site isn't good for complex CSS so I moved the demo over to Plunker
Summary
2 major properties in use are:
media query
flexbox
Mobile mode 758px or less in width for viewport
flexbox layout is stacked (flex-flow: column nowrap)
Desktop mode 759px or more in width for viewport
media query triggers at 759px or more
flex-flow is now row nowrap
Use media query:
#media only screen and (min-width: 759px) {
.video_responsive {
display: table;
}
.video-cpntainer {
display: table-cell;
max-width: 45vw;
}
.video_responsive {
padding-left: 1em;
padding-right: 1em;
}
.video-container {
position: relative;
padding-bottom: 56.25%;
padding-top: 35px;
margin-top: 20px;
height: 0;
overflow: hidden;
}
.video-container iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
#media only screen and (min-width: 759px) {
.video_responsive {
flex-flow: row nowrap;
}
.video-container {
min-width: 380px;
max-width: 48vw;
min-height: 214px;
max-height: 27vh;
}
}
<div class="video_responsive">
<div class="video-container">
<iframe width="560" height="315" src="https://www.youtube.com/embed/9_zGXEmNKPo" frameborder="0" allowfullscreen></iframe>
</div>
<div class="video-container">
<iframe width="560" height="315" src="https://www.youtube.com/embed/zEI8CT7cElQ" frameborder="0" allowfullscreen></iframe>
</div>
</div>
In your CSS set max-width: 759px; for your iframe.

Scale iFrame css width 100% like an image

I want to scale an iFrame through CSS to width: 100%, and the height should scale proportionally to the width.
With an <img> tag this works fine.
Both the image and the iFrame have defined width and height in the html.
Here some examples:
<html>
<style>
#a{ width: 500px; }
img{ width: 100%; height: auto }
</style>
<body>
<div id="a">
<img src="http://lorempixel.com/200/150/" width="200" height="150" />
</div>
</body>
This works great on images, but I would like the same behaviour for iFrames:
<html>
<style>
#a{ width: 900px; background: grey;}
iframe{ width: 100%; height: auto }
</style>
<body>
<div id="a">
<iframe width="560" height="315" src="http://www.youtube.com/embed/RksyMaJiD8Y" frameborder="0" allowfullscreen></iframe>
</div>
</body>
The iFrame renders 100% wide but does not scale it's height proportional like the image does.
Big difference between an image and an iframe is the fact that an image keeps its aspect-ratio. You could combine an image and an iframe with will result in a responsive iframe.
Hope this answerers your question.
Check this link for example : http://jsfiddle.net/Masau/7WRHM/
HTML:
<div class="wrapper">
<div class="h_iframe">
<!-- a transparent image is preferable -->
<img class="ratio" src="http://placehold.it/16x9"/>
<iframe src="http://www.youtube.com/embed/WsFWhL4Y84Y" frameborder="0" allowfullscreen></iframe>
</div>
<p>Please scale the "result" window to notice the effect.</p>
</div>
CSS:
html,body {height:100%;}
.wrapper {width:80%;height:100%;margin:0 auto;background:#CCC}
.h_iframe {position:relative;}
.h_iframe .ratio {display:block;width:100%;height:auto;}
.h_iframe iframe {position:absolute;top:0;left:0;width:100%; height:100%;}
note: This only works with a fixed aspect-ratio.
I suppose this is a cleaner approach.
It works with inline height and width properties (I set random value in the fiddle to prove that) and with CSS max-width property.
HTML:
<div class="wrapper">
<div class="h_iframe">
<iframe height="2" width="2" src="http://www.youtube.com/embed/WsFWhL4Y84Y" frameborder="0" allowfullscreen></iframe>
</div>
<p>Please scale the "result" window to notice the effect.</p>
</div>
CSS:
html,body {height: 100%;}
.wrapper {width: 80%; max-width: 600px; height: 100%; margin: 0 auto; background: #CCC}
.h_iframe {position: relative; padding-top: 56%;}
.h_iframe iframe {position: absolute; top: 0; left: 0; width: 100%; height: 100%;}
http://jsfiddle.net/7WRHM/1001/
I like this solution best. Simple, scalable, responsive. The idea here is to create a zero-height outer div with bottom padding set to the aspect ratio of the video. The iframe is scaled to 100% in both width and height, completely filling the outer container. The outer container automatically adjusts its height according to its width, and the iframe inside adjusts itself accordingly.
<div style="position:relative; width:100%; height:0px; padding-bottom:56.25%;">
<iframe style="position:absolute; left:0; top:0; width:100%; height:100%"
src="http://www.youtube.com/embed/RksyMaJiD8Y">
</iframe>
</div>
The only variable here is the padding-bottom value in the outer div. It's 75% for 4:3 aspect ratio videos, and 56.25% for widescreen 16:9 aspect ratio videos.
You could use viewport units here instead of %. Like this:
iframe {
max-width: 100vw;
max-height: 56.25vw; /* height/width ratio = 315/560 = .5625 */
}
DEMO (Resize to see the effect)
body {
margin: 0;
}
.a {
max-width: 560px;
background: grey;
}
img {
width: 100%;
height: auto
}
iframe {
max-width: 100vw;
max-height: 56.25vw;
/* 315/560 = .5625 */
}
<div class="a">
<img src="http://lorempixel.com/560/315/" width="560" height="315" />
</div>
<div class="a">
<iframe width="560" height="315" src="http://www.youtube.com/embed/RksyMaJiD8Y" frameborder="0" allowfullscreen></iframe>
</div>
#Anachronist is closest here, #Simone not far off. The caveat with percentage padding on an element is that it's based on its parent's width, so if different to your container, the proportions will be off.
The most reliable, simplest answer is:
body {
/* for demo */
background: lightgray;
}
.fixed-aspect-wrapper {
/* anything or nothing, it doesn't matter */
width: 60%;
/* only need if other rulesets give this padding */
padding: 0;
}
.fixed-aspect-padder {
height: 0;
/* last padding dimension is (100 * height / width) of item to be scaled */
padding: 0 0 56.25%;
position: relative;
/* only need next 2 rules if other rulesets change these */
margin: 0;
width: auto;
}
.whatever-needs-the-fixed-aspect {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
/* for demo */
border: 0;
background: white;
}
<div class="fixed-aspect-wrapper">
<div class="fixed-aspect-padder">
<iframe class="whatever-needs-the-fixed-aspect" src="/"></iframe>
</div>
</div>
None of these solutions worked for me inside a Weebly "add your own html" box. Not sure what they are doing with their code. But I found this solution at https://benmarshall.me/responsive-iframes/ and it works perfectly.
CSS
.iframe-container {
overflow: hidden;
padding-top: 56.25%;
position: relative;
}
.iframe-container iframe {
border: 0;
height: 100%;
left: 0;
position: absolute;
top: 0;
width: 100%;
}
/* 4x3 Aspect Ratio */
.iframe-container-4x3 {
padding-top: 75%;
}
HTML
<div class="iframe-container">
<iframe src="https://player.vimeo.com/video/106466360" allowfullscreen></iframe>
</div>

Full-Width Google Map And CSS

I have a full-width google map on a page using height="100%" and width="100%". I want to know put some css div stuff on top of it. I read that using negative values such as margin-top: -500px; will work, and they do, however when I try to assign a color to the background of the div, it doesn't display it over the map, it displays it under it, which isn't what I want at all. Is there a solution to this?
Code:
<iframe width="100%" height="100%" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?hl=en&ie=UTF8&ll=37.0625,-95.677068&spn=56.506174,79.013672&t=m&z=4&output=embed"></iframe>
<div class="content"> I want this text to be in a div box that has a black background. <br /> </div>
Style:
.content {
margin-top: -500px;
width: 390px;
background-color: black;
}
html, body {
width: 100%;
height: 100%;
}
.map-frame {
width: 100%;
height: 100%;
position: relative;
}
.map-content {
z-index: 10;
position: absolute;
top: 50px;
left: 50px;
width: 390px;
background-color: black;
color: #FFF;
}
http://jsfiddle.net/oswaldoacauan/tx67C/
add this to your css. works in chrome at least
position:absolute;
z-index:9999;
color:#fff;
If I may, I used the following even without css for the map.
Just put width="100%" in the iframe
<iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d1994.873621232488!2d32.58519671879903!3d0.342002233462275!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x177dbbaed45800cb%3A0xfba72049a6cb630c!2sKamwokya%20Market%2C%20Kampala!5e0!3m2!1sen!2sug!4v1633372520333!5m2!1sen!2sug" width="100%" height="500" style="border:0;" allowfullscreen="" loading="lazy"></iframe>
<iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d1994.873621232488!2d32.58519671879903!3d0.342002233462275!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x177dbbaed45800cb%3A0xfba72049a6cb630c!2sKamwokya%20Market%2C%20Kampala!5e0!3m2!1sen!2sug!4v1633372520333!5m2!1sen!2sug" width="100%" height="500" style="border:0;" allowfullscreen="" loading="lazy"></iframe>
the div you want to style should be positioned absolutely and given a positive z-index so that it shows above the map not below

Resources