Add a floating tooltip relative to the content of the 'cover' video - html

Let's say I have a video with a known aspect ratio. I need to make it act like a fixed background-cover for a full-screen section (100vw / 100vh).
But also I need to attach some floating absolute-positioned elements, relative to the content of the video. For example, 60% left / 40% top point relative to the content of the video .
So If I use object-fit: cover on the video, and position: absolute for the floating elements, I can no longer just provide left / top percentage offset, because those percents no longer match the content of the video.
I tried to overcome this problem by using some math formulas like:
--video-aspect-ratio: 1680 / 944;
--video-height: max(100vh, 100vh / var(--video-aspect-ratio));
--video-width: max(100vw, 100vw * var(--video-aspect-ratio));
--vertical-video-offset: min(0%, (100vh - var(--video-height)) / 2);
--horizontal-video-offset: min(0%, (100vw - var(--video-width)) / 2);
position: absolute;
inset: var(--vertical-video-offset) var(--horizontal-video-offset);
But it just doesn't seems to work for some reason.
Need some help in figuring out how to achieve this layout, preferably in plain CSS.
Any ideas? Thanks!

So, looks like I found 2 possible solutions for this:
Using 2 wrappers and flex:
<div class='bg-frame'>
<div class='video-container'>
<video />
...Floating elements
</div>
</div>
.bg-frame {
position: absolute;
inset: 0;
display: flex;
place-content: center;
> .video-container {
aspect-ratio: var(--video-aspect-ratio);
flex: 0 0 auto;
min-height: 100%;
min-width: 100%;
width: max(100vw, 100vh * var(--video-aspect-ratio));
> video {
object-fit: cover;
height: 100%;
width: 100%;
}
}
}
Using 1 wrapper and position absolute:
<div class='video-container'>
<video />
...Floating elements
</div>
.video-container {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
aspect-ratio: var(--aspect-ratio);
min-height: 100%;
min-width: 100%;
width: max(100vw, 100vh * var(--aspect-ratio));
> video {
object-fit: cover;
height: 100%;
width: 100%;
}
}
I don't really like the transform: translate(-50%, -50%) part of the second solution, but it seems to work fine in the cases I tested.

I tried to get the right formula for positioning the texts when using cover for the video.
For reasons I have been unable to explain this worked when the video height was fixed to the viewport height but not when the width was fixed.
So this snippet uses another sizing and positioning method by setting the position of the video relative to a container depending on whether the viewport aspect ratio is greater or less than the video's aspect ratio.
The text positions are set as % within the video (so you can measure these on the original video) and therefore their positions relative to the overall video do not change. Of course, texts near an edge of the video may disappear or be seen only partially if the viewport gets too narrow or too wide.
* {
margin: 0;
}
body {
overflow: hidden;
}
.container {
height: 100vh;
width: 100vw;
overflow: hidden;
position: absolute;
}
.inner {
position: absolute;
width: fit-content;
height: fit-content;
overflow: hidden;
}
video {
position: relative;
/* here put the aspect ratio of your video */
aspect-ratio: 1920 / 1080;
overflow: hidden;
}
.overlay {
position: absolute;
top: var(--t);
left: var(--l);
transform: translate(-50%, -50%);
background: white;
padding: 10px;
}
/* here put the aspect ratio of your video */
#media (min-aspect-ratio: 1920 / 1080) {
.inner {
position: relative;
top: 50%;
transform: translateY(-50%);
width: 100vw;
}
video {
width: 100vw;
height: auto;
}
}
/* here put the aspect ratio of your video */
#media (max-aspect-ratio: 1920 / 1080) {
.inner {
position: relative;
left: 50%;
transform: translateX(-50%);
height: 100vh;
}
video {
height: 100vh;
width: auto;
}
}
<div class="container">
<div class="inner">
<video src="https://ahweb.org.uk/easter-card.mp4" autoplay muted></video>
<!-- --t and --l are the % top and left positions of the center of the overlaid text element -->
<div class="overlay" style="--t: 20%; --l: 20%;">a tree</div>
<div class="overlay" style="--t:45%; --l: 50%;">another tree</div>
<div class="overlay" style="--t:80%; --l: 90%;">yet another tree</div>
<div class="overlay" style="--t: 20%; --l: 80%;">the church</div>
</div>
</div>
Unfortunately it is not possible to build in the dimensions (or aspect ratio) of the video as the media queries do not allow CSS variables to be used so there are several places where you need to put the ratio in by hand.

Related

make the webpage fit into the screen

Trying to make the webpage fit into the screen so that you don't have to scroll up or down, or left and right. And the image is centered. Here is the current code:
#copyright {
top: 0px;
right: 11;
z-index: index 1;
}
#image {
position: absolute;
height: 2;
width: 2;
z-index: index 2;
}
<a href="https://lensdump.com/i/rSRDHc">
<img id="image" src="https://i3.lensdump.com/i/rSRDHc.png" alt="rSRDHc.png" border="0" /></a>
<div id="copyright"> ©apple </div>
Overflow wouldn't work if you wanted to scroll down to show more of the page. To fix that, you would have to crop the white background out of the image, which I'm unsure of why it's there in the first place, and it would also be best to make the background transparent after you crop it, in case you wanted to change the background colour.
You had the right set to 11, but didn't specify a value (same as your width and height on the image), which doesn't work at all; you have to specify a value e.g pixels or rem. And z-index: index [num] also isn't valid, it's just z-index: [num].
The alt tag on your image is a png file, which doesn't make much sense, it should be a description, or the link to the image.
#copyright {
right: 11px;
}
#image {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%) scale(.7);
z-index: -1;
}
body {
overflow: hidden;
}
<a href="https://lensdump.com/i/rSRDHc">
<img id="image" src="https://i3.lensdump.com/i/rSRDHc.png" alt="rSRDHc.png" border="0" /></a>
<div id="copyright"> ©apple </div>
You can achieve this with a containing div, flexbox, and the vh and vw declarations in css.
vh determines the view height, so 100vh is 100% of the window. With your assets I've stacked the image on 95% of the screen and the text below on 5%. vw works the same but for view width.
.container{
height: 95vh;
width: 100vw;
overflow: hidden;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}
#image{
width: 100%;
height: 100%;
}
#copyright{
height: 5vh;
width: 100vw;
text-align: center;
}
Here is a codepen: codepen

Setting video as div background and bring the text in front

