I am trying to come up with a website of my own where I have a background image that covers the entire page when it loads initially. I want this to be responsive as well so that I can see the entire background image irrespective of screen size.
Here is the catch though, As I scroll the page I want the next div or element to be ready just below (on scrolling). The best reference i can think of right away would be fiftythree.com.
What you’re seeing on fiftythree.com is merely a div that has the size of the viewport height with a background-image set.
To make the image scale based on the screen size, set it to background-size: cover.
.hero {
background-color: salmon;
/* background-image: url(...); */
background-position: center;
background-size: cover;
color: #fff;
height: 100vh;
overflow: hidden;
}
.hero__title {
margin-top: calc(50vh - 2em);
text-align: center;
}
.wrapper {
padding: 1em;
}
<link href="https://necolas.github.io/normalize.css/3.0.2/normalize.css" rel="stylesheet"/>
<div class="hero">
<h1 class="hero__title">Look at this</h1>
</div>
<div class="wrapper">
<h2>Hello earthling</h2>
</div>
I have had the same question some time ago, I did fix it by using this tutorial:
http://responsivedesign.is/resources/javascript-jquery/back-stretch
Backstretch is a jQuery plugin that allows you to add a dynamically-resized background image to any page. The image will stretch to fit the page, and will automatically resize as the window size changes.
it is very easy to use and it does the job:)
CSS
#header{
background: url(images/bg.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
Javascript
<script type="text/javascript">
$.backstretch("/path/to/image.jpg", {speed: 150});
// Perhaps you'd like to change the image on a button click
$(".button").click(function() {
$.backstretch("/path/to/next_image.jpg");
});
</script>
don't forget to copy this in the html:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"> </script>
<script type="text/javascript" src="/jquery.backstretch.min.js"></script>;
anyway, check the link:)
edit: http://srobbin.com/jquery-plugins/backstretch/ even simpler
Related
I am attempting to make a feature like https://www.artyofficial.com/ has.
When scrolling down, the bottom of the second image will appear while the first image starts to get cut away. How is something like this achievable with CSS? I would post a style sheet but unfortunately I don't even know where to begin here.
What you are looking for is the parallax scrolling effect
The most important property is the background-attachment: fixed; here.
I made you a JSFiddle easy as possible, take a look at it.
I would start by looking at the background-attachment property, it has a value of:
fixed The background is fixed with regard to the viewport
It is called as parallax effect. You can refer to link: Parallax Effect
<style>
.parallax {
/* The image used */
background-image: url("img_parallax.jpg");
/* Set a specific height */
height: 500px;
/* Create the parallax scrolling effect */
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
</style>
<!-- Container element -->
<div class="parallax"></div>
You just need to give all your such consecutive div's background-attachment: fixed. Check the example below:
.panels{
width: 100vw;
height: 100vh;
background-attachment: fixed;
background-size: cover;
background-position: center center;
}
.panels:nth-child(1){background-image: url('http://lorempixel.com/400/200/sports/');}
.panels:nth-child(2){background-image: url('http://lorempixel.com/400/200/nightlife/');}
.panels:nth-child(3){background-image: url('http://lorempixel.com/400/200/cats/');}
.panels:nth-child(4){background-image: url('http://lorempixel.com/400/200/food/');}
.panels:nth-child(5){background-image: url('http://lorempixel.com/400/200/nature/');}
/* this is for hiding stackoverflow console */
.as-console-wrapper{ display: none !important; }
<div class="panels"></div>
<div class="panels"></div>
<div class="panels"></div>
<div class="panels"></div>
<div class="panels"></div>
I have an image called myImage.jpg. This is my CSS:
body {
background-image:url("../images/myImage.jpg");
background-repeat: no-repeat;
background-size: 100% 100%;
}
For some reason, when I do this, the width of myImage stretches across the entire screen but the height only stretches until the height of everything else on the page. So if I put a bunch of
<br>
in my html page, then the height will increase. If my HTML page consists only of a
<div id='header'>
<br>
</div>
then the height of the background image would just be the height of one
<br>
How do I make the height of my background image 100% of the screen which the user is using to view the webpage?
You need to set the height of html to 100%
body {
background-image:url("../images/myImage.jpg");
background-repeat: no-repeat;
background-size: 100% 100%;
}
html {
height: 100%
}
http://jsfiddle.net/8XUjP/
I would recommend background-size: cover; if you don't want your background to lose its proportions: JS Fiddle
html {
background: url(image/path) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
Source: http://css-tricks.com/perfect-full-page-background-image/
The VH unit can be used to fill the background of the viewport, aka the browser window.
(height:100vh;)
html{
height:100%;
}
.body {
background: url(image.jpg) no-repeat center top;
background-size: cover;
height:100vh;
}
html, body {
min-height: 100%;
}
Will do the trick.
By default, even html and body are only as big as the content they hold, but never more than the width/height of the windows. This can often lead to quite strange results.
You might also want to read http://css-tricks.com/perfect-full-page-background-image/
There are some great ways do achieve a very good and scalable full background image.
I have an image that is my header. Here is my simple HTML:
<html>
<body>
<div class="wrapper" />
</body>
</html>
It fills the full width of the page, but I had to specify a height for it show up. Here is the css:
.wrapper {
background-image: url(../assets/bridge.jpg);
background-repeat: no-repeat;
background-size: 100%;
width: 100%;
height: 250px;
}
How do I make this image responsive? Right now when I expand the page it gets to the point where the pic is unrecognizable.
Didn't got your question quiet well, but I think you are missing a value here
background-size: 100%; /* 1 value is not wrong but you'll probably need 2 */
--^---
CSS
.wrapper {
background-image: url(http://images.google.co.in/intl/en_ALL/images/logos/images_logo_lg.gif);
background-repeat: no-repeat;
background-size: 100% 100%;
width: 100%;
height: 250px;
}
Demo
As ralph.m suggested, if you are using this image as your website background, than use the background property on body element instead of div
You need to use following CSS to make the background responsive
body {
background: url(bg.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
Reference Link
You need to think carefully about how you want/expect this to work. Without some actual content in the div, it will have zero height, which is why you needed to set a height on it; but in general, try to avoid setting heights. Presumably, if this is a "wrapper", it will be wrapping some content that will hold it open without you having to set a height.
As for the background image, you need to think about how it will behave. Do you just want it to appear in a strip along the top? If you use Mr Alien's solution, be aware that the image will stretch wider and wider and start to look odd. So we need some more information on what you are trying to do here.
I'm brand new to this site so apologies if I'm stepping on toes but I've been trying to create a page where you are met by a fullscreen header, with a height and width of 100% of the viewport, however you are still able to scroll to the content below.
Perfect example of this is http://www.bklynsoap.com/
I have tried to achieve this with purely CSS by creating a 100% height and width on an absolute positioned Div, but this hides the content below.
No need for absolute position in this. Your example uses javascript to change the size of the div and the content inside.
You can do this with pure CSS
Example:
<html>
<body>
<section class="fullscreen"></section>
<section class="other-content></section>
</body>
</html>
html {
height: 100%;
}
body {
height: 100%;
}
.fullscrenn {
height: 100%;
width: 100%;
background: url('../images/fullscreen.jpg') no-repeat center center;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
I'm trying to position one div after another, easily achieved when the divs are positioned relatively, but I want each of these divs to fit the size of the browser.
I've very roughly achieved it using this method:
<div id="container">
<div id="test-1" style="background: url(../images/background-v2.jpg)"></div>
<div id="test-2" style="background: url(../images/background-v2.jpg)"></div>
</div>
CSS:
#test-1 {
position: absolute;
top: 0px;
left: 0px;
width: 100%;
height: 100%;
background: no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
#test-2 {
position: absolute;
top: 101%;
left: 0px;
width: 100%;
height: 100%;
background: no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
So #test-1 scales to fit the size of the browser then when you scale down #test-2 also fits the size of the browser and so on. But I've achieved these by positioning them absolutely and setting #test-2 101% from the top, which I don't want to do every time I add another div, thus the reason I'd like them positioned relatively while still retaining the scale to browser background images. If that is at all possible? Might it require jQuery?
I'm completely stumped on this one!
There was something else I couldn't figure out with these background images as well, as they're nested inside divs, when the browser size gets below about 750px in width, the background images become fixed, is there anyway they can always stay centre to the browser, basically overflowing the image off the left of the browser?
I haven't used background images as I'm not really sure what you're doing with them but here is a solution using jQuery for the sizing issue:
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
<style>
.fill-browser{
width:100%;
}
</style>
<!-- uncomment the line below to include jquery, I had to comment it out for stackoverflow -->
<!--script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script-->
<script>
$(function() {
Resize();
});
//Every resize of window
$(window).resize(function() {
Resize();
});
//Dynamically assign height
function Resize() {
// Handler for .ready() called.
var windowHeight = $(window).height() + 'px';
$('.fill-browser').css('height', windowHeight);
}
</script>
</head>
<body>
<div class="fill-browser" style="background-color:#aa0000;"></div>
<div class="fill-browser" style="background-color:#00aa00;"></div>
<div class="fill-browser" style="background-color:#0000aa;"></div>
</body>
</html>
You might also want to check out - http://srobbin.com/jquery-plugins/backstretch/
It's a jQuery plugin to easily stretch background images.