- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在嵌套网格元素中创建三个元素。正如您从代码中看到的那样,我将“面板”div 放在“巨型”和“内容”div 之间。我还在里面嵌套了三个div。在 CSS 中,我在 .panels
中添加了一个嵌套网格。
我希望“面板”div 在垂直轴上分成三个大小相等的部分。想象三个方 block 一个接一个地叠起来。但是嵌套元素不会填满整个“面板”div。如果运行代码片段,您可以看到面板是嵌套的,但没有占据整个空间。他们占 parent 的一小部分。我将 background-color: white !important
添加到嵌套面板之一以显示它有多小。
另一个例子可以在这里看到:https://codepen.io/rachelandrew/pen/NqQPBR/
但同样,嵌套的 E、F 和 G 项不会展开以填满整个 D 部分。
有没有办法让三个面板填充它们的父面板?
.container {
display: grid;
width: 100%;
height: 100%;
grid-gap: 3px;
grid-template-columns: repeat(10, 1fr);
grid-template-rows: 40px 130px 130px 130px 60px 330px 40px;
}
.header {
grid-column: 1 / -1;
}
.jumbo {
grid-column: 1 / -1;
grid-row: 2 / 5;
}
.panels {
grid-column: 3 / 9;
grid-row: 4 / 6;
z-index: 1;
display: grid;
grid-template-columns: repeat(3, 1fr);
}
.panel1 {
grid-row: 1 / 2;
grid-row: 1;
background-color: white !important;
z-index: 2;
}
.content {
grid-column: 1 / -1;
grid-row: 5 / 7;
}
.footer {
grid-column: 1 / -1;
}
/* Styling */
.container > div {
display: grid;
justify-content: center;
align-items: center;
font-size: 2em;
color: #ffeead;
}
html, body {
background-color: #ffeead;
box-sizing: border-box;
height: 100%;
margin: 0px;
font-family: "Work Sans"
}
.container > div:nth-child(1n) {
background-color: #96ceb4;
}
.container > div:nth-child(3n) {
background-color: #88d8b0;
}
.container > div:nth-child(2n) {
background-color: #ff6f69;
}
.container > div:nth-child(4n) {
background-color: #ffcc5c;
}
.panels > div:nth-child(1n) {
background-color: #96ceb4;
}
<div class="container">
<div class="header">
HEADER
</div>
<div class="jumbo">
JUMBO
</div>
<div class="panels">
<div class="panel1">PANEL1</div>
<div class="panel2">PANEL2</div>
<div class="panel3">PANEL3</div>
</div>
<div class="content">
CONTENT
</div>
<div class="footer">
FOOTER
</div>
</div>
最佳答案
您已将 align-items: center
应用于嵌套网格容器 (.panels
)。
使用该规则,您可以覆盖默认的 align-items: stretch
,这会将您的网格元素设置为父级的全高。相反,您将元素垂直居中。
因此它们可以是全高,从 .panels
元素中删除 align-items: center
:
.container > div:not(.panels) {
align-items: center;
}
.container {
display: grid;
width: 100%;
height: 100%;
grid-gap: 3px;
grid-template-columns: repeat(10, 1fr);
grid-template-rows: 40px 130px 130px 130px 60px 330px 40px;
}
.header {
grid-column: 1 / -1;
}
.jumbo {
grid-column: 1 / -1;
grid-row: 2 / 5;
}
.panels {
grid-column: 3 / 9;
grid-row: 4 / 6;
z-index: 1;
display: grid;
grid-template-columns: repeat(3, 1fr);
}
.panel1 {
grid-row: 1 / 2;
grid-row: 1;
background-color: white !important;
z-index: 2;
}
.content {
grid-column: 1 / -1;
grid-row: 5 / 7;
}
.footer {
grid-column: 1 / -1;
}
/* Styling */
.container > div {
display: grid;
justify-content: center;
/* align-items: center; */
font-size: 2em;
color: #ffeead;
}
/* new */
.container > div:not(.panels) {
align-items: center;
}
html, body {
background-color: #ffeead;
box-sizing: border-box;
height: 100%;
margin: 0px;
font-family: "Work Sans"
}
.container > div:nth-child(1n) { background-color: #96ceb4; }
.container > div:nth-child(3n) { background-color: #88d8b0; }
.container > div:nth-child(2n) { background-color: #ff6f69; }
.container > div:nth-child(4n) { background-color: #ffcc5c; }
.panels > div:nth-child(1n) { background-color: #96ceb4; }
<div class="container">
<div class="header">HEADER</div>
<div class="jumbo">JUMBO</div>
<div class="panels">
<div class="panel1">PANEL1</div>
<div class="panel2">PANEL2</div>
<div class="panel3">PANEL3</div>
</div>
<div class="content">CONTENT</div>
<div class="footer">FOOTER</div>
</div>
然后,为了使 .panels
的内容垂直居中,我会直接定位内容:
.panels > div {
display: flex;
align-items: center;
}
.container {
display: grid;
width: 100%;
height: 100%;
grid-gap: 3px;
grid-template-columns: repeat(10, 1fr);
grid-template-rows: 40px 130px 130px 130px 60px 330px 40px;
}
.header {
grid-column: 1 / -1;
}
.jumbo {
grid-column: 1 / -1;
grid-row: 2 / 5;
}
.panels {
grid-column: 3 / 9;
grid-row: 4 / 6;
z-index: 1;
display: grid;
grid-template-columns: repeat(3, 1fr);
}
.panel1 {
grid-row: 1 / 2;
grid-row: 1;
background-color: white !important;
z-index: 2;
}
.content {
grid-column: 1 / -1;
grid-row: 5 / 7;
}
.footer {
grid-column: 1 / -1;
}
/* Styling */
.container > div {
display: grid;
justify-content: center;
/* align-items: center; */
font-size: 2em;
color: #ffeead;
}
/* new */
.container > div:not(.panels) {
align-items: center;
}
/* new */
.panels > div {
display: flex;
align-items: center;
}
html, body {
background-color: #ffeead;
box-sizing: border-box;
height: 100%;
margin: 0px;
font-family: "Work Sans"
}
.container > div:nth-child(1n) { background-color: #96ceb4; }
.container > div:nth-child(3n) { background-color: #88d8b0; }
.container > div:nth-child(2n) { background-color: #ff6f69; }
.container > div:nth-child(4n) { background-color: #ffcc5c; }
.panels > div:nth-child(1n) { background-color: #96ceb4; }
<div class="container">
<div class="header">HEADER</div>
<div class="jumbo">JUMBO</div>
<div class="panels">
<div class="panel1">PANEL1</div>
<div class="panel2">PANEL2</div>
<div class="panel3">PANEL3</div>
</div>
<div class="content">CONTENT</div>
<div class="footer">FOOTER</div>
</div>
请记住,网格容器中存在三个结构级别:
网格属性只在父子之间起作用。
因此,当您在容器上应用网格居中属性时,它们将应用于元素,而不是内容。要使内容居中,您需要将元素视为父项,将内容视为子项。
这里有对这些概念和方法的更深入的解释:Centering in CSS Grid
关于html - 网格项不会占据父容器的全高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50894127/
我有一个布局对话框 主题 View .xml
我有两个 JPanel 放置在 JFrame 内,我希望它们都占据框架的整个宽度,但我不知道如何。另外,我不是 setBounds() 方法的忠实粉丝,因此我想知道是否有一种方法可以使组件采用完整宽度
我希望我的图表占据 100% 的 View ,我只想查看图表。我想删除那个空白。但我得到了这个结果: 这是我的 XMl 布局: 最佳答案 从 RelativeLayout 中删
我遇到以下困境: 单元格的尺寸正确,但 imageView 未填充单元格。我尝试通过控制从 imageView 拖动到单元格来向 imageView 添加约束。 这仍然没有填充单元格内的 imageV
我在显式宽度的父项中有许多子项,例如 document.querySelector('.parent').scrollTo(100,0) .parent { overflow: auto; w
我有一个 TextBox,旁边是一个带有 float:right 的 Button,它们都在一个具体宽度的 div 中。 Button 具有由宽度和高度定义的具体尺寸。我希望 TextBox 占据 B
我有一个如下所示的产品模板: 它的 html 是:
你好,当我的应用程序在网络上是响应式应用程序时,我遇到了一个小问题,当我调整浏览器大小时,一切看起来都很好,但是当我选择响应式选项时,我遇到了问题,看起来我的导航栏占用了大约 90%屏幕宽度而不是 1
我正在尝试创建 QScrollArea,其中 QHBoxLayout 包含一行 od QWidgets。我希望小部件占据所有可用高度并相应地调整其大小。 我的小部件是 QLabel,在内部显示按比例缩
我有一个具有以下项目布局的自定义 ListView : 除了 start_point 和 end_point 的高度不相等外,它显示的大部分与我想要的一样
我发现如果我有一个JMenuBar,并且它的最后一个元素(最右边的一个)是一个JMenuItem,它将占据上所有剩余的空白空间JMenuBar,这绝对不是我们想要的。 将“关于”JMenuItem 想
我在特定页面中有一个 DropDownMenu,该页面呈现在表格列内。 当我点击打开菜单时,它以 100% 的页面高度打开。 我在文档和社区中都找不到类似的东西。 我与子组件没有相对/绝对容器关系。
我有以下片段。它有一个问题 - Enter your passcode: td 占用太多空间,而且看起来很丑。 我尝试将 width:50% 设置为那个,但没有成功。 我想要实现的是输入字段与 Exp
这个问题在这里已经有了答案: CSS Single-column layout centered fixed-width 100% height w header and footer (5 个答案
我有以下网页。 我正在尝试让蓝红白渐变填充整个屏幕高度。目前我在内容容器的两侧都有两个标志,它们都被一个主容器包围。 .container{ width: 100%; background:
我无法让复选框 div 占据 100% 的高度,如下面的代码/ fiddle 所示。正如您从第二个 child 看到的那样,高度为 100%(悬停不会占据 100% 的高度),但我也希望第一个 chi
如何使 container>ul>li>a 占据 container 的整个高度? http://www.bootply.com/119627 除非 100%,否则我不想设置它的高度 最佳答案 您可以
我有一个 super View ,我在其中添加两个 subview (subview1 和 subview2)。 我希望 subview1 与 superview 具有相同的宽度并与 supervie
我有几个嵌套的 DIV。无论屏幕大小如何,我都希望我的第二个 Div 占据视口(viewport)的 100%。我在这个 div 中有两个图像和两个按钮。每当屏幕“变大或变小”时,按钮就会被推到父 d
在下面的代码中,我将 div 与类 container 的 display 属性设置为 flex 并且它有两个 div 类 sidebarcontainer 和 mainpage 的 child ,我
我是一名优秀的程序员,十分优秀!