对齐浮动内容
我正在使用 Twitter Bootstrap 工具包 来设计我的网站。我正在实现一个带有类似于以下标记的拆分按钮下拉菜单。
<div class="btn-group">
<a class="btn btn-small btn-info" href="#">
Add New Provider
</a>
<a class="btn btn-info dropdown-toggle" data-toggle="dropdown" href="#">
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a href="#">...</a></li>
<li><a href="#">...</a></li>
</ul>
</div>
这按预期工作;但是,我想将整个按钮向右对齐。但是 btn-group
和 btn
类都向左浮动,因此任何文本对齐方式都会被忽略。
如果我知道按钮的总宽度,我可以将外部
设置为该宽度,并在父 中对齐整个
。但我不知道总宽度。有没有什么方法可以将这些内容向右对齐,而无需重新设计底层 Bootstrap 类?
注意:我已经发布了 jsFiddle 演示
I'm using the Twitter Bootstrap toolkit to style my site. I'm implementing a split button dropdown with markup similar to the following.
<div class="btn-group">
<a class="btn btn-small btn-info" href="#">
Add New Provider
</a>
<a class="btn btn-info dropdown-toggle" data-toggle="dropdown" href="#">
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a href="#">...</a></li>
<li><a href="#">...</a></li>
</ul>
</div>
This works as intended; however, I would like to align the entire button to the right. But the btn-group
and btn
classes are both floated left so any text alignment is ignored.
If I knew the total width of the button, I could set the outer <div>
to that width and align the entire <div>
in a parent <div>
. But I don't know the total width.
Is there any way to align this content to the right, without reworking the underlying Bootstrap classes?
Note: I've posted a jsFiddle demo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不能保证这适用于您的特定用例(因为我还没有亲自尝试过,而且我不知道您正在使用什么标记和 CSS),但这确实适用于您的 jsFiddle 演示:
I can't guarantee this will work on your specific use-case (because I haven't tried it for myself, and I don't know exactly what markup and CSS you're using), but this does work on your jsFiddle demo:
jsFiddle 解决方案
我建议使用左/右
padding
而不是使用text-align
,尽管两者仍然有效,无论它们是否浮动。重要的是它们的宽度被声明为width: auto
。如果您不想修改核心 Bootstrap 文件,则可以在单独的样式表中使用相同的类并覆盖 Bootstrap 中的样式。只需确保在 Bootstrap 之后调用单独的个人样式表即可。例如...
在 bootstrap.css 中:
在您的 individual-stylesheet.css 中:
在您的标头中:
jsFiddle solution
I recommend using left/right
padding
instead of usingtext-align
, although both will still work, regardless if they're being floated or not. What matters is that their widths are declaredwidth: auto
.If you don't want to modify the core Bootstrap files, you can use the same classes in a separate stylesheet and override the styles from Bootstrap instead. Just ensure that the separate, personal stylesheet is called after Bootstrap. For example...
in bootstrap.css:
in your personal-stylesheet.css:
in your header:
这个问题已在 bootstrap 2.0.2 中修复。使用引导类“pull-right”
https://github.com/twitter/bootstrap/issues/ 2045
http://jsbin.com/ebemiz/2/edit#html,live
This was fixed in bootstrap 2.0.2. Use the bootstrap class "pull-right"
https://github.com/twitter/bootstrap/issues/2045
http://jsbin.com/ebemiz/2/edit#html,live