如何设置 HTML 选择/选项标签的样式? [英] How to style HTML select/option tag?
问题描述
我想创建一个带有 html select 的下拉菜单,因此我也想对其进行样式设置.我想为选项标签设置一个填充,这样它们就不会那么拥挤,但我不能这样做.(它不起作用)有什么想法吗?
#categories{填充:10px 10px 10px 10px;背景色:#826B78;字体大小:20px;白颜色;边距:10px 10px 10px 10px;边界:无;大纲:无;}#categories 选项{边距:30px 30px 30px 30px;填充:30px 30px 30px 30px;字体大小:20px;}
categories id 属于 select 标签
使用 css 很难设置选择菜单的样式.最好的方法是使用 jquery,您可以使用 jquery 设置样式并更好地控制代码.只需使用像 http://gregfranko.com/jquery.selectBoxIt.js/
我刚刚用jquery做了一个样式选择的例子,你可以在这里查看演示
//将 select 转换为 UL 的代码$('.select').each(function() {var $select = $(this).find('select'),$list = $('
');$select.find('option').each(function() {$list.append('' + $(this).text() + ' ');});//取值后移除select$select.after($list).remove();//附加默认文本以显示选定的$(this).append('<span>select</span>')var firsttxt = $(this).find('li:first-child').text();$(this).find('span').text(firsttxt)//单击时显示 UL$(this).on('click', 'span', function(e) {e.stopPropagation();$(this).parent().find('ul').show();});//在选择列表时选择项目$(this).on('click', 'li', function() {var gettext = $(this).text();$(this).parents('.select').find('span').text(gettext);$(this).parent().fadeOut();});})//点击后隐藏 UL$(document).on('click', function() {$('.select ul').fadeOut();});
body {font-family: 'arial', san-serif;}.选择 {宽度:200px;高度:30px;边框:1px 实心#ddd;行高:30px;位置:相对;底边距:40px;}.选择跨度{显示:块;颜色:#333;字体大小:12px;填充:0 10px;}.select ul {显示:无;背景:#fff;边框:1px 实心#ddd;最小宽度:300px;位置:绝对;顶部:100%;左:0;列表样式类型:无;边距:0;填充:0;z-索引:10;}.select ul >李{字体大小:12px;}.select ul >李{颜色:#333;显示:块;填充:2px 8px;文字装饰:无;行高:24px;}.select ul >李:悬停{背景:#e4f4fa;光标:指针;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><label>首选</label><div class="选择"><选择><option>项目</option><option>项目 2</option><option>项目 3</option></选择>
<label>第二个选择</label><div class="选择"><选择><option>这个1</option><option>这个 2</option><option>这个3</option></选择>
I would like to create a dropdown menu with html select thus I would like to style it as well. I would like to set a padding to the option tags so they wouldn't be so overcrowded, but I can't do so. (it doesn't work) any idea?
#categories
{
padding: 10px 10px 10px 10px;
background-color: #826B78;
font-size: 20px;
color: white;
margin: 10px 10px 10px 10px;
border: none;
outline: none;
}
#categories option
{
margin: 30px 30px 30px 30px;
padding: 30px 30px 30px 30px;
font-size: 20px;
}
the categories id belongs to the select tag
It is difficult to style select menu with css. The Best way of doing it is with jquery u can style and have a better control over the code with jquery. Just use a custom jquery like http://gregfranko.com/jquery.selectBoxIt.js/
I have just made an example of styling select with jquery, you can check the demo here
// Code to convert select to UL
$('.select').each(function() {
var $select = $(this).find('select'),
$list = $('<ul />');
$select.find('option').each(function() {
$list.append('<li>' + $(this).text() + '</li>');
});
//Remove the select after the values are taken
$select.after($list).remove();
//Append Default text to show the selected
$(this).append('<span>select</span>')
var firsttxt = $(this).find('li:first-child').text();
$(this).find('span').text(firsttxt)
// On click show the UL
$(this).on('click', 'span', function(e) {
e.stopPropagation();
$(this).parent().find('ul').show();
});
// On select of list select the item
$(this).on('click', 'li', function() {
var gettext = $(this).text();
$(this).parents('.select').find('span').text(gettext);
$(this).parent().fadeOut();
});
})
// On click out hide the UL
$(document).on('click', function() {
$('.select ul').fadeOut();
});
body {
font-family: 'arial', san-serif;
}
.select {
width: 200px;
height: 30px;
border: 1px solid #ddd;
line-height: 30px;
position: relative;
margin-bottom: 40px;
}
.select span {
display: block;
color: #333;
font-size: 12px;
padding: 0 10px;
}
.select ul {
display: none;
background: #fff;
border: 1px solid #ddd;
min-width: 300px;
position: absolute;
top: 100%;
left: 0;
list-style-type: none;
margin: 0;
padding: 0;
z-index: 10;
}
.select ul > li {
font-size: 12px;
}
.select ul > li {
color: #333;
display: block;
padding: 2px 8px;
text-decoration: none;
line-height: 24px;
}
.select ul > li:hover {
background: #e4f4fa;
cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>First Select</label>
<div class="select">
<select>
<option>Item</option>
<option>Item 2</option>
<option>Item 3</option>
</select>
</div>
<label>Second Select</label>
<div class="select">
<select>
<option>this 1</option>
<option>this 2</option>
<option>this 3</option>
</select>
</div>
这篇关于如何设置 HTML 选择/选项标签的样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!