setting div width according to the screen size of user

孤人 提交于 2019-12-10 11:17:01

问题


I am trying to set the width and height of a div exactly equal to the size of my users screen. How can it be done. Should I use jquery ? If so, how ?


回答1:


What about some CSS? http://jsfiddle.net/TJGZU/

body, html { /* set size of body to full page and remove margins */
    width: 100%;
    height: 100%;
    padding: 0;
    margin: 0;
}

div { /* set div to full width and height */
    width: 100%;
    height: 100%;
}



回答2:


by css seems ideal as pimvdb has suggested...doing it in jquery would be inline styling which would be a bad practice which can be avoided unless necessary.

by means of jquery:

    var windowWidth = $(window).width();
    var windowHeight =$(window).height();
    $('div').css({'width':windowWidth ,'height':windowHeight });

Edited Part:

var windowWidth = ((parseInt($(window).width())) / 2) - 120;

like this whatever height and width seems ideal for you, you can obtain via this.




回答3:


below is a sample code for your problem:

var windowWidth = ((parseInt($(window).width())) / 2) - 120;

Also, note that this answer makes use of jQuery.



来源:https://stackoverflow.com/questions/6762564/setting-div-width-according-to-the-screen-size-of-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!