【发布时间】:2021-01-18 16:28:34
【问题描述】:
注意div.text前两个div总是在顶部(第二个div也是粘性div),其他div将由jsinsertAdjacentHTML()从底部动态添加
我在两个 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 时垂直空间的初始问题
*{
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 后最初的样子
【问题讨论】:
-
问题不清楚。你想达到什么目标?
-
@Becky
<div class="text">第一个和第二个 div 将从顶部垂直放置.. 其他潜水将从底部开始 -
请..如果有人在阅读我试图实现的问题后不清楚..我提出了我的答案..请检查答案..查看两个结果...
标签: html css flexbox alignment css-grid