如何在div的右侧放置一个按钮

时间:2015-11-29 18:52:16

标签: html css

这似乎是一个显而易见的问题,但请检查包含以下代码的this fiddle

Console.WriteLine();

for (int i = 0; i < text.Length; i++)
{

    newtext = newtext + "@";
}

Console.WriteLine(newtext);
Console.ReadLine();

如您所见,该按钮不会位于<div style="border:1px #aaa solid;" id="srd_header"> <button style=" float:right" onClick="window.external.Test();">Close</button> </div> 内部,并且该部门不会围绕该按钮!我使用div按钮将其推到右侧。

2 个答案:

答案 0 :(得分:2)

您需要将clearfix解决方案应用于父div,因为您正在浮动孩子(button)。

一种clearfix方法是overflow: auto

<div style="border:1px #aaa solid; background-color:red; overflow: auto;" id="srd_header">
    <button style=" float:right" onClick="window.external.Test();">Close</button>
</div>

DEMO

另一种更简单,更现代的方法是避免浮动并使用flexbox

<div style="border:1px #aaa solid; background-color:red; 
                display: flex; justify-content: flex-end;" id="srd_header">
    <button onClick="window.external.Test();">Close</button>
</div>

DEMO

答案 1 :(得分:1)

这是因为按钮上的float

编辑:如果您希望按钮位于右侧,请使用text-align:right;上的<div>

EDIT2:这是你的小提琴更新,以便一起看。只是CSS中的一个小改动,一切正常:-) http://luisfarzati.github.io/ng-bs-daterangepicker/ng-bs-daterangepicker.js