javascript - 向 CSS 媒体查询添加重要信息
问题描述
我有一个带有内联 css 和 css 媒体查询的 html 代码在运行内联 css 时不会被媒体查询取代。我需要找到一种方法来动态地在媒体查询中的每种样式中添加那些 !important(可能与 js 一起使用)。除了以字符串形式遍历 html 内容并找到样式并添加它之外,还有其他方法可以做到这一点。我添加了一个示例代码供参考
body {
background-color: yellow;
}
@media only screen and (max-width: 600px) {
body {
background-color: lightblue;
}
}
<body style="background-color:red;">
</body>
解决方案
从body
标签中删除内联 css,因为内联 css 具有高特异性值
试试这个,在媒体查询中添加重要的内容,因为当屏幕大小改变时你需要lightblue
背景颜色
body {
background-color: yellow;
}
@media only screen and (max-width: 600px) {
body {
background-color: lightblue;
}
}
推荐阅读
- java - 为什么 processing3 禁止 size() 的变量参数?
- excel - 有条件地格式化 VBA 中的单元格
- enums - 使用 Rust 宏转换和拆分由 bindgen 生成的 C 样式枚举
- bash - 如何通过 bash 脚本连接到 ftp 服务器?
- python - 完成工作时的无限池进程,在 python 中,函数应用于一系列 pandas.DataFrame
- django - 如何在 django 中从一页转到另一页
- google-cloud-platform - 在 Google Cloud 中创建 OAuth 客户端 ID 的权限
- numpy - 用矩阵广播张量除法
- python - 使用 sqlite3 将值列表添加为列
- linux - 当信号被挂起时,内核是否调用了所有信号处理程序?