gpt4 book ai didi

html - Safari 选择/下拉选项未与输入标签对齐

转载 作者:行者123 更新时间:2023-12-04 14:07:52 27 4
gpt4 key购买 nike

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

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

enter image description here


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

enter image description here

但我希望它们对齐/齐平,看起来像

enter image description here


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

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>

最佳答案

我的尝试:我将选择包裹在一个带有圆形边框的容器中,并将嵌套的选择移到右侧。然后我调整了 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>

关于html - Safari 选择/下拉选项未与输入标签对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66961458/

27 4 0