Home Web Front-end HTML Tutorial compass自动生成雪碧图_html/css_WEB-ITnose

compass自动生成雪碧图_html/css_WEB-ITnose

Jun 21, 2016 am 08:52 AM

为了减少网页中的请求数,我们通常会将很多icon拼接到一个大的图片中,通过background和background-position来控制元素的显示。但是,拼接雪碧图以及后续的具体元素在雪碧图中位置的确定都是很麻烦的,精确也不准确。今天,就来介绍一种简便的方法。使用此方法,当拼接工作量大时,能够很大程度地减少前端的工作;而且不会出现像素上的差别,十分准确。

1.环境配置

  • ruby
  • sass

    1

    gem install sass

    Copy after login
    但是往往会报:

    1

    gem 在国内的镜像不能用Could not find a valid gem 'sass' (>= 0), here is why:      Unable to download data from https://rubygems.org/ - Errno::E...

    Copy after login
    所以我们来使用淘宝的镜像,执行以下操作:

    1

    gem sources --remove https://rubygems.org/gem sources -a https://ruby.taobao.org/gem sources -l*** CURRENT SOURCES ***https://ruby.taobao.org按照步骤进行就行,之后再进行如下命令:sudo gem install sass

    Copy after login
  • compass

    1

    sudo gem install compass

    Copy after login
  • 查看上面的ruby,sass,compass是否安装正确,如果正确,就能看到对应的版本状态

    1

    ruby -vsass -vcompass -v

    Copy after login

2.compass项目

  • 在自己新建的项目中

    1

    compass init

    Copy after login

    会在命令行输出以下内容,表示初始化成功:

    1

    directory sass/ directory stylesheets/  create config.rb  create sass/screen.scss  create sass/print.scss  create sass/ie.scss   write stylesheets/ie.css  write stylesheets/print.css  write stylesheets/screen.cssCongratulations! Your compass project has been created.

    Copy after login
  • 也可以直接在一个目录下,compass create compass-test

  • 查看compass项目配置文件config.rb

    1

    require 'compass/import-once/activate'# Require any additional compass plugins here.# Set this to the root of your project when deployed:http_path = "/"css_dir = "stylesheets"sass_dir = "sass"images_dir = "images"javascripts_dir = "javascripts"# You can select your preferred output style here (can be overridden via the command line):# output_style = :expanded or :nested or :compact or :compressed# To enable relative paths to assets via compass helper functions. Uncomment:# relative_assets = true  // 使用相对目录# To disable debugging comments that display the original location of your selectors. Uncomment:# line_comments = false //关闭行注释# If you prefer the indented syntax, you might want to regenerate this# project again passing --syntax sass, or you can uncomment this:# preferred_syntax = :sass# and then run:# sass-convert -R --from scss --to sass sass scss && rm -rf sass && mv scss sass

    Copy after login
  • 准备要拼接的icon

    1

    根据config.rb中的设置,我们需要将图片放在images文件夹下所以,我们先在项目根目录下新建一个images文件夹,比如我们现在项目㓟多个模块,每个模块需要对应一个雪碧图,那么我们在images中新建多个文件夹保存原始的图片;

    Copy after login
  • 现在假设我们有一个share模块,然后share模块有2张需要合并的背景图

    pic1.png

  • 针对share模块,我们在sass文件夹下新建一个叫share.scss,内容为:

    1

    @import "compass/utilities/sprites";    // 加载compass sprites模块@import "share/*.png";                    // 导入share目录下所有png图片@include all-share-sprites;                // 输出所有的雪碧图css

    Copy after login
  • 在根目录下,执行

    1

    compass compile

    Copy after login

    如果命令执行成功,我们可以在images文件夹下生成一个以share开头的文件,比如我这的是:share-s0876535d58.png。

pic2.png

同时,我们在stylesheets文件夹下,看到新生成的share.css文件,内容如下:

1

/* line 56, share/*.png */.share-sprite, .share-github, .share-weibo {  background-image: url('/images/share-s0876535d58.png');  background-repeat: no-repeat;}/* line 84, ../../../../../../Library/Ruby/Gems/2.0.0/gems/compass-core-1.0.3/stylesheets/compass/utilities/sprites/_base.scss */.share-github {  background-position: 0 0;}/* line 84, ../../../../../../Library/Ruby/Gems/2.0.0/gems/compass-core-1.0.3/stylesheets/compass/utilities/sprites/_base.scss */.share-weibo {  background-position: 0 -23px;}

Copy after login
到此为止,基础的compass生成雪碧图的操作就完成了。使用此方法,当拼接工作量大时,能够很大程度地减少前端的工作;而且不会出现像素上的差别,十分准确。
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)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
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)

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

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

What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

See all articles