2016-12-19 156 views
3

我想弄清楚如何使用CSS來以同樣的方式替換我所有的單選按鈕。無論輸入的位置在哪裏,是否可以使用css來使這些單選按鈕正常工作?我不想要調整行或任何東西。只需抓住單選按鈕並按照我在下面的示例中的方式進行操作即可。單選按鈕CSS

body { 
 
    background-color: white; 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 
input[type='radio'] { 
 
    display: none; 
 
} 
 
.radio { 
 
    border: 1px solid #b3b4b4; 
 
    border-radius: 50%; 
 
    box-sizing: border-box; 
 
    cursor: pointer; 
 
    display: block; 
 
    float: left; 
 
    height: 16px; 
 
    margin: 10px 10px 0 0; 
 
    padding: 10px; 
 
    position: relative; 
 
    width: 16px; 
 
} 
 
.row:hover .radio { 
 
    border: 2px solid #218996; 
 
} 
 
input[type='radio']:checked + .radio { 
 
    background-color: #218996; 
 
    border: 2px solid #218996; 
 
} 
 
input[type='radio']:checked + .radio::before { 
 
    content: "✓ "; 
 
    position: absolute; 
 
    color: white; 
 
    left: 50%; 
 
    top: 50%; 
 
    transform: translate(-50%, -50%); 
 
    font-size: 
 
} 
 
.row { 
 
    border-bottom: 1px solid #dcdcdc; 
 
    padding: 0 5px; 
 
} 
 
.row label { 
 
    display: inline-block; 
 
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; 
 
    font-size: 14px; 
 
    font-weight: bold; 
 
} 
 
.row > label:last-child { 
 
    padding: 12px 0; 
 
    width: calc(100% - 50px); 
 
    cursor: pointer; 
 
}
<form name="titleTypes" id="titleTypes" method="post" action="process.cfm"> 
 
    <div> 
 
    <!---Label is here for placement of error message---> 
 
    <label for="rgroup" class="error" style="display:none;">Please choose one.</label> 
 
    </div> 
 

 
    <div class='row'> 
 
    <input id="one" type="radio" name="rgroup" value="MV/titleTypeMV.cfm" /> 
 
    <label for="one" class='radio' tabindex='1'></label> 
 
    <label for="one">MOTOR VEHICLE/TRAVEL TRAILER TITLE</label> 
 
    </div> 
 

 
    <div class='row'> 
 
    <input id="two" type="radio" name="rgroup" value="MH/mobileHome.cfm" /> 
 
    <label for="two" class='radio' tabindex='1'></label> 
 
    <label for="two">MOBILE HOME</label> 
 
    </div> 
 

 
    <div class='row'> 
 
    <input id="three" type="radio" name="rgroup" value="BOAT/boat.cfm" /> 
 
    <label for="three" class='radio' tabindex='1'></label> 
 
    <label for="three">VESSEL</label> 
 
    </div> 
 

 
    <div class='row'> 
 
    <input id="four" type="radio" name="rgroup" value="DUPL/duplicate.cfm" /> 
 
    <label for="four" class='radio' tabindex='1'></label> 
 
    <label for="four">DUPLICATE</label> 
 
    </div> 
 

 
    <div> 
 
    <button class="btn-u" type="submit" name="submit" id="submitBtn" class="submitBtn"><i></i>Next</button> 
 
    </div>

我想輸入無線電能夠公正是否有在桌子上或任何地方只是通過調用類工作完全因爲這些單選按鈕。這可能嗎?

+1

是的,這是可能的。你有什麼樣的例子,所以我們可以告訴你如何做到這一點? –

+0

我發佈了上面的CSS作爲我想要的例子 –

+0

我希望單選按鈕在我的例子中像上面那樣工作,但在任何地方工作。如果您嘗試在表格中使用上述內容,則會打破單選按鈕。試圖看看我可以如何通用 –

回答

4

根據我們談話的意見,我稍微修改你的CSS到他們設計的行之外的工作。主要是,我添加了一個clear: both;.radio將每個輸入放在一個新行中,並將float: left;添加到兩個標籤中,以便它們保持一致。

input[type='radio'] { 
 
    clear: both; 
 
    float: left; 
 
    width: 20px; 
 
    height: 10px; 
 
    opacity: 0; 
 
} 
 
.radio { 
 
    border: 1px solid #b3b4b4; 
 
    border-radius: 50%; 
 
    box-sizing: border-box; 
 
    cursor: pointer; 
 
    display: block; 
 
    height: 16px; 
 
    margin: 0 10px 10px -20px; 
 
    padding: 10px; 
 
    position: relative; 
 
    width: 16px; 
 
} 
 
input[type='radio']:checked + .radio { 
 
    background-color: #218996; 
 
    border-color: #218996; 
 
} 
 
input[type='radio']:active + .radio, 
 
input[type='radio']:focus + .radio { 
 
    border-color: lightblue; 
 
} 
 
input[type='radio']:checked + .radio::before { 
 
    content: "✓ "; 
 
    position: absolute; 
 
    color: white; 
 
    left: 50%; 
 
    top: 50%; 
 
    transform: translate(-50%, -50%); 
 
} 
 
label { 
 
    float: left; 
 
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; 
 
    font-size: 14px; 
 
    font-weight: bold; 
 
    line-height: 20px; 
 
}
<input id="one" type="radio" name="rgroup" tabindex='1' /> 
 
<label for="one" class='radio'></label> 
 
<label for="one">Button 1</label> 
 

 
<input id="two" type="radio" name="rgroup" tabindex='2' /> 
 
<label for="two" class='radio'></label> 
 
<label for="two">Button 2</label>

+0

當你點擊你的時候它沒有顯示覆選標記。另外,當我通過他們標籤如何來空間欄不選擇它? –

+0

@DavidBrierton刷新 - 我不小心在背景顏色中犯了一個錯字。輸入按鈕將填充單選按鈕,但不包含空格鍵。 –

+0

請爲此代碼添加一些解釋來幫助OP。這將有助於提供未來觀衆可以從中學習的答案。有關更多信息,請參閱[答案]。 –

0

當你說按類去做時,你的意思是你希望能夠在沒有JS/jQ的情況下從一種狀態切換到另一種狀態?這是我在CSS中做的一個演示,演示如何掛入radiocheckbox``:checked狀態。

增加了OP的例子,並修復了懸停時的抖動。當狀態具有更多的填充,邊距,字體大小,邊框等時,會發生這種副作用。例如,以Snippet 2爲例。

  • 在上空盤旋,單選按鈕和行會挺舉各地,這是非常刺激和分心。在這種情況下,有幾個規則集的邊界爲1px,而其他規則則爲2px。你所要做的就是確保所有的邊框都有相同的寬度。

  • 向每個無線電臺添加了插頁box-shadow,給他們一些懸停的深度。

  • 新增box-shadow到各行,以便確定他們一點點。

  • 向每行添加了動畫邊框。

SNIPPET 1

html, 
 
body { 
 
    box-sizing: border-box; 
 
    background: #111; 
 
    color: #DDD; 
 
    font: 400 16px/1.4'Verdana'; 
 
    height: 100vh; 
 
    width: 100vw; 
 
} 
 
*, 
 
*:before, 
 
*:after { 
 
    box-sizing: inherit; 
 
    margin: 0; 
 
    padding: 0; 
 
    border: 0 none hlsa(0%, 0, 0, 0); 
 
    outline: 0 none hlsa(0%, 0, 0, 0); 
 
} 
 
fieldset { 
 
    margin: 0 1em 1em 0; 
 
    padding: 8px; 
 
    border-radius: 9px; 
 
    border: 3px double #FF8; 
 
    width: 100%; 
 
    max-width: 19em; 
 
} 
 
legend { 
 
    font: small-caps 700 1.5rem/2"Palatino Linotype"; 
 
    color: #FD1; 
 
} 
 
/* CheX */ 
 

 
#chex input.chkrad { 
 
    display: none; 
 
} 
 
