html - 容器宽度需要根据表格宽度动态变化

标签 html css bootstrap-4

我正在尝试为我的表实现一个容器,因此选择使用卡片类。如屏幕截图所示,容器扩展到屏幕的整个宽度。我希望容器宽度基于表格的宽度。在我的例子中,表格的宽度可以随着数据的呈现而改变 水平地。我怎样才能使我的容器动态。如果超出屏幕大小,则应出现水平滚动条。正如您在下面的屏幕截图中看到的那样,它换行到下一行并且顶部右侧有间隙

截图

enter image description here

html

<style>
    th,
    td {
        padding: 7px;
    }

   .scrollClass {
    overflow-x: scroll;
    }
    .fundClassesTable {
        margin: 0 auto;
        font-size: 11px;
        width: 100%;
    }

    .tableItem {
        text-align: center;
        border-left: solid 1px lightgrey;
        border-top: solid 1px lightgrey;
        border-right: solid 1px lightgrey;
        border-bottom: solid 1px lightgrey;

    }

    .rowItem:hover {
        background-color: #f5f7f7;
    }



    tr {
        display: block;
        float: left;
    }

    th,
    td {
        display: block;
        min-height: 46px;
        min-width: 300px;
    }

    label {
        margin-left: 0.5rem;
        vertical-align: middle
    }


    .panel-heading {
        color: white;
        background-color: #F59850;
        border-color: #ddd;
        overflow: hidden;
        padding-top: 5px !important;
        padding-bottom: 5px !important;
    }

    .panel-heading .left-label {
        display: inline-block;
        padding-top: 5px !important;

    }

    .panel-heading label {
        margin-bottom: 0px !important;
    }
</style>

<div>
    <input type="checkbox" id="chk" style="width: 13px; height: 13px;" />
    <label for="chk">Invested</label>

</div>

