Responsive images and video in intro page - html

I am designing an intro page. I place a background and video piece on that page and also I want to get the view of desktop in mobile also. i tried the following code:
css:
.trailer-content {
max-width: 75%;
width: 600px;
margin-left: auto;
margin-right: auto;
}
.video-container {
position: relative;
padding-bottom: 56.25%;
padding-top: 30px;
height: 0;
}
.video-container iframe,
.video-container object,
.video-container embed {
position: fixed;
bottom:40px !important;
width: 50%;
height: 50%;
margin-left: auto !important;
margin-right: auto !important;
}
#background {
width: 100%;
height: 100%;
position: fixed;
left: 0px;
top: 0px;
z-index: -1; /* Ensure div tag stays behind content; -999 might work, too. */
}
.stretch {
width:100%;
height:100%;
}
html :
<div id="background">
<img src="img/bg.jpg" class="stretch" alt="" />
</div>
<div class="trailer-content">
<div class="video-container"><iframe width="420" height="315" src="//www.youtube.com/" frameborder="0" allowfullscreen></iframe>
</div>
</div>
Output result
the background appears stretched
the video is normal in desktop but in mobile view, the video is also stretched to some size.

Responsive background image:
html {
background: url(https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcSvctt0I--skoKQVfuVt-i4Et6vQ5ve7lXPkLy9sTElCWLKh1Ps) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
See demo
Responsive video:
HTML:
<div class="videoWrapper">
<!-- Copy & Pasted from YouTube -->
<iframe width="560" height="349" src="http://www.youtube.com/embed/n_dZNLr2cME?rel=0&hd=1" frameborder="0" allowfullscreen></iframe>
</div>
CSS:
.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%;
}
See demo

And again I must notice never use ID for styling
To resolve your case properly
Easiest way is to use #media rule w3school link
In that case you'll have any number of diffirent rules that applies to the same class but in diffirent situations.
That way is much better than just stretch video or backgrounds, because you'll have full control of all elements. They'll behave as you want them to behave at diffirent devices.

remove height:100% from #background... so the image will stretch 100% in width, but keep the right dimensions.
However, I think is better you put it as a background in the body like this:
body{background:url(img/bg.jpg);
background-repeat:no-repeat;
background-size:100% auto;}
For the video frame tags that holds videos, remeoving the height might scale the video properly as well.
You can also set specific width, height etc by using meidatypes, see more information about responsive webdesign (RWD) here http://www.hongkiat.com/blog/responsive-web-tutorials/

Related

Best way to have a cover sized responsive video background on a website

I have tried to have to go with the mp4 approach with HTML5 <video> tags. Seems to work great, fully responsive, and takes the whole available space, without showing any weird black padding as YouTube player does. Except that I cannot make it always centered (cover size background). And I cannot find a way to upload a video and get a direct mp4 link. I tried uploading to Dropbox and Streamable. Both come with their own annoying landing page which does not work with <video>.
I gave up on that <video> idea and decided to go with a Youtube player. Which does not work at all. Here is what I have,
<section class="hero">
<iframe frameborder="0" height="100%" width="100%" class="hero-video" allowfullscreen allow="autoplay; encrypted-media;"
src="https://www.youtube-nocookie.com/embed/1CL_X5O71gk?vq=hd1080&autoplay=1&loop=1&modestbranding=1&showinfo=0&rel=0&iv_load_policy=3&fs=0&controls=0&disablekb=1&controls=0&mute=1&showinfo=0&rel=0&html5=1">
</iframe>
<img class="logo" src="./assets/logo.png" alt="logo" title="logo" />
</section>
and the CSS used,
.hero-video {
position: absolute;
left: 0;
top: 0;
min-width: 100%;
min-height: 100%;
z-index: -1;
}
.hero {
position: relative;
text-align: center;
width: 100%;
height: 100vh;
background-image: linear-gradient(rgba(94,88,88,0.5), rgba(94,88,88,0.5));
background-repeat: no-repeat;
background-size: cover;
background-attachment: fixed;
background-position: center;
}
To play around with the ideas,
https://jsfiddle.net/temurih/xpkoe08j/7/
Second Approach:
In <video> tag approach, I was not able to center (cover size) the video. It looks weird on cellular devices.
<section class="hero">
<video autoplay muted loop class="hero-video">
<source src="https://player.vimeo.com/external/490883126.hd.mp4?s=ce7a3266505de5d5f8f51cd9c134e21d7b1d21bf&profile_id=175" type="video/mp4">
Your browser does not support HTML5 video.
</video>
<img class="logo" src="./assets/logo.png" alt="logo" title="logo" />
</section>
CSS is more or less the same,
.hero-video {
position: absolute;
left: 0;
top: 0;
min-width: 100%;
min-height: 100%;
z-index: -1;
}
.hero {
position: relative;
text-align: center;
width: 100%;
height: 100vh;
background-image: linear-gradient(rgba(94,88,88,0.5), rgba(94,88,88,0.5));
background-repeat: no-repeat;
background-size: cover;
background-attachment: fixed;
background-position: center;
overflow: hidden;
}
To play around with the ideas,
https://jsfiddle.net/temurih/xpkoe08j/12/
In your example code, just set the CSS to set the video size to be the size of its parent. Then, set object-fit to cover:
.hero-video {
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
z-index: -1;
object-fit: cover;
width: 100%;
height: 100%;
}

