【发布时间】:2019-10-03 18:58:07
【问题描述】:
考虑一个绝对定位的<aside>,它有一个display: grid 和grid-template-columns。
我希望黄色框出现在蓝色框的左侧,并且都与容器(黑框)的右侧对齐。而是将黄色框放置在蓝色框的顶部。
另请注意,将.fix1 或.fix2 添加到<aside> 会使网格按预期将项目排成一排(但会破坏其他内容)。
为什么网格项目(<span>'s)放在一列而不是一行?如何解决这个问题仍然使用 CSS 网格定位内容? (我对 Flexbox、浮动等不感兴趣)
main {
width: 300px;
}
div {
position: relative;
}
section {
width: 100%;
height: 30px;
background-color: black;
}
aside {
position: absolute;
display: grid;
grid-template-columns: repeat(auto-fill, 2em);
top: 0;
right: 5px;
height: 100%;
}
span {
display: block;
}
span:nth-child(1) {
background-color: yellow;
}
span:nth-child(2) {
background-color: blue;
}
/* Adding this class to <aside>
fixes the issue, but aligns
grid contents to the left */
.fix1 {
width: 100%;
}
/* Adding this class to <aside>
fixes the issue, but breaks
the placement of <aside> */
.fix2 {
position: relative;
}
<main>
<div>
<aside class="">
<span>.</span>
<span>.</span>
</aside>
<section/>
</div>
</main>
【问题讨论】:
标签: css css-position css-grid