首页 > 解决方案 > 网格模板区域 - 无效的属性值

问题描述

我试图搜索堆栈溢出、mozilla 文档和 CSS 技巧,以找出为什么我的 grid-template-areas 总是得到一个无效的属性值并且无法弄清楚。有人可以指出为什么我收到此错误消息以及我做错了什么吗?

我正在尝试创建一个计算器。为此,我尝试创建网格单元以将运算符和键号输入其中。

提前致谢。

html, body {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    width: 100%;
}


#gridContainer {
    border: 1px solid black;
    display: grid;
    grid-template-columns: 4rem 4rem 4rem 4rem;
    grid-template-rows: 6rem 6rem 6rem 6rem 6rem 6rem;
    grid-template-areas: 
    "s s s s"
    "o o o o"
    "n n n o"
    "n n n o"
    "n n n o"
    "z z d c";
}
<html>
<body>
    <div id="gridContainer">
        <div id="screen"></div>
        <button id="clear" class="clear" type="button" value="clear" >AC</button>
        <button id="negativeNumber" class="negative" type="button" value="signs">+ / -</button>
        <button id="percent" class="percentage" type="button" value="percent">%</button>
        <button id="divide" class="operator" type="button" value="/">/</button>
        <button id="seven" class="number" type="button" value="7">7</button>
        <button id="eight" class="number" type="button" value="8">8</button>
        <button id="nine" class="number" type="button" value="9">9</button>
        <button id="multiply" class="operator" type="button" value="*">*</button>
        <button id="four" class="number" type="button" value="4">4</button>
        <button id="five" class="number" type="button" value="5">5</button>
        <button id="six" class="number" type="button" value="6">6</button>
        <button id="subtract" class="operator" type="button" value="-">-</button>
        <button id="one" class="number" type="button" value="1">1</button>
        <button id="two" class="number" type="button" value="2">2</button>
        <button id="three" class="number" type="button" value="3">3</button>
        <button id="addition" class="operator" type="button" value="+">+</button>
        <button id="zero" class="number" type="button" value="0">0</button>
        <button id="decimal" class="number" type="button" value=".">.</button>
        <button id="equals" class="equal" type="button" value="=">=</button>
    </div>
</body>
</html>

标签: csscss-grid

解决方案


使用 . 创建CSS 网格布局时有一些规则grid-template-areas

  1. 您必须描述一个完整的网格,即必须填充网格上的每个单元格。
  2. 您只能定义每个区域一次,这意味着您不能使用此属性将内容复制到网格上的两个位置。
  3. 不能创建非矩形区域,否则声明无效。

违反这些规则将导致您的值无效,因此布局无法正常工作。

您的grid-template-areas声明无效的原因是它违反了规则 2。您尝试多次定义每个网格区域并将内容复制到网格上的两个位置。您已经复制了o第三、第四和第五行中的区域。一旦你修复了属性值变得有效。如果需要,请随意更改区域名称,但在使用grid-template-areas.

我假设s代表“屏幕”、o“操作”、n“数字”等等。如果您将第二行定义为使每个区域都o像,o o o o那么您不能n n n o在下一行中使用,因为您违反了规则 #2,即多次定义网格区域并将内容复制到网格中的两个位置。我更新了您的代码,因此grid-template-areas声明有效。

html, body {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    width: 100%;
}


#gridContainer {
    border: 1px solid black;
    display: grid;
    grid-template-columns: 4rem 4rem 4rem 4rem;
    grid-template-rows: 6rem 6rem 6rem 6rem 6rem 6rem;
    grid-template-areas: 
                        "s s s s"
                        "O O O O"
                        "o n n n"
                        "o n n n"
                        "o n n n"
                        "z z d c";
}
<html>
<body>
    <div id="gridContainer">
        <div id="screen"></div>
        <button id="clear" class="clear" type="button" value="clear">AC</button>
        <button id="negativeNumber" class="negative" type="button" value="signs">+ / -</button>
        <button id="percent" class="percentage" type="button" value="percent">%</button>
        <button id="divide" class="operator" type="button" value="/">/</button>
        <button id="seven" class="number" type="button" value="7">7</button>
        <button id="eight" class="number" type="button" value="8">8</button>
        <button id="nine" class="number" type="button" value="9">9</button>
        <button id="multiply" class="operator" type="button" value="*">*</button>
        <button id="four" class="number" type="button" value="4">4</button>
        <button id="five" class="number" type="button" value="5">5</button>
        <button id="six" class="number" type="button" value="6">6</button>
        <button id="subtract" class="operator" type="button" value="-">-</button>
        <button id="one" class="number" type="button" value="1">1</button>
        <button id="two" class="number" type="button" value="2">2</button>
        <button id="three" class="number" type="button" value="3">3</button>
        <button id="addition" class="operator" type="button" value="+">+</button>
        <button id="zero" class="number" type="button" value="0">0</button>
        <button id="decimal" class="number" type="button" value=".">.</button>
        <button id="equals" class="equal" type="button" value="=">=</button>
    </div>
</body>
</html>


推荐阅读