关于CSS媒体查询的问题
我有一个关于CSS媒体查询的问题。
我正在使用desktop.css和ipad.css实现一个HTML页面,并希望它们位于2个单独的文件中。 在desktop.css中,我使用@import url("ipad.css");并且不仅添加@media屏幕和具有实际桌面样式的(设备宽度:768px)块。
所以desktop.css看起来像
@import url("ipad.css");
@media not only screen and (device-width:768px)
{
//Desktop styles
}
现在,虽然iPad CSS在iPad上正确应用,但由于某种原因桌面的CSS没有得到应用.. 不确定“@media 不仅是屏幕......”出了什么问题,
我提到了 http:// www.w3.org/TR/css3-mediaqueries
上面写着“媒体查询开头出现关键字‘not’会否定结果。即,如果媒体查询为真而没有 ' not' 关键字会变成 false,反之反之亦然。”
但由于某种原因,桌面永远不会呈现样式...请帮忙..谢谢...
*******已编辑**
使用这种方法可能会出现什么问题?
@import url("desktop.css") screen;
@import url("ipad.css") only screen and (device-width:768px);
I have a question on CSS media query..
I am implementing a HTML page using desktop.css and ipad.css and want them in 2 separate file..
In desktop.css, I use @import url("ipad.css"); and also add @media not only screen and (device-width:768px) block which has the actual desktop styles..
So desktop.css looks like
@import url("ipad.css");
@media not only screen and (device-width:768px)
{
//Desktop styles
}
Now while the iPad CSS gets applied correctly on the iPad, for some reason the CSS for desktop does not get applied..
Not sure what is wrong with the "@media not only screen ..."
I referred http://www.w3.org/TR/css3-mediaqueries
and it says " The presence of the keyword ‘not’ at the beginning of the media query negates the result. I.e., if the media query had been true without the ‘not’ keyword it will become false, and vice versa. "
But for some reason, the desktop never renders the styles...Please help..Thank you..
*******EDITED**
What might be the issue with using this approach ?
@import url("desktop.css") screen;
@import url("ipad.css") only screen and (device-width:768px);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下链接可能会有所帮助。
developer.mozilla.org 上的媒体查询
css3.info 上的媒体查询
Following link could be helpfull.
media-queries at developer.mozilla.org
media-queries at css3.info
您是否尝试过类似
@media only screen and (device-width: 768px) {
/* 这里是桌面样式 */
因此
,丢失
not
,因为这就是使规则适用于除屏幕(即您的情况下的桌面)之外的所有内容的原因。Have you tried something like
@media only screen and (device-width: 768px) {
/* desktop styles here */
}
So, lose the
not
as that is what's making the rule apply to everything but the screen i.e. the desktop in your case.only screen
意味着只有 screen 而没有其他东西来渲染它。not
否定了这一点,以便除screen
之外的所有内容都将渲染它。only screen
means that just screen and nothing else renders this. Thenot
negates this so that everything butscreen
will render it.可以这样操作:
desktop.css 开头没有任何 @ 媒体查询规则,ipad.css 开头为
@media screen 和 (max-width: 768px) {/*您的代码在此处*/}
规则。或者像平常一样链接到两者,将 ipad.css 放在 Desktop.css 下方。
您还可以在没有任何@ media规则的情况下启动desktop.css文件,而ipad.css则以
@media屏幕和(max-width: 768px) {/*您的代码在此处*/}
规则开始。这样,所有桌面浏览器都会读取desktop.css文件,支持媒体查询的浏览器也会读取ipad.css(当屏幕分辨率低于768px时)。
如果您按照您在编辑中描述的方式使用它们,IE8 及以下版本会将其弄乱,本文对此进行了很好的解释 => http://dev.opera.com/articles/view/safe-media- >
如果您希望 IE 也重新调整大小并理解您的媒体查询,解决问题的唯一方法就是使用这个轻量级 JavaScript 库 = http://filamentgroup.com/lab/respondjs_fast_css3_media_queries_for_internet_explorer_6_8_and_more/ 这将为您带来魔力(基本上使与 IE 一起工作成为可能,就像使用普通浏览器一样):)
问题是你只能让 IE 忽略媒体查询,这样它就不会弄乱你的网站外观。为了使 IE 理解媒体查询并调整大小并适应窗口大小的变化,您需要我提到的库的帮助。
Do it either this way:
Where desktop.css starts without any @ media query rules, and ipad.css starts with
@media screen and (max-width: 768px) {/*your code here*/}
rule.Or simply link to both like would you normally do, having ipad.css below desktop.css.
Where also you start the desktop.css file without any @ media rules and ipad.css starts with a
@media screen and (max-width: 768px) {/*your code here*/}
rule.This way, all desktop browsers will read the desktop.css file, and browsers that support media queries will also read the ipad.css (when the screen resolution is below 768px).
If you use them as you describe in your edit, IE8 and below will make a mess of it, it is well explained in this article => http://dev.opera.com/articles/view/safe-media-queries/
And if you want IE to also re-size and understand your media queries, the only way to solve the problem is by using this lightweight JavaScript library => http://filamentgroup.com/lab/respondjs_fast_css3_media_queries_for_internet_explorer_6_8_and_more/ that will do the magic for you (and basically make possible to work wit IE like with a normal browser) :)
The problem is that you can only make IE ignore the media queries so that that it wont mess up your sites look. To make IE understand media queries and re-size and adept with changes in window size you need help from the library I mention.