搜尋

首頁  >  問答  >  主體

Select2 選擇下拉式選單:清除與文字重疊的圖標

我遇到一個問題,文字在選擇選單中懸停的 X「清除」按鈕下重疊。在右側添加填充並不是解決方案,因為它將圖標推到框外。我正在使用此處找到的 Select2 JS 庫 https://select2.org/ 和 Bootstrap 5

$("#search_bar").select2({
  theme: 'bootstrap-5',
  placeholder: "Some text",
  allowClear: true
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js" integrity="sha512-2ImtlRlf2VVmiGZsjm9bEyhjGW4dU7B6TNwh/hx/iSByxNENtj3WVE6o/9Lj4TJeVXPi4bnOIMXFIJJAeufa0A==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css" integrity="sha512-nMNlpuaDPrqlEls3IX/Q56H36qvBASwb3ipuo3MxeWbsQB1881ox0cRv7UPTgBlriqoynt35KjEwgGUeUXIPnw==" crossorigin="anonymous" referrerpolicy="no-referrer"
/>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/select2-bootstrap-5-theme.min.css" />

<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">


<div class="">
  <select id="search_bar" class="form-select w-25">
    <option value="1">
      This is a long option string
    </option>
    <option value="2">
      This is another long option string
    </option>
  </select>
</div>

P粉156415696P粉156415696331 天前541

全部回覆(2)我來回復

  • P粉155832941

    P粉1558329412024-02-26 13:39:01

    在下面加入 CSS。

    .select2-selection__clear {
        位置:絕對!重要;
        右:2px!重要;
    }

    https://jsfiddle.net/8n23bd6q/

    #

    回覆
    0
  • P粉345302753

    P粉3453027532024-02-26 11:25:34

    依照此處的文檔操作 Select2 文檔。您可以使用 width: 'style' 和標籤 <select> 中的內聯樣式來擴展 select2 寬度。這裡我將select的寬度設定為200px

    #然後,透過查看 select2 的 DOM 結構(使用 Chrome Devtool),我發現我們可以透過新增以下 CSS 來讓 X 按鈕不與文字重疊:

    .select2-selection.select2-selection--single {
      padding-right: 50px !important;
    }

    $("#search_bar").select2({
      theme: 'bootstrap-5',
      placeholder: "Some text",
      allowClear: 真,
      width: 'style'
    });
    .select2-selection.select2-selection--single {
      padding-right: 50px !important;
    }
    
    
    
    [email protected]/dist/select2-bootstrap-5-theme.min.css" />
    
    [email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXFonyjkDbrCEXFonyjginjginDun​​okijginjDbrC21oBorD
    
    
    
    #

    回覆
    0
  • 取消回覆