html - Sass mixin 中的单元未按预期工作
问题描述
我写了这个mixin,我真的不确定为什么它没有按预期工作。
@mixin responsive($breakpoint){
@if $breakpoint == xs {
@media only screen and (max-width:35.99875em ) {
@content
}
}
@if $breakpoint == sm {
@media only screen and (min-width:35.99876em) and (max-width:47.99875em) {
@content
}
}
@if $breakpoint == md {
@media only screen and (min-width:47.99876em) and (max-width:61.99875em) {
@content
}
}
@if $breakpoint == lg {
@media only screen and (min-width:61.99876em) and (max-width:74.99875em) {
@content
}
}
@if $breakpoint == xl {
@media only screen and (min-width:74.99876em) and (max-width:85.37375em) {
@content
}
}
@if $breakpoint == xxl {
@media only screen and (min-width:85.37376em) {
@content
}
}
}
这里一切似乎都很好,但在 VSC 中,一些“ems”没有突出显示,那些没有突出显示的“ems”工作不正常。(见图1)
当我编译 sass 时,它没有显示任何错误,但即使在 CSS 代码中,“ems”仍然是灰色的。
下面的代码是 mixin 的示例用法。
body {
@include responsive(xxl){
background: rgb(147, 48, 228);
}
@include responsive(xl){
background: rgb(44, 255, 150);
}
@include responsive(lg){
background: rgb(255, 245, 100);
}
@include responsive(md){
background: rgb(255, 146, 146);
}
@include responsive(sm){
background: rgb(145, 145, 145);
}
@include responsive(xs){
background: #000;
}
}
下面的代码是在 CSS 文件中编译的 sass。
@media only screen and (min-width: 85.37376em) {
body {
background: #9330e4; } }
@media only screen and (min-width: 74.99876em) and (max-width: 85.37375em) {
body {
background: #2cff96; } }
@media only screen and (min-width: 61.99876em) and (max-width: 74.99875em) {
body {
background: #fff564; } }
@media only screen and (min-width: 47.99876em) and (max-width: 61.99875em) {
body {
background: #ff9292; } }
@media only screen and (min-width: 35.99876em) and (max-width: 47.99875em) {
body {
background: #919191; } }
@media only screen and (max-width: 35.99875em) {
body {
background: #000; } }
起初我以为那些“em”突出显示只是在 VSC 中,但是当我打开浏览器并查看调试器时,我得到了相同的结果,仍然没有按预期突出显示那些“em”。(见图3)
现在,当我评论未突出显示的“ems”并在 CSS 文件中重写相同的内容时,它可以正常工作。(见图4)
这是一个使用 sass 编译的 CSS 文件的示例。(见图 1)
这是一个从一开始就用 CSS 编写的具有相同断点和值的示例。(见图 2)
查看发布的问题,似乎那些不起作用的“ems”也没有在堆栈溢出中突出显示。
我忘了提到重写相同的断点可以解决问题,但是谁能解释为什么会发生这种情况?
解决方案
您的 SASS 代码中有一些数值和“em”之间的(流行方向格式)字符。这样的值85.37376em
实际上在您的代码中为85.37376‬em
.
请在 Chrome 的开发工具中检查生成的 CSS。我附上了截图。
当您在每个内容规则之后添加分号时,VSCode 中的语法突出显示会得到进一步纠正: @content;
推荐阅读
- javascript - Ajax 请求返回 200 OK,但触发了错误事件而不是成功 || xlswriter
- jboss - 无法将 postgres 与 keycloak 一起使用
- reactjs - Oidc 客户端使用用户名和密码登录
- sql - 基于数据中不连续的日期的多天变化
- javascript - 如何测试我的 React 组件渲染另一个?
- kotlin - Kotlin 中流的等价物
- here-api - 没有国家信息的 Here Maps API 自动建议
- java - 从 Firestore 获取数据后,字符串未添加到 OnClick 侦听器中的 ArrayList
- vue.js - Vue Uncaught TypeError:_c 不是函数
- codeigniter - 在codeigniter查询的where子句中检查值为空白