新手问题 Rails 中如何更改 Bootstrap 默认样式 & Rails 中 stylesheets 优先级问题

skgod · 2016年09月24日 · 最后由 skgod 回复于 2016年09月26日 · 2747 次阅读

这个问题可以通过——仔细阅读“说明书”解决。

下面这是 application.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, vendor/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 .(stylesheets下所有scss文件会被挂进来)
 *= require_self
 *= require font-awesome
 *= require trix
 */

@import "bootstrap-sprockets";
@import "bootstrap";
@import "lightbox-bootstrap";

重点:

  • Rails 中优先级 : !important > 页面中的行内样式 > application.scss > stylesheets 文件下其他 SCSS 文件
  • 你可以添加全局样式到本文件,这些样式会出现在编译页面底部,因而比本目录下其他 scss 文件具有更高优先级
  • 行内样式 >ID 选择器 > 类选择器 > 标记选择器。style 属性将覆盖任何全局的样式设定,例如在 标签或在外部样式表中规定的样式(包括 id,class)。 <p>菜鸟帮菜鸟,社会才会更和谐~ 欢迎拍砖、补充。</p>

后面的会覆盖前面的

*= require bootstrap
*= require self

当前 css 文件里的样式表会覆盖 bootstrap 中定义的

#1 楼 @mingyuan0715 谢谢补充 我又查了下 require tree 和 require self 区别 贴过来了

需要 登录 后方可回复, 如果你还没有账号请 注册新账号