I’m trying to change the height of the box that contains the arrow and the selected option. I’m using select2 and I don’t know much about it. Here’s my code:
HTML:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" crossorigin="anonymous">
<link rel="stylesheet" href="styles.css">
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<title>Hello, world!</title>
</head>
<body>
<h1>Hello, world!</h1>
<select class="form-select" id="searchSelect">
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
<option>Option 4</option>
</select>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.slim.min.js" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" crossorigin="anonymous"></script>
<script src="script.js"></script>
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
</body>
</html>
CSS:
body{
padding: 25px;
}
#select2-searchSelect-container {
height: 50px; //This didn't work
}
JavaScript:
$( document ).ready(function() {
$("#searchSelect").select2();
});
I tried to add height to .select2-containter, .selection, .select2-selection, .select2-selection--single, neither worked out.
Appreciate your help!
I tried to add height to .select2-containter, .selection, .select2-selection, .select2-selection--single, neither worked out.
>Solution :
I’m not enirely sure what you want but it looks like you’re just selecting the wrong element. Form what I see the selector should be something like .select2-container .select2-selection.select2-selection--single. Specificity may have been another issue, in any instance where you may have selected the correct element but the was another style overwriting your own.
$( document ).ready(function() {
$("#searchSelect").select2();
});
body{
padding: 25px;
}
.select2-container .select2-selection.select2-selection--single {
height: 50px; //Try this
}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" crossorigin="anonymous">
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<title>Hello, world!</title>
</head>
<body>
<h1>Hello, world!</h1>
<select class="form-select" id="searchSelect">
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
<option>Option 4</option>
</select>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.slim.min.js" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" crossorigin="anonymous"></script>
<script src="script.js"></script>
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
</body>
</html>