LESS - 內聯匯入選項關鍵字



描述

@import (inline) 語句會將你的 CSS 程式碼複製到輸出的 CSS 檔案中,而不會對其進行處理。這在 CSS 檔案不相容 LESS 時非常有用。雖然 LESS 支援大多數標準 CSS,但在某些地方不支援註釋,如果不修改 CSS,它將不支援所有已知的 CSS hacks。即使@import (inline)不會處理 CSS,它也能確保所有 CSS 程式碼都在一個檔案中。此功能在 1.5.0 版本中釋出。

示例

以下示例演示了在 LESS 檔案中使用reference關鍵字:

<html>
   <head>
      <link rel = "stylesheet" href = "style.css" type = "text/css" />
      <title>Import Option Inline</title>
   </head>

   <body>
      <h1>Welcome to Tutorialspoint</h1>
      <p>LESS is a CSS pre-processor that enables customizable, 
      manageable and reusable style sheet for web site.</p>
   </body>
</html>

接下來,建立style.less檔案。

style.less

@import (inline) "https://tutorialspoint.tw/less/import_inline.css";
p {
   color:red;
}

以下程式碼將從路徑https://tutorialspoint.tw/less/import_inline.css使用以下程式碼將import_inline.css檔案匯入到style.less中:

import_inline.css

.style {
   font-family: "Comic Sans MS";
   font-size: 20px;
}

您可以使用以下命令將style.less編譯成style.css

lessc style.less style.css

執行上述命令;它將自動建立包含以下程式碼的style.css檔案:

style.css

.style {
   font-family: "Comic Sans MS";
   font-size: 20px;
}

p {
   color: red;
}

輸出

按照以下步驟檢視上述程式碼的工作原理:

  • 將上述 html 程式碼儲存在import_options_inline.html檔案中。

  • 在瀏覽器中開啟此 HTML 檔案,將顯示以下輸出。

LESS Import Options Inline

如果您嘗試在style.less中的p標籤內使用.style類,它將丟擲一個未定義的錯誤

less_import_options.htm
廣告
© . All rights reserved.