html - Flexbox 中间列居中问题
问题描述
如何防止左列增长?希望徽标列居中,左右列占据结果空间。做了一个小提琴来演示效果。 https://jsfiddle.net/roberthenniger/1qyeghm7/
.nav-column:nth-child(1), .nav-column:nth-child(3) {
flex-grow:1;
flex-basis:50%;
}
我们有一个 javascript 来检查内容宽度,然后更改为不同的布局,但我仍然不希望外部列增长。我知道它会增长,因为在左边还有空间。
body .nav {
display: flex;
flex-direction: row;
justify-content: center;
margin-bottom: 100px;
}
body .nav span {
white-space: nowrap;
}
body .nav .nav-column:nth-child(1) {
text-align: left;
justify-content: flex-start;
flex-grow: 1;
flex-basis: 50%;
}
body .nav .nav-column:nth-child(2) {
text-align: center;
justify-content: center;
background-color: #99CCFF;
flex-basis: 160px;
}
body .nav .nav-column:nth-child(3) {
text-align: right;
justify-content: flex-end;
flex-grow: 1;
flex-basis: 50%;
}
<div class="nav">
<div class="nav-column">
<span>Menu Links with long content but it should not overlap and not push to the right</span>
</div>
<div class="nav-column">
<span>Logo</span>
</div>
<div class="nav-column">
<span>CTA</span>
</div>
</div>
<div class="nav">
<div class="nav-column">
<span>here it looks fine</span>
</div>
<div class="nav-column">
<span>Logo</span>
</div>
<div class="nav-column">
<span>CTA</span>
</div>
</div>
解决方案
添加flex: 1
到右列和左列并添加一个长省略号span
(我注意到您有white-space: nowrap
内容),方法是添加:
overflow: hidden;
text-overflow: ellipsis;
请参阅下面的演示:
body .nav {
display: flex;
flex-direction: row;
justify-content: center;
margin-bottom: 100px;
}
body .nav span {
white-space: nowrap;
}
body .nav .nav-column:nth-child(1) {
text-align: left;
justify-content: flex-start;
/*flex-grow: 1;*/
/*flex-basis: 50%;*/
flex: 1; /* ADDED */
overflow: hidden; /* ADDED */
text-overflow: ellipsis; /* ADDED */
}
body .nav .nav-column:nth-child(2) {
text-align: center;
justify-content: center;
background-color: #99CCFF;
flex-basis: 160px;
}
body .nav .nav-column:nth-child(3) {
text-align: right;
justify-content: flex-end;
/*flex-grow: 1;*/
/*flex-basis: 50%;*/
flex: 1; /* ADDED */
overflow: hidden; /* ADDED */
text-overflow: ellipsis; /* ADDED */
}
<div class="nav">
<div class="nav-column">
<span>Menu Links with long content but it should not overlap and not push to the right</span>
</div>
<div class="nav-column">
<span>Logo</span>
</div>
<div class="nav-column">
<span>CTA</span>
</div>
</div>
<div class="nav">
<div class="nav-column">
<span>here it looks fine</span>
</div>
<div class="nav-column">
<span>Logo</span>
</div>
<div class="nav-column">
<span>CTA</span>
</div>
</div>
请注意,只有添加省略号才能使您的徽标居中。
body .nav {
display: flex;
flex-direction: row;
justify-content: center;
margin-bottom: 100px;
}
body .nav span {
white-space: nowrap;
}
body .nav .nav-column:nth-child(1) {
text-align: left;
justify-content: flex-start;
flex-grow: 1;
flex-basis: 50%;
overflow: hidden; /* ADDED */
text-overflow: ellipsis; /* ADDED */
}
body .nav .nav-column:nth-child(2) {
text-align: center;
justify-content: center;
background-color: #99CCFF;
flex-basis: 160px;
}
body .nav .nav-column:nth-child(3) {
text-align: right;
justify-content: flex-end;
flex-grow: 1;
flex-basis: 50%;
overflow: hidden; /* ADDED */
text-overflow: ellipsis; /* ADDED */
}
<div class="nav">
<div class="nav-column">
<span>Menu Links with long content but it should not overlap and not push to the right</span>
</div>
<div class="nav-column">
<span>Logo</span>
</div>
<div class="nav-column">
<span>CTA</span>
</div>
</div>
<div class="nav">
<div class="nav-column">
<span>here it looks fine</span>
</div>
<div class="nav-column">
<span>Logo</span>
</div>
<div class="nav-column">
<span>CTA</span>
</div>
</div>
推荐阅读
- clone - 如何在 Perl 6 中制作对象的单独副本?
- android - 读取json文件表单主机在活动中显示白屏我该如何解决这个问题?
- linux - 从所有子目录中的所有 mp3 文件中删除前 3 个字符和后 1 个字符
- oauth-2.0 - oauth2 - 在 OIDC 之前如何识别用户?
- javascript - plotOptions.column.pointPlacement 一致性问题
- visual-studio-code - 结合两个 launch.json 配置在 Visual Studio Code 中按顺序运行
- jsf - JSF 请求范围 Bean 不在页面中显示数据
- android - 文本搜索时 Android WebView 视口溢出
- sql - SQL SELECT 取决于值
- file - Unix shell 中文件路径中的单引号或无引号