Background Video, Always Scale-to-height, Crop Width if Needed - html

Apologies if this has been asked before, but I've been checking quite a few other related threads and they've not been exactly what I'm looking for.
So I have a 1280x720 video that I was hoping to make my page's video background. I want it to scale to fit viewport height at all times, but NOT change aspect-ratio.
So this means that if the viewport ends up wider than 16:9, then I can have black-bars/letterboxing on the sides.
But if the viewport is narrower than 16:9, the sides of the video are cropped (I'm fine with this, as all important parts of the vid are in the center).
How would you suggest I do it? It's been driving me crazy.

I had the same issue earlier. This is how I resolved it.
.containervideo {
width: 100%;
height: 100%;
position: absolute;
padding: 0;
margin: 0;
left: 0px;
top: 0px;
overflow: hidden;
}
The code is straight forward.

Well, without a working example it is hard to give a suggestion that will definitely work but here may be something that will send you in the right direction.
Create a wrapper around the video that has an absolute position and use flexbox to center the contents (the video) in the center of the page, then give it a black background. You can do that with something like this:
.wrapper {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
display: flex;
justify-content: center;
align-items: center;
background: #000000;
}
You may need to play around with the CSS for the video perhaps like this:
video {
width: auto;
height: auto;
max-width: 100vw;
max-height: 100vh;
}
Something like that should give you the results you are looking for, or at least close to them. (I did not test this though)
Hope this at least gives you some ideas if it does not work. In the future, it is best to add your current code to make it easier for us to see what you have actually tried.

Related

Large space appearing underneath my flexbox when child extends past the window

I'm trying to create a horizontal image scroll using flexboxes, which I am fairly new to. I'm very happy with its abilities but here is one thing I cannot figure out.
So it seems to be fine as long as the parent and the child are within the width of the window but as soon as the child (and also the parent) extend past the width of the window something causes it to give the window some extra height. I've watch a bunch of videos and googled a lot. I don't know this is.
Here's my SCSS:
.images {
position: absolute;
left: 0;
top: 0;
height: calc(100% - 70px);
z-index:0;
display: flex;
flex-direction: row;
.image {
width: 100vw;
height: 100%;
overflow: hidden;
border-right: 4px solid white;
}
}
Okay, I figured it out. I didn't declare overflow-y: hidden. And there was an image in the child container that was making my whole document taller. Yeah, I didn't provide enough information for this question to be properly answered.

Is there a cross browser way to make an image shrink to fit?

Apologies if this is obvious, I'm no CSS expert.
When you drop an image directly onto a web browser on any browser, they all implement some sort of "shrink to fit" functionality. Example is this video which shows shrink to fit in action on Firefox:
http://youtu.be/1LW-eByYXik
I want to implement what is shown in the video in my application and have it work cross browser to the greatest extent practical.
Is there a way to do this? Various documents on the web cover some sort of discussion about shrink to fit but none seem to discuss how to implement this for an image across browsers in a consistent manner.
I've looked at the code on the browser when an image has been dropped on and they all seem to take a different approach.
#slaks I have tried your suggestion just then on Chrome and it did not work. Here's the code I tried:
<html>
<head>
</head>
<body>
<style>
img {
width: auto;
height: auto;
max-width: 100%
max-height: 100%
}
</style>
<img src="whn-data/image.png">
</body>
</html>
</head>
This code seems to work:
img {
margin: auto;
position: fixed;
top: 0;
left: 0;
bottom: 0;
right: 0;
max-height:100%;
max-width: 100%;
}
JSFiddle
Margin: auto is added to keep the image centered (both horizontal and vertical).
The max-height and max-width limit the image from going bigger than the screen.
BUT this technique has a disadvantage: the default size of your image has to be bigger than the height/width of the browser window or container it is in. If it is not margins will appear on all sides to keep the image's default dimensions.
You're looking for background-size: contain.
(assuming that the image is a background-image)
For an <img> tag, use
width: auto;
height: auto;
max-width: 100%
max-height: 100%
I think what browsers implement in those cases is the property zooom.
I FIGURED IT OUT. Sorry it took me a while. This is actually pretty obvious.
Use this:
body, html {
height: 100%;
margin: 0;
padding: 0;
}
div {
height: 100%;
}
img {
max-width: 100%;
max-height: 100%;
}
JSFIDDLE HERE

Transparent div with large image behind Effect

I've looked around but it doesn't help that I don't even know the name of the effect.
What is the effect called which has a transparent div section with a large image behind it. It's similar to a paralax effect but I don't think it falls under that category.
Please see here: http://shield.nvidia.co.uk/
At the "All you favourite games to go." section.
I've seen it on lots of sites.
Another example here: http://www.wekeroad.com/
If I can at least know what the effect is named I can proceed to learn it.
Edit: Thanks guys, I thought it was some fancy effect. It's very simple, I made my own too just playing around: http://jsfiddle.net/uuepunx8/
html, body{
height: 100%;
width: 100%;
}
*{
padding: 0;
margin: 0;
}
.first{
width: 100%;
height: calc(100% + 100px);
background-color: red;
}
.second{
height: 50%;
background: url('http://hdwallpaper.freehdw.com/0004/3d-abstract_hdwallpaper_equations_33432.jpg') fixed 0 0;
}
Setting a main container's background to position:fixed; is what you're looking for. Your internal content will need to be big enough to scroll and have something breaking up the content, such as a margin-bottom: 300px:
.background{
background: url(http://lorempixel.com/300/300/) repeat;
background-size: cover;
position: fixed;
height: 100%;
width: 100%;
}
Here is a simple JSFiddle to play with.
I don't know the name, but that effect is not hard to implement using CSS.
All you have to do is add a background image to each of your scrolling divs (or other element type structuring your page) setting the background-attachment to "fixed".

CSS Height % and Px

I'm working on designing a full-page site, which will be powered mostly with javascript (ajax in particular). Right now, I'm working on the basic structure and such.
I've seen several questions with similar goals, but none of them really helped. Maybe I'm misinterpreting, or something. I dunno. Anyway, my goal is to create a page that takes up exactly the amount of space a user's browser provides, without empty space on the sides or top. This means I have to rely upon percent-based measurements for my structure.
Problem is, one of the two key elements is to be a specific size, in pixels. Any bigger, and there will be space left empty and put to waste. Any smaller, and my site's logo won't fit. Take a look at my code:
HTML
[nav]The Beef[/nav]
[footer]The Cream Filling[/footer]
CSS
html, body{height: 100%; margin: 0; overflow: hidden; padding: 0; position: relative; width: 100%; z-index: 0;}
nav{display: block; height: 100%; position: absolute; width: 100%; z-index: 1;}
footer{bottom: 0; display: block; height: 170px; position: absolute; width: 100%; z-index: 2;}
The problem is, now the full-page navigation (as I mentioned, javascript-powered site) continues on "under" the footer. What I want it to do, is take up all of the space the footer isn't using, without extending the page beyond the capacity of the user's screen (IE, no scroll bars).
I'd rather not use javascript for this, but I'm willing to do so if there are absolutely no other options.
Why not specify the bottom position of the content block:
bottom: 170px;

Centering issues with large backgrounds

After a long long while I had to do some frontend, and I have a annoying issue to resolve.
So basically I have 1900x1200 Div wrapper and while the whole thing looks pretty and centered on huge monitors, when I open it on a small/resolution monitor it isn't centered.
I would like the opening position to be in the middle of the whole thing regardless of my resolution.
Any ideas?
The whole thing is driving me nuts and I can't seem to be able to wrap my head around it. :(
(and I'm sorry if I don't make much sense, its 2:25 am here, I'm not a native speaker and my brain is fizzling out)
You could center the div through absolute positioning.
#DivWrapper {
width: 1900px;
height: 1200px;
position: absolute;
left: 50%;
margin-left: -950px;
}
Example: http://jsfiddle.net/AXenC/
If your interface is base on a smaller width than 1900px probably more like a 960 grid, you could center your background in in the body tag and center the wrapper(width of your content ie: 960 px) with the margin auto on the left and right.
body {
padding: 0;
margin: 0;
background: #aeaeae url(../images/back__v_1.jpg) 50%;
}
#wrapper {
width: 960px;
margin-right: auto;
margin-left: auto;
}