简体   繁体   English

在 ReactJS 环境中设置复选框样式

[英]Styling a checkbox in a ReactJS environment

I'm trying to style a checkbox in a ReactJS environment for IE11, but don't seem to be having much success.我正在尝试在 IE11 的ReactJS环境中设置复选框的样式,但似乎并没有取得太大的成功。 Can anyone please advise?任何人都可以请指教吗? The code is as follows:代码如下:

CSS: CSS:

 .squared  {
      input[type=checkbox] {
        border-radius: 4px;
        background: #ff0000;
        width:100px;
    }

HTML: HTML:

<Col numCol={2} className="col-w-select squared">
  <input style={{float: 'left', borderColor: 'red', border: '3px'}} type="checkbox" checked={isChecked} /> <span className={(String(currentlyClicked) !== String(item[idField])) ? 'l-collapse-icon' : 'l-expand-icon'}>&nbsp;</span>
</Col>

Please note that applying styling attributes within the input tag itself seems to have no affect!?请注意,在 input 标签本身内应用样式属性似乎没有影响!?

Thanks谢谢

The environment is basically irrelevant here for just styling elements with CSS.环境在这里基本上与 CSS 样式元素无关。 The real problem here is that you are trying to style a checkbox which doesn't really work in browsers.这里真正的问题是您正在尝试设置一个在浏览器中不起作用的复选框。

Here's what the MDN says about this:以下是MDN对此的说明:

Styling a check box or a radio button by itself is kind of messy.单独设置复选框或单选按钮的样式有点麻烦。 For example, the sizes of check boxes and radio buttons are not really meant to be changed, and browsers can react very differently if you try to do it.例如,复选框和单选按钮的大小并不是真的要改变,如果你尝试这样做,浏览器的反应可能会有很大不同。

A workaround to this is to create a "fake" checkbox and style it the way you want, while hiding the "real" one.一种解决方法是创建一个“假”复选框并按照您想要的方式对其进行样式设置,同时隐藏“真实”复选框。

Below is an implementation I use in my project.下面是我在我的项目中使用的一个实现。 The MSN link above provides some alternative (CSS) solutions as well...上面的 MSN 链接也提供了一些替代 (CSS) 解决方案......

 var Demo = React.createClass({ getInitialState() { return {isChecked: false}; }, toggleCheck() { this.setState({isChecked: !this.state.isChecked}); }, render: function() { return ( <span onClick={this.toggleCheck}> <input type="checkbox" checked={this.state.isChecked} /> <span></span> </span> ); } }); ReactDOM.render( <Demo />, document.getElementById('container') );
 input[type="checkbox"] { display: none; } input[type="checkbox"] + span { display: inline-block; position: relative; top: -1px; width: 12px; height: 12px; margin: -1px 0px 0 0; vertical-align: middle; background: white left top no-repeat; border: 1px solid #ccc; cursor: pointer; } input[type="checkbox"]:checked + span { background: #D9534F -19px top no-repeat; } input[type="checkbox"] + span { margin-right: 4px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="container"></div>


If you are using , just replace the JSX bit above with this:如果您使用的是 ,只需将上面的 JSX 位替换为:

<Checkbox onChange={this.toggleCheck} checked={this.toggleCheck} inline>
  <span></span>
</Checkbox>

Good luck.祝你好运。

Just add this code to your CSS, and you will be able to style your checkbox!只需将此代码添加到您的 CSS,您就可以设置复选框的样式!

input[type=checkbox] {
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    /* create custom checkbox appearance */
    display: inline-block;
    width: 25px;
    height: 25px;
    padding: 6px;
    /* background-color only for content */
    background-clip: content-box;
    border: 1.5px solid #bbbbbb;
    border-radius: 6px;
    background-color: #e7e6e7;
    margin-left: 15px;
    margin-right: 15px;

    &:checked{
        background-color: #ff0000;
    }

    &:focus{
        outline: none !important;
    }
}

Following @ Chris answer I've changed a bit the styles to suit my needs, sharing in case anyone else is looking for something like it.按照@ Chris 的回答,我改变了一些样式以满足我的需要,如果其他人正在寻找类似的东西,请分享。 Haven't tested it in IE11.没有在 IE11 中测试过。

Thanks!谢谢!

 var Demo = React.createClass({ getInitialState() { return {isChecked: false}; }, toggleCheck() { this.setState({isChecked: !this.state.isChecked}); }, render: function() { return ( <span className="checkbox"> <input type="checkbox" checked={this.state.isChecked} /> <span className="wrapper" onClick={this.toggleCheck}> <span className="knob"></span> </span> </span> ); } }); ReactDOM.render( <Demo />, document.getElementById('container') );
 .checkbox input[type="checkbox"] { display: none; } .checkbox input[type="checkbox"]:checked + .wrapper { background-color: #8cde95; } .checkbox input[type="checkbox"]:checked + .wrapper .knob { left: 20px; } .checkbox .wrapper { background-color: #666; border: 1px solid #666; border-radius: 10px; width: 42px; height: 20px; display: inline-flex; align-items: center; cursor: pointer; outline: none; } .checkbox .knob { background-color: white; border: 1px solid #666; border-radius: 100%; display: inline-block; margin-left: 2px; position: relative; width: 16px; height: 16px; left: 0; transition: left 100ms ease-in-out 0s; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="container"></div>

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

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