如何找到屏幕宽度并将其应用于特定的CSS [英] How to find the screen width and apply it to a particular css

查看:114
本文介绍了如何找到屏幕宽度并将其应用于特定的CSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到用户屏幕宽度,并需要将此宽度应用到样式。

I need to find the user screen width and needs to apply this width to a style.

我有一个容器

<div id="container">

</div>

我需要使用JavaScript或任何其他选项将宽度放入css。

I need to put the width into css using JavaScript or any other options.

我如何做到这一点?
提前获得帮助。

How can I do this?? Thanks for help in advance.

推荐答案

要获得屏幕分辨率,请使用 code> object:

To get the screen resolution use screen object:

document.getElementById("container").style.width = screen.width + "px";

要获得浏览器窗口的宽度(适用于IE6及以下版本以外的所有浏览器, href =http://andylangton.co.uk/articles/javascript/get-viewport-size-javascript/ =nofollow>此跨浏览器方法)使用:

To get the width of you browser window (for all browsers except IE6 and below, otherwise check this cross-browser method) use:

document.getElementById("container").style.width = window.innerWidth + "px";

此外,请注意,只有当文档完全加载时才应调用脚本。

Also, pay attention that the script should be called only when the document is fully loaded.

演示: http://jsfiddle.net / DNPNz /

这篇关于如何找到屏幕宽度并将其应用于特定的CSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