hiding and displaying responsive code with #media query - css

I'm trying to create a responsive code to get the best of both worlds from iframes and HTML 5 video with clickable links. In my set up I want to write an inline #media query to play only the Iframe when bigger than 375px and if smaller than 375px to play HTML video in the same div block item white
I'm getting stuck on how to display hidden and instead show <video>
and my boss is convinced this is the way he wants to do it :S
#media only screen and (min-width: 375px) {
div.video {
position: relative;
height: 0;
padding-bottom: 56.25%;
}
}
div.video>iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
<div class="item white">
<div class="video">
<iframe width="560" height="315" src="https://www.youtube.com/embed/TIB3q68ZHYw" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
</div>
</div>
<div class="item white">
<a href="https://www.walmart.com">
<video width="100%" height="100%" loop autoplay muted preload="metadata"> .
<source src="https://player.vimeo.com/external/274117996.sd.mp4?
s=d8982e09554557a0a5db18f8c5d450252fbcddbc&profile_id=165"
type="video/mp4" />
Your browser does not support the video tag. I suggest you upgrade
your browser.
</video>
</a>
</div>

In css, (min-width:500px) means that anything above 500px will trigger the css below that. If you instead say (max-width:500px), that means anything below 499px will trigger the css under that. For example:
/* This will effect the class as long as the screen width is > 500px */
#media (min-width:500px) {
.myContent {
display:none;
}
}
/* This will effect the class as long as the screen width is < 500px */
#media (max-width:500px) {
.myContent {
display:block;
}
}
Does this help a little bit?

One approach is (as you've noted) to combine the elements under one containing div and hide/show the content respectively (although the containing div here is moot). So with each breakpoint being reached, the iframe will be hidden and the video shown (and vice versa).
I couldn't get your mp4 file to play, so I've substituted it with an example as this may have impacted your effort and those of other responders...
Simple CSS:
#media only screen and (min-width: 375px) {
#iframe {
position: absolute;
height: 0;
padding-bottom: 56.25%;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
#video {
display: none;
}
}
#media only screen and (max-width: 375px) {
#video {
display: block;
}
#iframe {
display: none;
}
}
HTML:
<div class="item white">
<div id="iframe">
<iframe width="100%" height="100%" src="https://www.youtube.com/embed/TIB3q68ZHYw" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
</div>
<div id="video">
<video width="100%" height="100%" loop autoplay muted preload="metadata">
<source src="https://archive.org/download/BigBuckBunny_124/Content/big_buck_bunny_720p_surround.mp4"
type="video/mp4" />
Your browser does not support the video tag. I suggest you upgrade
your browser.
</video>
</div>
</div>
You can find this code working in a fiddle here:
http://jsfiddle.net/zh2b0n1e/
You will need to adjust the CSS styling elements to suit your needs (padding, width, borders etc) and improve the HTML, but this method is a simple way of achieving your goal.

Related

CSS: make image take up space before loading when inside other inline-block element

