使伸缩行拉伸到单元格中内容的高度-内容从容器DIV中渗出

时间:2019-04-09 09:22:45

标签: html css css3 flexbox less

我通常会问有关Angular的问题,但是今天我有关于CSS和FlexBox的问题。在我的应用程序中,我应该在界面上显示一些信息(用户名,公司名称和部门名称),如果信息很长(例如带有多个字符的长名),我希望文本包含在其单元格中并在显示文本的行中显示在成长以适应文本。文本不应渗入下面的行。

我的HTML看起来像这样...

<div class="assignment">
  <!-- Here we want to colums, one to contain the rows of info
      The other to contain a single button -->
  <div class="assignment__details">
    <div class="assignment__row">
      <div class="assignment__row__title">User</div>
      <div class="assignment__row__value">
        Oh this is a long user name
      </div>
    </div>
    <div class="assignment__row">
      <div class="assignment__row__title">Company:</div>
      <div class="assignment__row__value">
        Oh this is a long company name
      </div>
    </div>
    <div class="assignment__row">
      <div class="assignment__row__title">Department:</div>
      <div class="assignment__row__value" >
        This is Such a long Department Name
      </div>
    </div>
  </div>
  <!-- here is the col with the button -->
  <div class="assignment__controls">
    <button mat-icon-button (click)="doSomething()">
      <mat-icon>edit</mat-icon>
    </button>
  </div>
</div>

这是我的CSS(使用BEM编写的文章少)

.assignment {
  display: flex;
  background-color: #C0C0C0;
  padding: 10px;
  width: 300px; // just for demo puropses

  &__details {
    flex: 1 1 80%;
  }

  &__controls {
    display: flex;
    flex: 1 1 20%;
    justify-content: flex-end;
    align-items: center;
  }

  &__row {
    display: flex;
    height: 21px;

    &__title {
      flex: 1 0 50%;
    }

    &__value {
      flex: 1 0 50%;
    }
  }
}

这是一个JS Bin https://jsbin.com/juguvovelo/edit?html,css,output

您会看到文本渗入下面的行并从容器中渗出!如该屏幕截图所示!我正在设置flex-grow中的.assignment__row,但似乎没有得到想要的效果吗?

enter image description here

如果有人可以帮助我解决这个问题,我将非常感激。也许我的HTML结构有问题?

2 个答案:

答案 0 :(得分:2)

您需要删除.assignment__row的高度。这是JSBin link

答案 1 :(得分:1)

height中删除assignment__row

.assignment {
	 display: flex;
	 background-color: #c0c0c0;
	 padding: 10px;
	 width: 300px;
}
 .assignment__details {
	 flex: 1 1 80%;
}
 .assignment__controls {
	 display: flex;
	 flex: 1 1 20%;
	 justify-content: flex-end;
	 align-items: center;
}
 .assignment__row {
	 display: flex;
}
 .assignment__row__title {
	 flex: 1 0 50%;
}
 .assignment__row__value {
	 flex: 1 0 50%;
}
 
<div class="assignment">
  <!-- Here we want to colums, one to contain the rows of info
      The other to contain a single button -->
  <div class="assignment__details">
    <div class="assignment__row">
      <div class="assignment__row__title">User</div>
      <div class="assignment__row__value">
        Oh this is a long user name
      </div>
    </div>
    <div class="assignment__row">
      <div class="assignment__row__title">Company:</div>
      <div class="assignment__row__value">
        Oh this is a long company name
      </div>
    </div>
    <div class="assignment__row">
      <div class="assignment__row__title">Department:</div>
      <div class="assignment__row__value" >
        This is Such a long Department Name
      </div>
    </div>
  </div>
  <!-- here is the col with the button -->
  <div class="assignment__controls">
    <button mat-icon-button (click)="doSomething()">
      <mat-icon>edit</mat-icon>
    </button>
  </div>
</div>