简体   繁体   English

如何在 html/css/js 中将项目与其上方的项目对齐?

[英]How can I align an item with an item above it in html/css/js?

I have 3 input blanks on my form (see the screenshot below).我的表单上有 3 个输入空格(请参见下面的屏幕截图)。 I have 1 dropdown button and one <input type="text"> in 1 row and another <input type="text"> in the 2nd row.我在第一行有 1 个下拉按钮和一个<input type="text"> <input type="text"> I want the 2 <input type="text"> to align.我希望 2 <input type="text">对齐。 In other words, I want the "Choice 2" box to be right below the "Choice 1" box.换句话说,我希望“选择 2”框位于“选择 1”框的正下方。 How can I do this?我怎样才能做到这一点? Thanks.谢谢。

截屏

By the way, This is my html:顺便说一句,这是我的 html:

<div class="container">
<div class="dropdown">
      <button class="btn btn-info dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
        2
        <span class="caret"></span>
      </button>
      <ul id="list" class="dropdown-menu dropdown-info" aria-labelledby="dropdownMenu1">
        <li><a href="#">2</a></li>
        <li><a href="#">3</a></li>
        <li><a href="#">4</a></li>
      </ul>
      <input type="text" name="" value="" placeholder="Choice 1"> <br>
</div>
<div class="container">
<input type="text" name="" value="" placeholder="Choice 2">
    </div></div>

And this is my css:这是我的 css:

.container {padding: 1% 20%;
  margin: auto;
  text-align: center;}

body {
  background-color: #b6e6bd;
  line-height: 1.6;
}

How can I make the choice 1 and choice 2 boxes which are both <input type="text"> be directly above and below each other?如何使<input type="text">的选择 1 和选择 2 框彼此直接位于上方和下方? Thanks谢谢

Maybe you are allowed to slightly change HTML?也许您可以稍微更改 HTML?

Here is my try.这是我的尝试。 I've moved two input[text] to one div and the dropdown to another.我已将两个input[text]移至一个div并将下拉列表移至另一个。 Also I changed .container class to be flex@center (but you can use any other layout - table or div with known widths).此外,我将.container class 更改为flex@center (但您可以使用任何其他布局 - 具有已知宽度的tablediv )。

 .container { padding: 1% 20%; margin: auto; text-align: center; display: flex; justify-content: center; } body { background-color: #b6e6bd; line-height: 1.6; }.container-inner { margin-left: 10px; }
 <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script> <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet" /> <div class="container"> <div class="dropdown"> <button class="btn btn-info dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"> 2 <span class="caret"></span> </button> <ul id="list" class="dropdown-menu dropdown-info" aria-labelledby="dropdownMenu1"> <li><a href="#">2</a></li> <li><a href="#">3</a></li> <li><a href="#">4</a></li> </ul> </div> <div class="container-inner"> <input type="text" name="" value="" placeholder="Choice 1"> <br> <input type="text" name="" value="" placeholder="Choice 2"> </div> </div>

The best way is to use a framework like Bootstrap .最好的方法是使用像Bootstrap这样的框架。 But if you won't I suggest you to change your code to this:但是,如果您不这样做,我建议您将代码更改为:

 .container { padding: 1% 20%; margin: auto; text-align: center;} body { background-color: #b6e6bd; line-height: 1.6; }
 <div class="container"> <div class="dropdown"> <button class="btn btn-info dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"> 2 <span class="caret"></span> </button> <select id="list" class="dropdown-menu dropdown-info" aria-labelledby="dropdownMenu1"> <option><a href="#">2</a></option> <option><a href="#">3</a></option> <option><a href="#">4</a></option> </select> <input type="text" name="" value="" placeholder="Choice 1"> <br> </div> <div class="container"> <input type="text" name="" value="" placeholder="Choice 2" style="margin-right: -64px;"> </div> </div>

I hope this will helpful.我希望这会有所帮助。

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

相关问题 如何使输入框中的第三个项目具有不同的颜色? (JS、HTML、CSS) - How can I make the third item entered into an input box a different color? (JS, HTML, CSS) 如何使用HTML / CSS / JavaScript在一个项目上垂直对齐列表? - How to vertically align a list on one item, with HTML/CSS/JavaScript? 如何让 css 仅影响 .js 中的列表项,而不影响我的 .html 中的列表项 - How do I make css affect list item in .js only and not in my .html 定位项(包括html,css,js) - Positioning item (html, css, js included) 将项目添加到表格-HTML / CSS / JS - Add Item to Table - HTML/CSS/JS 如何使用HTML和CSS正确对齐图像(带有文本)? - How can I properly align images (with text) with HTML and CSS? 如何在不更改当前 HTML 结构的情况下使用 css 将底部元素对齐(不覆盖)顶部元素 - How do I align bottom element above (not overlay) top element using css without changing the current HTML structure CLeditor使用HTML的“ align”属性。 如何将其更改为CSS“文本对齐”? - CLeditor uses the HTML 'align' attribute. How can I change that to CSS 'text-align'? 如何在html角js中获取项目内的项目? - how to get a item inside item in html angular js? 如何在 react js 中为数组中的项目分配键? - How can I assign a key to an item in my array in react js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM