我正在尝试使用css-grid创建3个区域,2个具有固定高度,而底部用于填充可用空间,其中按钮网格也适应可用空间。按钮将水平调整但不垂直调整。你能看到我正在做些什么来阻止它的运作吗?
* {
margin: 2px 2px 2px 2px;
padding: 0;
border: 0;
outline: 0;
font-size: 100%;
vertical-align: middle;
background: transparent;
letter-spacing: normal;
}
body {
color: #00000;
background-color: #335155
}
p {
color: #669990;
font-size: 50px;
text-decoration: none;
font-family: sans-serif;
}
p:after {
content: "";
display: inline-block;
width: 0px;
height: auto;
}
.panel {
display: grid;
grid-template-columns: 1fr;
grid-template-rows: 80px 80px 1fr;
}
.top-panel {
display: grid;
grid-template-columns: 60% 1fr 40px;
grid-template-rows: 80px;
grid-row-gap: 20px;
grid-column-gap: 20px;
padding: 0px 50px 0px 20px;
}
#paragraph-1 {
text-align: right;
padding: 0px 10px 0px 0px;
}
#paragraph-2 {
border-width: 1px;
border-style: solid;
border-color: #669990;
min-height: 75px;
max-width: 100px;
padding: 0px 20px 0px 0px;
text-align: center;
}
#paragraph-3 {
min-height: 75px;
max-width: 100px;
padding: 0px 10px 0px 0px;
text-align: center;
}
.middle-panel {
display: grid;
grid-template-columns: 1fr;
grid-template-rows: 1fr;
grid-row-gap: 20px;
}
button {
width: 100%;
height: 100%;
background-color: #669990;
border: none;
color: #335155;
text-align: center;
text-decoration: none;
font-size: 20px;
}
button:hover {
background-color: white;
}
.button-panel {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(4, minmax(20px, 300px);
grid-row-gap: 10px;
grid-column-gap: 10px;
width: 100vw;
height: 100vh;
}
.grid-position-1-1 {
grid-column: 1;
grid-row: 1;
}
.grid-position-2-1 {
grid-column: 2;
grid-row: 1;
}
.grid-position-3-1 {
grid-column: 3;
grid-row: 1;
}
.grid-position-1-2 {
grid-column: 1;
grid-row: 2;
}
.grid-position-2-2 {
grid-column: 2;
grid-row: 2;
}
.grid-position-3-2 {
grid-column: 3;
grid-row: 2;
}
.grid-position-1-3 {
grid-column: 1;
grid-row: 3;
}
.grid-position-2-3 {
grid-column: 2;
grid-row: 3;
}
.grid-position-3-3 {
grid-column: 3;
grid-row: 3;
}
.grid-position-1-4 {
grid-column: 1;
grid-row: 4;
}
.grid-position-2-4 {
grid-column: 2;
grid-row: 4;
}
.grid-position-3-4 {
grid-column: 3;
grid-row: 4;
}
<div class="panel">
<div class="grid-position-1-1">
<!-- of panel-->
<div class="top-panel">
<div class="grid-position-1-1">
<!-- of top-panel-->
<p id="paragraph-1"></p>
</div>
<div class="grid-position-2-1">
<!-- of top-panel-->
<p id="paragraph-2"></p>
</div>
<div class="grid-position-3-1">
<!-- of top-panel-->
<p id="paragraph-3"></p>
</div>
</div>
<div class="grid-position-2-1">
<!-- of panel-->
<div class="middle-panel">
<p id="another-paragraph"></p>
</div>
</div>
<div class="grid-position-3-1">
<!-- of panel-->
<div class="button-panel">
<div class="grid-position-1-1">
<!-- of button-panel-->
<button id="button-1" class="input-button" value=x>x</button>
</div>
<div class="grid-position-2-1">
<button id="button-2" class="input-button" value=x>x</button>
</div>
<div class="grid-position-3-1">
<button id="button-3" class="input-button" value=x>x</button>
</div>
<div class="grid-position-1-2">
<button id="button-4" class="input-button" value=x>x</button>
</div>
<div class="grid-position-2-2">
<button id="button-5" class="input-button" value=x>x</button>
</div>
<div class="grid-position-3-2">
<button id="button-6" class="input-button" value=x>x</button>
</div>
<div class="grid-position-1-3">
<button id="button-7" class="input-button" value=x>x</button>
</div>
<div class="grid-position-2-3">
<button id="button-8" class="input-button" value=x>x</button>
</div>
<div class="grid-position-3-3">
<button id="button-9" class="input-button" value=x>x</button>
</div>
<div class="grid-position-1-4">
<button id="button-10" class="input-button" value=x>x</button>
</div>
<div class="grid-position-2-4">
<button id="button-10" class="input-button" value=x>x</button>
</div>
<div class="grid-position-3-4">
<button id="button-10" class="input-button" value=x>x</button>
</div>
</div>
</div>
</div>
根据Michael_B的建议修改后的代码
* {
margin: 2px 2px 2px 2px;
padding: 0;
border: 0;
outline: 0;
font-size: 100%;
vertical-align: middle;
background: transparent;
letter-spacing: normal;
}
body {color: #00000; background-color: #335155}
p {
color: #669990;
font-size: 50px;
text-decoration: none;
font-family: sans-serif;
}
p:after {
content:"";
display:inline-block;
width:0px;
height:auto;
}
.panel {
display: grid;
grid-template-columns: 1fr;
grid-template-rows: 80px 80px 1fr;
}
.top-panel {
grid-column: 1;
grid-row: 1;
display: grid;
grid-template-columns: 60% 1fr 40px;
grid-template-rows: 80px;
grid-row-gap: 20px;
grid-column-gap: 20px;
padding: 0px 50px 0px 20px;
}
#paragraph-1 {
text-align: right;
padding: 0px 10px 0px 0px;
}
#paragraph-2 {
border-width: 1px;
border-style: solid;
border-color: #669990;
min-height: 75px;
max-width: 100px;
padding: 0px 20px 0px 0px;
text-align: center;
}
#paragraph-3 {
min-height: 75px;
max-width: 100px;
padding: 0px 10px 0px 0px;
text-align: center;
}
.middle-panel {
grid-column: 1;
grid-row: 2;
display: grid;
grid-template-columns: 1fr;
grid-template-rows: 1fr;
grid-row-gap: 20px;
}
button {
width: 100%;
height: 100%;
background-color: #669990;
border: none;
color: #335155;
text-align: center;
text-decoration: none;
font-size: 20px;
}
button:hover {
background-color: white;
}
.button-panel {
grid-column: 1;
grid-row: 3;
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(4, 1fr);
grid-row-gap: 2px;
grid-column-gap: 2px;
}
.grid-position-1-1 {
grid-column: 1;
grid-row: 1;
}
.grid-position-2-1 {
grid-column: 2;
grid-row: 1;
}
.grid-position-3-1 {
grid-column: 3;
grid-row: 1;
}
.grid-position-1-2 {
grid-column: 1;
grid-row: 2;
}
.grid-position-2-2 {
grid-column: 2;
grid-row: 2;
}
.grid-position-3-2 {
grid-column: 3;
grid-row: 2;
}
.grid-position-1-3 {
grid-column: 1;
grid-row: 3;
}
.grid-position-2-3 {
grid-column: 2;
grid-row: 3;
}
.grid-position-3-3 {
grid-column: 3;
grid-row: 3;
}
.grid-position-1-4 {
grid-column: 1;
grid-row: 4;
}
.grid-position-2-4 {
grid-column: 2;
grid-row: 4;
}
.grid-position-3-4 {
grid-column: 3;
grid-row: 4;
}
<body>
<div class="panel">
<div class="top-panel">
<div class="grid-position-1-1"><!-- of top-panel-->
<p id="paragraph-1"></p>
</div>
<div class="grid-position-2-1"><!-- of top-panel-->
<p id="paragraph-2"></p>
</div>
<div class="grid-position-3-1"><!-- of top-panel-->
<p id="paragraph-3"></p>
</div>
</div>
<div class="middle-panel">
<p id="another-paragraph"></p>
</div>
<div class="button-panel">
<div class="grid-position-1-1"><!-- of button-panel-->
<button id="button-1" class="input-button" value=x>x</button>
</div>
<div class="grid-position-2-1">
<button id="button-2" class="input-button" value=x>x</button>
</div>
<div class="grid-position-3-1">
<button id="button-3" class="input-button" value=x>x</button>
</div>
<div class="grid-position-1-2">
<button id="button-4" class="input-button" value=x>x</button>
</div>
<div class="grid-position-2-2">
<button id="button-5" class="input-button" value=x>x</button>
</div>
<div class="grid-position-3-2">
<button id="button-6" class="input-button" value=x>x</button>
</div>
<div class="grid-position-1-3">
<button id="button-7" class="input-button" value=x>x</button>
</div>
<div class="grid-position-2-3">
<button id="button-8" class="input-button" value=x>x</button>
</div>
<div class="grid-position-3-3">
<button id="button-9" class="input-button" value=x>x</button>
</div>
<div class="grid-position-1-4">
<button id="button-10" class="input-button" value=x>x</button>
</div>
<div class="grid-position-2-4">
<button id="button-10" class="input-button" value=x>x</button>
</div>
<div class="grid-position-3-4">
<button id="button-10" class="input-button" value=x>x</button>
</div>
</div>
</div>
</body>
答案 0 :(得分:0)
您的网格容器设置为<div class="panel">
:
.panel {
display: grid;
grid-template-columns: 1fr;
grid-template-rows: 80px 80px 1fr;
}
但是你在问题中提到的三个方面是:
.top-panel
.grid-position-2-1
.grid-position-3-1
不是网格容器的子项。他们是.panel
的孙子。
因此,它们不是网格项目。
grid formatting context的范围仅限于父子关系。
这意味着网格容器始终是父级,网格项始终是子级。网格属性仅在此关系中起作用。
如规范中所述:
网格容器的每个流入子项都成为网格项。
子项之外的网格容器的后代不是网格项,也不接受网格属性。此外,正如上面的规范语言所暗示的那样,children that are out-of-flow(即绝对定位)也不是网格项。
结论:您始终需要将display: grid
或display: inline-grid
应用于父级,以便网格属性可以对孩子起作用。