LoginSignup
20
19

More than 5 years have passed since last update.

cssのみでいい感じにselectタグをカスタマイズ

Last updated at Posted at 2015-04-09
<label for="animals" class="select-group">
    <select id="animals" class="select">
        <option value="1">dog</option>
        <option value="2">cat</option>
        <option value="3">bear</option>
    </select>
</label>
.select-group
{
    position: relative;
    display: inline-block;
    overflow: hidden;

    box-sizing: border-box;
    width: 100%;
    padding: 0;

    vertical-align: middle;

    border: 1px solid #ddd;
    background-color: #fff;
    background-repeat: no-repeat;
}

.select
{
    position: relative;
    z-index: 1;  

    line-height: 1.8;

    display: block;

    width: 120%;
    padding: 0;

    text-indent: 8px;

    border: none;
    border-radius: 0;
    background-color: transparent;
    background-image: none;
    box-shadow: none;

    -webkit-appearance: none;
       -moz-appearance: none;
            appearance: none;
}

.select-group:after
{
    position: absolute;
    top: 0;
    right: 8px;
    bottom: 0;

    width: 0;
    height: 0;
    margin: auto;

    content: '';

    border-top: 4px solid #999;
    border-right: 4px solid transparent;
    border-left: 4px solid transparent;
}

div {
    width: 300px;
    height: 100px;
    padding: 10px;
    background-color: #eee;
}

jsfiddleのような感じになります。

select要素のブラウザ毎に異なる ▼(down triangle icon?) の部分をラップする要素の領域外にだして、擬似要素afterで生成して代替する。
javascriptプラグイン不要なシンプルな設計です。

select要素をラップしている要素のボーダーをクリックされるとアウトです。ここも気になる場合ならbox-shadowや擬似要素あたりで対応できそう。

20
19
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
19