Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python cssutils parseString无法进行媒体查询。-错误CSSStyleDeclaration:意外标记_Python_Css_Media Queries - Fatal编程技术网

Python cssutils parseString无法进行媒体查询。-错误CSSStyleDeclaration:意外标记

Python cssutils parseString无法进行媒体查询。-错误CSSStyleDeclaration:意外标记,python,css,media-queries,Python,Css,Media Queries,我无法使用cssutils.parseString解析包含媒体查询的CSS字符串 在中,它明确表示支持媒体查询 如何使用cssutils >>> from cssutils import parseString >>> css = '.font-size-24-r { font-size: 24px;\n\n@media only screen and (max-width: 720px) {\n\tfont-size: 21.3px;\n}' >>

我无法使用
cssutils.parseString
解析包含媒体查询的CSS字符串

在中,它明确表示支持媒体查询

如何使用
cssutils

>>> from cssutils import parseString
>>> css = '.font-size-24-r { font-size: 24px;\n\n@media only screen and (max-width: 720px) {\n\tfont-size: 21.3px;\n}'
>>> ps = parseString(css)
ERROR   CSSStyleDeclaration: Unexpected token, ignoring upto '@media only screen and (max-width: 720px) {\n\tfont-size: 21.3px;\n}'. [3:1: @media]

问题不是我使用cssutils的方式。相反,问题是我使用了无效的CSS

来自
cssutils
的错误消息有点不尽如人意,尽管它通常表示问题在媒体查询附近的CSS中。它没有说明为什么只是
意外的标记
。原因是
@media
查询不能嵌套在选择器块中

无效CSS-由于在
.font-size-48-s
中嵌套媒体查询

.font-size-48-s {
    font-size: 3em;

    @media only screen and (max-width: 45em) {
        font-size: 2.6667em
    }
    @media only screen and (max-width: 30em) {
        font-size: 2.4em
    }
}
有效CSS-
cssutils.parseString
现在生成预期结果

.font-size-48-s { font-size: 3em }

@media only screen and (max-width: 45em) {
    .font-size-48-s { font-size: 2.6667em }
}

@media only screen and (max-width: 30em) {
    .font-size-48-s { font-size: 2.4em }
}