How to change the size of the radio button using CSS?

前端 未结 14 1627
野的像风
野的像风 2020-11-28 06:17

Is there a way to control the size of the radio button in CSS ?

相关标签:
14条回答
  • 2020-11-28 06:55

    Resizing the default widget doesn’t work in all browsers, but you can make custom radio buttons with JavaScript. One of the ways is to create hidden radio buttons and then place your own images on your page. Clicking on these images changes the images (replaces the clicked image with an image with a radio button in a selected state and replaces the other images with radio buttons in an unselected state) and selects the new radio button.

    Anyway, there is documentation on this subject. For example, read this: Styling Checkboxes and Radio Buttons with CSS and JavaScript.

    0 讨论(0)
  • 2020-11-28 06:56

    Here's one approach. By default the radio buttons were about twice as large as labels.
    (See CSS and HTML code at end of answer)


    Safari: 10.0.3


    Chrome: 56.0.2924.87


    Firefox: 50.1.0


    Internet Explorer: 9 (Fuzziness not IE's fault, hosted test on netrenderer.com)


    CSS:

    .sortOptions > label {
        font-size:          8px;
    }
    
    .sortOptions > input[type=radio] {
        width:              10px;
        height:             10px;
    }
    

    HTML:

    <div class="rightColumn">Answers
        <span class="sortOptions">
            <input type="radio" name="answerSortList" value="credate"/>
            <label for="credate">Creation</label>
    
            <input type="radio" name="answerSortList" value="lastact"/>
            <label for="lastact">Activity</label>
    
            <input type="radio" name="answerSortList" value="score"/>
            <label for="score">Score</label>
    
            <input type="radio" name="answerSortList" value="upvotes"/>
            <label for="upvotes">Up votes</label>
    
            <input type="radio" name="answerSortList" value="downvotes"/>
            <label for="downvotes">Down Votes</label>
    
            <input type="radio" name="answerSortList" value="accepted"/>
            <label for="downvotes">Accepted</label>
    
        </span>
    </div>                
    
    0 讨论(0)
  • 2020-11-28 06:56

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <title>Bootstrap Example</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
      <style>
    input[type="radio"] {
        -ms-transform: scale(1.5); /* IE 9 */
        -webkit-transform: scale(1.5); /* Chrome, Safari, Opera */
        transform: scale(1.5);
    }
      </style>
    </head>
    <body>
    
    <div class="container">
      <h2>Form control: inline radio buttons</h2>
      <p>The form below contains three inline radio buttons:</p>
      <form>
        <label class="radio-inline">
          <input type="radio" name="optradio">Option 1
        </label>
        <label class="radio-inline">
          <input type="radio" name="optradio">Option 2
        </label>
        <label class="radio-inline">
          <input type="radio" name="optradio">Option 3
        </label>
      </form>
    </div>
    
    </body>
    </html>

    0 讨论(0)
  • 2020-11-28 07:06

    Yes, you should be able to set its height and width, as with any element. However, some browsers do not really take these properties into account.

    This demo gives an overview of what is possible and how it is displayed in various browsers: https://www.456bereastreet.com/lab/styling-form-controls-revisited/radio-button/

    As you'll see, styling radio buttons is not easy :-D

    A workaround is to use JavaScript and CSS to replace the radio buttons and other form elements with custom images:

    • https://github.com/emblematiq/Niceforms
    • https://web.archive.org/web/20161119112447/http://ryanfait.com/resources/custom-checkboxes-and-radio-buttons/
    0 讨论(0)
  • 2020-11-28 07:08

    Old question but now there is a simple solution, compatible with most browsers, which is to use CSS3. I tested in IE, Firefox and Chrome and it works.

    input[type="radio"] {
        -ms-transform: scale(1.5); /* IE 9 */
        -webkit-transform: scale(1.5); /* Chrome, Safari, Opera */
        transform: scale(1.5);
    }
    

    Change the value 1.5, in this case an increment of 50% in size, according to your needs. If the ratio is very high, it can blur the radio button. The next image shows a ratio of 1.5.

    0 讨论(0)
  • 2020-11-28 07:08

    You can control radio button's size with css style:

    style="height:35px; width:35px;"

    This directly controls the radio button size.

    <input type="radio" name="radio" value="value" style="height:35px; width:35px; vertical-align: middle;">
    
    0 讨论(0)
提交回复
热议问题