I wanted to recreate something like this:
And then I would use it for social medias and external sites :) This is what I could come up with:
As you can see, there are two problems here:
I thought if I could align the text to the center, the picture would automatically fit as well, but I can't seem to be able to do it.
Here's the code I wrote:
.child {
height: 250px;
margin: 20px;
}
.external-links {
display: table-cell;
background: #ccc;
vertical-align: middle;
text-align: center;
border-radius: 32px;
color: black;
text-decoration: none;
padding: 1px;
}
<div class="child">
<a class="external-links" href="https://github.com/amirashabani" target="_blank">
<img src="{% static 'app/images/icons/github.ico' %}">
<span>github</span>
</a>
<a class="external-links" href="https://twitter.com/amirashabani" target="_blank">
<img src="{% static 'app/images/icons/twitter.ico' %}">
<span>twitter</span>
</a>
<a class="external-links" href="https://stackoverflow.com/users/6282576/amir-a-shabani" target="_blank">
<img src="{% static 'app/images/icons/stackoverflow.ico' %}">
<span>stackoverflow</span>
</a>
</div>
I thought the two lines of vertical-align: middle;
& text-align: center;
would center the text, but it doesn't.
To center an inline element like a link or a span or an img, all you need is text-align: center . For multiple inline elements, the process is similar. It's possible by using text-align: center .
Select the text that you want to center, and then click Paragraph on the Format menu. On the Indents and Spacing tab, change the setting in the Alignment box to Centered, and then click OK.
To vertical align the text you need to put vertical-align: middle;
on the img
tag.
Regarding the image not fitting into the div
, you need to set the border-radius
to the image too.
a img {
vertical-align: middle;
border-radius: 32px;
}
.child {
height: 250px;
margin: 20px;
}
.external-links {
display: inline-block;
background: #ccc;
text-align: center;
border-radius: 32px;
color: black;
text-decoration: none;
padding: 1px;
margin-right: 4px;
}
.external-links img {
vertical-align: middle;
border-radius: 32px;
margin-left: -2px;
}
.external-links span {
margin-right: 5px;
}
<div class="child">
<a class="external-links" href="https://github.com/amirashabani" target="_blank">
<img src="https://cdnjs.cloudflare.com/ajax/libs/webicons/2.0.0/webicons/webicon-github-m.png">
<span>github</span>
</a>
<a class="external-links" href="https://twitter.com/amirashabani" target="_blank">
<img src="https://cdnjs.cloudflare.com/ajax/libs/webicons/2.0.0/webicons/webicon-twitter-m.png">
<span>twitter</span>
</a>
<a class="external-links" href="https://stackoverflow.com/users/6282576/amir-a-shabani" target="_blank">
<img src="https://cdnjs.cloudflare.com/ajax/libs/webicons/2.0.0/webicons/webicon-stackoverflow-m.png">
<span>stackoverflow</span>
</a>
</div>
In addition to the above, I made a few minor changes in the snippet (like changing display to inline-block
and changed some margins) to make the result look a bit better.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With