0

I want to give my bg pic, both a height and a weight with! But it will only let me set one value! Does anybody know what to do?

$(document).ready(function() {
  function setHeight() {
    windowHeight = $(window).innerHeight();
    $('body').css('background-size', windowHeight, '100%');
  };
  setHeight();

  $(window).resize(function() {
    setHeight();
  });
});
7
  • 1
    Do it with clean js: document.body.style.cssText = 'width:100%; height:100%'; Commented Oct 9, 2015 at 13:24
  • i not intended it to be in the body there shot be styled, but a full screen div. Commented Oct 9, 2015 at 13:26
  • 1
    create a function setWidth that does the same thing but with width, and call it in the resize function? Commented Oct 9, 2015 at 13:29
  • Well it's the same thing. Select the element somehow (document.querySelector(...) maybe?) and then elem.style.cssText = 'styles here...'; Commented Oct 9, 2015 at 13:29
  • 2
    What is the desired CSS result? I find the question a bit unclear. Commented Oct 9, 2015 at 13:33

2 Answers 2

4

Put the width and height together in a string. The first value sets the width and the second value sets the height. And .innerHeight() returns just a value so you need to add px.

 $('body').css('background-size', '100% ' + windowHeight + 'px');
Sign up to request clarification or add additional context in comments.

1 Comment

No, he is missing a space between the values.
2

I can see that you want to get your background-image to fill the whole body. You can easily achieve this via css properties.

background-size: cover;

or

background-size: contain;

Here's one of many tutorials: https://css-tricks.com/perfect-full-page-background-image/

html { 
  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;
}

DEMO

2 Comments

it is for get set same a resualte like background-size: cover; but it have to work on iphone
shouldn't be a problem caniuse.com/#feat=background-img-opts just be sure to add it to the html and not to the body - appending it to the body is known to create strange behaviors

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.