代码之家  ›  专栏  ›  技术社区  ›  Mykita

向右浮动,不创建新行

  •  3
  • Mykita  · 技术社区  · 6 年前

    HTML:

    <div>Due date:<input type="text" name="dueDate" size="30" value="{{ticket.fields.interval.lastExecution|e}}" required  disabled="disabled"></input></div>
    <div>Created by:<input type="text" name="createdBy" size="30" value="{{ticket.fields.personCreated|e}}" required  disabled="disabled"></input></div>
    

    CSS:

    .open-tickets-view input {
        border:         0px solid #474a52;
        border-radius:  0px;
        max-width:      200px;
    

    结果: enter image description here

    如果我尝试使用内联块显示向右浮动:

    CSS:

    .open-tickets-view input {
        border:         0px solid #474a52;
        border-radius:  0px;
        max-width:      200px;
        display:        inline-block;
        float:          right;
    

    结果: enter image description here

    我试过几种不同的组合 display: flex use justify-content: space-between 但文本总是换行。

    2 回复  |  直到 6 年前
        1
  •  3
  •   caramba    6 年前

    使用输入元素是一件好事 <label> . 因为它是输入的标签。标签的默认浏览器行为是,如果单击标签,鼠标将聚焦输入。 Read more about it here

    使用浮点:

    div.myDiv {
       width: 300px;
       border: solid 2px red;
    }
    div.myDiv:after {
       /* clear floats is a good thing */
       content: '';
       display: block;
       clear: both;
    }
    div.myDiv input {
       float: right;
       border:solid 2px green;
    }
    div.myDiv label {
       border:solid 2px green;
    }
    <div class="myDiv">
      <label for="uname">Choose a username: </label>
      <input type="text" id="uname" name="name">
    </div>

    使用定位:

    div.myDiv {
       position: relative;
       width: 300px;
       border: solid 2px red;
    }
    div.myDiv input {
       border:solid 2px green;
       position: absolute;
       right: 0;
    }
    div.myDiv label {
       border:solid 2px green;
    }
    <div class=“myDiv”>
    <标签=“uname”>选择用户名(</标签(>);
    <输入type=“text”id=“uname”name=“name”>
    </div>
        2
  •  2
  •   Sreekanth Reddy Balne    6 年前

    您可以使用 margin-left:auto 在的子元素上 display:flex 具有 flex-direction:row 如下所示。

    .container, .container > div {
      display: flex;
      flex-direction: row;
      white-space:nowrap;
    }
    
    .container .right {
      margin-left: auto;
    }
    <div class='container'>
      <div>
        <span>Due date:</span>
        <input type="text" name="dueDate" size="30" value="{{ticket.fields.interval.lastExecution|e}}" required disabled="disabled" />
      </div>
      <div class='right'>
        <span>Created by:</span><input type="text" name="createdBy" size="30" value="{{ticket.fields.personCreated|e}}" required disabled="disabled" />
      </div>
    </div>

    或者可能:

    .container,
    .container>div {
      display: flex;
    }
    
    .container {
      flex-direction: column;
    }
    
    .container>div {
      flex-direction: row;
      padding:1em;
    }
    
    .container .right {
      margin-left: auto;
    }
    <div class='container'>
      <div>
        <span>Due date:</span>
        <input class='right' type="text" name="dueDate" size="30" value="{{ticket.fields.interval.lastExecution|e}}" required disabled="disabled" />
      </div>
      <div>
        <span>Created by:</span><input class='right' type="text" name="createdBy" size="30" value="{{ticket.fields.personCreated|e}}" required disabled="disabled" />
      </div>
    </div>