#chex input.chkrad + label { 
 
    color: #DDD; 
 
    font-size: 16px; 
 
} 
 
#chex input.chkrad + label span { 
 
    display: inline-block; 
 
    width: 12px; 
 
    height: 12px; 
 
    margin: -1px 15px 0 0; 
 
    vertical-align: baseline; 
 
    cursor: pointer; 
 
} 
 
#chex input + label span { 
 
    background: #555; 
 
    line-height: 100%; 
 
} 
 
input.X:checked + label span { 
 
    padding: -3px 10px 3px; 
 
} 
 
input.X:checked + label span:before { 
 
    content: 'X'; 
 
    color: #F00; 
 
    font-family: cursive; 
 
    font-style: oblique; 
 
    font-weight: 900; 
 
    font-size: 20px; 
 
} 
 
/* RadZ */ 
 

 
#radz input.chkrad { 
 
    display: none; 
 
} 
 
#radz input.chkrad + label { 
 
    color: #EEE; 
 
    font-size: 16px; 
 
} 
 
#radz input.chkrad + label span { 
 
    display: inline-block; 
 
    width: 18px; 
 
    height: 18px; 
 
    margin: -1px 15px 0 0; 
 
    vertical-align: baseline; 
 
    cursor: pointer; 
 
} 
 
