【问题标题】:How to make one of vertically aligned children shrink to width set by other children?如何使垂直对齐的孩子之一缩小到其他孩子设置的宽度?
【发布时间】:2018-07-24 12:56:42
【问题描述】:

问题

有一个内联容器,其中包含 2 个垂直堆叠的子容器。如下图:

顶部的绿色子项应将其宽度缩小到底部橙色子项的宽度,将文本包裹在自身内部。所需的布局如下图所示:

问题:如何单独使用 CSS 完成?

其他情况

橙色容器比绿色容器宽(下图)。绿色保持在它的自然最大宽度。请注意,即使解决方案将绿色框拉伸到 100%,通过为绿色框添加一个额外的容器然后将绿色框居中居中,实现目标解决方案也很容易。

橙色框比父级的最大可能宽度更宽(下图)。橙色应该开始包裹。

注意事项

  • 子项的内容是动态的 - 解决方案需要使用可变宽度的子项。
  • 父级是内联的 - 它的宽度不是 100%,而是它最宽的子级的宽度。
  • 浏览器支持并不重要。解决方案甚至可以使用 CSS Grid。

代码

小提琴

http://jsfiddle.net/twfky2cr/24/

HTML

<div class="container">
  <div class="fitChild">
    Some long text that I would like to wrap to fit the container size
    set by the orange box
  </div>
  <div class="fullWidthChild">
    This one should set the container size
  </div>
</div>

CSS

.container {
  display: inline-flex;
  flex-direction: column;
  flex-wrap: nowrap;
  align-items: center;
  background-color: black;
}

.container,
.fitChild,
.fullWidthChild {
  padding: 10px;
}

.fitChild {
  color: white;
  background-color: green;;
}

.fullWidthChild {
  margin-top: 10px;
  background-color: orange;
}

【问题讨论】:

  • 它们的宽度是否为100%,并控制父级的宽度?或者您是否希望根据最大或最小 div 的宽度使其更具动态性?
  • 哦,是的,我忘了澄清这一点。我将在问题中添加注释。
  • 如果 2 个孩子有很多文字怎么办?假设每个孩子有1000个单词,那么宽度应该是多少?
  • @Observer:然后父容器应该增长到与父容器一样宽,然后子容器应该开始包裹。
  • 我不认为这只能通过 css 来实现。但是用js就很简单了

标签: html css


【解决方案1】:

我有一个部分解决方案。

http://jsfiddle.net/twfky2cr/166/

我将display: tablewidth:1px 用于单元格。

<div class="table">
  <div class="row">
    <span class="cell wrap">
      Some long text that I would like to wrap to fit the container size
    </span>
  </div>
  <div class="row">
    <span class="cell nowrap">
      This one should set the container size
    </span>
  </div>
</div>

.table {
  background-color: Black;
  display: table;
  padding: 1rem;
}

.row {
  display: table-row;
}

.cell {
  padding: 1rem;
  display: table-cell;
  width: 1px;
}

.wrap {
  color: white;
  background-color: Green;
  white-space: wrap;
}

.nowrap {
  background-color: Orange;
  white-space: nowrap;
}    

【讨论】:

【解决方案2】:

在这里查看这个小提琴http://jsfiddle.net/kjr9aghp/

$(document).ready(function() {

    $('.fitChild').width($('.fullWidthChild').width());

});

【讨论】:

  • 如果 fitChild 的字符数少于 fullWitdthChild 怎么办?
  • 虽然这个解决方案可以工作(@Observer 指出它需要一些小的改进),但我主要感兴趣的是是否可以仅使用 CSS。现在问题中已经澄清了这一点。
【解决方案3】:
 .container {
   display: inline-flex;
   flex-direction: column;
   flex-wrap: nowrap;
   align-items: center;
   background-color: black;
 }

 .container,
 .fitChild,
 .fullWidthChild {
   padding: 10px;
   width: 100% !important;
 }

 .mainwrapper{
   max-width:50%;
 }

.fitChild {
  color: white;
  background-color: green;;
}

.fullWidthChild {
  margin-top: 10px;
  background-color: orange;
}



<div class="container">
  <div class="mainwrapper">
    <div class="fitChild">
      Some long text that I would like to wrap to fit
      the container size set by the orange box
    </div>
    <div class="fullWidthChild">
      This one should set the container size
    </div>
  </div>
</div>

【讨论】:

  • 请添加您更改的cmets
  • .mainwrapper{ 最大宽度:50%; }
  • @RjAdii:修改橙色框内容时不起作用。见这里:jsfiddle.net/twfky2cr/66
  • @Robert Kusznier 请检查我已经更新了这个橙色框的内容,但看起来不错jsfiddle.net/dkb65r73
  • @RjAdii:不,它仍然没有抓住重点。增加表格大小,橙色框应该保持在它的小宽度,但它没有。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-02
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 2018-05-23
  • 2018-03-21
相关资源
最近更新 更多