【发布时间】:2022-02-05 20:04:04
【问题描述】:
是否可以在不调整标记并使用 CSS Grid 或 Flexbox 的情况下实现以下布局?
+-------------------------+
|| || ||
|+-+ +-----------+|
| |
| |
| |
+-------------------------+
该行中有两个项目,第一个向左对齐,第二个与页面中心对齐,并将剩余宽度填充到右侧。我尝试了各种方法,例如没有宽度的空的第三个网格项目等,但没有运气。这是我最后一次尝试,很好地说明了这个问题。
header {
display: grid;
grid-template-columns: auto auto;
justify-items: stretch;
}
ul {
display: flex;
}
li:last-child {
margin-left: auto;
}
/* for demo only */
ul {
padding: 0;
margin: 0;
list-style: none;
}
li {
padding: 5px;
background: #aaa;
}
p {
text-align: center;
}
<header>
<span>Title</span>
<nav>
<ul>
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
</ul>
</nav>
</header>
<p>| true center |</p>
【问题讨论】:
-
the second aligned to the center of the page and filling the remaining width, with a right aligned child--> 对不起,但这没有意义...对齐到中心并填充剩余宽度? -
@TemaniAfif 是的,我已经更新了描述以使其更清晰。
-
好吧,这仍然令人困惑:)
the second aligned to the center of the page and filling the remaining width to the right--> 如何将某些东西对齐到中心并同时填充宽度?这不合逻辑 -
@TemaniAfif 好吧,这就是问题所在。如果您运行 sn-p,示例应该是不言而喻的。
-
不能单独使用 Grid/Flexbox,可以使用定位或 JS。