<div class="card scrollClass">
    <div class="card-header panel-heading">
        <span class="left-label" style="font-size: 18px; font-weight: bold; ">Fund Classes</span>
        <div class="pull-right" style="padding-right:10px; display: inline-block; vertical-align:middle">
            <!-- <label style="text-align: center; vertical-align:middle" class="btn btn-default pull-right"> <i
                    data-bind="visible: true" class="fa fa-plus-square"></i><input type="checkbox" class="hidden" /> Add
                Class</label> -->
            <button style="text-align: center; vertical-align:middle" class="btn btn-default pull-right" (click)="openFundClassModal()"> <i
                data-bind="visible: true" class="fa fa-plus-square"></i> Add Class</button>    
        </div>
    </div>

    <div *ngIf="FundClasses && FundClasses.FundDetailsViewModel">

        <table class="fundClassesTable table-striped">

            <tr>
                <th class="tableItem bold">Fund Name</th>
                <th class="tableItem bold">Accounting Class Name</th>
                <th class="tableItem bold">Class ID</th>
                <th class="tableItem bold">Legal Fund Class</th>
                <th class="tableItem bold">Inception Date</th>
                <th class="tableItem bold">Invested Amount</th>
                <th class="tableItem bold">Vehicle Type</th>
                <th class="tableItem bold">Closure Status</th>
                <th class="tableItem bold">Is Side Pocket?</th>
                <th class="tableItem bold">Is Thematic?</th>
                <th class="tableItem bold">Cogency Class?</th>
                <th class="tableItem"></th>
            </tr>

            <ng-container *ngFor="let fundClass of FundClasses.FundDetailsViewModel">
                <tr *ngFor="let f of fundClass['FundClassDetailsViewModel'] | keyvalue">
                    <td class="tableItem">{{ f.value.FundName}}</td>
                    <td class="tableItem">{{ f.value.Description}}</td>
                    <td class="tableItem">{{f.value.Id}}</td>
                    <td *ngIf="EditMode[f.value.Id]" class="tableItem">
                        <kendo-dropdownlist style="width:100%" [(ngModel)]="f.value.LegalFundClassId"
                            class="form-control  form-control-sm" [data]="fundClass.PrimaryLegalFundClasses"
                            [filterable]="false" textField="Description" [valuePrimitive]="true" valueField="Id">
                        </kendo-dropdownlist>
                    </td>
                    <td [attr.id]="'f.value.Id'" *ngIf="!EditMode[f.value.Id]" class="tableItem" style="width:100%">
                        {{ f.value.LegalFundClassName}}
                    </td>
                    <td [attr.id]="'f.value.Id'" *ngIf="EditMode[f.value.Id]" class="tableItem">
                        <kendo-datepicker style="width:100%" [format]="'MMMM yyyy'" [topView]="'decade'"
                            [bottomView]="'year'" [(ngModel)]="f.value.InceptionDate"
                            class="form-control  form-control-sm">
                        </kendo-datepicker>
                    </td>
                    <td [attr.id]="'f.value.Id'" *ngIf="!EditMode[f.value.Id]" class="tableItem" style="width:100%">
                        {{ f.value.InceptionDate | date:"'MMMM yyyy"}}
                    </td>
                    <td [attr.id]="'f.value.Id'" *ngIf="EditMode[f.value.Id]" class="tableItem" style="width:100%">
                        <input kendoTextBox [(ngModel)]="f.value.InvestedAmount"
                            style="width: 284px; height: 29.5px;" />
                    </td>
                    <td [attr.id]="'f.value.Id'" *ngIf="!EditMode[f.value.Id]" class="tableItem" style="width:100%">
                        {{ f.value.InvestedAmount | number : '.2-2'}}
                    </td>
                    <td [attr.id]="'f.value.Id'" *ngIf="EditMode[f.value.Id]" class="tableItem">
                        <kendo-dropdownlist style="width:100%" [(ngModel)]="f.value.VehicleTypeId"
                            class="form-control  form-control-sm" [data]="FundClasses.VehicleTypes" [filterable]="false"
                            textField="Name" [valuePrimitive]="true" valueField="Id"></kendo-dropdownlist>
                    </td>
                    <td [attr.id]="'f.value.Id'" *ngIf="!EditMode[f.value.Id]" class="tableItem" style="width:100%">
                        {{ f.value.VehicleTypeName}}
                    </td>
                    <td [attr.id]="'f.value.Id'" *ngIf="EditMode[f.value.Id]" class="tableItem">
                        <kendo-dropdownlist style="width:100%" [(ngModel)]="f.value.ClosureStatusId"
                            class="form-control  form-control-sm" [data]="FundClasses.ClosureStatuses"
                            [filterable]="false" textField="Name" [valuePrimitive]="true" valueField="Id">
                        </kendo-dropdownlist>
                    </td>
                    <td [attr.id]="'f.value.Id'" *ngIf="!EditMode[f.value.Id]" class="tableItem" style="width:100%">
                        {{ f.value.ClosureStatusName}}
                    </td>
                    <td [attr.id]="'f.value.Id'" *ngIf="EditMode[f.value.Id]" class="tableItem">
                        <input type="checkbox" value="{{f.value.IsSidePocket}}" id="chk"
                            style="width: 13px; height: 13px;" />
                        <label for="chk">Yes</label>

                    </td>
                    <td [attr.id]="'f.value.Id'" *ngIf="!EditMode[f.value.Id]" class="tableItem" style="width:100%">
                        {{ f.value.IsSidePocket == true ? 'Yes' : 'No'}}
                    </td>
                    <td [attr.id]="'f.value.Id'" *ngIf="EditMode[f.value.Id]" class="tableItem">
                        <input type="checkbox" value="{{f.value.IsThematic}}" style="width: 13px; height: 13px;" />
                        <label for="chk">Yes</label>
                    </td>
                    <td [attr.id]="'f.value.Id'" *ngIf="!EditMode[f.value.Id]" class="tableItem" style="width:100%">
                        {{ f.value.IsThematic == true ? 'Yes' : 'No'}}
                    </td>
                    <td [attr.id]="'f.value.Id'" *ngIf="EditMode[f.value.Id]" class="tableItem">
                        <kendo-dropdownlist style="width:100%" [(ngModel)]="f.value.CogencyClassId"
                            class="form-control  form-control-sm" [data]="fundClass.CogencyClasses" [filterable]="false"
                            textField="Name" [valuePrimitive]="true" valueField="Id"></kendo-dropdownlist>
                    </td>
                    <td [attr.id]="'f.value.Id'" *ngIf="!EditMode[f.value.Id]" class="tableItem" style="width:100%">
                        {{ f.value.CogencyClassId}}
                    </td>
                    <td class="tableItem">

                        <button *ngIf="!EditMode[f.value.Id]" type="button" class="btn btn-primary btn mr-1 col-sm-4"
                            (click)="buttonClicked(f.value.Id)">Edit</button>
                        <button *ngIf="EditMode[f.value.Id]" type="button" class="btn btn-primary btn mr-1 col-sm-3"
                            (click)="Update(f.value.Id)">Save</button>
                        <button *ngIf="EditMode[f.value.Id]" type="button" class="btn btn-primary btn mr-1  col-sm-3"
                            (click)="buttonClicked(f.value.Id)">Delete</button>
                        <button *ngIf="EditMode[f.value.Id]" type="button" class="btn btn-primary btn mr-1  col-sm-3"
                            (click)="buttonClicked(f.value.Id)">Cancel</button>

                    </td>
                </tr>
            </ng-container>
        </table>
    </div>
