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

Custom Radio CSS and multiline text wrapping

我使用 CSS 为调查表自定义了我们的单选按钮。但是,由于隐藏了标准单选按钮,当文本较长时,它会换行到替换图形下方。

我们尽量避免编辑 HTML,因为这部分是由我们的 CRM 系统生成的,编辑 HTML 意味着我们必须从头开始重建整个调查,并且时间就是生命!

我没有足够的代表 post 图片,但这里有一个 link 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:'14';
    text-indent:.6em;
    color:#C2C2C2;
}

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

/* toggle on */


input[type=radio]:checked + label:before {
    content:'14';
    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%;
}

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:'14';
    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:'14';
    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>

您可以将 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;
}

真正简单的修复。

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: '14';
  text-indent: .6em;
  color: #C2C2C2;
}
input[type=radio]:hover:not(:checked) + label {
  color: #888;
}
/* toggle on */

input[type=radio]:checked + label:before {
  content: '14';
  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%;
}
<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>