【发布时间】:2021-11-08 06:20:29
【问题描述】:
我有以下 HTML:
<!doctype html>
<html>
<body>
<div style="width:501px;background:#EEE;">
<div style="width:100%;max-width:400px;margin:auto;background:#DDD;">
<select style="width:200px;box-sizing:border-box;">
<option>test1</option>
<option>test2</option>
</select>
</div>
</div>
</body>
</html>
由于容器设置为奇数并且选择的父 div 设置为自动边距,因此无法对齐它。结果是选项列表在选择下没有正确对齐。 这发生在 FF 88.01 中,但不在 Edge 93 中:screenshot
我可以通过将 select 设置为 199px 或将容器设置为 500px 来解决它,但是还有其他不涉及牺牲宽度的解决方案吗?
编辑:即使没有所有容器也会发生这种情况。即使我将选择放在 100% 宽度上,它本身也放在一个空白页面上。这将是 Firefox 的事情,而不是我的代码。根据视口,有时会错位,有时不会。
注意:我在 jsfiddle 中看到了错误,但是在此处运行 sn-p 不会重现该问题,无论视口如何。我想我会忍受它。我无法更改它 - 奇怪的浏览器行为。
$('#msg').text($(window).width());
$(window).resize(function() {
$('#msg').text($(window).width());
});
select{width:100%;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select>
<option>test1</option>
<option>test2</option>
</select>
<div id="msg"></div>
【问题讨论】:
标签: html css select offset option