自定义单选CSS和多行文本换行

时间:2015-03-23 12:08:51

标签: html css radio-button

我使用CSS为调查表单定制了单选按钮。但是,由于隐藏了标准单选按钮,因此当文本较长时,它会包裹在替换图形下方。

我们正在努力避免编辑HTML,因为我们的CRM系统已经生成了这部分,并且编辑HTML意味着我们必须从头开始重建整个调查,时间非常重要!

我没有足够的代表来发布图片,但这里是指向它的链接http://i.stack.imgur.com/YV79T.png

HTML

    <td style="vertical-align: top;">
    <input name="q_197" value="1042" id="q_197_1042" type="radio">
    <label for="q_197_1042">I didn't get the chance to say everything I wanted 
to about stuff</label>
    </td>

CSS

label {
    min-width: 230px;
    padding-right: 10px;
    border-radius:3px;
    border:1px solid #D1D3D4;
}

/* hide input */


input[type=radio]:empty {
    display:none;
}
/* style label */



input[type=radio]:empty + label {
    position:relative;
    float:left;
    line-height:2.5em;
    text-indent:3em;
    margin:5px 1px 5px;
    cursor:pointer;
    -webkit-user-select:none;
    -moz-user-select:none;
    -ms-user-select:none;
    user-select:none;
}

input[type=radio]:empty + label:before {
    position:absolute;
    display:block;
    top:0;
    bottom:0;
    left:0;
    content:'';
    width:2em;
    background:#D3D3DB;
    border-radius:3px 0 0 3px;
}


/* toggle hover */


input[type=radio]:hover:not(:checked) + label:before {
    content:'\2714';
    text-indent:.6em;
    color:#C2C2C2;
}

input[type=radio]:hover:not(:checked) + label {
    color:#888;
}

/* toggle on */


input[type=radio]:checked + label:before {
    content:'\2714';
    text-indent:.6em;
    color:#F4F5F8;
    background-color:#0099FF;
}

input[type=radio]:checked + label {
    color:#777;
}

/* radio focus */


input[type=radio]:focus + label:before {
    box-shadow:0 0 0 3px #999;
}

textarea {
  -webkit-box-sizing: border-box;
  -mox-box-sizing: border-box;
  box-sizing: border-box;
  width: 100%;
}

2 个答案:

答案 0 :(得分:0)

input[type=radio]:empty + label替换为以下内容,它可以正常运行:

input[type=radio]:empty + label {
    position:relative;
    float:left;
    margin:5px 1px 5px;
    padding:10px 10px 10px 50px;
    cursor:pointer;
    -webkit-user-select:none;
    -moz-user-select:none;
    -ms-user-select:none;
    user-select:none;
}

工作示例:

label {
    min-width: 230px;
    padding-right: 10px;
    border-radius:3px;
    border:1px solid #D1D3D4;
}
/* hide input */
 input[type=radio]:empty {
    display:none;
}
/* style label */
 input[type=radio]:empty + label {
    position:relative;
    float:left;
    margin:5px 1px 5px;
    padding:10px 10px 10px 50px;
    cursor:pointer;
    -webkit-user-select:none;
    -moz-user-select:none;
    -ms-user-select:none;
    user-select:none;
}
input[type=radio]:empty + label:before {
    position:absolute;
    display:block;
    top:0;
    bottom:0;
    left:0;
    content:'';
    width:2em;
    background:#D3D3DB;
    border-radius:3px 0 0 3px;
}
/* toggle hover */
 input[type=radio]:hover:not(:checked) + label:before {
    content:'\2714';
    text-indent:.6em;
    padding-top: 10px;
    color:#C2C2C2;
}
input[type=radio]:hover:not(:checked) + label {
    color:#888;
}
/* toggle on */
 input[type=radio]:checked + label:before {
    content:'\2714';
    text-indent:.6em;
    color:#F4F5F8;
    background-color:#0099FF;
    padding-top: 10px;
}
input[type=radio]:checked + label {
    color:#777;
}
/* radio focus */
 input[type=radio]:focus + label:before {
    box-shadow:0 0 0 3px #999;
}
textarea {
    -webkit-box-sizing: border-box;
    -mox-box-sizing: border-box;
    box-sizing: border-box;
    width: 100%;
}
<td style="vertical-align: top;">
    <input name="q_197" value="1042" id="q_197_1042" type="radio">
    <label for="q_197_1042">I didn't get the chance to say everything I wanted to about stuff</label> 
</td>

答案 1 :(得分:0)

您只需将text-indent切换为padding-left

即可
input[type=radio]:empty + label {
  position: relative;
  float: left;
  line-height: 2.5em;
  text-indent: 0em; /* here */
  padding-left: 3em; /* and here */
  margin: 5px 1px 5px;
  cursor: pointer;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

真的很简单。

&#13;
&#13;
label {
  min-width: 230px;
  max-width: 230px;
  /* for demo purposes only */
  padding-right: 10px;
  border-radius: 3px;
  border: 1px solid #D1D3D4;
}
/* hide input */

input[type=radio]:empty {
  display: none;
}
/* style label */

input[type=radio]:empty + label {
  position: relative;
  float: left;
  line-height: 2.5em;
  text-indent: 0em;
  padding-left: 3em;
  margin: 5px 1px 5px;
  cursor: pointer;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}
input[type=radio]:empty + label:before {
  position: absolute;
  display: block;
  top: 0;
  bottom: 0;
  left: 0;
  content: '';
  width: 2em;
  background: #D3D3DB;
  border-radius: 3px 0 0 3px;
}
/* toggle hover */

input[type=radio]:hover:not(:checked) + label:before {
  content: '\2714';
  text-indent: .6em;
  color: #C2C2C2;
}
input[type=radio]:hover:not(:checked) + label {
  color: #888;
}
/* toggle on */

input[type=radio]:checked + label:before {
  content: '\2714';
  text-indent: .6em;
  color: #F4F5F8;
  background-color: #0099FF;
}
input[type=radio]:checked + label {
  color: #777;
}
/* radio focus */

input[type=radio]:focus + label:before {
  box-shadow: 0 0 0 3px #999;
}
textarea {
  -webkit-box-sizing: border-box;
  -mox-box-sizing: border-box;
  box-sizing: border-box;
  width: 100%;
}
&#13;
<input name="q_197" value="1042" id="q_197_1042" type="radio">
<label for="q_197_1042">I didn't get the chance to say everything I wanted to about stuff</label>
&#13;
&#13;
&#13;