Table of Contents
grammar
Example 1: Escape special characters in CSS property values
Output
Example 2: Using variables in media queries
Example 3: Escape special characters in less variable values
Example 4: Using Less functions with escaped values
Home Web Front-end CSS Tutorial What is the use of Escape in LESS?

What is the use of Escape in LESS?

Aug 31, 2023 pm 11:29 PM

LESS 中的 Escape 有什么用?

In LESS, "escaping" allows us to use arbitrary strings as property or variable values. Sometimes, we may use special characters or symbols in LESS code, which may cause problems while compiling the code. Escaping is a technique that helps prevent such problems by encapsulating these special characters and symbols in special containers.

In this tutorial we will explore why escaping is necessary for LESS and how it works.

grammar

Users can use "escape" in LESS according to the following syntax.

@property_name: ~"anything";

In the above syntax, we use the tilde (~) before the string that needs to be escaped. The tilde (~) tells LESS to process the string as is, without making any changes to it except inserting any variables within the string.

Example 1: Escape special characters in CSS property values

In the example below, we use the ~ operator to escape single quotes in the URL in @my-bg.

In the output, the user can observe the correct URL in the compiled CSS. Quotes around the URL will cause compilation errors if not escaped.

@my-bg: ~"url('https://example.com/image.jpg')";
.background {
   background-image: @my-bg;
}

Output

.background {
   background-image: url('https://example.com/image.jpg');
}

Example 2: Using variables in media queries

In the example below, we define a variable @viewport-max-width with a value of 600px. We use escaping to ensure that the value of @viewport-max-width is passed to the CSS code as is, without LESS processing.

NOTE: Please note that as of LESS 3.5, escaping is not required in many cases where variables are used in media queries. In this case, we can simply reference the variable using @{} syntax.

In the output, the user can observe that the output of the two examples below is the same, which causes the media query to target the screen with a maximum width of 600 pixels.

@viewport-max-width: 600px;
@media screen and (max-width: ~"@{viewport-max-width}") {
   .my-class {
      font-size: 1.2rem;
   }
}
In LESS 3.5+, the above example can be written without the need for escaping as follows: 
@viewport-max-width: 600px; 
@media screen and (max-width: @{viewport-max-width}) { 
   .my-class { 
      font-size: 1.2rem; 
   } 
}

Output

@media screen and (max-width: 600px) {
   .my-class {
      font-size: 1.2rem;
   }
}

Example 3: Escape special characters in less variable values

In the following example, we use the tilde and double quote syntax ~"..." to define the variable @my-string as an arbitrary string. The string contains a pair of double quotes, which usually causes problems when LESS is compiled.

In the output, the user can observe that the value of @my-string is output as This is my "quoted" string without any problems due to escaping.

@my-string: ~"This is my "quoted" string";
.my-class {
   content: @my-string;
}

Output

.my-class {
   content: This is my "quoted" string;
}

Example 4: Using Less functions with escaped values

In the example below, we define a variable @my-color that has an arbitrary string value representing an RGBA color. The value is escaped using the tilde character followed by double quotes.

Then, we use the LESS function darken() to darken the color by 10% as the background color of the .my-class element. This function understands escaped string values ​​and applies calculations accordingly.

In the output, the user can observe that the original color (rgba(255, 0, 0, 0.5)) has been darkened by 10% to the new color (rgba(204, 0, 0, 0.5)) and applied as .my The background color of -class elements.

@my-color: ~"rgba(255, 0, 0, 0.5)";
.my-class {
   background-color: darken(@my-color, 10%);
}

Output

.my-class {
   background-color: rgba(204, 0, 0, 0.5);
}

Users learned to use escaping in LESS. Basically, escaping in LESS is an important technique that allows developers to write more efficient and maintainable CSS code.

Overall, by using escape syntax and functions, developers can ensure that special characters and reserved keywords are correctly encoded, thus preventing compilation errors and ensuring that the final output is correct.

The above is the detailed content of What is the use of Escape in LESS?. For more information, please follow other related articles on the PHP Chinese website!

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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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

PHP Tutorial
1596
276
How to use the CSS backdrop-filter property? How to use the CSS backdrop-filter property? Aug 02, 2025 pm 12:11 PM