How to scale video to be as large as possible so that the background area is completely covered by the background video

I want to create a background video using HTML5 so it will scale as large as possible so that the background area is completely covered by the background video, something similarly to the background-size: cover;, but to the video.
I want it to be as cross-platform/browser as possible.
EDIT
I meant background-size: cover;
Sorry for that :/
You can easily do this using the transform property. It adjusts the video at the center of the screen and the width properties fill the screen with the entire video.
video {
position: fixed;
top: 50%;
left: 50%;
min-width: 100%;
min-height: 100%;
width: auto;
height: auto;
z-index: -100;
transform: translateX(-50%) translateY(-50%);
background-size: cover;
}
you can try using
background-size: 100vw 100vh;
vw- view width of the screen
vh-view height of the screen
read this
You can use the width and height properties and set it to 100% or use vh and vw units if the percentage does not work for you
I think what you are looking for is:
video{
object-fit:cover;
width:100%;
height:100%;
}
You could do like this, which behave similar to background-size: cover
html, body {
margin: 0;
}
.video-container {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
height: 100%;
overflow: hidden;
}
.video-container video {
position: absolute;
min-width: 100%;
min-height: 100%;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
<div class="video-container">
<video autoplay>
<source type="video/mp4" src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4">
</video>
</div>

How can I make the background image to be responsive without losing the proportions

I am doing an animation with HTML and CSS3 and I need adapt along with the background image. The problem is that the content stay within that div. I put the height and width fixed for this but don’t work. When I try using dynamic proportions (% or auto) and background-size: contain; the animation does not follow the original path.
With fixed size following the path:
and mobile works fine too
but, not is responsive:
With dynamic size is responsive, but not follow path:
Changed code:
#main{
position:relative;
- left: 0;
- height: 1366px;
- width: 980px;
+ // left: 0;
+ height: 100%;
+ width: 100%;
overflow:hidden;
background: url('../images/bg.png');
background-repeat: no-repeat;
-
+ background-size: contain;
}
DEMO
This is my index.html
<div id="main">
<div class="participant" style="z-index: 4;">
<div class="car">
<img class="photo" src="https://scontent-atl3-1.xx.fbcdn.net/v/t1.0-1/c21.21.259.259/s50x50/529297_568082979888645_1727470385_n.jpg?oh=c75505b8b23ff9abd26be1fd5771f81d&oe=582BAD0F" alt="">
<img class="sprite" src="http://i.imgur.com/OwYhg9T.png" alt="">
</div>
</div>
</div>
And my animation.css
#charset "utf-8";
/* CSS Document */
body, html {
height: 100%;
}
body {
padding:0;
margin:0;
}
#main{
position:relative;
left: 0;
height: 1366px;
width: 980px;
overflow:hidden;
background: url("http://i.imgur.com/G4gs6EG.png");
background-repeat: no-repeat;
}
#-moz-keyframes move
{
from {
right: -30%;
top: 8%;
}
to {
right: 140%;
top: 80%;
}
}
#-webkit-keyframes move
{
from {
right: -30%;
top: 8%;
}
to {
right: 140%;
top: 80%;
}
}
.participant {
position: absolute;
display: inline-block;
height:200px;
width: 200px;
right: 140%;
top: 80%;
-moz-animation:move 10s linear infinite;
-webkit-animation:move 10s linear infinite;
}
.sprite{
width: 100%;
height: 100%;
}
.photo{
position: relative;
top: 128px;
left: 99px;
width: 50px;
height: 50px;
}
This is a little tricky and requires fixed aspect ratio of the background image.
1. Make everything responsive.
First of all, it won't work if everything is %-based but the car is px-based (because if you resize your window everything will get smaller but the car will stay the same), so for starters you are going to have to change the size of your car to percentages.
2. Fix aspect ratio.
Then you need to fix the aspect ratio using mix of absolute and relative positions and paddings.
In your case your wrapper's CSS will look something like:
width: 100%;
padding-bottom: 71.74%; /* 980/1366 = ~ 0.7174 */
(your background image is 980x1366px)
DEMO
3. FUTURE PROOF: Fill the screen on every screen.
Unfortunately you can't do much about white space around your image because of the aspect ratio itself, I'd personally look for a 16:9 image for the background and it will fit most of the desktop/laptop screens perfectly, if you need to cover wide range of screens then you should use media queries with different-sized backgrounds.
Remember to adjust the padding-bottom of your container along with the image itself.
Hope it helps!
Try replacing height and width in your #main css class to:
#main{
height: 100%;
width: 100%;
background: url("http://i.imgur.com/G4gs6EG.png") no-repeat fixed center;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
I've got this working on codepen.io
http://codepen.io/NosNits/pen/RRqzPy