I want to set my text background as video. Upon my trying I am not able to bring the text in front. The text always hidden behind. Another issue is that the video background is not responsive in smaller screen and it covers the whole screen. More so, it seems that the video's frame width and height are removed (e.g. doesn't play in origianl width and height reatio).
Could anyone please help me solving this problem?
HTML:
<div class="container-fluid no-left-padding no-right-padding welcome-text video-content">
<!-- Container -->
<div class="container">
<video autoplay muted loop>
<source src="assets/videos/welcome_video.mp4" type="video/mp4">
</video>
<h2>Hello, I want to display this text, and play the video on background.
</h2>
</div>
</div>
CSS:
.welcome-text {
padding-bottom: 180px;
padding-top: 180px;
}
.video-content{
height: 100%;
width: 100%;
overflow: hidden;
position: relative;
}
video{
min-width: 100%;
min-height: 100%;
position: absolute;
top: 50%;
left: 50%;
transform: translateX(-50%) translateY(-50%);
}
Try putting<h2>Hello I want to play this text and play the video on the background.<h2\> before <video><video\>tags
I would also advice that parameters
Like height and width be
width:auto;height:auto;
Try to put a z-index: 2 in the text so that it comes to the front of the video.
z-index
Regarding the size of the video, you have to define a size of the div.container and the
video{
min-width: 100%;
min-height: 100%;
[...]
}
Be this example at jsfiddle
I advise you to take a look at these two articles:
video height
how to change height of the video
Bring text in front of the video:
.welcome-text {
position: relative;
/* make it position:absolute instead if you want it to be centered as shown below*/
z-index: 1;
}
Center text in the container:
.container {
position: relative;
}
.welcome-text {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
Regarding making the video responsive and mantaining the aspect ratio, you have to figure out what's the aspect ratio. Let's say the video is 1280 x 720, then 1.78:1 is the aspect ratio. It means if the width is 100% the height should be 56%.
You can't tell the div to have 56% of the container's width, but there is a trick, you can tell it to have 56% of the width as padding:
.container {
position: relative;
height: 0;
padding-bottom: 56%;
overflow: hidden;
}
video {
position: absolute;
width: 100%;
height: 100%;
left: 0;
top: 0;
}

Fitting a YouTube iframe (both horizontally and vertically) without black bars

I want to fit a YouTube iframe embed in the middle of a page (70% width) so I'm doing this:
.wrapper {
margin: auto;
width: 70%;
}
.wrapper iframe {
left: 0;
top: 0;
width: 100%;
height: 100%;
}
The problem is, I'm getting black bars around the video to fill the available space. If instead I used padding (as in this answer) the iframe would only fit horizontally and might require scrolling in the vertical direction. Any idea what might help? (older browser support is not an issue)
Edit: I know the aspect ratio of the video I want to play.
You can try to use flex css for this issue. you can use the static height width to the iframe as per your requirement
.wrapper {
display: flex;
align-items: center;
justify-content: center;
}
.wrapper iframe{
width: 100px;
height: 100px;
}
Black bars appear when the height/width ratio of the iframe is too high (over .5, approximately).
Use this CSS to maintain a fixed height/width ratio, even when the wrapper is resized:
.wrapper {
width: 70%;
padding-top: 35%; // 35/70 = 0.5, the ratio can be changed here
margin: auto;
position: relative;
}
.wrapper iframe {
width: 100%;
position: absolute;
height: 100%;
transform: translate(0, -100%);
}

fluid images inside a fluid container that changes aspect ratio

Hi I have a fluid container that is based on screen height and width in my website. I would like to have an image fill the container at all times as it expands in any direction based on screen size. The images used will vary in size and aspect ratio.
I have made an attempt with html and css below:
div {
position: absolute;
top: 0;
left: 50%;
transform: translate(-50%, 0);
height: 80vh;
width: 80%;
background-color: red;
position: relative;
overflow: hidden;
}
img {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
min-height: 100%;
min-width: 100%;
width: auto;
height: auto;
/* max-width: 100%; // If used Image needs to be tall enough to fill div */
/* max-height: 100%; // If used Image needs to be wide enough to fill div */
}
<div>
<img src="http://i.imgur.com/kOY2G57.jpg">
</div>
Without max-height and max-width, this works well if the img is smaller than the container but does not work if the images come out larger as they come out in their natural size and get cropped.
jsfiddle example
Is it possible to accomplish this task with just pure css?
Update
I also would like to avoid using background images as a solution and see if this is possible with just the img tag in place at the dom so as to avoid programing the img tags if possible.
Instead of using the <img> tag you can just give the <div> a background image with background-size: cover property. The background image will maintain the aspect ratio while covering the entire div container at all times.
<div></div>
div {
position: absolute;
top: 0;
left: 50%;
transform: translate(-50%, 0);
height: 80vh;
width: 80%;
background: red url("http://i.imgur.com/kOY2G57.jpg") center center;
background-size: cover;
position: relative;
overflow: hidden;
}
Use object-fit for images to achieve the same result akin to background-size cover, contain:
.imgFit {
object-fit: cover; /* cover, contain */
display: block;
width: 100%;
height: 100%;
}
Use like:
<img class="imgFit" src="" alt="">

Make image scale with parent container when screen is re-sized

In my liquid layout, my div elements have the property position-fixed. This means that as I re-size the browser, all the elements remain in the same position but have shrunk or increased in size.
The problem is when I place a picture in one of my div elements, it does not scale to fit in my div element, therefore the image 'leaks' out of its div container.
What I need: a property on my div element and/or image so that the image stays the same size as the div container and when the page is re-sized, the image re-sizes as well. Here's what I have:
#div1 {
position: fixed;
background-color: blue;
width: 100%;
height: 10%;
opacity: .3;
}
#div2 {
background-color: green;
position: fixed;
opacity: .3;
left: 20%;
right: 20%;
top: 10%;
height: 40%;
width: 60%;
}
#div3 {
background-color: red;
opacity: .3;
position: fixed;
left: 20%;
right: 20%;
top: 50%;
height: 40%;
width: 60%;
}
#div4 {
background-color: tan;
opacity: .3;
position: fixed;
height: 80%;
right: 80%;
width: 20%;
top: 10%;
}
#div5 {
background-color: black;
opacity: .3;
position: fixed;
height: 80%;
width: 20%;
left: 80%;
top: 10%;
}
#div6 {
background-color: purple;
opacity: .3;
position: fixed;
top: 90%;
width: 100%;
height: 10%;
}
img {}
<div id="div1">
<p>div1</p>
</div>
<div id="div2">
<figure>
<img class="pictures" src="assets/me.jpg" />
<figcaption>
This is a picture.
</figcaption>
</figure>
</div>
<div id="div3">
<header>
<h1>Introducing Me</h1>
</header>
<p>div3</p>
<p>Hello eveyrone i am adan ramirez</p>
</div>
<div id="div4">
<p>div4</p>
</div>
<div id="div5">
<p>div5</p>
</div>
<div id="div6">
<p>div6</p>
</div>
make image background-image: url(..img);
and apply background-size: cover; on the same div.
The key here is cover property value as it tells browser to resize image while keeping aspect ratio to fit all sides.
#Sphinxxx suggested to use background-size: contain; which solved OP problem;`
Try this:
img {
height: 100%;
width: 100%;
object-fit: contain;
}
object-fit is a pretty cool CSS3 property.
Used with the contain value the image will increase or decrease in size within its container while maintaining its aspect-ratio.
Here's how CSS-Tricks describes it:
The object-fit property defines how an element responds to the height
and width of its content box. It's intended for images, videos and
other embeddable media formats in conjunction with the object-position
property. Used by itself, object-fit lets us crop an inline image by
giving us fine-grained control over how it squishes and stretches
inside its box.
Because browser support for this property is still somewhat weak, here's a polyfill that covers all major browsers including IE9: Polyfill for CSS object-fit property
For a deeper look here are a few references:
W3C CSS Image Values and Replaced Content Module Level 3
MDN object-fit
CSS-Tricks `object-fit
Have you tried :
img {
width: 100%;
}
Try:
img {
max-width: 100%;
max-height: 100%;
}
figure {
height: 100%;
width: 100%;
margin: 0;
}
figure is the parent element, so you need to set it's height/width as well. Also, the default styling on figure includes a margin, so you need to remove that to keep the image inside of the parent div. Also, you may need to make the max-height smaller to account for the caption if you want to keep that inside of the parent div.
You can also use width and height instead of max-* if you want the image to always fill the parent regardless of its native size.