在 asp.net 中两个并排按钮之间放置空间的最简单方法是什么

发布于 2024-10-19 09:12:36 字数 380 浏览 2 评论 0原文

我有两个并排的按钮,我想在它们之间放一些按钮。

以下代码将有 2 个彼此相邻的按钮。我尝试了 div 的边距,但无法在两者之间获得一些合适的空间。

<div style="text-align: center"> 
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" Width="89px" OnClick="btnSubmit_Click" />
    <asp:Button ID="btnClear" runat="server" Text="Clear" Width="89px" OnClick="btnClear_Click" />
</div>

I have 2 buttons side by side, and I would like to have some inbetween them.

Following code will have 2 buttons right next to each other. I have tried margin for the div, and just couldn't get some nice space between the two.

<div style="text-align: center"> 
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" Width="89px" OnClick="btnSubmit_Click" />
    <asp:Button ID="btnClear" runat="server" Text="Clear" Width="89px" OnClick="btnClear_Click" />
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

寂寞清仓 2024-10-26 09:12:36

创建一个分隔符类,如下所示:

.divider{
    width:5px;
    height:auto;
    display:inline-block;
}

然后将其附加到两个按钮之间的 div

<div style="text-align: center"> 
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" Width="89px" OnClick="btnSubmit_Click" />
    <div class="divider"/>
    <asp:Button ID="btnClear" runat="server" Text="Clear" Width="89px" OnClick="btnClear_Click" />
</div>

这是最好的方法,因为它避免了盒模型,这对于旧浏览器来说可能是一个痛苦,并且不会添加任何会被选取的额外字符由屏幕阅读器显示,因此可读性更好。

