简体   繁体   English

如何并排创建两个 HTML 按钮

[英]How to create two HTML buttons side by side

This is a snippet of code for two buttons.这是两个按钮的代码片段。 I want them to be side by side but currently, one button is on top of other.我希望它们并排放置,但目前,一个按钮位于另一个按钮之上。 as you can see如你看到的

按钮在彼此之上 我希望它看起来像这样

HTML HTML

    <div class="buttons">

            <div class="action_btn">

            <button name="submit" class="action_btn submit" type="submit" value="Save" onclick="myFunction()">Save</button>
            <button name="submit" class="action_btn cancel" type="submit" value="Cancel" onclick="myFunction2()">Cancel</button>

            <p id="saved"></p>

            </div>

        </div>

CSS CSS

    .buttons {
width: 960px;
margin: 0 auto;}

    .action_btn {
width: 500px;
margin: 0 auto;}

 .buttons { width: 200px; margin: 0 auto; display: inline;} .action_btn { width: 200px; margin: 0 auto; display: inline;}
 <div class="buttons"> <div class="action_btn"> <button name="submit" class="action_btn submit" type="submit" value="Save" onclick="myFunction()">Save</button> <button name="submit" class="action_btn cancel" type="submit" value="Cancel" onclick="myFunction2()">Cancel</button> <p id="saved"></p> </div> </div>

One problem is that class action_btn is used for div and button and it interferes.一个问题是 action_btn 类用于 div 和按钮,它会干扰。 Also in your code, div that has action_btn class look like extra and needs to be deleted.同样在您的代码中,具有 action_btn 类的 div 看起来像额外的,需要删除。 Considering the above, your code will be converted as follows.考虑到上述情况,您的代码将被转换如下。

 .buttons { width: 960px; margin: 0 auto; } .action_btn { display: inline-block; width: calc(50% - 4px); margin: 0 auto; }
 <div class="buttons"> <button name="submit" class="action_btn submit" type="submit" value="Save" onclick="myFunction()">Save</button> <button name="submit" class="action_btn cancel" type="submit" value="Cancel" onclick="myFunction2()">Cancel</button> <p id="saved"></p> </div>

 .form-control { display: inline-block; vertical-align: middle; width: auto; height: 34px; padding: 6px 12px; font-size: 13px; line-height: 1.42857143; color: #555555; background-color: #ffffff; background-image: none; border: 1px solid #cccccc; border-radius: 4px; .form-control::-moz-placeholder { color: #999999; opacity: 1; } .form-control:-ms-input-placeholder { color: #999999; } .form-control::-webkit-input-placeholder { color: #999999; } .btn { display: inline-block; margin-bottom: 0; font-weight: normal; text-align: center; vertical-align: middle; cursor: pointer; background-image: none; border: 1px solid transparent; white-space: nowrap; padding: 6px 12px; font-size: 14px; line-height: 1.42857143; border-radius: 4px; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; color: #ffffff; background-color: #428bca; border-color: #357ebd; } /* additions */ .btn-inline { height: 34px; border-radius: 0 4px 4px 0; margin-left: -10px; } .form-control { -webkit-box-shadow: none; -moz-box-shadow: none; box-shadow: none; padding-left: 10px; } .form-control:focus { -webkit-box-shadow: none; -moz-box-shadow: none; box-shadow: none; }
 <form class="form-inline"> <button class="btn btn-primary btn-inline">Update</button> <button class="btn btn-primary btn-inline">Update</button> </form>

.btn{display: inline-block; margin-right: 20px;}

use this in css using the class or id of your button.使用按钮的类或 id 在 css 中使用它。

this means that you want the buttons to be side by side这意味着您希望按钮并排

Change this:改变这个:

.buttons {
  width: 960px;
  margin: 0 auto;
}

.action_btn {
  width: 500px;
  margin: 0 auto;
}

To this:对此:

.buttons {
  width: auto;
}

.action_btn {
  width: auto;
}

Or simply delete those CSS rules altogether.或者干脆完全删除这些 CSS 规则。

I have no idea why did you give such large numbers for the buttons' widths but the effect is that both buttons are too wide to fit side-by-side我不知道你为什么给按钮的宽度提供这么大的数字,但结果是两个按钮太宽而无法并排放置

Use float使用浮动

<div>
  <div>
    <button name="submit" type="submit" value="Save" onclick="myFunction()" class="button">Save</button>
    <button name="submit" type="submit" value="Cancel" onclick="myFunction2()" class="button">Cancel</button>
    <p id="saved"></p>
  </div>
</div>

And

.button{
    float:left;
    margin:2px;
 }

You want the buttons themselves to have inline-block in the css.您希望按钮本身在 css 中具有内联块。 Like this像这样

    button.action_btn{display: inline-block;}

Once you have that, add some padding and/or margins to separate them.完成后,添加一些填充和/或边距以将它们分开。

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

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