I'm working on an image gallery site, which shows a bunch of images of different sizes and aspect ratios. The images should be shown many on a line if they fit.
Each image should be say no wider than 20vw, and no taller than 300px.
Critically, I don't want any "jank" as the page loads - each picture should take up it's space before it has loaded so it all loads smoothly without jank / layout shift, even when internet is slow.
Now, I've managed to achieve this for images that are just sitting straight in the document.
But I can't get it to work when they are wrapped inside a <figure> element. (Eventually I want to add <figcations> to them, but this is not relevant for my question). The figure elements could just as well be <div>s or something else.
An example will illustrate what I'm trying to do:
* {
margin: 0;
}
body {
background-color: lightgray;
}
figure {
display: inline-block;
background-color: lightblue;
}
img {
max-width: 20vw;
max-height: 300px;
height: auto;
width: auto;
background-color: black;
display: inline-block;
}
figure img {
background-color: gray;
}
<h3>These 2 images take up space correctly while loading:</h3>
<img src="https://deelay.me/1500/https://dummyimage.com/300x600/f0f/000.png" width="300" height="600">
<img src="https://deelay.me/1500/https://dummyimage.com/400x200/0f0/000.png" width="400" height="200">
<h3>These 2 images just jank into existence when they load:</h3>
<figure>
<img src="https://deelay.me/1500/https://dummyimage.com/500x1000/f00/000.png" width="500" height="1000">
</figure>
<figure>
<img src="https://deelay.me/1500/https://dummyimage.com/300x150/0f0/000.png" width="400" height="200">
</figure>
<p>
some text that shouldn't move as the page loads
</p>
How can I make the second two images inside <figure>s take up space before they've loaded?
Change inline-block to inline on figure.
* {
margin: 0;
}
body {
background-color: lightgray;
}
figure {
display: inline; /* The only thing I changed */
background-color: lightblue;
}
img {
max-width: 20vw;
max-height: 300px;
height: auto;
width: auto;
background-color: black;
display: inline-block;
}
figure img {
background-color: gray;
}
<h3>These 2 images take up space correctly while loading:</h3>
<img src="https://deelay.me/1500/https://dummyimage.com/300x600/f0f/000.png" width="300" height="600">
<img src="https://deelay.me/1500/https://dummyimage.com/400x200/0f0/000.png" width="400" height="200">
<h3>These 2 images just jank into existence when they load:</h3>
<figure>
<img src="https://deelay.me/1500/https://dummyimage.com/500x1000/f00/000.png" width="500" height="1000">
</figure>
<figure>
<img src="https://deelay.me/1500/https://dummyimage.com/300x150/0f0/000.png" width="400" height="200">
</figure>
<p>
some text that shouldn't move as the page loads
</p>
However...
In Edge and Chrome, neither the images with or without wrapping figures load immediately, in your example or mine. Only in FireFox do they have a height and width before loading. You should wrap them in tags with a specified height and width for a cross browser solution.
New Solution:
I honestly had no idea what made the first example work in FireFox, I just had some intuition that the display mode often affects the volume of elements in strange ways. This exploration did demonstrate to us that browsers do not all render the above example the same way, which is unfortunate. I have an alternate solution for you though. After reading this excerpt from the page you linked me to I found something that seemed to work:
When the width/height of an element — as set using HTML
attributes — is overidden using CSS using something like this:
img {
max-width: 100%;
height: auto;
}
The aspect ratio is then used to calculate the height and therefore
the correct size is applied to the element, meaning that the
aforementioned jank will not occur when the image loads.
My solution is to remove the width:auto, it seems to work in both Firefox and Chrome. Honestly I also do not entirely know why this works either... My guess would be that the width:auto was overriding the width as it was defined on the element itself, thus not allowing the auto-magic aspect-ratio to be calculated, or something... If anyone wants to chime in an explain it properly, that would be appreciated.
Here is the demo:
* {
margin: 0;
}
body {
background-color: lightgray;
}
figure {
display: inline-block; /* I put this back to how you had it */
background-color: lightblue;
}
img {
max-width: 20vw;
max-height: 300px;
height: auto;
/*width: auto;*//*New solution, don't specify width, only height*/
background-color: black;
display: inline-block;
}
figure img {
background-color: gray;
}
<h3>These 2 images take up space correctly while loading:</h3>
<img src="https://deelay.me/1500/https://dummyimage.com/300x600/f0f/000.png" width="300" height="600">
<img src="https://deelay.me/1500/https://dummyimage.com/400x200/0f0/000.png" width="400" height="200">
<h3>These 2 images just jank into existence when they load:</h3>
<figure class="fig-1">
<img src="https://deelay.me/1500/https://dummyimage.com/500x1000/f00/000.png" width="500" height="1000">
</figure>
<figure class="fig-2">
<img src="https://deelay.me/1500/https://dummyimage.com/300x150/0f0/000.png" width="400" height="200">
</figure>
<p>
some text that shouldn't move as the page loads
</p>
I wrapped your rows with a div and I fixed their width as you write in question. All images has holder (you already do that with the <figure>) and your images need to load these holders. Your holders need max-height: 300px;, so you need to allocate maximum 300px height while you are waiting your images.
.row {
display:block;
}
.holder {
display:inline-block;
width:20vw;
max-height: 300px;
}
.holder img {
width:100% !important;
max-height:300px;
}
<h3>Some header here</h3>
<div class="row">
<div class="holder">
<img src="https://deelay.me/1500/https://dummyimage.com/300x600/f0f/000.png" width="300" height="600">
</div>
<div class="holder">
<img src="https://deelay.me/1500/https://dummyimage.com/400x200/0f0/000.png" width="400" height="200">
</div>
</div>
<h3>These 2 images just jank into existence when they load:</h3>
<div class="row">
<div class="holder">
<img src="https://deelay.me/1500/https://dummyimage.com/500x1000/f00/000.png" width="500" height="1000">
</div>
<div class="holder">
<img src="https://deelay.me/1500/https://dummyimage.com/300x150/0f0/000.png" width="400" height="200">
</div>
</div>
<h3>Txt should be stay here3:</h3>

HTML5 video in fullscreen: how to manipulate the screen size?

