【问题标题】:How to reduce the height of select dropdown box如何降低选择下拉框的高度
【发布时间】:2021-12-12 18:47:33
【问题描述】:

我在选择 html 标记时遇到问题。当我有一个太大的列表,有这么多元素时,我有一个项目。问题是我想缩小下拉框的高度,不知道怎么做。

当我点击选择标签时会发生以下情况:

我希望它更小,比如一次显示 10 个元素。

这是我的html:

    .selectWrapper, .selectBrand {
        width: 60%;
        display: grid;
        grid-template-areas: "select";
        align-items: center;
            
        &::after {
            content: '';
            clip-path: polygon(100% 0%, 0 0%, 50% 100%);
            width: 0.8em;
            height: 0.5em;
            grid-area: select;
            justify-self: end;
            margin-right: 1em;
            background-color: #110F30;
            pointer-events: none;
            transition: transform 0.2s ease-out;
        }
            
        &:focus-within {
            &::after {
                transform: rotate(180deg);
            }
        }
            
        select {
            appearance: none;
            width: 100%;
            height: 3em;
            border-radius: 50px;
            border: none;
            outline: none;
            padding-inline: 1em;
            align-items: center;
            grid-area: select;
            font-family: 'Open Sans', sans-serif;
            font-size: 0.8em;
            color: #535353;
            background: linear-gradient(145deg, #d3d3d3, 
            #fafafa);
            box-shadow: 10px 10px 20px #a4a4a4,
            -10px -10px 20px #ffffff;
            
            &:hover {
                cursor: pointer;
            }
        }         
    }
    <div class="selectWrapper">
        <select name="typeSol" class="titSol" required>
            <option value="" disabled selected>Select a Type</option>
            <option value="">Select a Type</option>
            <option value="">Select a Type</option>
            <option value="">Select a Type</option>
            <option value="">Select a Type</option>
            <option value="">Select a Type</option>
            <option value="">Select a Type</option>
            <option value="">Select a Type</option>
            <option value="">Select a Type</option>
            <option value="">Select a Type</option>
            <!-- More options -->
        </select>
    </div>

希望任何人都可以提供帮助。

【问题讨论】:

    标签: javascript html jquery css sass


    【解决方案1】:

    您可以使用以下onfocusonbluronchange 事件来更改此设置。其中this 代表 HTML DOM 元素。您可以相应地更改值

    .titSol, .selectBrand {
            width: 60%;
            display: grid;
            grid-template-areas: "select";
            align-items: center;
                
            &::after {
                content: '';
                clip-path: polygon(100% 0%, 0 0%, 50% 100%);
                width: 0.8em;
                height: 0.5em;
                grid-area: select;
                align-self: end;
                margin-right: 1em;
                background-color: #110F30;
                pointer-events: none;
                transition: transform 0.2s ease-out;
            }
                
            &:focus-within {
                &::after {
                    transform: rotate(180deg);
                }
            }
                
            select {
                appearance: none;
                width: 100%;
                height: 3em;
                border-radius: 50px;
                border: none;
                outline: none;
                padding-inline: 1em;
                align-items: center;
                grid-area: select;
                font-family: 'Open Sans', sans-serif;
                font-size: 0.8em;
                color: #535353;
                background: linear-gradient(145deg, #d3d3d3, 
                #fafafa);
                box-shadow: 10px 10px 20px #a4a4a4,
                -10px -10px 20px #ffffff;
                
                &:hover {
                    cursor: pointer;
                }
            }         
        }
    <div class="selectWrapper">
        <select name="typeSol" class="titSol" onfocus='this.size=8;' onblur='this.size=1;' onchange='this.size=1; this.blur();' required>
            <option value="" disabled selected>Select a Type</option>
            <option value="" disabled selected>Select a Type</option>
            <option value="" disabled selected>Select a Type</option>
            <option value="" disabled selected>Select a Type</option>
            <option value="" disabled selected>Select a Type</option>
            <option value="" disabled selected>Select a Type</option>
            <option value="" disabled selected>Select a Type</option>
            <option value="" disabled selected>Select a Type</option>
            <option value="" disabled selected>Select a Type</option>
            <option value="" disabled selected>Select a Type</option>
            <option value="" disabled selected>Select a Type</option>
            <option value="" disabled selected>Select a Type</option>
            <option value="" disabled selected>Select a Type</option>
            <!-- ...More Options -->
        </select>
    </div>

    【讨论】:

    • 出于某种原因,当我把它放在我的代码中时,这是https://i.imgur.com/mqBqB82.png的结果
    • @GLPG35 您是否应用了任何 CSS,如果是,请将其添加到您的 OP。
    • 我更新了我的问题。
    • 您提供的 HTML 和 CSS 似乎没有重现您刚刚将 img 链接到的问题
    • 对不起,我忘了更新 scss 中的类。现在它应该可以工作了。
    猜你喜欢
    • 2023-01-28
    • 2020-01-31
    • 1970-01-01
    • 1970-01-01
    • 2022-10-20
    • 2015-07-12
    • 1970-01-01
    • 1970-01-01
    • 2019-06-22
    相关资源
    最近更新 更多