If you see screenshot, When I use placeholder for select2 for multiple select box. It is working fine when I select something from select box, But when I see by default placeholder.
What I am doing: I need to show a placeholder for select2 box, How to fix this height issue?
$('#search_user_events').select2({
placeholder: 'Select events',
multiple: true,
allowClear: true,
dropdownAutoWidth: true,
width: '100%'
});
<select class="form-control" id="search_user_events" name="search_user_events">
<option></option>
@foreach($events as $key => $event)
<option value="{{$event->id}}">{{$event->name}}</option>
@endforeach
</select>
Your issue is up to the first option: empty and selected by default....
You may combine translateY() with .toggleClass( function [, state ] ) and change event:
$('#search_user_events').select2({ placeholder: 'Select events', multiple: true, allowClear: true, dropdownAutoWidth: true, width: '100%' }).on('change', function (e) { $('.select2-search__field').toggleClass('mcenter', $(this).val().length <= 1); }).trigger('change');
.mcenter { transform: translateY(-60%); }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-beta.1/dist/css/select2.min.css" rel="stylesheet" /> <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-beta.1/dist/js/select2.min.js"></script> <select class="form-control" id="search_user_events" name="search_user_events"> <option></option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> </select>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.