首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将标签和输入靠右对齐

问如何将标签和输入靠右对齐
EN

Stack Overflow用户
提问于 2021-01-12 19:54:23
回答 3查看 40关注 0票数 0

如何将标签和输入靠右对齐,使所有标签和输入都显示在同一行中

​

代码语言:javascript
复制
.radioContainer{
    width: fit-content;
    margin: 5px;
    margin-top: 20px;
    background-color: aqua;
    padding-bottom: 25px;
}
代码语言:javascript
复制
<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>

​

EN

回答 3

Stack Overflow用户

发布于 2021-01-12 20:03:41

为了让它们在同一行中,我首先从代码中删除<br />。然后将输入和标签的css设置为inline-block,如下所示:

​

代码语言:javascript
复制
.radioContainer{
   width: fit-content;
    margin: 5px;
    margin-top: 20px;
    background-color: aqua;
    padding-bottom: 25px;
}

label, input { display: inline-block; }
代码语言:javascript
复制
<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>

​

票数 0
EN

Stack Overflow用户

发布于 2021-01-12 20:06:36

要使所有输入都内联,只需删除所有<br />标记。

示例:

代码语言:javascript
复制
<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

票数 0
EN

Stack Overflow用户

发布于 2021-01-12 20:15:42

我理解你的问题,你希望它们仍然在另一个下面,但对齐在右边。所以我做了一个使用flexbox代替硬中断的解决方案。我注释了我所做更改的代码以及为什么,html和css。

基本上,我使用了一个列,并将每个项目组合(标签和复选框)放在一个新的row-div中。

​

代码语言:javascript
复制
.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;
}
代码语言:javascript
复制
<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>

​

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65683428

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档