使用 css 网格重新排序 html 计算器按钮(使用 sass)

发布于 2025-01-10 15:50:15 字数 2328 浏览 0 评论 0原文

大家好,我想弄清楚为什么 CSS 网格不重新排序我的计算器按钮。我应该得到的是这样的:

7 8 9 /
4 5 6 *
1 2 3 -
0 + = del

或者我只是得到 HTML 订单。网格不会对项目重新排序吗? (我很清楚其他方法可以做到这一点,我想弄清楚为什么网格不能像我认为的那样工作......)

在 Codepen 上查看完整代码

这是 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...)

See full code on Codepen

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆蝶 2025-01-17 15:50:15

定义 $zones 时需要删除引号。

$zones: zero,
one,
two,
three,
four,
five,
six,
seven,
eight,
nine,
divide,
multiply,
minus,
delete,
equal,
plus;

因为当您将 grid-area 设置为 $zone 时,您实际上是将其设置为 "one", "two"...这些是无效值,必须是 onetwo ...

You need to remove quotes when you define $zones.

$zones: zero,
one,
two,
three,
four,
five,
six,
seven,
eight,
nine,
divide,
multiply,
minus,
delete,
equal,
plus;

Because when you set grid-area to $zone you are essentially setting it to "one", "two"... which are invalid values it has to be one, two ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文