Bootstrap 4风格的select2下拉框

Bootstrap 4风格的select2下拉框


以 Select2 的 Bootstrap 3 风格 为基础,做了些修改,做成了 Bootstrap 4 风格的 Select2 下拉框。

效果:

使用方法:

将下面所有的代码复制到一个 CSS 文件中,然后在 HTML 文档中引入就行了,不需要再引入 Select2 默认的 CSS样式了。

.select2-container {
    box-sizing: border-box;
    display: inline-block;
    margin: 0;
    position: relative;
    vertical-align: middle;
}

.select2-container .select2-selection--single {
    box-sizing: border-box;
    cursor: pointer;
    display: block;
    height: 28px;
    user-select: none;
    -webkit-user-select: none;
}

.select2-container .select2-selection--single .select2-selection__rendered {
    display: block;
    padding-left: 8px;
    padding-right: 20px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.select2-container .select2-selection--single .select2-selection__clear {
    position: relative;
}

.select2-container[dir="rtl"] .select2-selection--single .select2-selection__rendered {
    padding-right: 8px;
    padding-left: 20px;
}

.select2-container .select2-selection--multiple {
    box-sizing: border-box;
    cursor: pointer;
    display: block;
    min-height: 32px;
    user-select: none;
    -webkit-user-select: none;
}

.select2-container .select2-selection--multiple .select2-selection__rendered {
    display: inline-block;
    overflow: hidden;
    padding-left: 8px;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.select2-container .select2-search--inline {
    float: left;
}

.select2-container .select2-search--inline .select2-search__field {
    box-sizing: border-box;
    border: none;
    font-size: 100%;
    margin-top: 5px;
    padding: 0;
}

.select2-container .select2-search--inline .select2-search__field::-webkit-search-cancel-button {
    -webkit-appearance: none;
}

.select2-dropdown {
    background-color: white;
    border: 1px solid #aaa;
    border-radius: 4px;
    box-sizing: border-box;
    display: block;
    position: absolute;
    left: -100000px;
    width: 100%;
    z-index: 1051;
}

.select2-results {
    display: block;
}

.select2-results__options {
    list-style: none;
    margin: 0;
    padding: 0;
}

.select2-results__option {
    padding: 6px;
    user-select: none;
    -webkit-user-select: none;
}

.select2-results__option[aria-selected] {
    cursor: pointer;
}

.select2-container--open .select2-dropdown {
    left: 0;
}

.select2-container--open .select2-dropdown--above {
    border-bottom: none;
    border-bottom-left-radius: 0;
    border-bottom-right-radius: 0;
}

.select2-container--open .select2-dropdown--below {
    border-top: none;
    border-top-left-radius: 0;
    border-top-right-radius: 0;
}

.select2-search--dropdown {
    display: block;
    padding: 4px;
}

.select2-search--dropdown .select2-search__field {
    padding: 4px;
    width: 100%;
    box-sizing: border-box;
}

.select2-search--dropdown .select2-search__field::-webkit-search-cancel-button {
    -webkit-appearance: none;
}

.select2-search--dropdown.select2-search--hide {
    display: none;
}

.select2-close-mask {
    border: 0;
    margin: 0;
    padding: 0;
    display: block;
    position: fixed;
    left: 0;
    top: 0;
    min-height: 100%;
    min-width: 100%;
    height: auto;
    width: auto;
    opacity: 0;
    z-index: 99;
    background-color: #fff;
    filter: alpha(opacity=0);
}

.select2-hidden-accessible {
    border: 0 !important;
    clip: rect(0 0 0 0) !important;
    clip-path: inset(50%) !important;
    height: 1px !important;
    overflow: hidden !important;
    padding: 0 !important;
    position: absolute !important;
    width: 1px !important;
    white-space: nowrap !important;
}

.select2-container--default {
    display: block;
}

.select2-container--default .select2-selection {
    background-color: #fff;
    border: 1px solid #ccc;
    border-radius: 4px;
    color: #555555;
    font-size: 14px;
    outline: 0;
}

.select2-container--default .select2-selection.form-control {
    border-radius: 4px;
}

.select2-container--default .select2-search--dropdown .select2-search__field {
    -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
    box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
    background-color: #fff;
    border: 1px solid #ccc;
    border-radius: 4px;
    color: #555555;
    font-size: 14px;
}

.select2-container--default .select2-search__field {
    outline: 0;
    /* Firefox 18- */
    /**
       * Firefox 19+
       *
       * @see http://stackoverflow.com/questions/24236240/color-for-styled-placeholder-text-is-muted-in-firefox
       */
}

.select2-container--default .select2-search__field::-webkit-input-placeholder {
    color: #999;
}

.select2-container--default .select2-search__field:-moz-placeholder {
    color: #999;
}

.select2-container--default .select2-search__field::-moz-placeholder {
    color: #999;
    opacity: 1;
}

.select2-container--default .select2-search__field:-ms-input-placeholder {
    color: #999;
}

.select2-container--default .select2-results__option {
    padding: 6px 12px;
}

.select2-container--default .select2-results__option[role=group] {
    padding: 0;
}

.select2-container--default .select2-results__option[aria-disabled=true] {
    color: #777777;
    cursor: not-allowed;
}

.select2-container--default .select2-results__option[aria-selected=true] {
    background-color: #f5f5f5;
    color: #262626;
}

.select2-container--default .select2-results__option--highlighted[aria-selected] {
    background-color: #007bff;
    color: #fff;
}

.select2-container--default .select2-results__option .select2-results__option {
    padding: 6px 12px;
}

.select2-container--default .select2-results__option .select2-results__option .select2-results__group {
    padding-left: 0;
}

.select2-container--default .select2-results__option .select2-results__option .select2-results__option {
    margin-left: -12px;
    padding-left: 24px;
}

.select2-container--default .select2-results__option .select2-results__option .select2-results__option .select2-results__option {
    margin-left: -24px;
    padding-left: 36px;
}

.select2-container--default .select2-results__option .select2-results__option .select2-results__option .select2-results__option .select2-results__option {
    margin-left: -36px;
    padding-left: 48px;
}

.select2-container--default .select2-results__option .select2-results__option .select2-results__option .select2-results__option .select2-results__option .select2-results__option {
    margin-left: -48px;
    padding-left: 60px;
}

.select2-container--default .select2-results__option .select2-results__option .select2-results__option .select2-results__option .select2-results__option .select2-results__option .select2-results__option {
    margin-left: -60px;
    padding-left: 72px;
}

.select2-container--default .select2-results__group {
    color: #777777;
    display: block;
    padding: 6px 12px;
    font-size: 12px;
    line-height: 1.5;
    white-space: nowrap;
}

.select2-container--default.select2-container--focus .select2-selection, .select2-container--default.select2-container--open .select2-selection {
    /*transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;*/
    box-shadow: 0 0 0 .2rem rgba(0, 123, 255, 0.25);
    border-color: #80bdff;
}

.select2-container--default.select2-container--open {}

.select2-container--default.select2-container--open .select2-selection .select2-selection__arrow b {
    border-color: transparent transparent #999 transparent;
    border-width: 0 4px 4px 4px;
}

.select2-container--default.select2-container--open.select2-container--below .select2-selection {
    border-bottom-right-radius: 0;
    border-bottom-left-radius: 0;
    border-bottom-color: transparent;
}

.select2-container--default.select2-container--open.select2-container--above .select2-selection {
    border-top-right-radius: 0;
    border-top-left-radius: 0;
    border-top-color: transparent;
}

.select2-container--default .select2-selection__clear {
    color: #999;
    cursor: pointer;
    float: right;
    font-weight: bold;
    margin-right: 10px;
}

.select2-container--default .select2-selection__clear:hover {
    color: #333;
}

.select2-container--default.select2-container--disabled .select2-selection {
    border-color: #ccc;
    -webkit-box-shadow: none;
    box-shadow: none;
}

.select2-container--default.select2-container--disabled .select2-selection,
.select2-container--default.select2-container--disabled .select2-search__field {
    cursor: not-allowed;
}

.select2-container--default.select2-container--disabled .select2-selection,
.select2-container--default.select2-container--disabled .select2-selection--multiple .select2-selection__choice {
    background-color: #eeeeee;
}

.select2-container--default.select2-container--disabled .select2-selection__clear,
.select2-container--default.select2-container--disabled .select2-selection--multiple .select2-selection__choice__remove {
    display: none;
}

.select2-container--default .select2-dropdown {
    box-shadow: .2rem .2rem 0 0 rgb(191,222,255),
        -0.2rem .2rem 0 0 rgb(191,222,255);
    overflow-x: hidden;
    margin-top: -1px;
    border-color: #80bdff;
}

.select2-container--default .select2-dropdown--above {
    -webkit-box-shadow: 0 -6px 12px rgba(0, 0, 0, 0.175);
    box-shadow: 0 -6px 12px rgba(0, 0, 0, 0.175);
    margin-top: 1px;
}

.select2-container--default .select2-results > .select2-results__options {
    max-height: 200px;
    overflow-y: auto;
}

.select2-container--default .select2-selection--single {
    height: 34px;
    line-height: 1.5;
    padding: 6px 24px 6px 12px;
}

.select2-container--default .select2-selection--single .select2-selection__arrow {
    position: absolute;
    bottom: 0;
    right: 12px;
    top: 0;
    width: 4px;
}

.select2-container--default .select2-selection--single .select2-selection__arrow b {
    border-color: #999 transparent transparent transparent;
    border-style: solid;
    border-width: 4px 4px 0 4px;
    height: 0;
    left: 0;
    margin-left: -4px;
    margin-top: -2px;
    position: absolute;
    top: 50%;
    width: 0;
}

.select2-container--default .select2-selection--single .select2-selection__rendered {
    color: #555555;
    padding: 0;
}

.select2-container--default .select2-selection--single .select2-selection__placeholder {
    color: #999;
}

.select2-container--default .select2-selection--multiple {
    min-height: 34px;
    padding: 0;
    height: auto;
}

.select2-container--default .select2-selection--multiple .select2-selection__rendered {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    display: block;
    line-height: 1.5;
    list-style: none;
    margin: 0;
    overflow: hidden;
    padding: 0;
    width: 100%;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.select2-container--default .select2-selection--multiple .select2-selection__placeholder {
    color: #999;
    float: left;
    margin-top: 5px;
}

.select2-container--default .select2-selection--multiple .select2-selection__choice {
    color: #555555;
    background: #fff;
    border: 1px solid #ccc;
    border-radius: 4px;
    cursor: default;
    float: left;
    margin: 5px 0 0 6px;
    padding: 0 6px;
}

.select2-container--default .select2-selection--multiple .select2-search--inline .select2-search__field {
    background: transparent;
    padding: 0 12px;
    height: 32px;
    line-height: 1.5;
    margin-top: 0;
    min-width: 5em;
}

.select2-container--default .select2-selection--multiple .select2-selection__choice__remove {
    color: #999;
    cursor: pointer;
    display: inline-block;
    font-weight: bold;
    margin-right: 3px;
}

.select2-container--default .select2-selection--multiple .select2-selection__choice__remove:hover {
    color: #333;
}

.select2-container--default .select2-selection--multiple .select2-selection__clear {
    margin-top: 6px;
}

.select2-container--default .select2-selection--single.input-sm,
.input-group-sm .select2-container--default .select2-selection--single,
.form-group-sm .select2-container--default .select2-selection--single {
    border-radius: 3px;
    font-size: 12px;
    height: 30px;
    line-height: 1.5;
    padding: 5px 22px 5px 10px;
    /* 2 */
}

.select2-container--default .select2-selection--single.input-sm .select2-selection__arrow b,
.input-group-sm .select2-container--default .select2-selection--single .select2-selection__arrow b,
.form-group-sm .select2-container--default .select2-selection--single .select2-selection__arrow b {
    margin-left: -5px;
}

.select2-container--default .select2-selection--multiple.input-sm,
.input-group-sm .select2-container--default .select2-selection--multiple,
.form-group-sm .select2-container--default .select2-selection--multiple {
    min-height: 30px;
    border-radius: 3px;
}

.select2-container--default .select2-selection--multiple.input-sm .select2-selection__choice,
.input-group-sm .select2-container--default .select2-selection--multiple .select2-selection__choice,
.form-group-sm .select2-container--default .select2-selection--multiple .select2-selection__choice {
    font-size: 12px;
    line-height: 1.5;
    margin: 4px 0 0 5px;
    padding: 0 5px;
}

.select2-container--default .select2-selection--multiple.input-sm .select2-search--inline .select2-search__field,
.input-group-sm .select2-container--default .select2-selection--multiple .select2-search--inline .select2-search__field,
.form-group-sm .select2-container--default .select2-selection--multiple .select2-search--inline .select2-search__field {
    padding: 0 10px;
    font-size: 12px;
    height: 28px;
    line-height: 1.5;
}

.select2-container--default .select2-selection--multiple.input-sm .select2-selection__clear,
.input-group-sm .select2-container--default .select2-selection--multiple .select2-selection__clear,
.form-group-sm .select2-container--default .select2-selection--multiple .select2-selection__clear {
    margin-top: 5px;
}

.select2-container--default .select2-selection--single.input-lg,
.input-group-lg .select2-container--default .select2-selection--single,
.form-group-lg .select2-container--default .select2-selection--single {
    border-radius: 6px;
    font-size: 18px;
    height: 46px;
    line-height: 1.3333333;
    padding: 10px 31px 10px 16px;
}

.select2-container--default .select2-selection--single.input-lg .select2-selection__arrow,
.input-group-lg .select2-container--default .select2-selection--single .select2-selection__arrow,
.form-group-lg .select2-container--default .select2-selection--single .select2-selection__arrow {
    width: 5px;
}

.select2-container--default .select2-selection--single.input-lg .select2-selection__arrow b,
.input-group-lg .select2-container--default .select2-selection--single .select2-selection__arrow b,
.form-group-lg .select2-container--default .select2-selection--single .select2-selection__arrow b {
    border-width: 5px 5px 0 5px;
    margin-left: -10px;
    margin-top: -2.5px;
}

.select2-container--default .select2-selection--multiple.input-lg,
.input-group-lg .select2-container--default .select2-selection--multiple,
.form-group-lg .select2-container--default .select2-selection--multiple {
    min-height: 46px;
    border-radius: 6px;
}

.select2-container--default .select2-selection--multiple.input-lg .select2-selection__choice,
.input-group-lg .select2-container--default .select2-selection--multiple .select2-selection__choice,
.form-group-lg .select2-container--default .select2-selection--multiple .select2-selection__choice {
    font-size: 18px;
    line-height: 1.3333333;
    border-radius: 4px;
    margin: 9px 0 0 8px;
    padding: 0 10px;
}

.select2-container--default .select2-selection--multiple.input-lg .select2-search--inline .select2-search__field,
.input-group-lg .select2-container--default .select2-selection--multiple .select2-search--inline .select2-search__field,
.form-group-lg .select2-container--default .select2-selection--multiple .select2-search--inline .select2-search__field {
    padding: 0 16px;
    font-size: 18px;
    height: 44px;
    line-height: 1.3333333;
}

.select2-container--default .select2-selection--multiple.input-lg .select2-selection__clear,
.input-group-lg .select2-container--default .select2-selection--multiple .select2-selection__clear,
.form-group-lg .select2-container--default .select2-selection--multiple .select2-selection__clear {
    margin-top: 10px;
}

.select2-container--default .select2-selection.input-lg.select2-container--open .select2-selection--single {}

.select2-container--default .select2-selection.input-lg.select2-container--open .select2-selection--single .select2-selection__arrow b {
    border-color: transparent transparent #999 transparent;
    border-width: 0 5px 5px 5px;
}

.input-group-lg .select2-container--default .select2-selection.select2-container--open .select2-selection--single {}

.input-group-lg .select2-container--default .select2-selection.select2-container--open .select2-selection--single .select2-selection__arrow b {
    border-color: transparent transparent #999 transparent;
    border-width: 0 5px 5px 5px;
}

.select2-container--default[dir="rtl"] {}

.select2-container--default[dir="rtl"] .select2-selection--single {
    padding-left: 24px;
    padding-right: 12px;
}

.select2-container--default[dir="rtl"] .select2-selection--single .select2-selection__rendered {
    padding-right: 0;
    padding-left: 0;
    text-align: right;
}

.select2-container--default[dir="rtl"] .select2-selection--single .select2-selection__clear {
    float: left;
}

.select2-container--default[dir="rtl"] .select2-selection--single .select2-selection__arrow {
    left: 12px;
    right: auto;
}

.select2-container--default[dir="rtl"] .select2-selection--single .select2-selection__arrow b {
    margin-left: 0;
}

.select2-container--default[dir="rtl"] .select2-selection--multiple .select2-selection__choice,
.select2-container--default[dir="rtl"] .select2-selection--multiple .select2-selection__placeholder,
.select2-container--default[dir="rtl"] .select2-selection--multiple .select2-search--inline {
    float: right;
}

.select2-container--default[dir="rtl"] .select2-selection--multiple .select2-selection__choice {
    margin-left: 0;
    margin-right: 6px;
}

.select2-container--default[dir="rtl"] .select2-selection--multiple .select2-selection__choice__remove {
    margin-left: 2px;
    margin-right: auto;
}

.has-warning .select2-dropdown,
.has-warning .select2-selection {
    border-color: #8a6d3b;
}

.has-warning .select2-container--focus .select2-selection,
.has-warning .select2-container--open .select2-selection {
    -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 6px #c0a16b;
    box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 6px #c0a16b;
    border-color: #66512c;
}

.has-warning.select2-drop-active {
    border-color: #66512c;
}

.has-warning.select2-drop-active.select2-drop.select2-drop-above {
    border-top-color: #66512c;
}

.has-error .select2-dropdown,
.has-error .select2-selection {
    border-color: #a94442;
}

.has-error .select2-container--focus .select2-selection,
.has-error .select2-container--open .select2-selection {
    -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 6px #ce8483;
    box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 6px #ce8483;
    border-color: #843534;
}

.has-error.select2-drop-active {
    border-color: #843534;
}

.has-error.select2-drop-active.select2-drop.select2-drop-above {
    border-top-color: #843534;
}

.has-success .select2-dropdown,
.has-success .select2-selection {
    border-color: #3c763d;
}

.has-success .select2-container--focus .select2-selection,
.has-success .select2-container--open .select2-selection {
    -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 6px #67b168;
    box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 6px #67b168;
    border-color: #2b542c;
}

.has-success.select2-drop-active {
    border-color: #2b542c;
}

.has-success.select2-drop-active.select2-drop.select2-drop-above {
    border-top-color: #2b542c;
}

.input-group > .select2-hidden-accessible:first-child + .select2-container--default > .selection > .select2-selection,
.input-group > .select2-hidden-accessible:first-child + .select2-container--default > .selection > .select2-selection.form-control {
    border-bottom-right-radius: 0;
    border-top-right-radius: 0;
}

.input-group > .select2-hidden-accessible:not(:first-child) + .select2-container--default:not(:last-child) > .selection > .select2-selection,
.input-group > .select2-hidden-accessible:not(:first-child) + .select2-container--default:not(:last-child) > .selection > .select2-selection.form-control {
    border-radius: 0;
}

.input-group > .select2-hidden-accessible:not(:first-child):not(:last-child) + .select2-container--default:last-child > .selection > .select2-selection,
.input-group > .select2-hidden-accessible:not(:first-child):not(:last-child) + .select2-container--default:last-child > .selection > .select2-selection.form-control {
    border-bottom-left-radius: 0;
    border-top-left-radius: 0;
}

.input-group > .select2-container--default {
    display: table;
    table-layout: fixed;
    position: relative;
    z-index: 2;
    width: 100%;
    margin-bottom: 0;
}

.input-group > .select2-container--default > .selection > .select2-selection.form-control {
    float: none;
}

.input-group > .select2-container--default.select2-container--open, .input-group > .select2-container--default.select2-container--focus {
    z-index: 3;
}

.input-group > .select2-container--default,
.input-group > .select2-container--default .input-group-btn,
.input-group > .select2-container--default .input-group-btn .btn {
    vertical-align: top;
}

.form-control.select2-hidden-accessible {
    position: absolute !important;
    width: 1px !important;
}

@media (min-width: 768px) {
    .form-inline .select2-container--default {
        display: inline-block;
    }
}

你可能感兴趣的:(bootstrap)