#radz input + label span { 
 
    background: #555; 
 
    line-height: 100%; 
 
} 
 
input.A:checked + label span { 
 
    padding: -5px 15px 5px; 
 
    font-size: 16px; 
 
} 
 
input.A:checked + label span:before { 
 
    content: ''; 
 
    color: #e3e; 
 
    font-family: cursive; 
 
    font-style: normal; 
 
    font-weight: 700; 
 
    font-size: 24px; 
 
} 
 
input.B:checked + label span { 
 
    padding: -5px 15px 5px; 
 
    font-size: 16px; 
 
} 
 
input.B:checked + label span:before { 
 
    content: ''; 
 
    color: #f99; 
 
    font-family: cursive; 
 
    font-style: normal; 
 
    font-weight: 300; 
 
    font-size: 20px; 
 
    margin: 0 30px 0 0; 
 
    text-align: left; 
 
} 
 
input.C:checked + label span { 
 
    padding: -2px 15px 2px; 
 
    font-size: 16px; 
 
} 
 
input.C:checked + label span:before { 
 
    content: ''; 
 
    color: #3ef; 
 
    font-family: cursive; 
 
    font-style: normal; 
 
    font-weight: 500; 
 
    font-size: 20px; 
 
    line-height: .75; 
 
    vertical-align: bottom; 
 
    margin-top: 5px; 
 
} 
 
input.D:checked + label span { 
 
    padding: -5px 15px 5px; 
 
    font-size: 16px; 
 
} 
 
input.D:checked + label span:before { 
 
    content: ''; 
 
    color: #eee; 
 
    font-family: cursive; 
 
    font-style: normal; 
 
    font-weight: 100; 
 
    font-size: 20px; 
 
    line-height: .75; 
 
} 
 
input.fade + label span, 
 
input.fade:checked + label span { 
 
    -webkit-transition: background 0.7s linear; 
 
    -moz-transition: background 0.7s linear; 
 
    transition: background 0.7s linear; 
 
} 
 
.red { 
 
    color: red; 
 
}
<fieldset id="chex" name="chex"> 
 
    <legend><span class="red">X</span> Marks the Checkbox</legend> 
 
    <input type='checkbox' name='chk3' id="chk3" class="chkrad X fade" value='x' /> 
 
    <label for="chk3"><span></span>Red "X" mark</label> 
 
</fieldset> 
 

 
<fieldset id="radz" name="radz"> 
 
    <legend>Shore Leave</legend> 
 
    <input type='radio' name='rad' id="rad1" class="chkrad A fade" value='1' /> 
 
    <label for="rad1"><span></span>Brawl</label> 
 
    <br/> 
 
    <input type='radio' name='rad' id="rad2" class="chkrad B fade" value='2' /> 
 
    <label for="rad2"><span></span>Women</label> 
 
    <br/> 
 
    <input type='radio' name='rad' id="rad3" class="chkrad C fade" value='3' /> 
 
    <label for="rad3"><span></span>Drink</label> 
 
    <br/> 
 
    <input type='radio' name='rad' id="rad4" class="chkrad D fade" value='4' /> 
 
    <label for="rad4"><span></span>All of the above, matey!</label> 
 
    <br/> 
 
</fieldset>

SNIPPET 2

