Home Web Front-end HTML Tutorial Sass@规则_html/css_WEB-ITnose

Sass@规则_html/css_WEB-ITnose

Jun 24, 2016 am 11:32 AM

@import
Sass 支持所有 CSS3 的 @ 规则, 以及一些 Sass 专属的规则,也被称为“指令(directives)”。
Sass 扩展了 CSS 的 @import 规则,让它能够引入 SCSS Sass 文件。 所有引入的 SCSS Sass 文件都会被合并并输出一个单一的 CSS 文件。 另外,被导入的文件中所定义的变量 mixins 都可以在主文件中使用。
Sass 会在当前目录下寻找其他 Sass 文件, 如果是 Rack、Rails 或 Merb 环境中则是 Sass 文件目录。 也可以通过 :load_paths 选项或者在命令行中使用 --load-path 选项来指定额外的搜索目录。
@import 根据文件名引入。 默认情况下,它会寻找 Sass 文件并直接引入, 但是,在少数几种情况下,它会被编译成 CSS 的 @import 规则:

  • 如果文件的扩展名是 .css。
  • 如果文件名以 http:// 开头。
  • 如果文件名是 url()。
  • 如果 @import 包含了任何媒体查询(media queries)。
  • 如果上述情况都没有出现,并且扩展名是 .scss .sass, 该名称的 Sass 或 SCSS 文件就会被引入。 如果没有扩展名, Sass 将试着找出具有 .scss 或 .sass 扩展名的同名文件并将其引入。
    例如:

    1 @import "foo.scss";
    Copy after login

    @import "foo";
    Copy after login

    两者都将引入 foo.scss 文件, 而

    @import "foo.css";@import "foo" screen;@import "http://foo.com/bar";@import url(foo);
    Copy after login
    Copy after login

    将被编译为:

    @import "foo.css";@import "foo" screen;@import "http://foo.com/bar";@import url(foo);
    Copy after login
    Copy after login

    也可以通过一个 @import 引入多个文件。例如:

    @import "rounded-corners", "text-shadow";
    Copy after login

    将引入 rounded-corners 和 text-shadow 两个文件。

    有一个 SCSS Sass 文件需要引入, 但是你又不希望它被编译为一个 CSS 文件, 这时,你就可以在文件名前面加一个下划线,就能避免被编译。 这将告诉 Sass 不要把它编译成 CSS 文件。 然后,你就可以像往常一样引入这个文件了,而且还可以省略掉文件名前面的下划线。

    例如,你有一个文件叫做 _colors.scss。 这样就不会生成 _colors.css 文件了, 而且你还可以这样做:

    @import "colors";//不用加下划线
    Copy after login

    来引入 _colors.scss 文件。

    注意:在同一个目录不能同时存在带下划线和不带下划线的同名文件。 例如, _colors.scss 不能与 colors.scss 并存

    嵌套 @import

    虽然大部分时间只需在顶层文件使用 @import 就行了, 但是,你还可以把他们包含在 CSS 规则 和 @media 规则中。

    假设要引入的样式文件`example.scss`文件中包含这样的代码:

    .example { color: red;}
    Copy after login

    然后这样引用:

    #main { @import "example";}
    Copy after login

    编译出来的 CSS:

    #main .example { color: red;}
    Copy after login

    @media
    Sass 中的 @media 指令和 CSS 的使用规则一样的简单,但它有另外一个功能,可以嵌套在 CSS 规则中。有点类似 JS 的冒泡功能一样,如果在样式中使用 @media 指令,它将冒泡到外面。来看一个简单示例:

    1 .sidebar {2  width: 300px;3  @media screen and (orientation: landscape) {4  width: 500px;5     }6 }
    Copy after login

    编译出来:

    1 .sidebar {2  width: 300px; 3 }4 @media screen and (orientation: landscape) {5  .sidebar {6  width: 500px;7     }8 }
    Copy after login

    @media 也可以嵌套 @media:

    1 @media screen {2  .sidebar {3  @media (orientation: landscape) {4  width: 500px;5         }6  }7 }
    Copy after login

    此时编译出来:

    1 @media screen and (orientation: landscape) {2  .sidebar {3  width: 500px; 4     } 5 }
    Copy after login

    在使用 @media 时,还可以使用插件#{}:

    1 $media: screen;2 $feature: -webkit-min-device-pixel-ratio;3 $value: 1.5;4 @media #{$media} and ($feature: $value) {5  .sidebar {6  width: 500px;7     }8 }
    Copy after login

    编译出来的 CSS:

    1 @media screen and (-webkit-min-device-pixel-ratio: 1.5) {2  .sidebar {3  width: 500px;4     }5 }
    Copy after login

    @extend
    Sass 中的 @extend 是用来扩展选择器占位符。比如:

     1 .error { 2  border: 1px #f00; 3 } 4 .error.intrusion { 5  background-image: url("/image/hacked.png"); 6 } 7 .seriousError { 8  @extend .error; 9  border-width: 3px;10 }
    Copy after login

    被编译为:

    1 .error, .seriousError {2  border: 1px #f00;3 }4 .error.intrusion, .seriousError.intrusion {5  background-image: url("/image/hacked.png"); 6 }7 .seriousError {8  border-width: 3px; 9 }
    Copy after login

    扩展选择器:

    @extend 不止扩展类选择器,还可以扩展任何选择器,比如 .special.cool, a:hover, 或 a.user[href^=“http://“],例如:

    1 .hoverlink {2  @extend a:hover;3 }4 a:hover {5  text-decoration: underline;6 }
    Copy after login

    编译出来:

    1 a:hover, .hoverlink {2  text-decoration: underline;3 }
    Copy after login

    再来看一个复杂点的:

    1 .hoverlink {2  @extend a:hover;3 }4 .comment a.user:hover {5  font-weight: bold;6 }
    Copy after login

    编译出来的CSS:

    1 .comment a.user:hover, .comment .user.hoverlink {2  font-weight: bold;3 }
    Copy after login

    多个扩展

    所设某个样式要继承多个地方的样式,那么可以使用 @extend 来继承多个选择器或占位符的样式

     1 .error { 2  border: 1px #f00; 3  background-color: #fdd; 4 } 5 .attention { 6  font-size: 3em; 7  background-color: #ff0; 8 } 9 .seriousError {10  @extend .error;11  @extend .attention;12  border-width: 3px;13 }
    Copy after login

    编译出来的CSS:

     1 .error, .seriousError { 2  border: 1px #f00; 3  background-color: #fdd; 4 } 5 .attention, .seriousError { 6  font-size: 3em; 7  background-color: #ff0; 8 } 9 .seriousError {10  border-width: 3px;11 }
    Copy after login

    扩展单一选择器

    我们知道 %placeholder 不使用@extend显示调用是不会生成任何样式代码。那么在选择器中使用占位符一样。比如下面的代码:

    1 #context a%extreme {2  color: blue;3  font-weight: bold;4  font-size: 2em;5 }
    Copy after login

    这段代码在不调用之前不产生任何代码,只有能过@extend调用之后才生成代码:

    1 .notice {2  @extend %extreme;3 }
    Copy after login

    编译出来的CSS:

    1 #context a.notice {2  color: blue;3  font-weight: bold;4  font-size: 2em;5 }
    Copy after login

    @at-root
    @at-root 从字面上解释就是跳出根元素。当你选择器嵌套多层之后,想让某个选择器跳出,此时就可以使用 @at-root。来看一个简单的示例:

     1 .a { 2  color: red; 3  .b { 4  color: orange; 5  .c { 6  color: yellow; 7  @at-root .d { 8  color: green; 9             }10  }11  } 12 }
    Copy after login

    编译出来的CSS

     1 .a { 2  color: red; 3 } 4 .a .b { 5  color: orange; 6 } 7 .a .b .c { 8  color: yellow; 9 }10 .d {11  color: green;12 }
    Copy after login

    @debug
    @debug 在 Sass 中是用来调试的,当你的在 Sass 的源码中使用了 @debug 指令之后,Sass 代码在编译出错时,在命令终端会输出你设置的提示 Bug:

    @debug 10em + 12em;
    Copy after login

    会输出:

    Line 1 DEBUG: 22em
    Copy after login

    @warn
    @warn @debug 功能类似,用来帮助我们更好的调试 Sass。如:

     1 @mixin adjust-location($x, $y) { 2  @if unitless($x) { 3  @warn "Assuming #{$x} to be in pixels"; 4  $x: 1px * $x; 5  } 6  @if unitless($y) { 7  @warn "Assuming #{$y} to be in pixels"; 8  $y: 1px * $y; 9  }10  position: relative; left: $x; top: $y;11 }
    Copy after login

     1 @mixin adjust-location($x, $y) { 2  @if unitless($x) {//unitless是内置函数,判断数值是否有“单位” 3  @warn "Assuming #{$x} to be in pixels"; 4  $x: 1px * $x; 5  } 6  @if unitless($y) { 7  @warn "Assuming #{$y} to be in pixels"; 8  $y: 1px * $y; 9  }10  position: relative; left: $x; top: $y;11 }12 .botton{13  @include adjust-location(20px, 30);14 }
    Copy after login

    编译出来的CSS:

    1 .botton {2  position: relative;3  left: 20px;4  top: 30px;5 }
    Copy after login

    @error

    @error 和 @warn、@debug 功能是如出一辙。

     1 @mixin error($x){ 2  @if $x < 10 { 3  width: $x * 10px; 4     }@else if $x == 10 { 5  width: $x; 6     }@else { 7  @error "你需要将#{$x}值设置在10以内的数"; 8  } 9 }10 .test {11  @include error(15);12 }
    Copy after login

    编译的时候:

    你需要将15值设置在10以内的数 on line 7 at column 5

     

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Repo: How To Revive Teammates
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Hello Kitty Island Adventure: How To Get Giant Seeds
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Mar 04, 2025 pm 12:32 PM

    The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

    How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

    The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

    What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

    Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

    How to efficiently add stroke effects to PNG images on web pages? How to efficiently add stroke effects to PNG images on web pages? Mar 04, 2025 pm 02:39 PM

    This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

    What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

    The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

    What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

    The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

    How do I use the HTML5 <time> element to represent dates and times semantically? How do I use the HTML5 <time> element to represent dates and times semantically? Mar 12, 2025 pm 04:05 PM

    This article explains the HTML5 &lt;time&gt; element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

    What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

    The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

    See all articles