</div>


<shared-modal [modalSize]="1" class="survey-edit" [open]="fundClassWindowOpened">
    <div style="width: 100%;" header>
        <h4 class="modal-title">
            <div style="text-align: right"><button aria-label="Dismiss" class="close"
                    style="margin-top: -48px" type="button" (click)="dismissFundClassModal()">X</button>
            </div>
        </h4>
    </div>
    <div body>
        <mgr-addFundClass></mgr-addFundClass>

    </div>
    <div footer>
    </div>
</shared-modal>

基于 Aarons 评论的应用解决方案

enter image description here

最佳答案

所以根据评论,我想我现在理解你了。首先,让我们删除使用 float: left 将容器排成一行的非常过时的方法:

tr {
  display: block;
  float: left; //<== Remove this line
  min-width: 300px; //<== Optional: Just to make sure your table columns don't get too small if they have less content.
}

让我们使用更现代的东西,比如 flexbox。所以将 display: flex; 添加到您的 tbody
问题是您仍然有一个空白空间。要填写它,请使用 flex-grow在每个 tr 上。最后归结为:

.fundClassesTable tbody {
  display: flex;
}

.fundClassesTable tbody tr {
  flex-grow: 1;
}

.scrollClass {
  overflow-x: scroll;
  display: grid; //<== Add this line.
}

这是您的示例(或 http://jsfiddle.net/h5oc70va/1/,如果您愿意):

.fundClassesTable tbody {
  display: flex;
}

th,
td {
  padding: 7px;
}

.scrollClass {
  overflow-x: scroll;
  display: grid;
}

.fundClassesTable {
  margin: 0 auto;
  font-size: 11px;
  width: 100%;
}

.tableItem {
  text-align: center;
  border-left: solid 1px lightgrey;
  border-top: solid 1px lightgrey;
  border-right: solid 1px lightgrey;
  border-bottom: solid 1px lightgrey;
}

.rowItem:hover {
  background-color: #f5f7f7;
}

tr {
  display: block;
  flex-grow: 1;
}

th,
td {
  display: block;
  min-height: 46px;
  min-width: 300px;
}

label {
  margin-left: 0.5rem;
  vertical-align: middle
}

.panel-heading {
  color: white;
  background-color: #F59850;
  border-color: #ddd;
  overflow: hidden;
  padding-top: 5px !important;
  padding-bottom: 5px !important;
}

.panel-heading .left-label {
  display: inline-block;
  padding-top: 5px !important;
}

.panel-heading label {
  margin-bottom: 0px !important;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" rel="stylesheet" />

<div>
  <input type="checkbox" id="chk" style="width: 13px; height: 13px;" />
  <label for="chk">Invested</label>

</div>

<div class="card scrollClass">
  <div class="card-header panel-heading">
    <span class="left-label" style="font-size: 18px; font-weight: bold; ">Fund Classes</span>
    <div class="pull-right" style="padding-right:10px; display: inline-block; vertical-align:middle">
      <!-- <label style="text-align: center; vertical-align:middle" class="btn btn-default pull-right"> <i
                    data-bind="visible: true" class="fa fa-plus-square"></i><input type="checkbox" class="hidden" /> Add
                Class</label> -->
      <button style="text-align: center; vertical-align:middle" class="btn btn-default pull-right" (click)="openFundClassModal()"> <i
                data-bind="visible: true" class="fa fa-plus-square"></i> Add Class</button>
    </div>
  </div>

  <div *ngIf="FundClasses && FundClasses.FundDetailsViewModel">

    <table class="fundClassesTable table-striped">

      <tr>
        <th class="tableItem bold">Fund Name</th>
        <th class="tableItem bold">Accounting Class Name</th>
        <th class="tableItem bold">Class ID</th>
        <th class="tableItem bold">Legal Fund Class</th>
        <th class="tableItem bold">Inception Date</th>
        <th class="tableItem bold">Invested Amount</th>
        <th class="tableItem bold">Vehicle Type</th>
        <th class="tableItem bold">Closure Status</th>
        <th class="tableItem bold">Is Side Pocket?</th>
        <th class="tableItem bold">Is Thematic?</th>
        <th class="tableItem bold">Cogency Class?</th>
        <th class="tableItem"></th>
      </tr>

      <ng-container *ngFor="let fundClass of FundClasses.FundDetailsViewModel">
        <tr *ngFor="let f of fundClass['FundClassDetailsViewModel'] | keyvalue">
          <td class="tableItem">{{ f.value.FundName}}</td>
          <td class="tableItem">{{ f.value.Description}}</td>
          <td class="tableItem">{{f.value.Id}}</td>
          <td *ngIf="EditMode[f.value.Id]" class="tableItem">
            <kendo-dropdownlist style="width:100%" [(ngModel)]="f.value.LegalFundClassId" class="form-control  form-control-sm" [data]="fundClass.PrimaryLegalFundClasses" [filterable]="false" textField="Description" [valuePrimitive]="true" valueField="Id">
            </kendo-dropdownlist>
          </td>
          <td [attr.id]="'f.value.Id'" *ngIf="!EditMode[f.value.Id]" class="tableItem" style="width:100%">
            {{ f.value.LegalFundClassName}}
          </td>
          <td [attr.id]="'f.value.Id'" *ngIf="EditMode[f.value.Id]" class="tableItem">
            <kendo-datepicker style="width:100%" [format]="'MMMM yyyy'" [topView]="'decade'" [bottomView]="'year'" [(ngModel)]="f.value.InceptionDate" class="form-control  form-control-sm">
            </kendo-datepicker>
          </td>
          <td [attr.id]="'f.value.Id'" *ngIf="!EditMode[f.value.Id]" class="tableItem" style="width:100%">
            {{ f.value.InceptionDate | date:"'MMMM yyyy"}}
          </td>
          <td [attr.id]="'f.value.Id'" *ngIf="EditMode[f.value.Id]" class="tableItem" style="width:100%">
            <input kendoTextBox [(ngModel)]="f.value.InvestedAmount" style="width: 284px; height: 29.5px;" />
          </td>
          <td [attr.id]="'f.value.Id'" *ngIf="!EditMode[f.value.Id]" class="tableItem" style="width:100%">
            {{ f.value.InvestedAmount | number : '.2-2'}}
          </td>
          <td [attr.id]="'f.value.Id'" *ngIf="EditMode[f.value.Id]" class="tableItem">
            <kendo-dropdownlist style="width:100%" [(ngModel)]="f.value.VehicleTypeId" class="form-control  form-control-sm" [data]="FundClasses.VehicleTypes" [filterable]="false" textField="Name" [valuePrimitive]="true" valueField="Id"></kendo-dropdownlist>
          </td>
          <td [attr.id]="'f.value.Id'" *ngIf="!EditMode[f.value.Id]" class="tableItem" style="width:100%">
            {{ f.value.VehicleTypeName}}
          </td>
          <td [attr.id]="'f.value.Id'" *ngIf="EditMode[f.value.Id]" class="tableItem">
            <kendo-dropdownlist style="width:100%" [(ngModel)]="f.value.ClosureStatusId" class="form-control  form-control-sm" [data]="FundClasses.ClosureStatuses" [filterable]="false" textField="Name" [valuePrimitive]="true" valueField="Id">
            </kendo-dropdownlist>
          </td>
          <td [attr.id]="'f.value.Id'" *ngIf="!EditMode[f.value.Id]" class="tableItem" style="width:100%">
            {{ f.value.ClosureStatusName}}
          </td>
          <td [attr.id]="'f.value.Id'" *ngIf="EditMode[f.value.Id]" class="tableItem">
            <input type="checkbox" value="{{f.value.IsSidePocket}}" id="chk" style="width: 13px; height: 13px;" />
            <label for="chk">Yes</label>

          </td>
          <td [attr.id]="'f.value.Id'" *ngIf="!EditMode[f.value.Id]" class="tableItem" style="width:100%">
            {{ f.value.IsSidePocket == true ? 'Yes' : 'No'}}
          </td>
          <td [attr.id]="'f.value.Id'" *ngIf="EditMode[f.value.Id]" class="tableItem">
            <input type="checkbox" value="{{f.value.IsThematic}}" style="width: 13px; height: 13px;" />
            <label for="chk">Yes</label>
          </td>
          <td [attr.id]="'f.value.Id'" *ngIf="!EditMode[f.value.Id]" class="tableItem" style="width:100%">
            {{ f.value.IsThematic == true ? 'Yes' : 'No'}}
          </td>
          <td [attr.id]="'f.value.Id'" *ngIf="EditMode[f.value.Id]" class="tableItem">
            <kendo-dropdownlist style="width:100%" [(ngModel)]="f.value.CogencyClassId" class="form-control  form-control-sm" [data]="fundClass.CogencyClasses" [filterable]="false" textField="Name" [valuePrimitive]="true" valueField="Id"></kendo-dropdownlist>
          </td>
          <td [attr.id]="'f.value.Id'" *ngIf="!EditMode[f.value.Id]" class="tableItem" style="width:100%">
            {{ f.value.CogencyClassId}}
          </td>
          <td class="tableItem">

            <button *ngIf="!EditMode[f.value.Id]" type="button" class="btn btn-primary btn mr-1 col-sm-4" (click)="buttonClicked(f.value.Id)">Edit</button>
            <button *ngIf="EditMode[f.value.Id]" type="button" class="btn btn-primary btn mr-1 col-sm-3" (click)="Update(f.value.Id)">Save</button>
            <button *ngIf="EditMode[f.value.Id]" type="button" class="btn btn-primary btn mr-1  col-sm-3" (click)="buttonClicked(f.value.Id)">Delete</button>
            <button *ngIf="EditMode[f.value.Id]" type="button" class="btn btn-primary btn mr-1  col-sm-3" (click)="buttonClicked(f.value.Id)">Cancel</button>

          </td>
        </tr>
      </ng-container>
    </table>
  </div>
</div>


<shared-modal [modalSize]="1" class="survey-edit" [open]="fundClassWindowOpened">
  <div style="width: 100%;" header>
    <h4 class="modal-title">
      <div style="text-align: right"><button aria-label="Dismiss" class="close" style="margin-top: -48px" type="button" (click)="dismissFundClassModal()">X</button>
      </div>
    </h4>
  </div>
  <div body>
    <mgr-addFundClass></mgr-addFundClass>

  </div>
  <div footer>
  </div>
</shared-modal>

如果您需要任何解释,请告诉我,但我相信代码应该是不言自明的。

关于html - 容器宽度需要根据表格宽度动态变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55922665/

相关文章:

html - 在Wordpress中使用Divi Page Builder,内容不再位于正确的布局中

html - 在 HTML 上嵌入 Mapbox 滑动 map

css - 覆盖 Bootstrap CSS(链接颜色)

html - 将一个 div 拉伸(stretch)到它的全宽

jquery - 将 div 覆盖在屏幕上,但背景色避开页面区域

html - Bootstrap 导航栏标题文本在较小的屏幕上重叠

jquery - 移动选择列表 - 隐藏默认选项

html - 未对齐的 Twitter Follow 和 Facebook Like 按钮

html - 如何在 Bootstrap 4 中添加下拉菜单?

html - 包含文本的部分与包含无序列表的部分重叠