search
HomeWeb Front-endHTML Tutorial使用Sass和Compass组合写CSS_html/css_WEB-ITnose

最近开始在尝试开始使用Sass来写CSS代码,刚开始虽然还是不太习惯用链式的方式写css,不过这是暂时的阶段。

如果你还不了解Sass,可以看之前发表过的文章来了解详情,Sass主要有下面这几种特性(主要内容来自这里)

左邊為原始scss檔,右邊為編譯過後的:
1.Variables 變數
使用$作為開頭當參數

2.Nesting 巢狀結構
很清楚的知道 誰是誰的子元素,不用像以前一樣 寫一大排重復的開頭了

3.Mixins 
其實就像function一樣使用,還可以帶參數

設定預設參數

4.Inheritance 繼承

另外幾個常用的功能

1.@import
可以將網站的各部份樣式拆開成_head.scss, _body.scss, _foot.scss放置在base資料夾下,可利用@import功能把3個檔納入到main.css裡 

只要在main.scss加上

@import "base/head"; @import "base/body"; @import "base/foot";<br />

_head.scss, _body.scss, _foot.scss 這些檔案都不會被編譯成css,記得檔案名稱需以 _ 底線作開頭!

2.算數 

3.顏色功能

lighten(red, 50%) //增亮50% darken(blue, 50%) //變暗50%

 

更多功能參見官方說明

其它筆記!

更換css壓縮樣式

以expanded壓縮方式

sass --watch --style expanded style.scss:style.css

編譯出來的css:

#main { color: #fff;  } #main p { width: 10em; } ...

以compressed壓縮方式

sass --watch --style compressed style.scss:style.css

編譯出來的css:

#main{color:#fff; line-height: 19.5px;"> 

要debug怎麼辨?

1.Firefox裝FireSass for Firebug外掛

2.重新編譯

sass --watch --debug-info style.scss:style.css

 #開啟debug模式 (記得要先刪原本編譯出的css)

之後在Firebug裡就可以看見啦 

還有線上可以直接測試 http://sass-lang.com/try.html

其實sass/scss只是利於編寫css而產生的一個語言,但今天要寫符合各瀏覽器前綴詞的css,你還是得自已寫@maxin把-moz, -webkit, -o, -ms寫起來,如果遇到要寫css3的gradient呢? 又要可以改參數呢? 那會瘋掉! 因為我寫過,後來就放棄了! 因為有個更強大的Compass來幫忙! 來看看Compass有多厲害!

首先,当然是安装Compass,

gem update --system #先更新 gem gem install compass #安裝Compass 

建立Compass项目

compass creat myproject

預設會產生如上面那些檔,接著可以在config.rb裡作設定css、sass、images等的資料夾設定,再依自已喜好吧! 接著在終端機輸入

compass watch  

此時你就可以開始編輯你的scss檔了,編輯完存檔,compass會馬上編譯css到你設定的資料夾內,一樣可以按command + c 取消。 下次要再編輯就再watch就好

来看看Compass有哪些方便的地方

reset css@import "compass/reset"; 

這樣reset的css就幫你寫好了! 超方便!

常見的css3圓角

@import "compass/css3"; .box{ @include border-radius(5px); } 

 

只要先import "compass/css3", 之後就可以使用所有css3 的內容,如border-radius、box-shadow、gradient…等,使用方式是@include,其實是compass幫你寫好了@mixin,你只要會用就好,而compass厲害的就是會同時幫你產生各個瀏覽器相對應的css,上面那行所產生的結果:

-moz-border-radius: 5px; -webkit-border-radius: 5px;  -o-border-radius: 5px;  -ms-border-radius: 5px; -khtml-border-radius: 5px;  border-radius: 5px;

所有css都幫你產生好了,再也不用全寫了,加上寫這麼多重復的css只會更難閱讀,尤其我非常不愛css屬性重寫再重寫又斷行,搞得打開css檔來,1~2千多行! 維護真的相當累人!加上sass的特性是巢狀結構,讓整個css乾淨清楚不少!

css3漸層

.box{ @include background(linear-gradient(lighten(red, 20%), red)); /*線性漸層*/ }

一行就搞定所有瀏覽器! 其中lighten(red, 20%)是使用sass的加亮顏色功能。

Sprite

我覺得…做Sprite是css designer的痛吧!維護非常費工,還要計算座標,萬一改個圖,就要座標重算!css再寫,而且改一個可能動全身!

哇~ 現在Compass都幫你搞定啦! Compass真是太強大了,三個願望一次滿足! 只要將要合併的圖片放在同一個資料夾下,compass會自動產生另一張合併檔,同時設定好座標。

@import "icons/*.png"; @include all-icons-sprites; //all-後面接著的「icons」代表著是資料夾名稱 .icons-sprite, .icons-facebook, .icons-twitter, .icons-yahoo { background: url('icons-s0859518ac7.png') no-repeat; } .icons-facebook { background-position: 0 0; } .icons-twitter { background-position: 0 -32px; } .icons-yahoo { background-position: 0 -64px; } 

更多compass sprite教學及設定: http://compass-style.org/help/tutorials/spriting/

其它還有更多功能: http://compass-style.org/reference/compass/

debug呢?

output_style = :compressed #css壓縮設定 sass_options = {:debug_info => true} #debug 

  開啟config.rb設定檔,加入上面文字後,重新產生css,Firebug就可以看到該樣式是寫在那一行了,sass debug外掛安裝說明。

Sass & Compass投影片介紹


來看看這份相當詳細又實用的投影片介紹吧!

转自前端开发博客 (http://caibaojian.com/use-sass-compass-write-css.html)

Statement
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
HTML, CSS, and JavaScript: Examples and Practical ApplicationsHTML, CSS, and JavaScript: Examples and Practical ApplicationsMay 09, 2025 am 12:01 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

How do you set the lang attribute on the  tag? Why is this important?How do you set the lang attribute on the tag? Why is this important?May 08, 2025 am 12:03 AM

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

What is the purpose of HTML attributes?What is the purpose of HTML attributes?May 07, 2025 am 12:01 AM

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

How do you create a list in HTML?How do you create a list in HTML?May 06, 2025 am 12:01 AM

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

HTML in Action: Examples of Website StructureHTML in Action: Examples of Website StructureMay 05, 2025 am 12:03 AM

HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.

How do you insert an image into an HTML page?How do you insert an image into an HTML page?May 04, 2025 am 12:02 AM

ToinsertanimageintoanHTMLpage,usethetagwithsrcandaltattributes.1)UsealttextforaccessibilityandSEO.2)Implementsrcsetforresponsiveimages.3)Applylazyloadingwithloading="lazy"tooptimizeperformance.4)OptimizeimagesusingtoolslikeImageOptimtoreduc

HTML's Purpose: Enabling Web Browsers to Display ContentHTML's Purpose: Enabling Web Browsers to Display ContentMay 03, 2025 am 12:03 AM

The core purpose of HTML is to enable the browser to understand and display web content. 1. HTML defines the web page structure and content through tags, such as, to, etc. 2. HTML5 enhances multimedia support and introduces and tags. 3.HTML provides form elements to support user interaction. 4. Optimizing HTML code can improve web page performance, such as reducing HTTP requests and compressing HTML.

Why are HTML tags important for web development?Why are HTML tags important for web development?May 02, 2025 am 12:03 AM

HTMLtagsareessentialforwebdevelopmentastheystructureandenhancewebpages.1)Theydefinelayout,semantics,andinteractivity.2)SemantictagsimproveaccessibilityandSEO.3)Properuseoftagscanoptimizeperformanceandensurecross-browsercompatibility.

See all articles

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

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools