在此CodePen中,您可以看到<button>
内有图片和文字(<span>
)。问题是当文本得到多行时。第二行文字不会立即靠近第一行,并显示一个大空格(计算<img>
的高度)。
我的代码(on CodePen):
button {
width: 300px;
}
img {
vertical-align: middle;
}
&#13;
<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>
&#13;
答案 0 :(得分:5)
您可以使用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;
}
&#13;
<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>
&#13;
答案 1 :(得分:0)
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>
答案 2 :(得分:0)
尝试这样做。还有其他方法,但这个方法很简单。
button {
width: 300px;
}
button img {
float:left;
width:20%;
}
button span {
float:right;
width:80%;
}
&#13;
<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>
&#13;