CSS: How to set the width and height dependent on the screen/window size?


Tags: javascript,html,css,twitter-bootstrap,screen-size

Problem :

Following up this question here I am trying to get a good rendering of a floating 3D cube using three.js. The camera proportions are window.innerWidth / window.innerHeight and since I am using a rectangular div container, I get a flattened cube. Something like this.

I tried to get my div container to be in proportion with my screen or window size and it worked! For example for a 1367x768 screen:

CSS:

#render {
    width: 450px;
    height: 250px;
}

But! I want to have this worked out also for different size, e.g. for smartphones too! It would be perfect to make this fully responsive. Is it possible to get the width and height fully proportional to the screen size in CSS?



Solution :

There are units displaying elements depending to the screen : vh and vw

vh : means viewport hight.

ie : height:50vh; will take 50% height of the screen ( and not of the parent element ).

vw : means viewport width.

ex : width:50vw; will take 50% width of the screen ( and not of the parent element ).


    CSS Howto..

    How to align 2 divs inside one div container with CSS (see image)

    How to replace with a
    filled with CSS?

    How can I align unsorted list horizontly ?

    How to Make Image Do All 3: Cover (left-to-right), Resize, and Scroll

    nth-of-type css, how to setup it properly

    How to change the font-size of the part of textarea text in HTML,CSS?

    How to create circles around a circle with css, javascript?

    Css radio buttons problem - How to style each radio button?

    How to write css for alt attribute of img tag (word-break: break-all property doesn't work in IE)

    how to scroll text area horizontally?

    How to prevent HTML element moving when resizing window?

    How to use multiple CSS locators as a selenium locator?

    How can I set CSS only for specific IE browsers?

    How to add tabs or spaces to a textline with CSS?

    How does tablesort.js create the sort indicator arrows

    how to display div one by one using css

    How to hide a nested table?

    how to make an image “coming” to foreground

    How do I apply css to only IE 8 and below

    slideshow banner without using flash

    CSS - How to force elements to 100% of remaining/available space of parent element without extending beyond it?

    How can I make this a horizontal list?

    How can I take pseudo:after { content: “Value”; } value dynamically in this snippet?

    How to style this form using CSS?

    How to locate element inside css footer

    How to change menu from on hover to on click

    How to get this hover effect with just CSS

    how to position nested list items?

    how can i add a css file to body of a page?

    How can I create HTML/CSS responsive tile?