简体   繁体   English

如何在li标签内对齐单选按钮

[英]How to align radio button inside a li tag

I am trying to create a to-do list using vanilla javascript, when text is entered into the text-input, it will dynamically create an < li >, < h2 > and text. 我正在尝试使用香草javascript创建待办事项,将文本输入到文本输入中时,它将动态创建<li>,<h2>和文本。 I want to add a radio button dynamically and align it to the end of the li in the center. 我想动态添加一个单选按钮,并将其与li的中心对齐。

I have tried adding display: table-cell; 我尝试添加display: table-cell; vertical-align center , to the < li > tag, but this didn't work as it made the < li > too small. vertical-align center到<li>标记,但这没有用,因为它使<li>变得太小。 I also tried display: flex on the < li > tag and justify-content: flex-end; 我还尝试过在<li>标记上display: flex并确定justify-content: flex-end; on the radio button, but it would move the radio button into a different position which still was not correct. 在单选按钮上,但会将单选按钮移到另一个位置,但这仍然不正确。

I have put the code into a fiddle: https://jsfiddle.net/oub5pm4g/ 我把代码放到了一个小提琴中: https : //jsfiddle.net/oub5pm4g/

 document.querySelector("#add").addEventListener("click", btnClick); document.querySelector("#input").addEventListener("keyup", keyUp) function keyUp(event) { if (event.keyCode === 13) { event.preventDefault(); btnClick(); } } function btnClick(event) { event.preventDefault() var input = document.querySelector("#input"); var ul = document.querySelector("ul"); li = document.createElement("li"); ul.appendChild(li); h2 = document.createElement("h2"); text = document.createTextNode(input.value); h2.appendChild(text); li.appendChild(h2); var radio = document.createElement("input"); radio.setAttribute("type", "radio"); li.appendChild(radio); document.querySelector("#form").reset(); } 
 body { background: rgb(238, 237, 237); } .container { background: white; max-width: 50%; max-height: 80%; margin: 0 auto; border-radius: 25px; padding: 2%; box-shadow: 0 5px 10px rgba(154, 160, 185, 0.05), 0 15px 40px rgba(166, 173, 201, 0.2); } h1 { font-family: "Roboto", sans-serif; font-weight: 900; text-align: center; } h2 { font-weight: 600; } h3 { font-weight: 00; } h4 { font-weight: 100; } ul { list-style: none; padding: 0; font-family: "Roboto", sans-serif; margin-top: 5%; color: white; } li { box-sizing: border-box; width: 100%; margin-bottom: 5%; font-size: 1em; background: crimson; padding: 1em; border-radius: 15px; } input[type="text"] { box-sizing: border-box; width: 85%; font-size: 1em; padding: 1em; } input[type="radio"] { float: right; align-content: center; } button { padding: 1em; width: 13%; font-size: 1em; color: white; background: crimson; margin-left: 1.4%; } 
 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Page Title</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" type="text/css" media="screen" href="main.css" /> <link href="https://fonts.googleapis.com/css?family=Roboto&display=swap" rel="stylesheet"> </head> <body> <div class="container"> <h1>To-Do List</h1> <form id="form"> <input type="text" placeholder="Add new task..." id="input"> <button id="add" type="submit">Add</button> </form> <ul> </ul> </div> </body> <script src="main.js"></script> </html> 

Thanks. 谢谢。

Try flex properties on li tag li标签上尝试flex属性

li {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

 document.querySelector("#add").addEventListener("click", btnClick); document.querySelector("#input").addEventListener("keyup", keyUp) function keyUp(event) { if (event.keyCode === 13) { event.preventDefault(); btnClick(); } } function btnClick(event) { event.preventDefault() var input = document.querySelector("#input"); var ul = document.querySelector("ul"); li = document.createElement("li"); ul.appendChild(li); h2 = document.createElement("h2"); text = document.createTextNode(input.value); h2.appendChild(text); li.appendChild(h2); var radio = document.createElement("input"); radio.setAttribute("type", "radio"); li.appendChild(radio); document.querySelector("#form").reset(); } 
 body { background: rgb(238, 237, 237); } .container { background: white; max-width: 50%; max-height: 80%; margin: 0 auto; border-radius: 25px; padding: 2%; box-shadow: 0 5px 10px rgba(154, 160, 185, 0.05), 0 15px 40px rgba(166, 173, 201, 0.2); } h1 { font-family: "Roboto", sans-serif; font-weight: 900; text-align: center; } h2 { font-weight: 600; } h3 { font-weight: 00; } h4 { font-weight: 100; } ul { list-style: none; padding: 0; font-family: "Roboto", sans-serif; margin-top: 5%; color: white; } li { box-sizing: border-box; width: 100%; margin-bottom: 5%; font-size: 1em; background: crimson; padding: 1em; border-radius: 15px; display: flex; justify-content: space-between; align-items: center; } input[type="text"] { box-sizing: border-box; width: 85%; font-size: 1em; padding: 1em; } input[type="radio"] { float: right; align-content: center; } button { padding: 1em; width: 13%; font-size: 1em; color: white; background: crimson; margin-left: 1.4%; } 
 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Page Title</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" type="text/css" media="screen" href="main.css" /> <link href="https://fonts.googleapis.com/css?family=Roboto&display=swap" rel="stylesheet"> </head> <body> <div class="container"> <h1>To-Do List</h1> <form id="form"> <input type="text" placeholder="Add new task..." id="input"> <button id="add" type="submit">Add</button> </form> <ul> </ul> </div> </body> <script src="main.js"></script> </html> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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