On my site I have a <video> element inside a <div>. I use this <div> to define the video aspect-ratio, and let the user change it if necessary (sometimes the encoded video has an incorrect ratio). The <video> always fills the <div> with the "object-fit: fill;" CSS property.
But... When the user enters to fullscreen mode, the screen becomes the container. And it is a problem, because of all the different display ratios out there.
Is there a way to manipulate/limit the fullscreen size?
<div style="width: 960px; height: 540px; background: #bfbfbf; display: block; margin: 0 auto;">
<video style="object-fit: fill;">
<source src="" />
</video>
</div>
Add "controls" to activate controls for the video
<html>
<head>
<style>
.div
{
background-color: #bfbfbf;
width: 960px;
height: 540px;
display: block;
margin: 0 auto;
background-size: cover;
}
</style>
</head>
<body>
<div class="div">
<video style="object-fit: fill; width: 960px; height: 540px;" controls>
<source src="video.mp4" type="video/mp4">
</video>
</div>
</body>
</html>

I want to add an image to the specific section used in the html code whenever the screen size is resized to mobile view using media query

How to add an image as a background using media queries to the section id= "vid" and hide the background video running on it.
My code HTML code is shown below
<section id="vid" class="vidd">
<video playsinline autoplay muted loop poster="css\img_forest1.jpg" id="bgvd">
<source src="video\Love-Coding.webm" type="video/webm">
</video>
</section>
and CSS code is shown below
#media screen and (max-width: 480px)
{
video { display: none; }
}
#media screen and (max-width: 480px)
{
#vid {
background: url(img_forest.jpg) no-repeat center center scroll;
}
}
Add your background image into <section> separately.
HTML
<section id="vid" class="vidd">
<img src="css\img_forest1.jpg" class="image1"/>
<video playsinline autoplay muted loop poster="css\img_forest1.jpg" id="bgvd">
<source src="video\Love-Coding.webm" type="video/webm">
</video>
</section>
CSS
.image1{ display: none; }
#media screen and (max-width: 480px)
{
video { display: none; }
.image1 { display: block; }
}

Youtube iframe responsive and float left

I need to make a youtube iframe responsive and also that the iframe is left floating to add content to the right, I tried with this code but the iframe is not responsive?
Thanks.
The JSFiddle : http://jsfiddle.net/94150148/vh04d7y2/
.videoWrapper {
position: relative;
padding-bottom: 51.44%; /* 16:9 */
padding-top: 25px;
height: 0;
}
.videoWrapper iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.box1 {
float:left;
width: 400px
}
<div class="box1">
<div class="videoWrapper">
<iframe width="560" height="315" src="https://www.youtube.com/embed/6apL89xgbR0"
frameborder="0" allowfullscreen></iframe></div></div>
<div class="box2">Some text</div>
I will suggest to use Bootstrap for making embedded videos responsive which are added through iframe, following is the code which you can use to make the iframe responsive
<!-- 16:9 aspect ratio -->
<div class="embed-responsive embed-responsive-16by9">
<iframe class="embed-responsive-item" src="..."></iframe>
</div>
<!-- 4:3 aspect ratio -->
<div class="embed-responsive embed-responsive-4by3">
<iframe class="embed-responsive-item" src="..."></iframe>
</div>
But make sure to add bootstrap.css and and other bootstrap dependencies.
All you need is max-width:
.left {max-width: 400px;}

responsive topsheet on background html5 video

I need to cover the html5 background video with black responsive layer with some opacity (let's call it "shadow layer"). Unfortunately, I can't pick a height of the video layer, because it has to be absolutely positioned, so it is not possible to pick the height of parent div.
Any advices? JS is absolutely not welcome.
Here is the code:
html
<div>
<div class="shadow-layout">
<div class="content">
Some content
</div>
</div>
<div id="video-bg">
<video class="video-bg" autoplay loop poster="">
<source src="http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4" type="video/mp4" />
</video>
</div>
</div>
css
.shadow-layout {
height: 100%;
width: 100%;
position: absolute;
background: rgba(0,0,0,0.5)
}
.content {
margin-top: 40px;
text-align: center;
color: white;
}
#video-bg, .video-bg {
width: 100%;
}
...and jsfiddle
Try changing your css to this...
.shadow-layout {
height: 100%;
width: 100%;
position: absolute;
background: rgba(0,0,0,0.5)
}
#video-bg, .video-bg {
width: 100%;
height:100%;
position:absolute;
}
And you should close your source tag... Change your html to this...
<div>
<div class="shadow-layout"></div>
<div id="video-bg">
<video class="video-bg" autoplay loop poster="">
<source src="http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4" type="video/mp4"/>
</video>
</div>
</div>
Working JSFiddle here http://jsfiddle.net/DivakarDass/k7cqprLz/1/
Hope you asked for a background for that video like this.
Cheers.

Resources