使用媒体查询和Grails获取条件CSS时遇到问题

时间:2015-02-17 20:31:48

标签: css grails media-queries stylesheet stylesheet-link-tag

我使用媒体查询在Grails应用程序中导入正确的样式表(mobile.css或main.css)。 如果我用我的电脑加载页面,它工作正常,当我用我的Android智能手机加载页面时,我得到一个没有添加样式的页面。

这是我的代码:

<!DOCTYPE html>

<head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title><g:layoutTitle default="Baoole"/></title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="shortcut icon" href="${createLinkTo(dir:'images',file:'favicon.ico')}" type="image/x-icon" /> 
    <link rel="apple-touch-icon" href="${resource(dir: 'images', file: 'apple-touch-icon.png')}">
    <link rel="apple-touch-icon" sizes="114x114" href="${resource(dir: 'images', file: 'apple-touch-icon-retina.png')}">
    <g:javascript library="application"/>   

<link rel="stylesheet" media = "screen and (min-width: 480px)" href="${resource(dir: 'css', file: 'main.css')}" type="text/css" title = "main" >

<link rel="stylesheet"  media = "screen and (max-width: 480px)" href="${resource(dir: 'css', file: 'mobile.css')}" type="text/css" title = "mobile" >



    <g:layoutHead/>


    <r:layoutResources />
<langs:selector langs="it, es, en, en_US, pt_BR, pt_PT, de, da, ru, ja, cs_CZ"/>
</head>

<body class="index">
some code...
</body>
</html>

如果我颠倒了标签的顺序,如:

<link rel="stylesheet"  media = "screen and (max-width: 480px)" href="${resource(dir: 'css', file: 'mobile.css')}" type="text/css" title = "mobile" >

 <link rel="stylesheet" media = "screen and (min-width: 480px)" href="${resource(dir: 'css', file: 'main.css')}" type="text/css" title = "main" >

我的行为与之前相反。 (当我从智能手机加载时工作正常,当我从PC加载时没有风格)。

在我的main.css和mobile.css中,我有相同的ID和类

的main.css

.index {
background-image: url(../images/stadiodue.jpg);
background-repeat: no-repeat;
color: white;
margin: 0 auto;
max-width: 1350px;
overflow-x: hidden; /* prevents box-shadow causing a horizontal scrollbar in firefox when viewport < 960px wide */
-moz-box-shadow: 0 0 0.3em #255b17;
-webkit-box-shadow: 0 0 0.3em #255b17;
        box-shadow: 0 0 0.3em #255b17;
            font-family:"Trebuchet MS","Yanone Kaffeesatz","Agency FB";
        font-size: 14.5pt;}  

mobile.css

.index {
background: black;}

更新:

我认为问题不在于媒体查询的错误语法。如果我删除了mobile.css样式表的导入,只有:

<link rel="stylesheet" media = "screen and (min-width: 480px)"  href="${resource(dir: 'css', file: 'main.css')}" type="text/css" title = "main" >

在我的代码中,应用程序以正确的行为响应:从PC加载页面我得到main.css样式,从智能手机加载页面我没有任何风格。

现在,添加第二个样式表导入(用于移动样式):

<link rel="stylesheet"  media = "screen and (max-width: 480px)"  href="${resource(dir: 'css', file: 'mobile.css')}" type="text/css" title = "mobile" >

所以:

<link rel="stylesheet" media = "screen and (min-width: 480px)"  href="${resource(dir: 'css', file: 'main.css')}" type="text/css" title = "main" >

<link rel="stylesheet"  media = "screen and (max-width: 480px)"  href="${resource(dir: 'css', file: 'mobile.css')}" type="text/css" title = "mobile" >

nothings 更改。从移动设备加载的页面仍然没有添加任何样式。

我已经尝试过:=(

1)这个不同的媒体查询sintax:     media =&#34; only screen and(max-device-width:480px)&#34;

2)媒体查询直接在样式表中编写的规则。

我无法弄明白!也许可能是Grails的行为?

提前致谢!

2 个答案:

答案 0 :(得分:0)

可能取决于导入CSS的语法,如果查看this示例,加载样式表的媒体规则应该看起来像<link rel="stylesheet" media="(max-width: 800px)" href="example.css" />


或者尝试直接在样式表中加载媒体查询规则, 适用于手机款式:

 @media screen and (max-width: 480px) {
    .index {
        /*all your prop*/
    }
}

和主要:

  @media screen and (min-width: 480px) {
    .index {
        /*all your prop*/
    }
}

答案 1 :(得分:0)

而不是

<link rel="stylesheet"  media = "screen and (max-width: 480px)" href="${resource(dir: 'css', file: 'mobile.css')}" type="text/css" title = "mobile" >

使用像这样:

<link rel="stylesheet"  media = "only screen and (max-device-width: 480px)" href="${resource(dir: 'css', file: 'mobile.css')}" type="text/css" title = "mobile" >