简体   繁体   English

如何将这些html元素连续对齐?

[英]How can I align these html elements in a row?

I'm creating a input to contain hours and minutes.I use this html elements: 我正在创建一个包含小时和分钟的输入。我使用以下html元素:

  • Two pairs of up and down Buttons; 两对向上和向下按钮;

  • Input text. 输入文本。

I'm having trouble aligning the html elements. 我在对齐html元素时遇到问题。 This is the image with the best alignment I could apply: 这是我可以应用的最佳对齐方式的图像:

在此处输入图片说明

This is my code: 这是我的代码:

 .col-centered{ float: none; margin: 0 auto; } .row-1{ padding-top: 3px; padding-right: 0px; padding-bottom: 1px; padding-left: 0px; } .triangle-up { width: 0; height: 0; border-left: 4px solid transparent; border-right: 4px solid transparent; border-bottom: 8px solid #555; } .triangle-down { width: 0; height: 0; border-left: 4px solid transparent; border-right: 4px solid transparent; border-top: 8px solid #555; } 
 <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <div class="container"> <div class="row"> <div class="col-xs-1"> <div class="row-1"> <div class = "triangle-up" id ="up1"></div> </div> <div class="row-1"> <div class = "triangle-down" id ="down1"></div> </div> </div> <div class="col-xs-2"> <div class = "col-centered"> <input type="text" id="hora" size="5" maxlength="5"> </div> </div> <div class="col-xs-1"> <div class="row-1"> <div class = "triangle-up" id ="up"></div> </div> <div class="row-1"> <div class = "triangle-down" id ="down"></div> </div> </div> </div> </div> 

I tried, but without success, lining up the elements with pulling to the right. 我尝试了一下,但没有成功,将元素向右拖动对齐。 Forcing the left pair of Buttons, to be near the html text entry. 强制左对按钮位于html文本条目附近。

I've changed this piece of code: 我更改了这段代码:

<div class="pull-right">
        <div class="row-1">
            <div class = "triangle-up" id ="up1"></div>
        </div>
        <div class="row-1">
            <div class = "triangle-down" id ="down1"></div>
        </div>
    </div>

How can I align the html elements so that the two pairs of buttons are close to the input element of text? 如何对齐html元素,使两对按钮靠近文本的输入元素?

This would be a great use case for flexbox! 这将是flexbox的绝佳用例! Before, you had things inside the bootstrap grid, which was probably adding the extra spacing you didn't want. 以前,您在Bootstrap网格内放置了东西,这可能会增加您不需要的额外间距。 For a custom component look, you'd need to just space things out the way you want them to look. 对于自定义组件外观,您只需要按照所需的外观排列即可。 Now the triangles are spaced out evenly vertically - even if the height of the <input> is increased! 现在,即使<input>的高度增加了,三角形也会在垂直方向上均匀地间隔开!

 .spinner-input-wrapper { display: flex; } .spinner { display: flex; flex-direction: column; justify-content: space-between; } .spinner-input { margin: 0 3px; } .triangle-up, .triangle-down { width: 0; height: 0; border: 4px solid transparent; } .triangle-up { border-bottom-width: 8px; border-bottom-color: #555; } .triangle-down { border-top-width: 8px; border-top-color: #555; } 
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <div class="container"> <div class="spinner-input-wrapper"> <div class="spinner"> <div class="triangle-up" id="up1"></div> <div class="triangle-down" id="down1"></div> </div> <input type="text" class="spinner-input" size="5" maxlength="5"> <div class="spinner"> <div class="triangle-up" id="up2"></div> <div class="triangle-down" id="down2"></div> </div> </div> 

If you don't want to learn something else like flexbox all you really need is to clean up your code a bit. 如果您不想学习其他东西,例如flexbox,那么您真正需要做的就是稍微整理一下代码。 In this case the bootstrap layout is making it more difficult. 在这种情况下,引导程序布局将使其变得更加困难。 Try simplifying to something like this. 尝试简化成这样。 Here's my codepen sample 这是我的Codepen示例

HTML HTML

<div class="container">
  <div class="triangles">
    <div class="triangle-up" id="up1"></div>
    <div class="triangle-down" id="down1"></div>
  </div>
  <div class="input-text">
    <input type="text" id="hora" size="5" maxlength="5">
  </div>
  <div class="triangles">
    <div class="triangle-up" id="up1"></div>
    <div class="triangle-down" id="down1"></div>
  </div>
</div>

CSS CSS

.triangle-up {
    margin-bottom: 6px;
    width: 0;
    height: 0;
    border-left: 4px solid transparent;
    border-right: 4px solid transparent;
    border-bottom: 8px solid #555;
}
.triangle-down {
    width: 0;
    height: 0;
    border-left: 4px solid transparent;
    border-right: 4px solid transparent;
    border-top: 8px solid #555;
}

.triangles {
    float: left;
    display: inline-block;
    padding: 2px 5px;
}

.input-text {
    float: left;
    display: inline-block;
}

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

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