对于某些场景来说,拥有许多此类类型的 div 是件好事(我最常用的是 vert5spacer,与此类似,但放置了一个高度为 5 且宽度为 auto 的块 div,用于分隔表单中的项目等。

create a divider class as follows:

.divider{
    width:5px;
    height:auto;
    display:inline-block;
}

Then attach this to a div between the two buttons

<div style="text-align: center"> 
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" Width="89px" OnClick="btnSubmit_Click" />
    <div class="divider"/>
    <asp:Button ID="btnClear" runat="server" Text="Clear" Width="89px" OnClick="btnClear_Click" />
</div>

This is the best way as it avoids the box model, which can be a pain on older browsers, and doesn't add any extra characters that would be picked up by a screen reader, so it is better for readability.

It's good to have a number of these types of divs for certain scenarios (my most used one is vert5spacer, similar to this but puts a block div with height 5 and width auto for spacing out items in a form etc.

风轻花落早 2024-10-26 09:12:36

在它们之间添加一个空格   (或更多,具体取决于您的偏好)

    <div style="text-align: center">         
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" Width="89px" OnClick="btnSubmit_Click" />
         
        <asp:Button ID="btnClear" runat="server" Text="Clear" Width="89px" OnClick="btnClear_Click" />
    </div>

Add a space   between them (or more depending on your preference)

    <div style="text-align: center">         
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" Width="89px" OnClick="btnSubmit_Click" />
         
        <asp:Button ID="btnClear" runat="server" Text="Clear" Width="89px" OnClick="btnClear_Click" />
    </div>
长发绾君心 2024-10-26 09:12:36
#btnClear{margin-left:100px;}

或者向按钮添加一个类并具有:

.yourClass{margin-left:100px;}

这可以实现这一点 - http://jsfiddle.net/QU93w/

#btnClear{margin-left:100px;}

Or add a class to the buttons and have:

.yourClass{margin-left:100px;}

This achieves this - http://jsfiddle.net/QU93w/

月棠 2024-10-26 09:12:36
    <style>
    .Button
    {
        margin:5px;
    }
    </style>

 <asp:Button ID="Button1" runat="server" Text="Button" CssClass="Button" />
 <asp:Button ID="Button2" runat="server" Text="Button" CssClass="Button"/>
    <style>
    .Button
    {
        margin:5px;
    }
    </style>

 <asp:Button ID="Button1" runat="server" Text="Button" CssClass="Button" />
 <asp:Button ID="Button2" runat="server" Text="Button" CssClass="Button"/>
饮惑 2024-10-26 09:12:36

老帖子,但我想说最干净的方法是向周围的 div 添加一个类,并在每个按钮上添加一个按钮类,以便您的 CSS 规则对更多用例有用:

/* Added to highlight spacing */
.is-grouped {   
    display: inline-block;
    background-color: yellow;
}

.is-grouped > .button:not(:last-child) {
    margin-right: 10px;
}
Spacing shown in yellow<br><br>

<div class='is-grouped'>
    <button class='button'>Save</button>
    <button class='button'>Save As...</button>
    <button class='button'>Delete</button>
</div>

Old post, but I'd say the cleanest approach would be to add a class to the surrounding div and a button class on each button so your CSS rule becomes useful for more use cases:

/* Added to highlight spacing */
.is-grouped {   
    display: inline-block;
    background-color: yellow;
}

.is-grouped > .button:not(:last-child) {
    margin-right: 10px;
}
Spacing shown in yellow<br><br>

<div class='is-grouped'>
    <button class='button'>Save</button>
    <button class='button'>Save As...</button>
    <button class='button'>Delete</button>
</div>

不再让梦枯萎 2024-10-26 09:12:36

尝试将以下类放在您的第二个按钮上

.div-button
{
    margin-left: 20px;
}

编辑:

如果您希望第一个按钮与 div 以及第二个按钮间隔开,则将该类应用于您的第一个按钮也是

Try putting the following class on your second button

.div-button
{
    margin-left: 20px;
}

Edit:

If you want your first button to be spaced from the div as well as from the second button, then apply this class to your first button also.

橙味迷妹 2024-10-26 09:12:36

我使用了   并且它工作正常。你可以尝试一下。
您不需要使用引号

I used   and it is working fine. You could try it.
You do not need to use the quotation marks

和我恋爱吧 2024-10-26 09:12:36

如果您使用 bootstrap,请将 ml-3 添加到第二个按钮:

 <div class="row justify-content-center mt-5">
        <button class="btn btn-secondary" type="button">Button1</button>
        <button class="btn btn-secondary ml-3" type="button">Button2</button>
 </div>

If you are using bootstrap, add ml-3 to your second button:

 <div class="row justify-content-center mt-5">
        <button class="btn btn-secondary" type="button">Button1</button>
        <button class="btn btn-secondary ml-3" type="button">Button2</button>
 </div>
往事风中埋 2024-10-26 09:12:36

还有另一种方法:

您可以使用 width 属性调整空间量。

你的代码是:

<div style="text-align: center"> 
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" Width="89px" OnClick="btnSubmit_Click" />
    <span style="width: 10px"></span>
    <asp:Button ID="btnClear" runat="server" Text="Clear" Width="89px" OnClick="btnClear_Click" />
</div>

There is another way of doing so:

<span style="width: 10px"></span>

You can adjust the amount of space using the width property.

Your code would be:

<div style="text-align: center"> 
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" Width="89px" OnClick="btnSubmit_Click" />
    <span style="width: 10px"></span>
    <asp:Button ID="btnClear" runat="server" Text="Clear" Width="89px" OnClick="btnClear_Click" />
</div>
感情废物 2024-10-26 09:12:36

你能只吃一些 吗?

<div style="text-align: center"> 
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" Width="89px" OnClick="btnSubmit_Click" />
      
    <asp:Button ID="btnClear" runat="server" Text="Clear" Width="89px" OnClick="btnClear_Click" />
</div>

Can you just just some   ?

<div style="text-align: center"> 
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" Width="89px" OnClick="btnSubmit_Click" />
      
    <asp:Button ID="btnClear" runat="server" Text="Clear" Width="89px" OnClick="btnClear_Click" />
</div>
懷念過去 2024-10-26 09:12:36

如果您希望样式全局应用,您可以使用 css 中的相邻同级组合器。

.my-button-style + .my-button-style {
  margin-left: 40px;
}

/* general button style */
.my-button-style {
  height: 100px;
  width: 150px;
}

这是一个小提琴: https://jsfiddle.net/caeLosby/10/

它类似于一些现有的答案,但它没有设置第一个按钮的边距。
例如,在这种情况下,

<button id="btn1" class="my-button-style"/>
<button id="btn2" class="my-button-style"/>

只有 btn2 将获得边距。

有关更多信息,请参阅 https://developer.mozilla.org/en- US/docs/Web/CSS/Adjacent_sibling_combinator

If you want the style to apply globally you could use the adjacent sibling combinator from css.

.my-button-style + .my-button-style {
  margin-left: 40px;
}

/* general button style */
.my-button-style {
  height: 100px;
  width: 150px;
}

Here is a fiddle: https://jsfiddle.net/caeLosby/10/

It is similar to some of the existing answers but it does not set the margin on the first button.
For example in the case

<button id="btn1" class="my-button-style"/>
<button id="btn2" class="my-button-style"/>

only btn2 will get the margin.

For further information see https://developer.mozilla.org/en-US/docs/Web/CSS/Adjacent_sibling_combinator

最后的乘客 2024-10-26 09:12:36

您可以使用 btnSubmit::before { margin-left: 20px; }

you can use btnSubmit::before { margin-left: 20px; }

寒尘 2024-10-26 09:12:36

通过引导 CSS,我们有一个“btn-toolbar”类。

<div class="btn-toolbar">
    <button class="btn">Start</button>
    <button class="btn">Stop</button>
</div>

https://getbootstrap.com/docs/4.0/components/button -group/#button-toolbar

With the bootstrap CSS, we have a 'btn-toolbar' class.

<div class="btn-toolbar">
    <button class="btn">Start</button>
    <button class="btn">Stop</button>
</div>

https://getbootstrap.com/docs/4.0/components/button-group/#button-toolbar

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文