如何通过CSS自定义选择元素来实现这种黑暗风格?

问题描述 投票:10回答:5

如何创建如下图所示的选择元素?

我的代码是

<select>
    <option>-- Select City --</optoin>
    <option>Delhi</option>
    <option>Gurgaon</option>
</select>

这在jQuery中可用,但我想使用纯CSS。

html css
5个回答
21
投票

这是一个CSS支持的选择菜单;您可以根据需要自定义:

label.custom-select {
    position: relative;
    display: inline-block;

}

.custom-select select {
    display: inline-block;
    padding: 4px 3px 3px 5px;
    margin: 0;
    font: inherit;
    outline:none; /* remove focus ring from Webkit */
    line-height: 1.2;
    background: #000;
    color:white;
    border:0;
}

/* Select arrow styling */
.custom-select:after {
    content: "▼";
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    font-size: 60%;
    line-height: 30px;
    padding: 0 7px;
    background: #000;
    color: white;
    pointer-events: none;
}

.no-pointer-events .custom-select:after {
    content: none;
}
<label class="custom-select">
    <select>
        <option>Sushi</option>
        <option>Blue cheese with crackers</option>
        <option>Steak</option>
        <option>Other</option>
    </select>
</label>

JSFiddle


3
投票

使用pointer-events:none;它会让你点击箭头并激活选择字段,所以CSS对于那个元素看起来像这样。

.custom-select:after {
    content: "▼";
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    font-size: 60%;
    line-height: 30px;
    padding: 0 7px;
    background: #000;
    color: white;
    pointer-events:none;
}

0
投票
option{
  background-image:url(image.png);
  background-repeat: no-repeat;
}

0
投票

如果没有Javascript,您无法自定义您在屏幕截图中看到的选择。

您可以在this example中看到,您可以自定义颜色,填充和边框,但不能自定义按钮本身。


-1
投票

即使您在选择框中添加了背景图像,也无法在右侧隐藏该处理程序。我建议使用这个:qazxsw poi

您只需稍微修改CSS以满足您的需要。

© www.soinside.com 2019 - 2024. All rights reserved.