<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="style/demo.css">
</head>
<body>
<div class="test">
<div class="color1"></div>
<div class="color2"></div>
<div class="color3"></div>
<div class="color4"></div>
<div class="color5"></div>
<div class="color6"></div>
<div class="color7"></div>
</div>
<div style="clear:both;"></div>
<div class="test2"></div>
</body>
</html>
@charset "UTF-8";
$list: red orange yellow green grey blue purple;
//$len: length($list);//自动获取长度
$ percentage(1/7);
@for $i from 1 through 7 {
.test .color#{$i} {
$width;
background-color: nth($list, $i);
height: 10px;
float: left;
}
}
.test {
100%;
}
//使用这种方法(background-image: linear-gradient)也可以实现各种颜色的布局
.test2 {
// 100%;
//background-image: linear-gradient(to right, red 0%, red $width,orange $width, orange $width * 2);
}
注意:sass里边写的div个数应该与html里的布局一致,否则是识别不到的。