简体   繁体   English

如何在不影响内联块的垂直对齐的情况下向右浮动?

[英]How to float right without affecting the vertical alignment of a inline-block?

I have a challenge, and would like to know the cleanest solution with the minimal effort custom side as I am using Bootstrap (and trying to solve without hardcoded values as I would like to use it as component with Angular...). 我有一个挑战,我想在使用Bootstrap的同时以最小的努力来了解最简洁的解决方案(并尝试解决没有硬编码值的问题,因为我想将其用作Angular的组件...)。

Below there is the code, but basically I have the problem on title. 下面是代码,但是基本上我在标题上有问题。 Two children, one is text and the other a div containing buttons which I would like to move to the right horizontally. 两个孩子,一个是文本,另一个是div,其中包含我想水平向右移动的按钮。 Unfortunately, when I do that, then the text supposed to stay to the left is loosing the vertical alignment. 不幸的是,当我这样做时,应该留在左侧的文本使垂直对齐方式失去了。

Here the code, where I would like to have Button in the middle: 这里是代码,我想在中间放置Button:

 .pi-button-with-buttons{ width: 100%; height: 50px !important; text-align: left !important; } .pi-button-with-buttons span { vertical-align: middle; } .pi-button-with-buttons div { display: inline-block; float:right; } 
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <div class="btn btn-default pi-button-with-buttons"> <span>Button</span> <div> <a class="btn btn-default" role="button">Inside 1</a> <a class="btn btn-default" role="button">Inside 2</a> </div> </div> 

Thanks in advance for your help! 在此先感谢您的帮助!

Option 1 : using flexbox 选项 1 :使用flexbox

 .pi-button-with-buttons{ width: 100%; height: 50px !important; text-align: left !important; } .pi-button-with-buttons span { display: inline-flex; align-items: center; height: 100%; } .pi-button-with-buttons div { display: inline-block; float:right; } 
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <div class="btn btn-default pi-button-with-buttons"> <span>Button</span> <div> <a class="btn btn-default" role="button">Inside 1</a> <a class="btn btn-default" role="button">Inside 2</a> </div> </div> 

Option 2 : using line-height 选项2 :使用line-height

 .pi-button-with-buttons { width: 100%; height: 50px !important; text-align: left !important; } .pi-button-with-buttons span { line-height: 36px; } .pi-button-with-buttons div { display: inline-block; float: right; } 
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <div class="btn btn-default pi-button-with-buttons"> <span>Button</span> <div> <a class="btn btn-default" role="button">Inside 1</a> <a class="btn btn-default" role="button">Inside 2</a> </div> </div> 

Yes, unfortunately float: right loses the display: inline-block behaviour. 是的,不幸的是浮动:右边失去了显示:内联块行为。 Instead, remove the float: right and use flexbox on the wrapper, like so: 相反,删除float:right并在包装器上使用flexbox,如下所示:

.btn-default {
  display: flex !important;
  justify-content: space-between;
  align-items: center;
}

You can simply use flex like this : 您可以像这样简单地使用flex

 .pi-button-with-buttons{ width: 100%; height: 50px !important; display:flex!important; align-items:center; } .pi-button-with-buttons div { flex:1; text-align:right; } 
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <div class="btn btn-default pi-button-with-buttons"> <span>Button</span> <div> <a class="btn btn-default" role="button">Inside 1</a> <a class="btn btn-default" role="button">Inside 2</a> </div> </div> 

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

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