繁体   English   中英

角材料-垫选择:自定义填充溢出父元素

[英]Angular Material - mat-select : Custom padding overflows parent element

我正在使用Angular Material组件,并且试图自定义组合框(选择)。 当我更改组件的填充时,它将使包装div元素溢出。 我期望它会继续发展。

添加填充之前的外观

在此处输入图片说明

添加填充后的外观 在此处输入图片说明

如我们所见,由于mat-select元素通过添加填充来增加其宽度,因此mat-select div没有增长

HTML代码

<div style="border: 1px solid black; min-width: 100px; margin: 0px 5px; float: left">
    <mat-form-field class="my-select template-select">
        <mat-select [(value)]="templateSelected">
            <mat-select-trigger>
                <div fxLayoutAlign="start center">
                    <i fxFlex="none" class="material-icons md-dark">visibility</i>
                    <span fxFlex>{{templateSelected}}</span>
                </div>
            </mat-select-trigger>
            <mat-option value="Full data">
                <div fxLayoutAlign="start center" class="template-option">
                    <i fxFlex="none" class="material-icons md-dark">done</i>
                    <span fxFlex>Full data</span>
                </div>
            </mat-option>
            <mat-option value="My quarterly report">
                <div fxLayoutAlign="start center" class="templateOption">
                    <i fxFlex="24px" class="material-icons md-dark"></i>
                    <span fxFlex>My quarterly report</span>
                </div>
            </mat-option>
        </mat-select>
    </mat-form-field>
</div>
<div style="border: 1px solid black; min-width: 100px; margin-right: 5px; float: left">
    <button class="toolbar-button" mat-stroked-button>FILTER</button>
</div>

我正在更改mat-select元素上的填充,如下所示。 (也许不是更改它的地方)

CSS代码

.my-select mat-select {
    background-color: rgba(58, 40, 8, 0.12);
    /*padding: 6px 0px 6px 0px;*/ /* NO Left and right padding*/
    padding: 6px 10px 6px 10px;   /* 10px left and right padding*/

    border-radius: 5px;
}

.my-select .mat-select-value {
    font-family: Roboto-Regular;
    font-size: 16px;
    color: rgba(58, 40, 8, 0.87);
}

.my-select .mat-form-field-underline {
    display: none;
}

.my-select mat-select-trigger div>i {
    margin-right: 8px;
}

有谁知道为什么这种行为? 我该如何解决?

可用的实时代码: https//stackblitz.com/edit/angular-mat-select-overflow

非常感谢您的帮助。

问候

我认为在这里您需要使用box-sizing: border-box; 具有CSS box-sizing属性。 box-sizing属性允许我们在元素的总宽度和高度中包括填充和边框。

style.css文件

/* Add application styles & imports to this file! */
.my-select mat-select {
    background-color: rgba(58, 40, 8, 0.12);
    /*padding: 6px 0px 6px 0px;*/ /* NO Left and right padding*/
    padding: 6px 10px 6px 10px;   /* 10px left and right padding*/
    box-sizing: border-box;
    border-radius: 5px;
}

Stackblitz上的分叉解决方案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM