在 Less 中,unit 表示单位,用于表示数值的单位,例如像素(px)、百分比(%)、em 等。在 Less 中,你可以使用单位对数值进行计算、比较和赋值。下面是一些示例:
- 使用单位进行计算:
@base-font-size: 16px;
@new-font-size: @base-font-size * 1.5; // 计算出24px
- 单位转换:
@base-size: 10px;
@base-size-rem: unit(@base-size, rem); // 将10px转换为10rem
- 比较不同单位的值:
@width-px: 200px;
@width-em: 12em;
div {
width: @width-px + @width-em; // 212px
}
- 单位混合使用:
@base-font-size: 16px;
@line-height: 1.5;
body {
font-size: @base-font-size;
line-height: @base-font-size * @line-height; // 24px
}
在 Less 中,单位的使用可以帮助你更灵活地处理数值,并根据需要进行单位转换和计算。