gpt4 book ai didi

css - 网格模板区域 - 无效的属性值

转载 作者:行者123 更新时间:2023-12-04 12:15:50 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Why aren't my grid-template-areas with ASCII art not working?

(1 个回答)


7 个月前关闭。




我试图搜索堆栈溢出、mozilla 文档和 CSS 技巧,以找出为什么我的网格模板区域的属性值一直无效并且无法弄清楚。有人可以指出为什么我收到此错误消息以及我做错了什么吗?
我正在尝试创建一个计算器。为此,我尝试创建网格单元以将运算符和关键数字输入其中。
提前致谢。

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使用 grid-template-areas 的布局.

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

  • Breaking these rules will result in your value being invalid and therefore the layout not working.


    你的原因 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>

    关于css - 网格模板区域 - 无效的属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66729030/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com