简体   繁体   English

如何将标签和输入对齐到右侧

[英]how can I align the labels & inputs to the right

how can I align the labels & inputs to the right like that all of then appear in the same line我怎样才能将标签和输入向右对齐,然后所有这些都出现在同一行

 .radioContainer{ width: fit-content; margin: 5px; margin-top: 20px; background-color: aqua; padding-bottom: 25px; }
 <div class="radioContainer" style="margin-bottom: 40px; margin-right: 0px; "> <label class="title" for="">fav food</label> <label for="burger">burger</label> <input type="checkbox" id="burger"> <br> <label for="fries">fries</label> <input type="checkbox" id="fries"> <br> <label for="onionRings">onion rings</label> <input type="checkbox" id="onionRings"> <br> <label for="cackes">cakes</label> <input type="checkbox" id="cackes" > <small></small> </div>

To have them in the same line, I would start by removing <br /> from your code.要将它们放在同一行中,我将从您的代码中删除<br />开始。 Then set the css for input and label to be inline-block , something like:然后将 css 设置为输入,并将 label 设置为inline-block ,例如:

 .radioContainer{ width: fit-content; margin: 5px; margin-top: 20px; background-color: aqua; padding-bottom: 25px; } label, input { display: inline-block; }
 <div class="radioContainer" style="margin-bottom: 40px; margin-right: 0px; "> <label class="title" for="">fav food</label> <label for="burger">burger</label> <input type="checkbox" id="burger"> <label for="fries">fries</label> <input type="checkbox" id="fries"> <label for="onionRings">onion rings</label> <input type="checkbox" id="onionRings"> <label for="cackes">cakes</label> <input type="checkbox" id="cackes" > <small></small> </div>

To make all inputs inline, just remove all the <br /> tags.要使所有输入内联,只需删除所有<br />标签。

Example:例子:

<div class="radioContainer" style="margin-bottom: 40px; margin-right: 0px; ">
  <label class="title" for="">fav food</label>
  <label for="burger">burger</label>
  <input type="checkbox" id="burger">
  <label for="fries">fries</label>
  <input type="checkbox" id="fries">
  <label for="onionRings">onion rings</label>
  <input type="checkbox" id="onionRings">
  <label for="cackes">cakes</label>
  <input type="checkbox" id="cackes">
  <small></small>
</div>

Codepen: https://codepen.io/manaskhandelwal1/pen/jOMeqex代码笔: https://codepen.io/manaskhandelwal1/pen/jOMeqex

I understood your Question that you want them still below each other but aligned to the right side.我理解您的问题,您希望它们仍然彼此下方但与右侧对齐。 So i made a solution using flexbox instead of the hard breaks.所以我使用 flexbox 而不是硬中断做了一个解决方案。 I commented the Code where i made changes and why, html and css.我评论了我进行更改的代码以及原因,html 和 css。

Basically i used a colum and put each item-combination (label and checkbox)in a new row-div.基本上我使用了一个列并将每个项目组合(标签和复选框)放在一个新的行 div 中。

 .radioContainer{ width: fit-content; margin: 5px; margin-top: 20px; background-color: aqua; padding-bottom: 25px; /*make item a flexbox-container*/ display: flex; /*combination of flex-direction and flex-wrap*/ flex-flow: column wrap; }.row{ /*make item a flexboc container*/ display: flex; /*flex-flow: row nowrap; this is the default value of flex, which is why you dont need it, just wanted to leave it in as a comment so you know whats happening*/ /*Align the contents on the end of each row*/ justify-content: flex-end; }
 <div class="radioContainer" style="margin-bottom: 40px; margin-right: 0px; "> <!--removed the hard breaks and added row-divs around each item combination--> <div class="row"> <label class="title" for="">fav food</label> </div> <div class="row"> <label for="burger">burger</label> <input type="checkbox" id="burger"> </div> <div class="row"> <label for="fries">fries</label> <input type="checkbox" id="fries"> </div> <div class="row"> <label for="onionRings">onion rings</label> <input type="checkbox" id="onionRings"> </div> <div class="row"> <label for="cackes">cakes</label> <input type="checkbox" id="cackes" > </div> <small></small> </div>

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

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