1

I got this script for detecting the size of the window -

function myFunction()
{
    var w = window.innerWidth;
    var h = window.innerHeight;

    if (w = 768) {
        document.getElementById("container").style.height = "15.155em";
    } else if (w = 1024) {
        document.getElementById("container").style.height = "25.155em";
    } else if (w = 1366) {
        document.getElementById("container").style.height = "35.155em";
    }
}
<body onload="myFunction()">
  <div id="container" style="width:50px;border:2px solid black;">
  </div>
</body>

What i would like to happen,is each time the height of the div would change,according to the size of window.

Whats happening now,is on every screen i run the website its showing me as if the screen size is the "768"

7
  • Why not use height:100% ? You need then of course set the body to height: 100% to make this work. You can also use the @media rule: w3schools.com/cssref/tryit.asp?filename=trycss3_media_example1 Commented Mar 16, 2016 at 6:50
  • its a matter of making it responsive Commented Mar 16, 2016 at 6:52
  • use css height:100%; or height:100vh;. Commented Mar 16, 2016 at 6:52
  • the @media ruleset is especially for creating responsive layouts Commented Mar 16, 2016 at 6:53
  • Jankapunkt can you give a link to some website with most usefull @media rules ? Ive checked a bunch,and nothing Commented Mar 16, 2016 at 6:54

5 Answers 5

1

You don't need javascript for this: it can be done entirely with css. You have a few options (that I can think of right now):

  1. Using the amazing flexbox classes (example)
  2. Setting height to 100% on the element itself and all parents (try the snippet below)
  3. Using the vh viewport units (1vh = 1% of viewport height)

Hope that helps!

html,
body {
  margin: 0;
  padding: 0;
  height: 100%
}
div {
  width: 100px;
  height: 100%;
  background: red;
}
<div></div>

Sign up to request clarification or add additional context in comments.

Comments

0

You can use CSS instead of using JS. Use the following CSS to fit the DIV to whatever size of the screen is.

    #container
    {
    position: absolute;
    overflow-x: hidden;
    overflow-y:auto;
    width: 100%;
    height: 100%;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    margin: auto; 
   }

Also you can use height:100vh but this Unit will not be support in older browser.

Thanks

1 Comment

Actully without the height given its not showing anything
0

because actual browser width is always lesser than screen resolution , and u are using screen resolution for comparison.

if (w = 768)
else if (w = 1024)
else if (w = 1366)

Either use

if(w<768)
//do something
else if(w<1024)
//do something
else if(w<1366)
//do something
else
//default

Or better use

screen.width
screen.height

if you want to use = as comparator.

Comments

0

It is because in a if condition to evaluate there should be == and not =. What you have used is an assignment operator and not comparision.

Please correct the code as below

function myFunction()
{
    var w = window.innerWidth;
    var h = window.innerHeight;

    if (w == 768) {
        document.getElementById("container").style.height = "15.155em";
    } else if (w == 1024) {
        document.getElementById("container").style.height = "25.155em";
    } else if (w == 1366) {
        document.getElementById("container").style.height = "35.155em";
    }
}

As other members suggested mediaQueries is a good option in case you are looking to make it responsive.

Comments

0

You can use CSS media queries like here

@media only screen and (min-width: 600px)
{
    .container
    {
        height: 15.155em;
    }
}

@media only screen and (min-width: 768px)
{
    .container
    {
        height: 25.155em;
    }
}

Comments

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.