[CSS] Customer focus / disabled style for select element

Because there is no parent selector in CSS, we'll need to add an additional element to assist us in providing a focus style. We'll also add it to our multiple select so that it applies in both scenarios. The placement is important because we can affect elements that follow another element easily in CSS which we'll see.

<div class="form-group">
    <label for="select">Select</label>
    <div class="form-field select">
        <select id="select" name="select">...
        </select>
        <span class="focus"></span>
    </div>
</div>

 

复制代码
.form-field {
    border-color: var(--color-default, color("default"));

    &:focus {
        @include field-focus;
    }

    &:disabled {
        @include field-disabled;
    }
}

.form-field.select {
    display: grid;
    align-items: center;
    grid-template-areas: "select";
    position: relative;

    background-image: linear-gradient(
        to top,
        scale-color(color("white"), $lightness: -10%),
        color("white") 33%
    );

    select, 
    &::after {
        grid-area: select;
    }

    &:not(.select--multiple)::after {
        content: "";
        width: 0.8em;
        height: 0.5em;
        background-color: var(--color-default, color("default"));
        justify-self: end;
        clip-path: polygon(100% 0%, 0 0%, 50% 100%);
    }

    select {
        z-index: 1;

        &[multiple] {
            padding-right: 0;
        }
    }

    .focus {
        position: absolute;
        top: -2px;
        left: -2px;
        right: -2px;
        bottom: -2px;
        border-radius: inherit;
        border: 2px solid transparent;
    }

    select:focus + .focus {
        @include field-focus;
    }

    &--disabled {
        @include field-disabled;
        
        background-image: linear-gradient(
            to top,
            rgba(black, 0.08),
            rgba(white, 0) 33%
        );
    }
}
复制代码

 

mixin:

复制代码
@mixin field-focus {
    border-color: var(--field-focus, color("primary"));
    box-shadow: 0 0 0.35em -0.1em var(--field-focus, color("primary"));
    outline: 2px solid transparent;
}

@mixin field-disabled {
    border-color: var(--color-disabled-border, color("disabled"));
    background-color: var(
        --color-disabled-background,
        color("disabled-background")
    );
    cursor: not-allowed;
    &,
    option {
        color: rgba(black, 0.45);
    }
}
复制代码

 

All the idea that use to focus is add new element which class is `focus`, styling it and positioning it order to complete focus style.

posted @   Zhentiw  阅读(69)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2020-03-05 [RxJS] Just enough about ShareReplay
2020-03-05 [AST Tools] Babel template: replace JQuery
2020-03-05 [Tools] Using z to jump to "frecent" folders - Command Line Power User
2019-03-05 [Algorithm] How to use Max Heap to maintain K smallest items
2019-03-05 [HTML5] Avoiding CSS Conflicts via Shadow DOM CSS encapsulation
2019-03-05 [React] Simplify and Convert a Traditional React Form to Formik
2019-03-05 [Javascript Crocks] Make your own functions safer by lifting them into a Maybe context
点击右上角即可分享
微信分享提示