Table of Contents
安装sass,compass
sass的使用及优点
compass的使用
Home Web Front-end HTML Tutorial sass,compass让开发效率飞起 - beidan

sass,compass让开发效率飞起 - beidan

May 21, 2016 am 08:54 AM
sass

最近开始学习并且使用,发现使用它写起css来真的是各种爽

安装sass,compass

  1. sass是依赖于ruby的,必须先安装Ruby,点击下载
  2. 下载完ruby之后,使用命令行安装sass        gem install sass
  3. 使用命令,sass-v    compass-v   查看是否安装成功

   

   出现上图情况则为安装成功


 

sass的使用及优点

  • sass官网: http://www.w3cplus.com/sassguide/
  • api文档   http://sass-lang.com/documentation/file.SASS_REFERENCE.html
  • sass功能:

定义变量,数值计算(左边为scss文件,右边为编译之后的css文件)

方便知道谁是谁的子元素,不必写一大堆重复的标签

还可以像函数一样使用,可以传参数,可以设置默认参数

这还远远不够,如果sass不和compass一起使用,很难体现它功能的强大性

compass的使用

1.官方文档 http://compass-style.org/help/tutorials/production-css/

2.compass的特性

  • 轻松实现浏览器兼容
  • 全面支持css3
  • 提供海量使用功能,颜色处理,获取客户端数据等
  • 轻松实现扩展

3.常用功能:

 1.@import(合并css文件,实现模块化)

可以将项目中的各部分样式拆分成 _head.scss,_body.scss,_foot.scss    (注意!要以_开头,这样子这些文件便不会被编译成css文件)

实现效果

使用方法,在main.scss中,加入以下代码

1

2

3

<span style="color: #008080;">1</span> @import "base/head"<span style="color: #000000;">;

</span><span style="color: #008080;">2</span> @import "base/body"<span style="color: #000000;">;

</span><span style="color: #008080;">3</span> @import "base/foot";

Copy after login

执行编译,即可实现

2.css3兼容性

      

无需每次都重复写多个浏览器前缀,只需在前面写上 @include  (这里用正常的css3方式写)

3.合并雪碧图

说明:CSS雪碧图是将几个图片拼合成一张图片,以此来减少HTTP请求的方法,并且可以提升网站加载速度

目录结构如下:share目录下放未经合并的图片,在images目录下的图片即为合并之后的图片

使用方法:

在share.scss中输入以下3行代码

1

2

3

<span style="color: #008080;">1</span> @import "compass/utilities/sprites"<span style="color: #000000;">;  

</span><span style="color: #008080;">2</span> @import "share/*.png";       <span style="color: #008000;">//</span><span style="color: #008000;">注意这里的路径,*表示匹配所有png格式的图片</span>

<span style="color: #008080;">3</span> @include all-share-sprites;   <span style="color: #008000;">//</span><span style="color: #008000;">中间的share为存放未合并文件的目录名字,</span>

Copy after login

再执行编译即可,效果图如下,随便测试的,有点丑嘿嘿

并且,在share.css文件中,还将图片的位置都计算好了,使用起来非常的方便,如下

出现过的bug:

1

<span style="color: #008080;">1</span> error sass/screen.sccc (c:/Ruby22-x64/lib/ruby/gems/2.2.0/compass-1.0.3/lib/compass/sass_extensions/functions/sprite.rb:137:in 'sprite_does_not_have_parent':undefined 'parent' for nil:NilClass)

Copy after login

解决方法:    图片名字,不要有数字,中文之类的

4.浏览器Normalize.css(解决各种浏览器样式不统一的问题)

(1)说明:Normalize.css是一种CSS reset的替代方案。

(2)优点:

  • 保护有用的浏览器默认样式而不是完全去掉它们
  • 一般化的样式:为大部分HTML元素提供
  • 修复浏览器自身的bug并保证各浏览器的一致性
  • 优化CSS可用性:用一些小技巧
  • 解释代码:用注释和详细的文档来

(3)官方文档 https://github.com/ksmandersen/compass-normalize

(4)安装    gem install compass-normalize

(5)使用方法

  • 新创建一个项目

compass create <my_project> -r compass-normalize --using compass-normalize</my_project>

  • 在已经存在的项目添加
在config.rb 中添加    require'compass-normalize'
  • 使用规范化的插件,只需要引入     @import "normalize"; //这个为全部引入

 正常情况下是选择需要的模块,单独引入,防止代码冗余,如下

1

2

3

4

5

6

7

8

<span style="color: #008080;">1</span> <span style="color: #000000;">@import 'normalize/html5';

</span><span style="color: #008080;">2</span> <span style="color: #000000;">@import 'normalize/base';

</span><span style="color: #008080;">3</span> <span style="color: #000000;">@import 'normalize/links';

</span><span style="color: #008080;">4</span> <span style="color: #000000;">@import 'normalize/typography';

</span><span style="color: #008080;">5</span> <span style="color: #000000;">@import 'normalize/embeds';

</span><span style="color: #008080;">6</span> <span style="color: #000000;">@import 'normalize/groups';

</span><span style="color: #008080;">7</span> <span style="color: #000000;">@import 'normalize/forms';

</span><span style="color: #008080;">8</span> @import 'normalize/tables';

Copy after login

scss编译,compass编译

 说了这么多,还差编译

1.使用可视化工具编译      例如:koala   (实时编译,可压缩,支持多语言,使用简单)

下载地址,点击下载

2.使用命令行

创建一个工程       compass create myproject  点击进入创建的目录,出现以下3个文件

 编译        compass compile   (在项目根目录下使用,将sass目录中的文件编译成css,css的路径为在config.rb配置的css-dir)

常用命令行如下

3.使用前端自动化工具 gulp  

(接下来会写一篇gulp的文章,未完待续)

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
3 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.

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 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.

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 <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

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 <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

See all articles