【问题标题】:How could prevent grow width of flex div by it's content?如何通过它的内容来防止 flex div 的宽度增长?
【发布时间】:2014-06-16 17:21:30
【问题描述】:

我已经创建了一个带有 flex 的嵌套 div,我希望在增长和收缩时遵循 flex 规则,但是当内部文本或内容变长时,div 会增长太长。在这个小提琴中,我用 “这是一个错误” 对其进行了标记,并且您可以看到此 div 中的文本比其他 div 更长,并且父 flex div 增长了我不想要的。我设置了flex: 1 1 auto;,但我不知道哪里需要更改。任何建议表示赞赏。

小提琴:http://jsfiddle.net/QMaster/JZHag/

【问题讨论】:

  • 你的意思是jsfiddle.net/JZHag/7
  • @Alek 我试图将最大宽度设为百分比,但我认为你是对的,我必须为其设置确切的长度。我添加溢出:隐藏;和一些有用的旁白,我会尽快更新。请问你为什么使用 word-wrap:break-word;因为我不想显示溢出。

标签: html css flexbox


【解决方案1】:

正如我在评论 fiddle 中发布的那样,我使用了静态最大宽度,因为我相信这是适合您的情况的好解决方案。

css

html, body {
    direction: rtl;
    font-family: 'Times New Roman';
}

.WholeDiv{
    margin-right: 20px;
    margin-left: 20px;
    background-color: #ffffff;
    border: 2px solid #e8e8e8;
}

.mainContainer{

    display: -webkit-box;
    display: -moz-box;
    display: -ms-flexbox;
    display: -webkit-flex;
    display: flex;

    -webkit-box-direction: normal;
    -moz-box-direction: normal;
    -ms-flex-direction: row;
    -webkit-flex-direction: row;
    flex-direction: row;

    background-color: blue;
    min-width: 512px;

    height: 232px;
    line-height: 232px;

    z-index: 999;

    text-align: center;
    top: 0px;
    width: 100%;
}

.mainContainer .firstContent {
    flex: 1 1 auto; 
    background: green;
    overflow: hidden;
    min-width: 140px;
    text-align: right; 
}

.mainContainer .secondContent {
    width: 232px;
    min-width: 232px;
    background-color: aqua;
}

.mainContainer .thirdContent {
    flex: 1 1 auto; 
    background-color: red;
    overflow: hidden;
    min-width: 140px;
    text-align: left;
}

.mainContainerVert{

    display: -webkit-box;
    display: -moz-box;
    display: -ms-flexbox;
    display: -webkit-flex;
    display: flex;

    -webkit-box-orient: vertical;
    -moz-box-orient: vertical;
    -ms-flex-direction: column;
    -webkit-flex-direction: column;
    flex-direction: column;


    -webkit-box-align: center;
    -moz-box-align: center;
    -ms-flex-align: center;
    -webkit-align-items: center;
    align-items: center;


    -webkit-box-pack: space-around;
    -moz-box-pack: space-around;
    -ms-flex-pack: space-around;
    -webkit-justify-content: space-around;
    justify-content: space-around;

    -webkit-flex-flow: column no-wrap;
    flex-flow: column no-wrap;    



    -webkit-align-content: center;
    align-content: center;

    background-color: blue;
    /*min-width: 100px;*/
    height: 232px;
    line-height: 232px;
    text-align: center;
    max-width: 100%;
}

.mainContainerVert .dvRow{
    flex: 1 1 auto;
    background-color: black;
    color: white;
    height: 32px;
    line-height: 32px;
    max-height: 32px;
    width: 98%;
    max-width: 300px;
    /*min-width: 100px;*/
    text-align: center;   
    white-space:nowrap;
    display: flex;
}

SPAN{
    /*flex: 1 1 auto;*/
    background-color: orange;
    text-align: center;
    text-overflow: ellipsis;
    width: 300px;
    max-width: 300px;
    /*min-width: 100px;*/
    overflow: hidden;
    /*white-space: no-wrap;*/
    margin: 0 auto;
}

fiddle

同时从span 中删除flex: 1 1 auto;。我觉得没必要。

【讨论】:

  • 正是我需要 .dvRow 或内部跨度本身来填充空间,但不要更改 .firstContent 和 .thirdContent 以使内部文本显示不相等。静态值不能做。如果我们不能解决它很好,但这是我真正的解决方案。如果您能做到,我将不胜感激并将其标记为答案。再次感谢。
  • 不明白你对这个“dvRow 或内部跨度本身填充空间但不改变”的确切含义。什么空间?并且“.thirdContent as to be shown not equal by inside text”:)
  • 你说得对,也许我描述得不好。假设我们改变浏览器宽度,所以正常的行为是增长的 .firstContent 和 .thirdContent 并且必须相等,因为 flex: 1 1 auto;现在,当我们更改浏览器大小而不是当内部文本变长或变短时,我需要这些增长或缩小。在我附加的第一个小提琴中,您可以看到文本“这是错误”影响 .firstContent 宽度并且增长不等于 .thirdContent 。请注意,当窗口大小变大时, .firstContent 再次增长超过 .thirdContent 。我能解释清楚吗? :)
  • 我更改了您可以在 Fiddle 中看到的源代码:jsfiddle.net/QMaster/JZHag/11 并通过添加 display: flex; 来解决这个问题。到 .dvRow 并设置 flex: 1 1 auto;为跨度。实际上,我再次在下层使用了 flex,它似乎可以提供帮助。请阅读并告诉我您的想法。
  • 是的,这就是我的想法(在您的第一条评论之后),但不确定。我认为这会奏效:)
猜你喜欢
  • 1970-01-01
  • 2021-05-27
  • 1970-01-01
  • 2016-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-01
  • 1970-01-01
相关资源
最近更新 更多