Safari select / 下拉选项未与输入标签对齐

Safari select / dropdown options are not aligned to input tag

这是我的下拉菜单。在 safari 中,下拉内容与可点击的边缘不对齐 select。包含选项的框的右侧和左侧应与 select 的右侧和左侧对齐。 Safari 是唯一发生这种情况的浏览器。

这是 Safari 浏览器中下拉菜单和选项的样子


现在,下拉选项和下拉菜单没有对齐,看起来像

但我希望它们是 aligned/flush,看起来像


这是创建下拉菜单的当前代码

select {
    padding: .375rem .75rem;
    background-color: #fff;
    width: 50%;
    border-radius: 1rem!important;
    cursor: pointer;
    box-shadow: rgba(0, 0, 0, 0.04) 0px 1px 0px 1px;
    appearance: none;
    -webkit-appearance: none;
    background-image: url(data:image/svg+xml;charset=US-ASCII,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22292.4%22%20height%3D%22292.4%22%3E%3Cpath%20fill%3D%22%23007CB2%22%20d%3D%22M287%2069.4a17.6%2017.6%200%200%200-13-5.4H18.4c-5%200-9.3%201.8-12.9%205.4A17.6%2017.6%200%200%200%200%2082.2c0%205%201.8%209.3%205.4%2012.9l128%20127.9c3.6%203.6%207.8%205.4%2012.8%205.4s9.2-1.8%2012.8-5.4L287%2095c3.5-3.5%205.4-7.8%205.4-12.8%200-5-1.9-9.2-5.5-12.8z%22%2F%3E%3C%2Fsvg%3E);
    background-size: 0.65em, 100%;
    background-repeat: no-repeat, repeat;
    background-position: right 0.7em top 50%, 0px 0px;
}

select:active, select:focus {
  outline: none;
}
<div>
<select >
  <option>Option 1</option>
  <option>Option 2</option>
  <option>Option 3</option>
  <option>Option 4</option>
</select>
</div>

根据我的研究,Safari 仅松散地支持 rem。这可能会导致问题,但我不确定。

select 或其父位置是绝对位置吗?

这里有几篇关于 select 的文章 https://moderncss.dev/custom-select-styles-with-pure-css/ https://css-tricks.com/the-current-state-of-styling-selects-in-2019/

我已经测试过了。我觉得是时候了!

.h {      
   position: absolute;
   border: 1px white solid;      
   border-radius: 1rem;
   width: 50%;
   cursor: pointer;
   box-shadow: rgba(0, 0, 0, 0.04) 0px 1px 0px 1px;
}

.h select {
   border: 0;
   cursor: pointer;
   -webkit-appearance: none;
   appearance: none;
   width: 100%;
}

@supports (-webkit-font-smoothing: auto) {

   .h::after {
       position: absolute;
       z-index: 2;
       top: calc(45% - 5px);
       right: .5rem;
       width: 10px;
       height: 10px;
   }

   .h select {
      padding: .375rem 0;
      width: calc(100% - 4px);
      transform: translateX(15px); 
   }
 
}
    
select:active, select:focus {
      outline: none;
}
<div class="h">
    <select>
      <option>Option 1</option>
      <option>Option 2</option>
      <option>Option 3</option>
      <option>Option 4</option>
    </select>
</div>

我的尝试:我将 select 包裹在一个带有圆形边框的容器中,并将嵌套的 select 移动到右侧。然后我调整了 select 的左填充,以便让第一个选项像您的示例中那样对齐

我还使用 @supports 在仅支持 webkit 的浏览器上应用了这种新样式。

 .select-wrapper {      
   position: relative;
   border: 1px currentColor solid;      
   border-radius: 1rem;
   width: 50%;
   overflow-x: hidden;
   cursor: pointer;
   box-shadow: rgba(0, 0, 0, 0.04) 0px 1px 0px 1px;
}

.select-wrapper select {
   border: 0;
   cursor: pointer;
   -webkit-appearance: none;
   appearance: none;
   width: 100%;
   box-sizing: border-box;
}

.select-wrapper select:active, 
.select-wrapper select:focus {
   outline: none;
}

/* Webkit only style */

@supports (-webkit-font-smoothing: auto) {

 .select-wrapper::after {
   content: "";
   position: absolute;
   z-index: 1;
   top: calc(50% - 5px);
   right: .5rem;
   width: 10px;
   height: 10px;
   pointer-events: none;
   background-image: url(data:image/svg+xml;charset=US-ASCII,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22292.4%22%20height%3D%22292.4%22%3E%3Cpath%20fill%3D%22%23007CB2%22%20d%3D%22M287%2069.4a17.6%2017.6%200%200%200-13-5.4H18.4c-5%200-9.3%201.8-12.9%205.4A17.6%2017.6%200%200%200%200%2082.2c0%205%201.8%209.3%205.4%2012.9l128%20127.9c3.6%203.6%207.8%205.4%2012.8%205.4s9.2-1.8%2012.8-5.4L287%2095c3.5-3.5%205.4-7.8%205.4-12.8%200-5-1.9-9.2-5.5-12.8z%22%2F%3E%3C%2Fsvg%3E);
   background-size: 100%, 100%;
   background-repeat: no-repeat, repeat;
   background-position: center, 0px 0px;
 }
  
 .select-wrapper select {
   padding: .375rem 0;
   width: calc(100% - 4px);
   transform: translateX(13px); /* adjust as you prefer */
 }
}


/* Non-Webkit style */

@supports (not (-webkit-font-smoothing: auto)) {
   
  .select-wrapper select {
   padding: .375rem .75rem;
   background-image: url(data:image/svg+xml;charset=US-ASCII,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22292.4%22%20height%3D%22292.4%22%3E%3Cpath%20fill%3D%22%23007CB2%22%20d%3D%22M287%2069.4a17.6%2017.6%200%200%200-13-5.4H18.4c-5%200-9.3%201.8-12.9%205.4A17.6%2017.6%200%200%200%200%2082.2c0%205%201.8%209.3%205.4%2012.9l128%20127.9c3.6%203.6%207.8%205.4%2012.8%205.4s9.2-1.8%2012.8-5.4L287%2095c3.5-3.5%205.4-7.8%205.4-12.8%200-5-1.9-9.2-5.5-12.8z%22%2F%3E%3C%2Fsvg%3E);
   background-size: 0.85em, 100%;
   background-repeat: no-repeat, repeat;
   background-position: right 0.7em top 50%, 0px 0px;
  }
}
<div class="select-wrapper">
  <select >
    <option>Option 1</option>
    <option>Option 2</option>
    <option>Option 3</option>
    <option>Option 4</option>
  </select>
</div>