想象一下下面的布局,其中圆点代表盒子之间的空间:

[Left box]......[Center box]......[Right box]

当我移除右边的方框时,我希望中间的方框仍然在中间,就像这样:

[Left box]......[Center box].................

如果我移除左边的方框也是一样的。

................[Center box].................

现在,当中心框中的内容变长时,它将根据需要占用尽可能多的可用空间,同时保持居中。左右方框永远不会缩小,因此当没有空格时,overflow:hidden和text-overflow:省略号将生效,破坏内容;

[Left box][Center boxxxxxxxxxxxxx][Right box]

以上都是我的理想情况,但是我不知道如何达到这个效果。因为当我创建一个这样的伸缩结构时:

.parent {
    display : flex; // flex box
    justify-content : space-between; // horizontal alignment
    align-content   : center; // vertical alignment
}

如果左右方框的大小完全相同,我就会得到想要的效果。然而,当其中一个的大小不同时,居中的盒子就不再是真正的居中。

有人能帮我吗?

更新

一个自我证明很好,这很理想

.leftBox {
     justify-self : flex-start;
}

.rightBox {
    justify-self : flex-end;
}

当前回答

如果左右方框的大小完全相同,我就会得到想要的效果。然而,当其中一个大小不同时,居中的盒子就不再是真正的居中了。有人能帮我吗?

下面是一个使用flexbox将中间项居中的方法,而不考虑兄弟项的宽度。

主要特点:

纯CSS 没有绝对定位 没有JS / jQuery

使用嵌套的伸缩容器和自动边距:

.container { display: flex; } .box { flex: 1; display: flex; justify-content: center; } .box:first-child > span { margin-right: auto; } .box:last-child > span { margin-left: auto; } /* non-essential */ .box { align-items: center; border: 1px solid #ccc; background-color: lightgreen; height: 40px; } p { text-align: center; margin: 5px 0 0 0; } <div class="container"> <div class="box"><span>short text</span></div> <div class="box"><span>centered text</span></div> <div class="box"><span>loooooooooooooooong text</span></div> </div> <p>&#8593;<br>true center</p>

下面是它的工作原理:

顶层的div (.container)是一个伸缩容器。 每个子div (.box)现在都是一个伸缩项。 每个.box项被赋予flex: 1,以便平均分配容器空间(更多细节)。 现在这些项占用了行中的所有空间,并且宽度相等。 将每个项目设置为(嵌套的)伸缩容器,并添加justify-content: center。 现在每个span元素都是居中的伸缩项。 使用弹性自动边距将外跨度左移和右移。

你也可以放弃合理内容,只使用汽车利润。

但合理内容在这里是可行的,因为汽车利润总是优先考虑的。

8.1. 与auto对齐 利润率 在通过justify-content和align-self对齐之前,any 正自由空间分布到该维度的自动边距。

其他回答

你也可以使用这个简单的方法来达到中间元素的精确中心对齐:

.container {
    display: flex;
    justify-content: space-between;
}

.container .sibling {
    display: flex;
    align-items: center;
    height: 50px;
    background-color: gray;
}

.container .sibling:first-child {
    width: 50%;
    display: flex;
    justify-content: space-between;
}

.container .sibling:last-child {
    justify-content: flex-end;
    width: 50%;
    box-sizing: border-box;
    padding-left: 100px; /* .center's width divided by 2 */
}

.container .sibling:last-child .content {
    text-align: right;
}

.container .sibling .center {
    height: 100%;
    width: 200px;
    background-color: lightgreen;
    transform: translateX(50%);
}

codepen: https://codepen.io/ErAz7/pen/mdeBKLG

虽然我在这个问题上可能晚了,但所有这些解决方案似乎都很复杂,而且根据你所面临的情况可能不会起作用。

非常简单,只需要用position: absolute来包装你想要居中的组件,而让其他两个组件用justify-content: space来分隔,如下所示:

CSS:

.container { display: flex; justify-content: space-between; align-items: center; background-color: lightgray; } .middle { position: absolute; margin-left: auto; margin-right: auto; /* You should adapt percentages here if you have a background ; else, left: 0 and right: 0 should do the trick */ left: 40%; right: 40%; text-align: center; } /* non-essential, copied from @Brian Morearty answer */ .element { border: 1px solid #ccc; background-color: lightgreen; } p { margin: 5px; padding: 5px; } <div class="container"> <p class="element">First block</p> <p class="middle element">Middle block</p> <p class="element">Third THICC blockkkkkkkkk</p> </div>

关键是要使用弹性基准。那么解决方案很简单:

.parent {
    display: flex;
    justify-content: space-between;
}

.left, .right {
   flex-grow: 1;
   flex-basis: 0;
}

CodePen可以在这里找到。

你可以这样做:

.bar { display: flex; background: #B0BEC5; } .l { width: 50%; flex-shrink: 1; display: flex; } .l-content { background: #9C27B0; } .m { flex-shrink: 0; } .m-content { text-align: center; background: #2196F3; } .r { width: 50%; flex-shrink: 1; display: flex; flex-direction: row-reverse; } .r-content { background: #E91E63; } <div class="bar"> <div class="l"> <div class="l-content">This is really long content. More content. So much content.</div> </div> <div class="m"> <div class="m-content">This will always be in the center.</div> </div> <div class="r"> <div class="r-content">This is short.</div> </div> </div>

稍微健壮一点的网格解决方案如下所示:

.container { overflow: hidden; border-radius: 2px; padding: 4px; background: orange; display: grid; grid-template-columns: minmax(max-content, 1fr) auto minmax(max-content, 1fr); } .item > div { display: inline-block; padding: 6px; border-radius: 2px; background: teal; } .item:last-child > div { float: right; } <div class="container"> <div class="item"><div contenteditable>edit the text to test the layout</div></div> <div class="item"><div contenteditable>just click me and</div></div> <div class="item"><div contenteditable>edit</div></div> </div>

在这里你可以看到它在Codepen: https://codepen.io/benshope2234/pen/qBmZJWN