CSS get height of screen resolution

后端 未结 9 1246
后悔当初
后悔当初 2020-12-02 12:10

I\'m having a hard time getting the height of lower screen resolution because my screen resolution is 1920x1080.

Does anyone know how to get height and width of the

相关标签:
9条回答
  • 2020-12-02 12:39

    You actually don't need the screen resolution, what you want is the browser's dimensions because in many cases the the browser is windowed, and even in maximized size the browser won't take 100% of the screen.

    what you want is View-port-height and View-port-width:

    <div style="height: 50vh;width: 25vw"></div>
    

    this will render a div with 50% of the inner browser's height and 25% of its width.

    (to be honest this answer was part of what @Hendrik_Eichler wanted to say, but he only gave a link and didn't address the issue directly)

    0 讨论(0)
  • 2020-12-02 12:41

    You could use viewport-percentage lenghts.

    See: http://stanhub.com/how-to-make-div-element-100-height-of-browser-window-using-css-only/

    It works like this:

    .element{
        height: 100vh; /* For 100% screen height */
        width:  100vw; /* For 100% screen width */
    }
    

    More info also available through Mozilla Developer Network and W3C.

    0 讨论(0)
  • 2020-12-02 12:41

    Use height and width in percentage.

    For example:

    width: 80%;
    height: 80%;
    
    0 讨论(0)
  • 2020-12-02 12:45

    In order to get screen resolution you can also use jquery. This link help you very much to resolve.

    0 讨论(0)
  • 2020-12-02 12:48

    Adding to @Hendrik Eichler Answer, the n vh uses n% of the viewport's initial containing block.

    .element{
        height: 50vh; /* Would mean 50% of Viewport height */
        width:  75vw; /* Would mean 75% of Viewport width*/
    }
    

    Also, the viewport height is for devices of any resolution, the view port height, width is one of the best ways (similar to css design using % values but basing it on the device's view port height and width)

    vh
    Equal to 1% of the height of the viewport's initial containing block.

    vw
    Equal to 1% of the width of the viewport's initial containing block.

    vi
    Equal to 1% of the size of the initial containing block, in the direction of the root element’s inline axis.

    vb
    Equal to 1% of the size of the initial containing block, in the direction of the root element’s block axis.

    vmin
    Equal to the smaller of vw and vh.

    vmax
    Equal to the larger of vw and vh.

    Ref: https://developer.mozilla.org/en-US/docs/Web/CSS/length#Viewport-percentage_lengths

    0 讨论(0)
  • 2020-12-02 12:50

    It is not possible to get the height of the screen from CSS. However, using since CSS3 you can use media queries to control the display of the template as per the resolution.

    If you want to code on the basis of height using media queries, you can define style-sheet and call it like this.

    <link rel="stylesheet" media="screen and (device-height: 600px)" />
    
    0 讨论(0)
提交回复
热议问题