用css實現html中單選框樣式改變


   我們都知道,input的單選框是一個小圓框,不能直接更改樣式。但是我們在很多網頁中看到的單選框樣式可不僅限於默認的那個樣式(看上去沒啥新意,也比較丑)。那么,接下來我將介紹下如何實現該功能。

  首先,讓我們來看下單選框的實現:

  在html中的input元素中,將其type屬性值設置為radio,即為單選框,此時只需將要設置的單選選項的name屬性設置成一致的,即可實現單選功能。

代碼實現如下:

<input type="radio" name="gender" value="male">男
<input type="radio" name="gender" value="female">女

  要實現單選框樣式改變,只需以下幾步:

1、首先給出input的type屬性為radio,設置相同name。同時用label將相應字段關聯上該input屬性(當label的for屬性值與input的id屬性值相同時 ,即可關聯。關聯上之后,點擊字段即可選中相應的單選框)。

html中:
       <div> <input type="radio" name="paixu" id="paixu1" checked> <label for="paixu1" style="cursor:pointer">按熱門排序</label> <input type="radio" name="paixu" id="paixu2"> <label for="paixu2" style="cursor:pointer">按時間排序</label> <input type="radio" name="paixu" id="paixu3"> <label for="paixu3" style="cursor:pointer">按評價排序</label> </div>

2、設置input的display屬性為none,將比較丑的那個框去掉。然后用偽元素添加相應的框。即在label中,用::before添加一個空心圓圈。

div>input{
    display: none;
}
div>label{
    position: relative;
    margin-right: 34px;
}
div>label::before{
    display: inline-block;
    content: "";
    width: 16px;
    height: 16px;
    border-radius: 50%;
    border: 1px solid rgb(219, 219, 219);
    margin-right: 6px;
    vertical-align: bottom;
}

3、在input單選框在選中的時候,原有before的單選框變為紅色實心框,同時添加label::after為白色圓,並將其定位到before的紅色實心框中間。從而達到重疊的效果,實現紅色圓框+白色圓心的效果。

div>input:checked+label::before{
    background-color: rgb(239, 66, 56);
}
div>input:checked+label::after{
    display: inline-block;
    content: "";
    width: 6px;
    height: 6px;
    border-radius: 50%;
    position: absolute;
    left: 6px;
    bottom: 6px;
    background-color: white;
}

  是不是挺簡單的呢?通過這樣的方法還可以設置很多樣式呢。

完整代碼如下:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
<style>
div>input{
    display: none;
}
div>label{
    position: relative;
    margin-right: 34px;
}
div>label::before{
    display: inline-block;
    content: "";
    width: 16px;
    height: 16px;
    border-radius: 50%;
    border: 1px solid rgb(219, 219, 219);
    margin-right: 6px;
    vertical-align: bottom;
}
div>input:checked+label::before{
    background-color: rgb(239, 66, 56);
}
div>input:checked+label::after{
    display: inline-block;
    content: "";
    width: 6px;
    height: 6px;
    border-radius: 50%;
    position: absolute;
    left: 6px;
    bottom: 6px;
    background-color: white;
 
}
</style>
</head>
<body>  
            <div>
                <input type="radio" name="paixu" id="paixu1" checked>
                <label for="paixu1" style="cursor:pointer">按熱門排序</label>
                <input type="radio" name="paixu" id="paixu2">
                <label for="paixu2" style="cursor:pointer">按時間排序</label>
                <input type="radio" name="paixu" id="paixu3">
                <label for="paixu3" style="cursor:pointer">按評價排序</label>
            </div>
</body>

</html>        

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM