使用 css 网格重新排序 html 计算器按钮(使用 sass)
大家好,我想弄清楚为什么 CSS 网格不重新排序我的计算器按钮。我应该得到的是这样的:
7 8 9 /
4 5 6 *
1 2 3 -
0 + = del
或者我只是得到 HTML 订单。网格不会对项目重新排序吗? (我很清楚其他方法可以做到这一点,我想弄清楚为什么网格不能像我认为的那样工作......)
这是 HTML 代码:
<main class="calculator">
<div class="result_screen"> </div>
<div class="enter_screen"> </div>
<div class="keyboard"> <button type="button" value="0" class="zero">0</button>
<button type="button" value="1" class="one">1</button>
<button type="button" value="2" class="two">2</button>
<button type="button" value="3" class="three">3</button>
<button type="button" value="4" class="four">4</button>
<button type="button" value="5" class="five">5</button>
<button type="button" value="6" class="six">6</button>
<button type="button" value="7" class="seven">7</button>
<button type="button" value="8" class="eight">8</button>
<button type="button" value="9" class="nine">9</button>
<button type="button" value="/" class="divide">\</button>
<button type="button" value="*" class="multiply">*</button>
<button type="button" value="-" class="minus">-</button>
<button type="button" value="del" class="delete">C</button>
<button type="button" value="=" class="equal">=</button>
<button type="button" value="+" class="plus">+</button>
</div>
</main>
这是 SCSS:
@mixin attribZones($zone) {
.#{$zone} {
grid-area: $zone;
}
}
$zones: "zero",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"divide",
"multiply",
"minus",
"delete",
"equal",
"plus";
@each $zone in $zones {
@include attribZones($zone);
}
.keyboard {
display: grid;
grid-template-columns: 1fr 1fr 1fr 1fr;
grid-template-areas:
"seven eight nine divide"
"four five six multiply"
"one two three minus"
"zero plus equal delete"
}
Hello all I'm trying to figure out why CSS grid doesn't reorder my calculator buttons. What I should get is something like that :
7 8 9 /
4 5 6 *
1 2 3 -
0 + = del
Or I just get the HTML order. Doesn't grid reorder the items ? (I know well of other ways to do this, I'd like to figure out why grid isn't working like I think it should...)
Here is the HTML code :
<main class="calculator">
<div class="result_screen"> </div>
<div class="enter_screen"> </div>
<div class="keyboard"> <button type="button" value="0" class="zero">0</button>
<button type="button" value="1" class="one">1</button>
<button type="button" value="2" class="two">2</button>
<button type="button" value="3" class="three">3</button>
<button type="button" value="4" class="four">4</button>
<button type="button" value="5" class="five">5</button>
<button type="button" value="6" class="six">6</button>
<button type="button" value="7" class="seven">7</button>
<button type="button" value="8" class="eight">8</button>
<button type="button" value="9" class="nine">9</button>
<button type="button" value="/" class="divide">\</button>
<button type="button" value="*" class="multiply">*</button>
<button type="button" value="-" class="minus">-</button>
<button type="button" value="del" class="delete">C</button>
<button type="button" value="=" class="equal">=</button>
<button type="button" value="+" class="plus">+</button>
</div>
</main>
Here's the SCSS :
@mixin attribZones($zone) {
.#{$zone} {
grid-area: $zone;
}
}
$zones: "zero",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"divide",
"multiply",
"minus",
"delete",
"equal",
"plus";
@each $zone in $zones {
@include attribZones($zone);
}
.keyboard {
display: grid;
grid-template-columns: 1fr 1fr 1fr 1fr;
grid-template-areas:
"seven eight nine divide"
"four five six multiply"
"one two three minus"
"zero plus equal delete"
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
定义
$zones
时需要删除引号。因为当您将
grid-area
设置为$zone
时,您实际上是将其设置为"one"
,"two"
...这些是无效值,必须是one
、two
...You need to remove quotes when you define
$zones
.Because when you set
grid-area
to$zone
you are essentially setting it to"one"
,"two"
... which are invalid values it has to beone
,two
...