我正在尝试对齐由3个内容块组成的顶部菜单。
我要实现的目标是:
如果所有3个块的大小都相同,我可以使用flexbox(如代码段中所示),但它们不是,所以不会产生我需要的输出。
相反,flexbox在3个块之间放置了相等的空间-导致中间块偏离中心对齐。
我想知道这是否可以通过flexbox实现,或者如果不能,则可以采用其他解决方案。这需要在生产中稳健地工作,因此由于支持不足,因此不适用“网格”解决方案。
.container {
margin: 20px 0;
}
.row {
background-color: lime;
display: flex;
justify-content: space-between;
}
.item {
background-color: blue;
color: #fff;
padding: 16px;
}
<div class="container">
<div class="row">
<div class="item">left, slightly longer</div>
<div class="item">center, this item is much longer</div>
<div class="item">right</div>
</div>
</div>
答案 0 :(得分:0)
您能否给flex-grow:1作为商品类别并检查
.item {
background-color: blue;
color: #fff;
padding: 16px;
flex-grow:1;
}
希望这就是您要寻找的
答案 1 :(得分:0)
您可以考虑将flex-grow:1;flex-basis:0%
用于左右元素,然后使用text-align
将内容对齐。我添加了一个额外的包装程序,以仅将背景保留在文本周围。
诀窍是通过仅删除中间内容并将其平均拆分为左右元素来计算可用空间。
.container {
margin: 20px 0;
padding-top:10px;
background:linear-gradient(#000,#000) center/5px 100% no-repeat; /*the center*/
}
.row {
background-color: lime;
display: flex;
color: #fff;
}
.item:not(:nth-child(2)) {
flex-basis: 0%;
flex-grow: 1;
}
.item:last-child {
text-align: right;
}
.item span{
background-color: blue;
display:inline-block;
padding: 16px;
height:100%;
box-sizing:border-box;
}
<div class="container">
<div class="row">
<div class="item"><span>left, slightly longer</span></div>
<div class="item"><span>center, this item is much longer</span></div>
<div class="item"><span>right</span></div>
</div>
</div>
答案 2 :(得分:0)
替代使用显示表(一个古老的受支持的网格)。
引用https://www.w3schools.com/cssref/pr_tab_table-layout.asp
如果第一行中没有宽度,则无论单元格内的内容如何,列的宽度在表中均等分配
.container {
display: table;
table-layout: fixed
} // would divide cells equally along table's 100% width.
.row {
display: table-row
}
.item {
display: table-cell
}
答案 3 :(得分:0)
我问什么似乎是一个非常相似的问题,堆栈溢出将我引导到这里。 @Paolamoralesval的响应使我意识到可以在CSS网格中实现所需的效果。现在,网格支持已经非常普遍了,我希望这可以满足每个人的需求。我相信这种解决方案可以完全响应窗口的大小以及标题项的高度和宽度,如果您在查看代码片段的窗口中调整了大小,应该会看到它。
.header {
grid-row: 1;
grid-column: 1;
display: grid;
grid-template-rows: min-content;
grid-template-columns: 1fr 1fr 1fr;
}
.header-left {
justify-self: start;
align-self: center;
text-align: left;
background-color: red;
}
.header-center {
justify-self: center;
align-self: center;
text-align: center;
background-color: green;
}
.header-right {
justify-self: end;
align-self: center;
text-align: right;
background-color: blue;
}
.shrink-kitty {
width: 200px;
}
<html>
<body>
<div class="page">
<div class="header">
<div class="header-left">
<img class="shrink-kitty" src="https://upload.wikimedia.org/wikipedia/commons/thumb/b/bb/Kittyply_edit1.jpg/1280px-Kittyply_edit1.jpg"/><br/>
By David Corby<br/>
Edited by: <a href="//commons.wikimedia.org/wiki/User:Arad" title="User:Arad">Arad</a><br/><a href="//commons.wikimedia.org/wiki/File:Kittyplya03042006.JPG" title="File:Kittyplya03042006.JPG">Image:Kittyplya03042006.JPG<a><br/><a href="https://creativecommons.org/licenses/by/2.5" title="Creative Commons Attribution 2.5">CC BY 2.5</a>, <a href="https://commons.wikimedia.org/w/index.php?curid=1839754">Link</a>
</div>
<div class="header-center">In the middle</div>
<div class="header-right">
Much much much much more on the right hand side</br>
Indeed two lines
</div>
</div>
<div class="body">Body of the page</div>
<div class="footer">At the bottom</div>
</div>
</body>
</html>