Centered full screen html image (not an image in css)

I'm trying to have a full screen image, easy enough with css using the code below.
width:100%;
height:100%;
background: url('photo2.jpg');
background-repeat: no-repeat;
background-position: center center;
background-attachment: fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
but the image is already placed in an html div, see here
<div class="fixed-background">
<img src="photo2.jpg"/>
</div>
It need's to be exactly how it would be using the css version, the only difference would be the image is called in html and not in the stylesheet.
try this
<style>
body {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
}
.fixed-background {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
overflow: hidden;
}
.myimg {
height: inherit;
}
</style>
<html>
<body>
<div class="fixed-background">
<img src="public/dbs/images/1.jpg" class="myimg" />
</div>
</body>
</html>
Use object-fit: cover; on the <img> tag:
<div>
<img src="photo2.jpg" style="object-fit: cover;"/>
</div>
that parameter is a rather new thing (not all browsers supported), but that's the way to go. See also http://caniuse.com/#search=object-fit
Without using a background, consider this:
#mydiv {
position: absolute;
top: 50%;
right: 50%;
bottom: 50%;
left: 50%;
margin-top: -100px; /* (calculate half the height of your image) */
margin-left: -100px; /* (calculate half the width of your image) */
}
Full screen Image? you could do something like this through HTML
<div class="fixed-background">
<img src="photo2.jpg" height="100%" width="100%">
</div>
http://jsfiddle.net/pj73m4po/
EDIT:
or are you looking for something like this?
http://jsfiddle.net/pj73m4po/1/
Try the following: http://jsfiddle.net/pj73m4po/4/
Put your image in a div 100% high and wide. If you don't want your image to be stretched you don't want to use width and height seperately.
body {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
}
.fixed-background {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
overflow: hidden;
}
img {
height: auto;
width: auto;
min-width: 100%;
min-height: 100%;
}
Instead use min-width and min-height. if you have a predefined image you can adjust the position in css. If you don't unfortunately you need javascript to center it.
The points that I gather from your css are the following:
Center the image
Fix the position of the image (so it doesn't scroll with the page)
Cover the viewport, scale proportionally to fit
That said, I suggest the following given your html
.fixed-background{
position:fixed;
width:100vh;
height:100vh;
overflow:hidden;
}
.fixed-background > img{
position:absolute;
width:100%;
height:auto;
top: 50%;
transform: translateY(-50%);
}
Honestly, I haven't tested the above but I would suspect you might get some weird results using fixed and absolute positioning together. But since the code defines the width and height directly using viewport units, it should be good. You might need 100vh of margin applied to a sibling element to get things to line up because position:fixed; will break the element out of the document flow.

How do size a simulated smartphone screen in a responsive div?

So I'm trying to show off a mobile site within a div that looks like a smartphone that is placed on a responsive html5 site. I've messed with the CSS so that the smartphone div keeps a solid aspect ratio as the width of the window is adjusted. Now I am trying to fit an iframe within the div to simulate the phone's screen. The problem I run into is that my responsive CSS trick is messing with the position of the screen.
Here is a jFiddle: http://jsfiddle.net/E6X4j/2/
I've tried to position it relative, but that messes with the responsive trick in CSS. Any suggestions?
HTML:
<div id="smartphonewidth">
<div id="smartphoneheight">
<object type="text/html" data="http://www.engadget.com" style="width:100%; height:100%;">
</object>
</div>
CSS:
#smartphonewidth {
margin-left: 5%;
background-image: url('http://i.imgur.com/lavEp8B.png?1?5267');
background-repeat: no-repeat;
width: 50%;
padding: 20%;
background-size: 100% 100%;}
#smartphoneheight {
position: absolute;}
Use this link CSS3 media queries for reponsive design
To target small devices we can use the following syntax:
#media only screen and (max-device-width: 480px) {
//your css here
}
please check this JSFIDDLE
CSS:
#smartphonewidth {
margin-left: 5%;
background-image: url('http://i.imgur.com/lavEp8B.png?1?5267');
background-repeat: no-repeat;
width: 50%;
padding: 20%;
background-size: 100% 100%;
}
#smartphoneheight {
position: relative;
z-index:1;
width:100%;
height:100%;
}
FIDDLE
#smartphonewidth {
margin-left: 5%;
background-image: url('http://i.imgur.com/lavEp8B.png?1?5267');
background-repeat: no-repeat;
width: 58%;
padding: 15%;
background-size: 100% 100%;
}
#smartphoneheight {
height: 15%;
position: relative;
margin-bottom: -69px;
margin-top: -50px;
}
<div id="smartphonewidth">
<div id="smartphoneheight">
<object type="text/html" data="http://www.engadget.com" style="width:100%; height:70%;">
</object>
</div>
</div>