css - 网格模板区域 - 无效的属性值
问题描述
我试图搜索堆栈溢出、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>
解决方案
使用 . 创建CSS 网格布局时有一些规则grid-template-areas
。
- 您必须描述一个完整的网格,即必须填充网格上的每个单元格。
- 您只能定义每个区域一次,这意味着您不能使用此属性将内容复制到网格上的两个位置。
- 不能创建非矩形区域,否则声明无效。
违反这些规则将导致您的值无效,因此布局无法正常工作。
您的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>
推荐阅读
- java - 处理不同的 JSON 请求 Jackson
- node.js - express.js CSURF cookie 和 header 匹配,返回 403
- python - Numpy:重载 str 函数以在所有代码中获取指数符号
- scala - 如何动态提供 N 个编解码器以将字段作为 VectorCodec 处理,用于不包含大小字节的二进制字段的记录
- c - 检查重叠的日期和时间
- mongodb - 如何使用 _id 列的自定义值将数据框保存在 Spark 中的 Mongodb 中
- arm - FreeRTOS 是否保证第一个计时器滴答时间正好是 1 毫秒?
- html - HWND 类型不匹配
- javascript - 如何动态地将标签附加到
标签?
- c# - 如何使第二个数据网格名称在 C# 中可见?