【发布时间】:2017-06-12 04:17:17
【问题描述】:
我在我的控制器 scss 文件中遇到了一个问题,我必须不断地用 !important 标记所有内容才能设置元素的样式。这是我的 Map 控制器 scss 中的示例输入元素:
.search-field {
width: 300px !important;
box-shadow: none !important;
border-bottom: none !important;
display: inline-block;
font-size: 16px;
margin-left: 15px !important;
}
这是因为在application.scss内部,我导入了我的材质库,它优先于map.scss:
/*
* This is a manifest file that'll be compiled into application.css, which will include all the files
* listed below.
*
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, or any plugin's
* vendor/assets/stylesheets directory can be referenced here using a relative path.
*
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
* compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
* files in this directory. Styles in this file should be added after the last require_* statement.
* It is generally better to create a new file per style scope.
*
*= require_tree .
*= require_self
*/
@import "materialize";
@import "font-awesome";
* {
font-family: 'Raleway', sans-serif;
}
html {
width: 100%;
height: 100%;
}
body {
margin: 0;
min-width: 100%;
min-height: 100%;
background-color: #1A1A1A;
}
如何避免在任何地方都使用!important 标志?
编辑:
我也尝试了以下(没有运气)
在application.html.erb 内,我添加了一个指向我的控制器 css 的附加链接
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= stylesheet_link_tag 'map', media: 'all', 'data-turbolinks-track': 'reload' %>
然后在 application.scss 中删除了 require_tree . 的使用
但是,当我重新启动服务器时,我仍然需要 !important 标记。还是没有运气
【问题讨论】:
-
在你的其他人下面添加
@import "map";怎么样(+重新启动)? -
Rails 资源按顺序加载,因此您可以通过更改布局中样式标签的顺序来设置优先级。
-
您能使用更具体的选择器吗?
-
@bkunzi01 不知道这对我有什么帮助,因为我使用了一个语句:
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> -
您可以在布局文件的头部单独调用这些文件,而不是将它们包含在您的
application.scss中。在您的application.scss脚本标记之前调用它们,以确保它优先于这些导入
标签: ruby-on-rails sass