如何在选择菜单中删除顶部/左侧填充?
我尝试将填充和边距设置为0,但这没有效果。这是HTML:
<form class="timePeriodMenu">
<div class="ui-field-contain">
<select name="select-native-2" id="select-native-2" multiple>
<optgroup>
<option value="1">last 30 days</option>
<option value="6" selected="selected">past 6 months</option>
<option value="12">past 12 months</option>
<option value="300">list all</option>
</optgroup>
</select>
</div>
</form>
jsFiddle:https://jsfiddle.net/wccnuxgt/
答案 0 :(得分:4)
您在图片中引用的空间不是padding
,而是optgroup
标记内Select
标记所占用的空间。由于SELECT
标记的浏览器限制以及HTML
方法,因此无法将其删除。 left
上的缩进代表分组结构。
如果您不想要该空格,请不要使用optgroup
标记,而是直接转到option
,如下方小提琴所示。
检查演示1
或者,如果您仍想保留optgroup
标记,则可以执行CSS hack,删除vertical-space
,但它无法帮助您删除left
边空格。
检查演示2
.demo-2 optgroup {
font-size: 0;
}
.demo-2 option {
font-size: 14px;
}
DEMO 1 - Removing OPTGROUP tag
<form class="timePeriodMenu demo-1">
<div class="ui-field-contain">
<select name="select-native-2" id="select-native-2" multiple>
<option value="1">last 30 days</option>
<option value="6" selected="selected">past 6 months</option>
<option value="12">past 12 months</option>
<option value="300">list all</option>
</select>
</div>
</form>
<hr> DEMO 2 - Keeping Optgroup, but removing vertical-space with CSS hack.
<form class="timePeriodMenu demo-2">
<div class="ui-field-contain">
<select name="select-native-2" id="select-native-2" multiple>
<optgroup label="German Cars">
<option value="1">last 30 days</option>
<option value="6" selected="selected">past 6 months</option>
<option value="12">past 12 months</option>
<option value="300">list all</option>
</optgroup>
</select>
</div>
</form>
答案 1 :(得分:2)
在 Chrome 和 Opera 中,用户代理样式表将一个:: before元素添加到选择中:
select:-internal-list-box optgroup option::before {
content: " ";
}
如果你想要一个CSS修复,应该这样做:
.demo-2 optgroup {
font-size: 0;
}
.demo-2 option {
font-size: 14px;
}
optgroup option::before {
content: "";
}
&#13;
DEMO 1 - Removing OPTGROUP tag
<form class="timePeriodMenu demo-1">
<div class="ui-field-contain">
<select name="select-native-2" id="select-native-2" multiple>
<option value="1">last 30 days</option>
<option value="6" selected="selected">past 6 months</option>
<option value="12">past 12 months</option>
<option value="300">list all</option>
</select>
</div>
</form>
<hr> DEMO 2 - Keeping Optgroup, but removing vertical-space with CSS hack.
<form class="timePeriodMenu demo-2">
<div class="ui-field-contain">
<select name="select-native-2" id="select-native-2" multiple>
<optgroup label="German Cars">
<option value="1">last 30 days</option>
<option value="6" selected="selected">past 6 months</option>
<option value="12">past 12 months</option>
<option value="300">list all</option>
</optgroup>
</select>
</div>
</form>
&#13;
编辑:
有趣的是根据浏览器的不同添加此空间。这个修复在Chrome和Opera中适用于我,但在 Firefox 中我必须添加:
padding-left: 0px;
到选项元素。
进一步编辑:
答案 2 :(得分:1)
两个选项:
解决方案1:
<form class="timePeriodMenu">
<div class="ui-field-contain">
<select name="select-native-2" id="select-native-2" multiple>
<option value="1">last 30 days</option>
<option value="6" selected="selected">past 6 months</option>
<option value="12">past 12 months</option>
<option value="300">list all</option>
</select>
</div>
</form>
解决方案2:
<form class="timePeriodMenu">
<div class="ui-field-contain">
<select name="select-native-2" id="select-native-2" multiple>
<optgroup label="here is your label">
<option value="1">last 30 days</option>
<option value="6" selected="selected">past 6 months</option>
<option value="12">past 12 months</option>
<option value="300">list all</option>
</optgroup>
</select>
</div>
</form>
答案 3 :(得分:0)
怎么样:
<form class="timePeriodMenu">
<div class="ui-field-contain">
<select name="select-native-2" id="select-native-2" multiple>
<option value="1">last 30 days</option>
<option value="6" selected="selected">past 6 months</option>
<option value="12">past 12 months</option>
<option value="300">list all</option>
</select>
</div>
</form>
Alos check:HTML optgroup Tag