Home Web Front-end HTML Tutorial Sass 基础(三)_html/css_WEB-ITnose

Sass 基础(三)_html/css_WEB-ITnose

Jun 24, 2016 am 11:24 AM

扩展/继承
    继承对于了解css 的同学来说一点都不陌生,先来看一张图
    在Sass 中也具有继承一说,也就是继承类中的样式代码块,在Sass中时通过关键词“@extend”来
    继承已经存在的类样式块,从而实现代码的继承。
  //SCSS
      .btn{
          border:1px solid #ccc;
          padding:6px 10px;
          font-size:14px;
      }
    .btn-primarg{
        background-color:#f36;
        color:#fff;
        @extend .btn;
      }
    .btn-second{
        background-color:organge;
        color:#fff;
        @extend .btn;
      }
  编译出来之后:
    //css
      .btn, .btn-primary, .btn-second {
                  border:1px solid #ccc;
                  padding:6px 10px;
                  font-size:14px;
              }
      .btn-primary{
            background-color:#f36;
            color:#fff;
          }
      .btn-second{
          background-color:orange;
          color:#fff;
        }
 在 Sass 中的继承,可以继承类样式块中所有样式代码,而且编译出来的 CSS 会将选择器合并在一起,形成组合选择器:
        .btn, .btn-primary, .btn-second {
                      border: 1px solid #ccc;
                      padding: 6px 10px;
                      font-size: 14px;
        }
占位符%placeholder
     Sass中的占位符%placeholder 功能是一个很强大,很实用的一个功能,可以取代以前的css中的基类造成的
     代码冗余的情形,因为%placeholder 声明的代码,如果不被@extend 调用的话,不会产生任何代码。
        %mt5{
            margin-top:5px;
          }
        %pt5{
            padding-top:5px;
          }
        .btn{
            @extend %mt5;
            @extend %pt5;
        }
        .block{
            @extend %mt5;
          span{
            @extend %pt5;
              }
        }
      编译出来的css
         //css
        .btn, .block{
              margin-top:5px;
          }
        .btn, .block span{
              padding-top:5px;
        }
    通过 @extend 调用的占位符,编译出来的代码会将相同的代码合并在一起。这也是我们希望看到的效果,也让你的代码变得更为干净。
混合

    混合宏vs继承vs占位符
     a Sass中的混合宏使用
      //SCSS中混合宏使用
      @mixin mt($var){
              margin-top:$var;
            }
      .block{
          @include mt(5px);
          span{
            display:block;
            @include mt(5px);
          }
        }
      .header{
          color:orange;
          @include mt(5px);
          span{
            dispay:block;
            @include mt(5px);
            }
        }
      编译结果:
        .block span {
            display: block;

            }
        .header {
            color: orange;

            }
        .header span {
            display: block;

          }
        .block, .block span, .header, .header span {
              margin-top: 5px;

          }
    建议:如果你的代码块中涉及到变量,建议使用混合宏来创建相同的代码块。
  b)Sass中继承
    同样的,将上面代码中的混合宏,使用类名来表示,然后通过继承来调用。
          .mt{
              margin-top:5px;
            }
          .block {
              @extend .mt;
            span{
                display:block;
                @extend .mt;
              }
            }
          .header{
              color:orange;
              @extend .mt;
          }
     编译结果:
        .block span {
            display: block;

          }
      .header {
          color: orange;

          }
      .header span {
          display: block;

          }
      .block, .block span, .header, .header span {
        margin-top: 5px;

          }

    建议:如果你的代码块不需要专任何变量参数,而且有一个基类已在文件中存在,那么建议使用Sass的继承。
  c)占位符
      最后来看占位符,将上面的代码中的基类.mt 换成Sass的占位符格式
    //SCSS中占位符的使用
      %mt{
        margin-top:5px;
      }
      .block{
        @extend %mt;
        span{
          display:block;
          @extend %mt;
          }
        }
      .header{
        color:orange;
        @extend %mt;
        span{
          display:block;
          @extend %mt;
        }
        }
    编译结果:
      .block span {
        display: block;

        }
      .header {
        color: orange;

         }
      .header span {
        display: block;

        }
    编译出来的 CSS 代码和使用继承基本上是相同,只是不会在代码中生成占位符 mt 的选择器。那么占位符和继承的主要区别的,“占位符是独立定义,
    不调用的时候是不会在 CSS 中产生任何代码;继承是首先有一个基类存在,不管调用与不调用,基类的样式都将会出现在编译出来的 CSS 代码中。”

 差值#{}
    使用css 预处理器语言的一个主要原因是想使用Sass 获得一个更好的结构体系,比如说你想写更干净的,搞笑的和面向对象的
    css. Sass 中的差值(Interpolation)就是重要的一部分,让我们看一下下面的例子。
      $properties:(margin ,padding);
      @mixin set-value($side,$value){
      @each $prop in $properties{
        #{$prop}-#{$side}:$value;
          }
        }
      .login-box{
        @include set-value(top,14px);
        }
      代码编译成css
      .login-box{
          margin-top:14px;
          padding-top:14px;
      }
    当你想设置属性值的时候你可以使用字符串插入进来,另一个使用的用法是构建一个选择器。
    @mixin generate-sizes($class,$small,$medium,$big)

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The Future of HTML: Evolution and Trends in Web Design The Future of HTML: Evolution and Trends in Web Design Apr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative Overview HTML vs. CSS vs. JavaScript: A Comparative Overview Apr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Building the Structure of Web Pages HTML: Building the Structure of Web Pages Apr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

See all articles