简体   繁体   English

样式化HTML选择和复选框

[英]Styling html select and checkbox

Here is the fiddle. 是小提琴。 I am trying to style the <select> and <input id='checkbox'> using CSS. 我正在尝试使用CSS设置<select><input id='checkbox'>样式。 I am currently using select {background: #4a4a4a} and it works, but I cannot get any other styles to work. 我目前正在使用select {background: #4a4a4a}并且可以使用,但是我无法使用其他任何样式。 The checkbox style doesn't work at all when using input[type='checkbox'] {background: #4a4a4a} 使用input[type='checkbox'] {background: #4a4a4a}时,复选框样式完全无效

HTML: HTML:

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

CSS: CSS:

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

JS: JS:

none

Edit 编辑

I have started a project where I am making my own not styleable form elements. 我开始了一个项目,在此项目中我制作了自己无法样式化的表单元素。 For more info see this question. 欲了解更多信息请参见问题。

Styling checkboxes 样式复选框

Styling checkboxes is tricky and inconsistent across browsers. 样式复选框在各个浏览器之间都是棘手且不一致的。 Here is pure CSS approach. 这是纯CSS方法。 It takes advantage of that when label and input are connected with an id= , clicking on the label activates the input box itself. 利用标签和输入的id=连接时,单击标签可激活输入框本身。 No JavaScript needed there. 那里不需要JavaScript。

HTML 的HTML

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

CSS Hide checkbox, style the <span> as you like. CSS隐藏复选框,根据需要设置<span>样式。 I've used a CSS sprite here. 我在这里使用了CSS Sprite。

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;
}


Styling select inputs 选择输入样式

I haven't found a simple working solution for this yet. 我还没有找到一个简单的解决方案。 Here is an article about a hack that seems to be on a good way. 这是一篇有关骇客的文章,似乎很不错。

Given how every browser has its own rules and exceptions when it comes to input element styling, I tend to use things like http://uniformjs.com/ for consistent input styling. 鉴于每种浏览器在输入元素样式方面都有自己的规则和例外,我倾向于使用http://uniformjs.com/之类的东西来实现一致的输入样式。 Slows things down on pages with thousands of input elements, but otherwise quite excellent. 在具有数千个输入元素的页面上放慢速度,但是在其他方面却非常出色。

You cannot style all form elements. 您不能设置所有表单元素的样式。 Browsers tend to not allow you to style check-boxes and select boxes (As well as drop downs, radios, file uploads etc...). 浏览器往往不允许您设置复选框的样式和选择框(以及下拉菜单,单选按钮,文件上传等)。 The general concept I have used before is to hide the actual element and use a replacement element such as a div to display to the user. 我之前使用的一般概念是隐藏实际元素,并使用替换元素(例如div)显示给用户。 That div can be styled to look and work the way you want. 可以将div样式设置为以所需方式显示和工作。 The tricky part and part most often missed is you have to actually change the state of the hidden form element when the user interacts with the mock element. 最容易错过的棘手部分是,当用户与模拟元素进行交互时,您必须实际更改隐藏表单元素的状态。

This is a JQuery Plugin that will provide the above functionality. 这是一个JQuery插件 ,将提供上述功能。 This plugin was written with the intent that the user would style the elements according to what they need. 编写此插件的目的是用户可以根据需要对元素进行样式设置。 Here is an example JsFiddle that demonstrates the plugin and exposes the CSS selectors with some basic styling. 这是一个JsFiddle示例,它演示了插件并以一些基本样式公开了CSS选择器。 Basic code below... 下面的基本代码...

HTML 的HTML

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

JQuery jQuery查询

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

CSS 的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;
}

Think in boxes, how many boxes does a populated select seem to have when you look at it in a browser... 想想在框中,当您在浏览器中查看时,填充的选择似乎有多少框...

a lot , and they have lots of associated styles/scripts (background/colors,paddings, the functionality open/close etc.) 很多 ,并且它们具有很多相关的样式/脚本(背景/颜色,填充,打开/关闭等功能)。

And actually you don't see anything of that in your code 而实际上你看不到任何东西在你的代码

So the code can only come from the browser 所以代码只能来自浏览器

and browsers are different , all answers are correct, don't try to style it, let a JavaScript replace the elements and functionality. 和浏览器不同 ,所有答案都是正确的,请勿尝试设置其样式,而应由JavaScript替换元素和功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM