css 网格的垂直间距问题
problem with vertical spacing with css grid
注意 div.text
前两个 div 总是在最前面(第二个 div 会很粘 div)并且其他 div 将由 js
insertAdjacentHTML()
从底部动态添加
我在两个 div.text
垂直
之间得到一个额外的 space
是否有可能用 css grid
解决的方法?
最终结果
*{
box-sizing: border-box;
margin:0;
padding:0;
}
h1,h2,h3,h4,h5,h6{
margin:0;
padding:0;
}
.grid-container {
display: grid;
grid-template-columns: repeat(2,1fr) ;
grid-gap: 1px;
background-color: #f7ca18;
padding: 10px;
color:#000;
height:300px;
}
.grid-container > div.item {
display: grid;
grid-template-columns: 1fr ;
grid-template-rows: auto;
background-color: #f4d03f;
text-align: center;
font-size: 14px;
overflow-y:auto;
overflow-x:hidden;
}
.grid-container > div.item div.text {
align-self: end;
display:grid;
grid-template-columns: 1fr max-content ;
}
.grid-container > div.item div.text:nth-child(2){
position: -webkit-sticky;
position: sticky;
top: 0;
background:red;
align-self: start;
}
<div class="grid-container">
<div class="item">
<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
</p>
</div>
<div class="item">
<div class="text">
<span>Web App</span>
</div>
<div class="text">
<h1>Sticky Dive</h1>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
</div>
</div>
添加两个或三个 div.text
时垂直 space 的初始问题
*{
box-sizing: border-box;
margin:0;
padding:0;
}
h1,h2,h3,h4,h5,h6{
margin:0;
padding:0;
}
.grid-container {
display: grid;
grid-template-columns: repeat(2,1fr) ;
grid-gap: 1px;
background-color: #f7ca18;
padding: 10px;
color:#000;
height:300px;
}
.grid-container > div.item {
display: grid;
grid-template-columns: 1fr ;
grid-template-rows: auto;
background-color: #f4d03f;
text-align: center;
font-size: 14px;
overflow-y:auto;
overflow-x:hidden;
}
.grid-container > div.item div.text {
align-self: end;
display:grid;
grid-template-columns: 1fr max-content ;
}
.grid-container > div.item div.text:nth-child(2){
position: -webkit-sticky;
position: sticky;
top: 0;
background:red;
align-self: start;
}
<div class="grid-container">
<div class="item">
<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
</p>
</div>
<div class="item">
<div class="text">
<span>Web App</span>
</div>
<div class="text">
<h1>Sticky Dive</h1>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
</div>
</div>
添加两个 div 时最初的样子
尝试删除 .grid-container>div.item { ... }
中的 align-content: end;
。我在代码里注释了。
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
h1,h2,h3,h4,h5,h6 {
margin: 0;
padding: 0;
}
.grid-container {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-gap: 1px;
background-color: #f7ca18;
padding: 10px;
color: #000;
height: 300px;
}
.grid-container>div.item {
display: grid;
grid-template-columns: 1fr;
grid-template-rows: auto;
/*align-content: end;*/ /* removed this */
align-items: end; /* added this */
background-color: #f4d03f;
text-align: center;
font-size: 14px;
overflow-y: auto;
overflow-x: hidden;
height: 100%;
}
.grid-container>div.item div.text:first-child {
align-self: start;
}
.grid-container>div.item div.text:first-child h1 {
/*align-self: start;*/ /* removed this */
position: -webkit-sticky;
position: sticky;
top: 0;
background: red;
color: white;
}
.grid-container>div.item div.text:not(:first-child) {
display: grid;
grid-template-columns: 1fr max-content;
}
<div class="grid-container">
<div class="item">
<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
</div>
<div class="item">
<div class="text">
<p>Web App</p>
<h1>Sticky Dive</h1>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
</div>
</div>
注:如果谁能给出更好的解决方案。答案将被接受
我通过包装标签解决了我的问题
<div class="text">
<span>Web App</span>
</div>
<div class="text">
<h1>Sticky Dive</h1>
</div>
<div class="text">
<div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
</div>
初始结果
*{
box-sizing: border-box;
margin:0;
padding:0;
}
h1,h2,h3,h4,h5,h6{
margin:0;
padding:0;
}
.grid-container {
display: grid;
grid-template-columns: repeat(2,1fr) ;
grid-gap: 1px;
background-color: #f7ca18;
padding: 10px;
color:#000;
height:300px;
}
.grid-container div.item {
display: grid;
grid-template-columns: 1fr ;
grid-template-rows: min-content ;
align-items: start;
background-color: #f4d03f;
text-align: center;
font-size: 14px;
overflow-y:auto;
overflow-x:hidden;
}
.grid-container > div.item:nth-child(2) div.text {
margin-top: 2px;
display:grid;
grid-template-columns: 1fr ;
}
.grid-container > div.item:nth-child(2) > div.text:nth-child(2){
position: -webkit-sticky;
position: sticky;
top: 0;
background:red;
align-self: start;
}
.grid-container > div.item:nth-child(2) > div.text:nth-child(3) {
align-self: end;
}
.grid-container > div.item:nth-child(2) > div.text:nth-child(3) div.message{
display: grid;
grid-template-columns: 1fr max-content;
grid-template-rows: min-content ;
border: 1px dotted royalblue;
margin-top: 2px;
padding: 2px 5px;
}
<div class="grid-container">
<div class="item">
<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
</p>
</div>
<div class="item">
<div class="text" >
<span>Web App</span>
</div>
<div class="text">
<h1>Sticky Dive</h1>
</div>
<div class="text">
<div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
</div>
</div>
</div>
最终结果
*{
box-sizing: border-box;
margin:0;
padding:0;
}
h1,h2,h3,h4,h5,h6{
margin:0;
padding:0;
}
.grid-container {
display: grid;
grid-template-columns: repeat(2,1fr) ;
grid-gap: 1px;
background-color: #f7ca18;
padding: 10px;
color:#000;
height:300px;
}
.grid-container div.item {
display: grid;
grid-template-columns: 1fr ;
grid-template-rows: min-content ;
align-items: start;
background-color: #f4d03f;
text-align: center;
font-size: 14px;
overflow-y:auto;
overflow-x:hidden;
}
.grid-container > div.item:nth-child(2) div.text {
margin-top: 2px;
display:grid;
grid-template-columns: 1fr ;
}
.grid-container > div.item:nth-child(2) > div.text:nth-child(2){
position: -webkit-sticky;
position: sticky;
top: 0;
background:red;
align-self: start;
}
.grid-container > div.item:nth-child(2) > div.text:nth-child(3) {
align-self: end;
}
.grid-container > div.item:nth-child(2) > div.text:nth-child(3) div.message{
display: grid;
grid-template-columns: 1fr max-content;
grid-template-rows: min-content ;
border: 1px dotted royalblue;
margin-top: 2px;
padding: 2px 5px;
}
<div class="grid-container">
<div class="item">
<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
</p>
</div>
<div class="item">
<div class="text" >
<span>Web App</span>
</div>
<div class="text">
<h1>Sticky Dive</h1>
</div>
<div class="text">
<div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div><div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div><div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div><div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div><div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div><div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div><div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
</div>
</div>
</div>
您可以使用 grid-template-rows: auto 1fr;
它会解决您的问题。
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
h1,
h2,
h3,
h4,
h5,
h6 {
margin: 0;
padding: 0;
}
.grid-container {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-gap: 1px;
background-color: #f7ca18;
padding: 10px;
color: #000;
height: 300px;
}
.grid-container>div.item {
display: grid;
grid-template-columns: 1fr;
grid-template-rows: auto 1fr;
background-color: #f4d03f;
text-align: center;
font-size: 14px;
overflow-y: auto;
overflow-x: hidden;
}
.grid-container>div.item div.text {
align-self: end;
display: grid;
grid-template-columns: 1fr max-content;
}
.grid-container>div.item div.text:nth-child(2) {
position: -webkit-sticky;
position: sticky;
top: 0;
background: red;
align-self: start;
}
<div class="grid-container">
<div class="item">
<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content
here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy.
Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.
The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem
Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
</p>
</div>
<div class="item">
<div class="text">
<span>Web App</span>
</div>
<div class="text">
<h1>Sticky Dive</h1>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>1</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>2</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>3</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>4</i></span>
</div>
</div>
</div>
注意 div.text
前两个 div 总是在最前面(第二个 div 会很粘 div)并且其他 div 将由 js
insertAdjacentHTML()
我在两个 div.text
垂直
是否有可能用 css grid
解决的方法?
最终结果
*{
box-sizing: border-box;
margin:0;
padding:0;
}
h1,h2,h3,h4,h5,h6{
margin:0;
padding:0;
}
.grid-container {
display: grid;
grid-template-columns: repeat(2,1fr) ;
grid-gap: 1px;
background-color: #f7ca18;
padding: 10px;
color:#000;
height:300px;
}
.grid-container > div.item {
display: grid;
grid-template-columns: 1fr ;
grid-template-rows: auto;
background-color: #f4d03f;
text-align: center;
font-size: 14px;
overflow-y:auto;
overflow-x:hidden;
}
.grid-container > div.item div.text {
align-self: end;
display:grid;
grid-template-columns: 1fr max-content ;
}
.grid-container > div.item div.text:nth-child(2){
position: -webkit-sticky;
position: sticky;
top: 0;
background:red;
align-self: start;
}
<div class="grid-container">
<div class="item">
<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
</p>
</div>
<div class="item">
<div class="text">
<span>Web App</span>
</div>
<div class="text">
<h1>Sticky Dive</h1>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
</div>
</div>
添加两个或三个 div.text
时垂直 space 的初始问题
*{
box-sizing: border-box;
margin:0;
padding:0;
}
h1,h2,h3,h4,h5,h6{
margin:0;
padding:0;
}
.grid-container {
display: grid;
grid-template-columns: repeat(2,1fr) ;
grid-gap: 1px;
background-color: #f7ca18;
padding: 10px;
color:#000;
height:300px;
}
.grid-container > div.item {
display: grid;
grid-template-columns: 1fr ;
grid-template-rows: auto;
background-color: #f4d03f;
text-align: center;
font-size: 14px;
overflow-y:auto;
overflow-x:hidden;
}
.grid-container > div.item div.text {
align-self: end;
display:grid;
grid-template-columns: 1fr max-content ;
}
.grid-container > div.item div.text:nth-child(2){
position: -webkit-sticky;
position: sticky;
top: 0;
background:red;
align-self: start;
}
<div class="grid-container">
<div class="item">
<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
</p>
</div>
<div class="item">
<div class="text">
<span>Web App</span>
</div>
<div class="text">
<h1>Sticky Dive</h1>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
</div>
</div>
添加两个 div 时最初的样子
尝试删除 .grid-container>div.item { ... }
中的 align-content: end;
。我在代码里注释了。
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
h1,h2,h3,h4,h5,h6 {
margin: 0;
padding: 0;
}
.grid-container {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-gap: 1px;
background-color: #f7ca18;
padding: 10px;
color: #000;
height: 300px;
}
.grid-container>div.item {
display: grid;
grid-template-columns: 1fr;
grid-template-rows: auto;
/*align-content: end;*/ /* removed this */
align-items: end; /* added this */
background-color: #f4d03f;
text-align: center;
font-size: 14px;
overflow-y: auto;
overflow-x: hidden;
height: 100%;
}
.grid-container>div.item div.text:first-child {
align-self: start;
}
.grid-container>div.item div.text:first-child h1 {
/*align-self: start;*/ /* removed this */
position: -webkit-sticky;
position: sticky;
top: 0;
background: red;
color: white;
}
.grid-container>div.item div.text:not(:first-child) {
display: grid;
grid-template-columns: 1fr max-content;
}
<div class="grid-container">
<div class="item">
<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
</div>
<div class="item">
<div class="text">
<p>Web App</p>
<h1>Sticky Dive</h1>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
</div>
</div>
注:如果谁能给出更好的解决方案。答案将被接受
我通过包装标签解决了我的问题
<div class="text">
<span>Web App</span>
</div>
<div class="text">
<h1>Sticky Dive</h1>
</div>
<div class="text">
<div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
</div>
初始结果
*{
box-sizing: border-box;
margin:0;
padding:0;
}
h1,h2,h3,h4,h5,h6{
margin:0;
padding:0;
}
.grid-container {
display: grid;
grid-template-columns: repeat(2,1fr) ;
grid-gap: 1px;
background-color: #f7ca18;
padding: 10px;
color:#000;
height:300px;
}
.grid-container div.item {
display: grid;
grid-template-columns: 1fr ;
grid-template-rows: min-content ;
align-items: start;
background-color: #f4d03f;
text-align: center;
font-size: 14px;
overflow-y:auto;
overflow-x:hidden;
}
.grid-container > div.item:nth-child(2) div.text {
margin-top: 2px;
display:grid;
grid-template-columns: 1fr ;
}
.grid-container > div.item:nth-child(2) > div.text:nth-child(2){
position: -webkit-sticky;
position: sticky;
top: 0;
background:red;
align-self: start;
}
.grid-container > div.item:nth-child(2) > div.text:nth-child(3) {
align-self: end;
}
.grid-container > div.item:nth-child(2) > div.text:nth-child(3) div.message{
display: grid;
grid-template-columns: 1fr max-content;
grid-template-rows: min-content ;
border: 1px dotted royalblue;
margin-top: 2px;
padding: 2px 5px;
}
<div class="grid-container">
<div class="item">
<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
</p>
</div>
<div class="item">
<div class="text" >
<span>Web App</span>
</div>
<div class="text">
<h1>Sticky Dive</h1>
</div>
<div class="text">
<div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
</div>
</div>
</div>
最终结果
*{
box-sizing: border-box;
margin:0;
padding:0;
}
h1,h2,h3,h4,h5,h6{
margin:0;
padding:0;
}
.grid-container {
display: grid;
grid-template-columns: repeat(2,1fr) ;
grid-gap: 1px;
background-color: #f7ca18;
padding: 10px;
color:#000;
height:300px;
}
.grid-container div.item {
display: grid;
grid-template-columns: 1fr ;
grid-template-rows: min-content ;
align-items: start;
background-color: #f4d03f;
text-align: center;
font-size: 14px;
overflow-y:auto;
overflow-x:hidden;
}
.grid-container > div.item:nth-child(2) div.text {
margin-top: 2px;
display:grid;
grid-template-columns: 1fr ;
}
.grid-container > div.item:nth-child(2) > div.text:nth-child(2){
position: -webkit-sticky;
position: sticky;
top: 0;
background:red;
align-self: start;
}
.grid-container > div.item:nth-child(2) > div.text:nth-child(3) {
align-self: end;
}
.grid-container > div.item:nth-child(2) > div.text:nth-child(3) div.message{
display: grid;
grid-template-columns: 1fr max-content;
grid-template-rows: min-content ;
border: 1px dotted royalblue;
margin-top: 2px;
padding: 2px 5px;
}
<div class="grid-container">
<div class="item">
<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
</p>
</div>
<div class="item">
<div class="text" >
<span>Web App</span>
</div>
<div class="text">
<h1>Sticky Dive</h1>
</div>
<div class="text">
<div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div><div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div><div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div><div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div><div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div><div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div><div class="message">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
</div>
</div>
</div>
您可以使用 grid-template-rows: auto 1fr;
它会解决您的问题。
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
h1,
h2,
h3,
h4,
h5,
h6 {
margin: 0;
padding: 0;
}
.grid-container {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-gap: 1px;
background-color: #f7ca18;
padding: 10px;
color: #000;
height: 300px;
}
.grid-container>div.item {
display: grid;
grid-template-columns: 1fr;
grid-template-rows: auto 1fr;
background-color: #f4d03f;
text-align: center;
font-size: 14px;
overflow-y: auto;
overflow-x: hidden;
}
.grid-container>div.item div.text {
align-self: end;
display: grid;
grid-template-columns: 1fr max-content;
}
.grid-container>div.item div.text:nth-child(2) {
position: -webkit-sticky;
position: sticky;
top: 0;
background: red;
align-self: start;
}
<div class="grid-container">
<div class="item">
<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content
here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy.
Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.
The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem
Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
</p>
</div>
<div class="item">
<div class="text">
<span>Web App</span>
</div>
<div class="text">
<h1>Sticky Dive</h1>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>X</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>1</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>2</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>3</i></span>
</div>
<div class="text">
<h6>Title One</h6>
<span>Hello - <i>4</i></span>
</div>
</div>
</div>