繁体   English   中英

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

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

在此CodePen中,您可以看到一个<button>其中带有图像和文本( <span> )。 问题是文本多行显示。 文本的第二行不是紧靠第一行,并且会出现一个很大的空格(计算<img>的高度)。

我的代码( 在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> 

您可以通过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> 

你可以尝试一下吗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> 

尝试这样做。 也有其他方法,但是这一方法很简单。

 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