body { 
 
    background-color: white; 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 
input[type='radio'] { 
 
    display: none; 
 
} 
 
.radio { 
 
    border: 2px solid #b3b4b4; 
 
    border-radius: 50%; 
 
    box-sizing: border-box; 
 
    cursor: pointer; 
 
    display: table-cell; 
 
    float: left; 
 
    line-height: 100%; 
 
    margin: 10px 10px 0 0; 
 
    padding: 10px; 
 
    position: relative; 
 
    width: 16px; 
 
} 
 
.row:hover .radio { 
 
    border: 2px solid #218996; 
 
    box-shadow: inset 0 0 10px #218996; 
 
} 
 
input[type='radio']:checked + .radio { 
 
    background-color: #218996; 
 
    border: 2px solid #218996; 
 
} 
 
input[type='radio']:checked + .radio::before { 
 
    content: "✓ "; 
 
    position: absolute; 
 
    color: white; 
 
    left: 50%; 
 
    top: 50%; 
 
    transform: translate(-50%, -50%); 
 
} 
 
.row { 
 
    border-bottom: 2px solid #dcdcdc; 
 
    padding: 0 5px; 
 
    box-shadow: 0 0 10px rgba(0, 128, 192, .3); 
 
} 
 
.row label { 
 
    display: block; 
 
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; 
 
    font-size: 14px; 
 
    font-variant: small-caps; 
 
    font-weight: bold; 
 
} 
 
.row > label:last-child { 
 
    padding: 12px 0; 
 
    width: calc(100% - 50px); 
 
    cursor: pointer; 
 
} 
 
.btn-u { 
 
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; 
 
    font-size: 14px; 
 
    font-variant: small-caps; 
 
    font-weight: bold; 
 
} 
 
.row.bdr { 
 
    display: block; 
 
    position: relative; 
 
    padding-bottom: 1.5px; 
 
} 
 
.row.bdr::before { 
 
    content: ''; 
 
    display: block; 
 
    position: absolute; 
 
    left: 0; 
 
    bottom: 0; 
 
    height: 3px; 
 
    width: 0; 
 
    transition: width 0s ease, background .5s ease; 
 
} 
 
.row.bdr::after { 
 
    content: ''; 
 
    display: block; 
 
    position: absolute; 
 
    right: 0; 
 
    bottom: 0; 
 
    height: 3px; 
 
    width: 0; 
 
    background: rgba(51, 112, 44, .4); 
 
    transition: width .5s ease; 
 
} 
 
.row.bdr:hover::before { 
 
    width: 100%; 
 
    background: rgba(100, 177, 255, .4); 
 
    transition: width .5s ease; 
 
} 
 
.row.bdr:hover::after { 
 
    width: 100%; 
 
    background: transparent; 
 
    transition: all 0s ease; 
 
}
<form name="titleTypes" id="titleTypes" method="post" action="process.cfm"> 
 
    <div> 
 
    <!---Label is here for placement of error message---> 
 
    <label for="rgroup" class="error" style="display:none;">Please choose one.</label> 
 
    </div> 
 

 
    <div class='row bdr'> 
 
    <input id="one" type="radio" name="rgroup" value="MV/titleTypeMV.cfm" /> 
 
    <label for="one" class='radio' tabindex='1'></label> 
 
    <label for="one">Motor Vehicle/Travel Trailer Title</label> 
 
    </div> 
 

 
    <div class='row bdr'> 
 
    <input id="two" type="radio" name="rgroup" value="MH/mobileHome.cfm" /> 
 
    <label for="two" class='radio' tabindex='1'></label> 
 
    <label for="two">Moble Home</label> 
 
    </div> 
 

 
    <div class='row bdr'> 
 
    <input id="three" type="radio" name="rgroup" value="BOAT/boat.cfm" /> 
 
    <label for="three" class='radio' tabindex='1'></label> 
 
    <label for="three">Vessel</label> 
 
    </div> 
 

 
    <div class='row bdr'> 
 
    <input id="four" type="radio" name="rgroup" value="DUPL/duplicate.cfm" /> 
 
    <label for="four" class='radio' tabindex='1'></label> 
 
    <label for="four">Duplicate</label> 
 
    </div> 
 

 
    <div> 
 
    <button class="btn-u" type="submit" name="submit" id="submitBtn" class="submitBtn"><i></i>Next</button> 
 
    </div>

+0

這個CSS技術已經在OP的例子中實現了。 –

+0

當我比你早一分鐘到達時,OP的帖子是不完整的,*:是的,這是可能的。你有什麼樣的例子,所以我們可以告訴你如何去做?* - @JonUleis 1小時前。 「我發佈上面的CSS作爲我想要的例子」我添加了一個OP示例的改進版本,只知道他想要模糊地運行* *通用* – zer00ne