技術(shù)頻道導(dǎo)航
HTML/CSS
.NET技術(shù)
IIS技術(shù)
PHP技術(shù)
Js/JQuery
Photoshop
Fireworks
服務(wù)器技術(shù)
操作系統(tǒng)
網(wǎng)站運(yùn)營(yíng)

贊助商

分類(lèi)目錄

贊助商

最新文章

搜索

簡(jiǎn)單又好看的CSS單選按鈕(radio)樣式

作者:admin    時(shí)間:2021-8-7 12:22:34    瀏覽:

單選按鈕(radio)在網(wǎng)頁(yè)設(shè)計(jì)中非常常用,但是默認(rèn)的樣式太死板,用起來(lái)體驗(yàn)不好,而有的人喜歡用圖片結(jié)合JS/CSS來(lái)代替,那樣雖然看起來(lái)比較好看,但是弄得又過(guò)于復(fù)雜了,不易遷移使用。本文給大家介紹一個(gè)簡(jiǎn)單又漂亮的純CSS單選按鈕(radio)樣式。

CSS單選按鈕(radio)樣式

從上圖看到,這個(gè)單選按鈕的樣式是不錯(cuò)的,雖然不是那種絢麗炫酷的風(fēng)格,勝在其設(shè)計(jì)偏向大眾化但又不失大氣穩(wěn)重,這種風(fēng)格適合大多數(shù)UI的設(shè)計(jì),放到網(wǎng)頁(yè)上非常合適。

CSS代碼

body {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
}

.radio {
  margin: 0.5rem;
}
.radio input[type=radio] {
  position: absolute;
  opacity: 0;
}
.radio input[type=radio] + .radio-label:before {
  content: "";
  background: #f4f4f4;
  border-radius: 100%;
  border: 1px solid #b4b4b4;
  display: inline-block;
  width: 1.4em;
  height: 1.4em;
  position: relative;
  top: -0.2em;
  margin-right: 1em;
  vertical-align: top;
  cursor: pointer;
  text-align: center;
  transition: all 250ms ease;
}
.radio input[type=radio]:checked + .radio-label:before {
  background-color: #3197EE;
  box-shadow: inset 0 0 0 4px #f4f4f4;
}
.radio input[type=radio]:focus + .radio-label:before {
  outline: none;
  border-color: #3197EE;
}
.radio input[type=radio]:disabled + .radio-label:before {
  box-shadow: inset 0 0 0 4px #f4f4f4;
  border-color: #b4b4b4;
  background: #b4b4b4;
}
.radio input[type=radio] + .radio-label:empty:before {
  margin-right: 0;
}

HTML代碼

<div class="container">
  <div class="radio">
    <input id="radio-1" name="radio" type="radio" checked>
    <label for="radio-1" class="radio-label">Checked</label>
  </div>

  <div class="radio">
    <input id="radio-2" name="radio" type="radio">
    <label  for="radio-2" class="radio-label">Unchecked</label>
  </div>

  <div class="radio">
    <input id="radio-3" name="radio" type="radio" disabled>
    <label for="radio-3" class="radio-label">Disabled</label>
  </div>
</div>

execcodegetcode

代碼解釋

1、內(nèi)容定位

CSS中 body 的幾行代碼,主要是定位了內(nèi)容區(qū)域的居中作用。display: flex; 是“彈性布局”,它輕易實(shí)現(xiàn)內(nèi)容的對(duì)齊方式。

2、點(diǎn)擊效果

點(diǎn)擊按鈕后有一個(gè)過(guò)度切換效果,看起來(lái)不顯得突兀,使用體驗(yàn)提升。實(shí)現(xiàn)代碼是這行:

transition: all 250ms ease;

3、增減新項(xiàng)

要添加新的單選按鈕,只需添加一段HTML代碼,無(wú)需改變CSS代碼。如下代碼:

<div class="radio">
    <input id="radio-4" name="radio" type="radio" >
    <label for="radio-4" class="radio-label">demo</label>
</div>

添加HTML代碼,只需更改下 id 的值,這里是第4個(gè)單選按鈕,所以是 radio-4。另外 for 的值也相應(yīng)寫(xiě)為 radio-4。這樣我們就可以得到了一個(gè)新的單選按鈕。

總結(jié)

本文介紹了用純CSS實(shí)現(xiàn)簡(jiǎn)單又漂亮的CSS單選按鈕(radio)樣式,其特點(diǎn)是風(fēng)格大眾化,適用范圍廣,遷移容易,代碼簡(jiǎn)單。

您可能對(duì)以下文章也感興趣

標(biāo)簽: radio  button  
x
  • 站長(zhǎng)推薦
/* 左側(cè)顯示文章內(nèi)容目錄 */