样式html选择和复选框

时间:2013-09-02 16:49:59

标签: html css checkbox html-select

Here是小提琴。我正在尝试使用CSS设置<select><input id='checkbox'>的样式。我目前正在使用select {background: #4a4a4a}并且它有效,但我无法使用任何其他样式。使用input[type='checkbox'] {background: #4a4a4a}

时,复选框样式根本不起作用

HTML:

<select>
    <option>Hello</option>
    <option>Hola</option>
    <option>Bonjour</option>
</select>
<input type='checkbox'>

CSS:

body {
    background: #252525;
}
select {
    background: #4a4a4a;
    border-radius: 0px;
}
input[type='checkbox'] {
    background: #4a4a4a;
    border-radius: 0px;
}

JS:

none

修改

我已经开始了一个项目,我正在创建自己的不可样式的表单元素。有关详细信息,请参阅this问题。

4 个答案:

答案 0 :(得分:2)

样式复选框

样式复选框在浏览器中很棘手且不一致。这是纯CSS方法。它利用了当标签和输入与id=连接时,单击标签激活输入框本身。那里不需要JavaScript。

<强> HTML

<input type="checkbox" id="my-checkbox">
<label for="my-checkbox">Checkbox label text 
   <span class="checkbox"></span>
</label>

<强> CSS 隐藏复选框,根据需要设置<span>的样式。我在这里使用过CSS精灵。

input[type="checkbox"] {
  display: none;
}
input[type="checkbox"] + label .checkbox {
  display: inline-block;
  width: 22px;
  height: 19px;
  vertical-align: middle;
  background: url('ui-sprite.png') left -90px no-repeat;
  cursor: pointer;
}
input[type="checkbox"]:checked + label .checkbox {
  background: url('ui-sprite.png') -30px -90px no-repeat;
}


样式选择输入

我还没有找到一个简单的工作解决方案。 Here is an article about a hack that seems to be on a good way.

答案 1 :(得分:1)

考虑到每个浏览器在输入元素样式方面都有自己的规则和异常,我倾向于使用像http://uniformjs.com/这样的东西来实现一致的输入样式。在具有数千个输入元素的页面上减慢速度,但是非常好。

答案 2 :(得分:0)

您无法设置所有表单元素的样式。浏览器往往不允许您设置复选框和选择框的样式(以及下拉菜单,收音机,文件上传等...)。我之前使用的一般概念是隐藏实际元素并使用替换元素(如div)显示给用户。该div可以按照您想要的方式进行设计。最常见的棘手部分和部分是当用户与模拟元素交互时,您必须实际更改隐藏表单元素的状态。

这是JQuery Plugin,它将提供上述功能。编写此插件的目的是用户根据需要对元素进行样式设置。下面是一个示例JsFiddle,它演示了插件并向CSS选择器公开了一些基本样式。以下基本代码......

<强> HTML

<form>
  <select>
    <option>Hello</option>
    <option>Hola</option>
    <option>Bonjour</option>
  </select>
  <br/>
  <input type='checkbox'>
</form>

<强> JQuery的

$(document).ready(function () {
    $('body').styleMyForms();
});

<强> CSS

 body {
     background: #252525;
 }
 .sf {
     position: relative;
     display: block;
     float: left;
 }
.sf-checkbox {
     top: 6px;
     margin-right: 5px;
     height: 15px;
     width: 15px;
     background: #fff;
     border: 1px solid #444;
     cursor: pointer;
     background: #4a4a4a;
     border-radius: 0px;
}
.sf-select {
     display: block;
     width: 220px;
     border: 1px solid #222;
     background: #4a4a4a;
     border-radius: 0px;
     padding: 0px 10px;
     text-decoration: none;
     color: #333;
     margin-bottom: 10px;
 }
.sf-select-wrap {
    position: relative;
    clear: both;
}
.sf-select-ul {
    background: #fff;
    display: none;
    list-style: none;
    padding: 0;
    margin: 0;
    position: absolute;
    border: 1px solid #888;
    width: 240px;
    padding: 0px;
    top: 33px;
}
.sf-select-ul li {
     position: relative;
     cursor: pointer;
     padding: 0px 10px;
     color: #333;
 }
.sf-select-ul li:hover {
     background: #efefef;
 }
 .sf-select-ul li.selected {
    background: #508196;
    color: #fff;
 }
 .sf-select:focus, .sf-radio:focus, .sf-checkbox:focus, input[type="text"]:focus {
     border-color: #222;
 }
 .sf-select:hover {
 }
 .sf-radio:hover, .sf-checkbox:hover, input[type="text"]:hover, input[type="text"]:focus,      .sf-select:focus {
     background: #efefef;
}
.sf-radio.selected, .sf-radio.selected:focus, .sf-radio.selected:hover, .sf-checkbox.selected, .sf-checkbox.selected:focus .sf-checkbox.selected:hover {
    background: #9cb7c3;
}
.clear {
    clear: both;
}
.buttonish {
    display: block;
    font-family:'Francois One', sans-serif;
    font-weight: normal;
    font-size: 2.8em;
    color: #fff;
    background: #9cb7c3;
    padding: 10px 20px;
    border-radius: 3px;
    text-decoration: none;
    width: 480px;
    text-align: center;
    margin-top: 50px;
    text-shadow: 2px 2px 4px #508196;
    box-shadow: 2px 2px 4px #222;
}

答案 3 :(得分:0)

在框中思考,当您在浏览器中查看时,填充的选择似乎有多少个框...

很多,他们有很多相关的样式/脚本(背景/颜色,填充,功能打开/关闭等)

实际上,您在 代码

中没有看到任何相关信息

因此代码只能来自浏览器

和浏览器不同,所有答案都是正确的,不要尝试设置样式,让JavaScript替换元素和功能。

相关问题