css根据纵向或横向屏幕尺寸扩展?
我有两个并排浮动的 div。我想要的是当你在纵向模式下查看它时,它的宽度为 100 像素,横向模式下为 200 像素。这实际上发生在移动设备上查看。
因此,div 在横向模式下会扩大,在纵向模式下会缩小一点。
有什么想法吗?
I have two divs that are floating next to each other. What i would like is to have it have a width of 100px when you are looking at it in portrait mode and lets say 200 px in landscape. This happens viewing on a mobile device actually.
So the div will expand when in landscape mode and shrink up a bit in portrait.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这对于 CSS2 来说是不可能的,但是使用 Javascript 可以做你想做的事情(读出屏幕尺寸等)。
我不确定您正在研究什么技术,但 CSS3 通过 CSS3 Media 提供了您想要的东西查询。
使用 CSS3,你可以得到像这样有趣的东西(或者你甚至可以指定宽度,例如 200px):
查看 此示例页面了解更多说明,或这个。
编辑只是因为我今天发现了这个页面:Hardbroiled CSS3 媒体查询 - 写得很好。
Well this is not possible with CSS2, but it would be possible to do what you want with Javascript (read out the screen size etc.).
I'm not sure what technology you are looking into, but CSS3 provides exactly what you want with CSS3 Media Queries.
With CSS3 you have fun stuff like this (or you could even specify width e.g. 200px):
Check out this example page for more explanation, or this one.
EDIT Just because I found this page today: Hardbroiled CSS3 Media Queries - very good writeup.
您可以使用 css 媒体功能“方向”来做到这一点。这样您就可以根据屏幕方向指定样式,与屏幕尺寸无关。您可以在此处找到有关此媒体功能的 w3.org 官方定义。结合 developer.mozilla.org 这将解释它是如何工作的。
引用 w3.org 关于“方向”媒体功能的内容:
来自developer.mozilla.org的关于“方向”功能的注释/引用:
因此,重申一下,实际上并不是屏幕方向触发纵向或横向媒体查询。然而它是屏幕的高度和宽度之间的比例!因此,在非移动/触觉设备上使用“方向功能”也是有意义的,因此我添加了一个小演示来展示这些媒体查询的行为。
JSFIDDLE DEMO (尝试调整视口大小)
以下是影响纵向和横向方向的代表性媒体查询。
You can do this by using the css media feature "orientation". This way you can specify styles depending on screen orientation, unrelated from screen size. You can find the official w3.org definition about this media feature here. Combined with the specifications on developer.mozilla.org this will explain how it works.
Quote from w3.org about the ‘orientation’ media feature:
A note/quote from developer.mozilla.org about the "orientation" feature:
So to reiterate, it is not actually the screen orientation that triggers portrait or landscape media queries. However it is the ratio between height and width of the screen! Because of this it also makes sense to use the "orientation feature" with non mobile/tactile devices hence I've added a small demo to show the behaviour of these media queries.
JSFIDDLE DEMO (try resizing the view port)
Here are the representative media queries affecting portrait and landscape orientation.