LESS-nth表达式

2018-01-10 18:24 更新

描述

nth表达式的形式在扩展中很重要,否则它将选择器视为不同。 nth表达式1n + 2和n + 2是等效的,但扩展将该表达式视为不同。


例如,使用以下代码创建一个LESS文件:

:nth-child(n+2)
{
color: #BF70A5;
  font-style: italic;
}
.child:extend(:nth-child(1n+2)){}


当我们在命令提示符中编译上面的代码时,你会得到一个错误消息,如下所示。


错误消息


编译之后,您将获得以下CSS代码。
:nth-child(n+2) {
  color: #BF70A5;
  font-style: italic;
}


在属性选择器中,引用类型不重要,您可以在以下示例中看到它:

例子

下面的例子演示了在LESS文件中使用nth表达式:

extend_syntax.htm

<!doctype html>
<head>
	<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div class="style">
   <h2>Hello!!!!!</h2>
</div>
<p class="img">Welcome to W3Cschool</p>
</body>
</html>


接下来,创建文件 style.less

style.less

[title=tutorialspoint] {
  font-style: italic;
}
[title='tutorialspoint'] {
 font-style: italic;
}
[title="tutorialspoint"] {
  font-style: italic;
}
.style:extend([title=tutorialspoint]) {}
.container:extend([title='tutorialspoint']) {}
.img:extend([title="tutorialspoint"]) {}


您可以使用以下命令将 style.less 文件编译为 style.css

lessc style.less style.css


接下来执行上面的命令,它将用下面的代码自动创建 style.css 文件:

style.css

[title=tutorialspoint],
.style,
.container,
.img {
  font-style: italic;
}
[title='tutorialspoint'],
.style,
.container,
.img {
  font-style: italic;
}
[title="tutorialspoint"],
.style,
.container,
.img {
  font-style: italic;
}


输出

让我们执行以下步骤,看看上面的代码如何工作:

  • 将上述html代码保存在 extend_syntax.htm 文件中。

  • 在浏览器中打开此HTML文件,将显示如下输出。


输出

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号