Home Web Front-end HTML Tutorial Sass函数字符串函数_html/css_WEB-ITnose

Sass函数字符串函数_html/css_WEB-ITnose

Jun 24, 2016 am 11:33 AM

Sass的函数简介
在 Sass 中除了可以定义变量,具有 @extend%placeholder mixins 等特性之外,还自备了一系列的函数功能。其主要包括:
  ● 字符串函数

  ● 数字函数

  ● 列表函数

  ● 颜色函数

  ● Introspection 函数

  ● 三元函数等

当然除了自备的函数功能之外,我们还可以根据自己的需求定义函数功能,常常称之为自定义函数

 

 

 

字符串函数
字符串函数顾名思意是用来处理字符串的函数。Sass 的字符串函数主要包括两个函数

unquote($string): 删除字符串中的引号;quote($string):给字符串添加引号。
Copy after login

1、unquote()函数
unquote() 函数主要是用来删除一个字符串中的引号,如果这个字符串没有带有引号,将返回原始的字符串

 1 //SCSS 2 .test1 { 3  content: unquote('Hello Sass!') ; 4 } 5 .test2 { 6  content: unquote("'Hello Sass!"); 7 } 8 .test3 { 9  content: unquote("I'm Web Designer");10 }11 .test4 {12  content: unquote("'Hello Sass!'");13 }14 .test5 {15  content: unquote('"Hello Sass!"');16 }17 .test6 {18  content: unquote(Hello Sass);19 }
Copy after login

编译后的 css 代码:

 1 //CSS 2 .test1 { 3  content: Hello Sass!;  4 } 5 .test2 { 6  content: 'Hello Sass!;  7 } 8 .test3 { 9  content: I'm Web Designer; 10 }11 .test4 {12  content: 'Hello Sass!'; 13 }14 .test5 {15  content: "Hello Sass!"; 16 }17 .test6 {18  content: Hello Sass; 19 }
Copy after login

注意: unquote() 函数只能删除字符串最最后引号(双引号或单引号),而无法删除字符串中间的引号。如果字符没有带引号,返回的将是字符串本身。

2、quote()函数
quote() 函数刚好与 unquote() 函数功能相反,主要用来给字符串添加引号。如果字符串,自身带有引号会统一换成双引号 ""
如:

 1 //SCSS 2 .test1 { 3  content: quote('Hello Sass!'); 4 } 5 .test2 { 6  content: quote("Hello Sass!"); 7 } 8 .test3 { 9  content: quote(ImWebDesigner);10 }11 .test4 {12  content: quote(' ');13 }
Copy after login

编译出来的 css 代码:

 1 //CSS 2 .test1 { 3  content: "Hello Sass!"; 4 } 5 .test2 { 6  content: "Hello Sass!"; 7 } 8 .test3 { 9  content: "ImWebDesigner";10 }11 .test4 {12  content: "";13 }
Copy after login

使用 quote() 函数只能给字符串增加双引号,而且字符串中间有单引号或者空格时,需要用单引号或双引号括起,否则编译的时候将会报错。

1 .test1 {2  content: quote(<strong>Hello Sass</strong>);3 }4 //这样使用,编译器马上会报错:error style.scss (Line 13: $string: ("Hello""Sass") is not a string for `quote')
Copy after login

解决方案就是去掉空格,或者加上引号:

1 .test1 {2  content: quote(<strong>HelloSass</strong>);3 }4 .test1 {5  content: quote(<strong>"Hello Sass"</strong>);6 }
Copy after login

同时 quote() 碰到特殊符号,比如: !、?、> 等,除中折号 - 和 下划线_ 都需要使用双引号括起,否则编译器在进行编译的时候同样会报错。

字符串函数-To-upper-case()、To-lower-case()
1、To-upper-case()
To-upper-case() 函数将字符串小写字母转换成大写字母。如:

1 //SCSS2 .test {3  text: to-upper-case(aaaaa);4  text: to-upper-case(aA-aAAA-aaa);5 }
Copy after login

编译出来的 css 代码:

1 //CSS2 .test {3  text: AAAAA;4  text: AA-AAAA-AAA;5 }
Copy after login
Copy after login

2、To-lower-case()
To-lower-case() 函数与 To-upper-case() 刚好相反,将字符串转换成小写字母

1 //SCSS2 .test {3  text: to-lower-case(AAAAA);4  text: to-lower-case(aA-aAAA-aaa);5 }
Copy after login

编译出来的 css 代码:

1 //CSS2 .test {3  text: AAAAA;4  text: AA-AAAA-AAA;5 }
Copy after login
Copy after login

 

 

 

 

 

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)

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

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

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

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.

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

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.

See all articles