I'm looking for suggestions on how to create this button using flex with as little html as possible. It has centered text but the icon is aligned to one side. Most important, the two are vertical-aligned in the middle.
The problem I have: once I setup the flex to align-items
along the horizontal, I can no longer control the vertical centering without adding additional HTML containers around each item.
JSFiddle: https://jsfiddle.net/mwwy6bg7/3/
Let's assume that the height
and padding
on the button
are not consistent, because there are other use-cases where I've had this same scenario, and the centered-string might be two or three lines long [while the icon is only one line]. So I cannot use padding
or absolute
positioning.
Up to this point, I've stuck to using non-flex because it uses less HTML. But I've found myself using absolute
positions in cases as a result.
Solved: requires combining flexbox and position-absolute. Just need to apply flexbox twice. See: https://jsfiddle.net/mwwy6bg7/8/
<a href="/" class="button">
Button with a long multiline label
<span class="icon">></span>
</a>
.button {
box-sizing: border-box;
position: relative;
width: 240px;
height: 100px;
padding: 0 20px;
border: 1px solid #ccc;
background: #f7f7f7;
text-decoration: none;
text-align: center;
font: 16px arial, sans-serif;
font-weight: bold;
color: #595959;
display: flex;
align-items: center;
justify-content: center;
}
a .icon {
position: absolute;
top: 0;
bottom: 0;
right: 10px;
display: flex;
align-items: center;
}