Backdrop-filter is used to apply visual effects to the content behind the elements. 1. Use backdrop-filter:blur(10px) and other syntax to achieve the frosted glass effect; 2. Supports multiple filter functions such as blur, brightness, contrast, etc. and can be superimposed; 3. It is often used in glass card design, and it is necessary to ensure that the elements overlap with the background; 4. Modern browsers have good support, and @supports can be used to provide downgrade solutions; 5. Avoid excessive blur values and frequent redrawing to optimize performance. This attribute only takes effect when there is content behind the elements.

What are user agent stylesheets? What are user agent stylesheets? Jul 31, 2025 am 10:35 AM

User agent stylesheets are the default CSS styles that browsers automatically apply to ensure that HTML elements that have not added custom styles are still basic readable. They affect the initial appearance of the page, but there are differences between browsers, which may lead to inconsistent display. Developers often solve this problem by resetting or standardizing styles. Use the Developer Tools' Compute or Style panel to view the default styles. Common coverage operations include clearing inner and outer margins, modifying link underscores, adjusting title sizes and unifying button styles. Understanding user agent styles can help improve cross-browser consistency and enable precise layout control.

css dark mode toggle example css dark mode toggle example Jul 30, 2025 am 05:28 AM

First, use JavaScript to obtain the user system preferences and locally stored theme settings, and initialize the page theme; 1. The HTML structure contains a button to trigger topic switching; 2. CSS uses: root to define bright theme variables, .dark-mode class defines dark theme variables, and applies these variables through var(); 3. JavaScript detects prefers-color-scheme and reads localStorage to determine the initial theme; 4. Switch the dark-mode class on the html element when clicking the button, and saves the current state to localStorage; 5. All color changes are accompanied by 0.3 seconds transition animation to enhance the user

What is the CSS aspect-ratio property and how to use it? What is the CSS aspect-ratio property and how to use it? Aug 04, 2025 pm 04:38 PM

Theaspect-ratioCSSpropertydefinesthewidth-to-heightratioofanelement,ensuringconsistentproportionsinresponsivedesigns.1.Itisapplieddirectlytoelementslikeimages,videos,orcontainersusingsyntaxsuchasaspect-ratio:16/9.2.Commonusecasesincludemaintainingres

How to style links in CSS? How to style links in CSS? Jul 29, 2025 am 04:25 AM

The style of the link should distinguish different states through pseudo-classes. 1. Use a:link to set the unreached link style, 2. a:visited to set the accessed link, 3. a:hover to set the hover effect, 4. a:active to set the click-time style, 5. a:focus ensures keyboard accessibility, always follow the LVHA order to avoid style conflicts. You can improve usability and accessibility by adding padding, cursor:pointer and retaining or customizing focus outlines. You can also use border-bottom or animation underscore to ensure that the link has a good user experience and accessibility in all states.

How to use vw and vh units in CSS How to use vw and vh units in CSS Aug 07, 2025 pm 11:44 PM

vw and vh units achieve responsive design by associating element sizes with viewport width and height; 1vw is equal to 1% of viewport width, and 1vh is equal to 1% of viewport height; commonly used in full screen area, responsive fonts and elastic spacing; 1. Use 100vh or better 100dvh in the full screen area to avoid the influence of the mobile browser address bar; 2. Responsive fonts can be limited with 5vw and combined with clamp (1.5rem, 3vw, 3rem) to limit the minimum and maximum size; 3. Elastic spacing such as width:80vw, margin:5vhauto, padding:2vh3vw, can make the layout adaptable; pay attention to mobile device compatibility, accessibility and fixed width content conflicts, and it is recommended to give priority to using dvh first;

How to use the CSS :empty pseudo-class? How to use the CSS :empty pseudo-class? Aug 05, 2025 am 09:48 AM

The:emptypseudo-classselectselementswithnochildrenorcontent,includingspacesorcomments,soonlytrulyemptyelementslikematchit;1.Itcanhideemptycontainersbyusing:empty{display:none;}tocleanuplayouts;2.Itallowsaddingplaceholderstylingvia::beforeor::after,wh

How to create a bouncing animation with CSS? How to create a bouncing animation with CSS? Aug 02, 2025 am 05:44 AM

Define@keyframesbouncewith0%,100%attranslateY(0)and50%attranslateY(-20px)tocreateabasicbounce.2.Applytheanimationtoanelementusinganimation:bounce0.6sease-in-outinfiniteforsmooth,continuousmotion.3.Forrealism,use@keyframesrealistic-bouncewithscale(1.1

See all articles