简体   繁体   English

按钮内文本与图像的垂直对齐

[英]vertically alignment of a text inside a button with an image

In this CodePen you can see a <button> with an image and text inside ( <span> ). 在此CodePen中,您可以看到一个<button>其中带有图像和文本( <span> )。 The problem is when the text got multiline. 问题是文本多行显示。 The second line of text is not immediately close to the first line and a big space appears (taking in count the height of the <img> ). 文本的第二行不是紧靠第一行,并且会出现一个很大的空格(计算<img>的高度)。

My code ( on CodePen ): 我的代码( 在CodePen上 ):

 button { width: 300px; } img { vertical-align: middle; } 
 <button> <img src="http://lorempixel.com/50/50/" /> <span> Some Text some text some text some text some text some text some text some text</span> </button> 

You can use the following solution using flexbox: 您可以通过flexbox使用以下解决方案:

 * { margin:0; padding:0; box-sizing:border-box; } button { align-items: center; display: flex; padding: 3px; justify-content: center; width: 300px; } button img { flex-shrink: 0; margin-right: 10px; } 
 <button> <img src="https://lorempixel.com/50/50/" alt=""> <span>Some Text Some Text Some Text Some Text Some Text Some Text Some Text</span> </button> <button> <img src="https://lorempixel.com/50/50/" alt=""> <span>Some Text</span> </button> <button> <img src="https://lorempixel.com/50/50/" alt=""> </button> 

can you try this https://codepen.io/anon/pen/qVKagg 你可以尝试一下吗https://codepen.io/anon/pen/qVKagg

 button { width: 300px; display: flex; text-align:left; } img { vertical-align: middle; margin-right:5px; } 
 <button> <img src="http://lorempixel.com/50/50/" /> <span> Some Text some text some text some text some text some text some text some text</span> </button> 

Try to do it like this way. 尝试这样做。 There are other methods also, but this one is simple. 也有其他方法,但是这一方法很简单。

 button { width: 300px; } button img { float:left; width:20%; } button span { float:right; width:80%; } 
 <button> <img src="http://lorempixel.com/50/50/" /> <span> Some Text some text some text some text some text some text some text some text</span> </button> 

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

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