CSS:通过文本框实现动态高度

3
我该如何使modal的容器在拖动/调整文本区域时自适应大小?顺便说一下,这是一个模态框。目前,当我调整文本区域的大小时,它会从容器中溢出。
以下是我的CSS和HTML:

.dialog-box {
    position: absolute;
    width: 100%;
    height: 100%;
    top:0;
    left:0;
    z-index: 1000;
}
.dimmer {
    content: '';
    position: absolute;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    top:0;
    left:0;
    z-index: -1;
}
.container{
    background-color: #fff;
    width: 600px;
    height: 460px;
    position: absolute;
    border-radius: 5px;
    top:0;
    right:0;
    bottom:0;
    left:0;
    margin: auto;
    border: 1px solid #ebebeb;
    padding: 20px;
    align-items: center;
    z-index: 1;
}
<div  class="dialog-box" *ngIf="showDialogBox">
  <div class="dimmer" (click)="hide()"></div>
  <div class="container" >
    <h3>
        <ng-content></ng-content>
    </h3>
    <form class="ui form" [formGroup]="dialogForm" novalidate>
        <div class="field required">
            <label>Name</label>
            <input type="text" class="name" formControlName="name" placeholder="Enter Name..." maxlength="100" required/>
        </div>
        <div class="field">
            <label>Remarks</label>
            <textarea class="remark" formControlName="remark" placeholder="Enter Remarks..." maxlength="1000"></textarea>
        </div>
    </form>
    <ng-content select="[action]"></ng-content>
  </div>
</div>


.dialog-box .container { overflow: auto; }。当高度超过模型容器时,它会添加滚动条。 - Eugine Joseph
提供信息,当我运行这个示例时(FF59),除了重新校准的滚动条之外,这里什么也没有显示。 - Sz.
2个回答

1
看看你是否想要这样的东西...

.dialog-box {
  
  width: 100%;
  height: 100%; 
  /* become a flex-container */
  display: flex;
  /* center flex-items vertically */ 
  align-items: center;
  /* center flex-items horizontally */
  justify-content: center;
}

.dimmer {
  content: '';
  position: absolute;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  top: 0;
  left: 0;
  z-index: -1;
}

.container {
  background-color: #fff;
  /* take size of content */
  display: inline-block;
  /* setting minimum size */
  min-width: 600px;
  max-width: calc(100% - 30px);
  min-height: 400px;
  max-height: calc(100% -30px);
  border-radius: 5px;
  border: 1px solid #ebebeb;
  padding: 20px;
  z-index: 1;
  margin: 30px;
}
.remark{
  max-height:70vh;
  max-width:60vw;
}
<div class="dialog-box" *ngIf="showDialogBox">
  <div class="dimmer" (click)="hide()"></div>
  <div class="container">
    <h3>
        <ng-content></ng-content>
    </h3>
    <form class="ui form" [formGroup]="dialogForm" novalidate>
      <div class="field required">
        <label>Name</label>
        <input type="text" class="name" formControlName="name" placeholder="Enter Name..." maxlength="100" required/>
      </div>
      <div class="field">
        <label>Remarks</label>
        <textarea class="remark" formControlName="remark" placeholder="Enter Remarks..." maxlength="1000"></textarea>
      </div>
    </form>
    <ng-content select="[action]"></ng-content>
  </div>
</div>


0

你可以更改以下内容:

  • container 设置 display: inline-block 以占据内容的尺寸。并用 min-width: 600pxmin-height: 400px 替换 width: 600pxheight: 400px
  • container 不需要绝对定位。你可以使用 flexbox 进行居中。

示例:

.dialog-box {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  z-index: 1000;
  /* become a flex-container */
  display: flex;
  /* center flex-items vertically */ 
  align-items: center;
  /* center flex-items horizontally */
  justify-content: center;
}

.dimmer {
  content: '';
  position: absolute;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  top: 0;
  left: 0;
  z-index: -1;
}

.container {
  background-color: #fff;
  /* take size of content */
  display: inline-block;
  /* setting minimum size */
  min-width: 600px;
  min-height: 400px;
  border-radius: 5px;
  border: 1px solid #ebebeb;
  padding: 20px;
  z-index: 1;
}
<div class="dialog-box" *ngIf="showDialogBox">
  <div class="dimmer" (click)="hide()"></div>
  <div class="container">
    <h3>
        <ng-content></ng-content>
    </h3>
    <form class="ui form" [formGroup]="dialogForm" novalidate>
      <div class="field required">
        <label>Name</label>
        <input type="text" class="name" formControlName="name" placeholder="Enter Name..." maxlength="100" required/>
      </div>
      <div class="field">
        <label>Remarks</label>
        <textarea class="remark" formControlName="remark" placeholder="Enter Remarks..." maxlength="1000"></textarea>
      </div>
    </form>
    <ng-content select="[action]"></ng-content>
  </div>
</div>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接