简体   繁体   中英

How to vertical align radio buttons

So right now i have three radio buttons that are aligned horizontally by default, but instead i want them to be aligned vertically . How do i do that the correct way?

 * { margin: 0; padding: 0; box-sizing: border-box; } .radio { display: inline-flex; align-items: center; cursor: pointer; margin-right: 10px; }
 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href = "style.css"> <title>Document</title> </head> <body> <h1>Custom Radio Button</h1> <label for="myRadioId" class = "radio"> <input type = "radio" name = "myRadioField" id="myRadioId" class = "radio__input"> Mercedes Benz </label> <label for="myRadioId2" class = "radio"> <input type = "radio" name = "myRadioField" id="myRadioId2" class = "radio__input"> BMW </label> <label for="myRadioId3" class = "radio"> <input type = "radio" name = "myRadioField" id="myRadioId3" class = "radio__input"> Volkswagen </label> </body> </html>

Replace inline-flex with flex

 * { margin: 0; padding: 0; box-sizing: border-box; } .radio { display: flex; align-items: center; cursor: pointer; margin-right: 10px; }
 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href = "style.css"> <title>Document</title> </head> <body> <h1>Custom Radio Button</h1> <label for="myRadioId" class = "radio"> <input type = "radio" name = "myRadioField" id="myRadioId" class = "radio__input"> Mercedes Benz </label> <label for="myRadioId2" class = "radio"> <input type = "radio" name = "myRadioField" id="myRadioId2" class = "radio__input"> BMW </label> <label for="myRadioId3" class = "radio"> <input type = "radio" name = "myRadioField" id="myRadioId3" class = "radio__input"> Volkswagen </label> </body> </html>

Instead of display: inline-flex; use display: table; in CSS.

* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

.radio {
display: table;
align-items: center;
cursor: pointer;
margin-right: 10px;
}

You can also use display:block; or display:flex; (as mentioned in the answer list).

Try to avoid radio as selector class name since it is a keyword and it is not good way to use in that way.

fiddle to playaround.

 * { margin: 0; padding: 0; box-sizing: border-box; } .radio { display: block; align-items: center; cursor: pointer; margin-left: 10px; }
 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="style.css"> <title>Document</title> </head> <body> <h1>Custom Radio Button</h1> <label for="myRadioId" class="radio"> <input type = "radio" name = "myRadioField" id="myRadioId" class = "radio__input"> Mercedes Benz </label> <label for="myRadioId2" class="radio"> <input type = "radio" name = "myRadioField" id="myRadioId2" class = "radio__input"> BMW </label> <label for="myRadioId3" class="radio"> <input type = "radio" name = "myRadioField" id="myRadioId3" class = "radio__input"> Volkswagen </label> </body> </html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM