display:grid;和 和有display:inline-grid;什么区别?
它们在此代码中具有相同的效果。在这段代码中,哪个更好用?
<body>
<main>
<p>box1</p>
<p>box2</p>
<p>box3</p>
<p>box4</p>
</main>
</body>
Run Code Online (Sandbox Code Playgroud)
* {
margin: 0px;
padding: 0px;
border: 0px;
box-sizing: border-box;
}
main {
display: grid; /* Not there */
grid-template-columns: repeat(4, 1fr);
grid-template-rows: 100px;
grid-gap: 20px;
}
p {
background-color: #eee;
/* In there */
display: grid; /* inline-grid */
place-items: center;
}
Run Code Online (Sandbox Code Playgroud) 当我添加 auto-fit 或 auto-fill 而不是数字时,它无法正常工作。
grid-template-columns: repeat(auto-fit, minmax(max-content, max-content));
Run Code Online (Sandbox Code Playgroud)
但是当我添加数字而不是最大内容时它可以正常工作。
grid-template-columns: repeat(auto-fit, minmax(50px, max-content));
Run Code Online (Sandbox Code Playgroud)
但我希望网格的大小与内容的大小相同(适合)。像这样:
grid-template-columns: repeat(30, minmax(max-content, max-content));
Run Code Online (Sandbox Code Playgroud)
(使用自动调整或自动填充)。我怎样才能做到这一点?
grid-template-columns: repeat(auto-fit, minmax(max-content, max-content));
Run Code Online (Sandbox Code Playgroud)
grid-template-columns: repeat(auto-fit, minmax(50px, max-content));
Run Code Online (Sandbox Code Playgroud)
当我添加以下代码时:
place-items: center;
Run Code Online (Sandbox Code Playgroud)
仅文本的背景颜色在变化。
当我删除此代码时:
place-items: center;
Run Code Online (Sandbox Code Playgroud)
整个列的颜色正在改变。
place-items: center;
Run Code Online (Sandbox Code Playgroud)
place-items: center;
Run Code Online (Sandbox Code Playgroud)
为什么会这样呢?