Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertically align text inside anchor

Tags:

html

css

I have this piece of HTML:

<a href="href" class="my-a-class>
    <img alt="alt" src="/path/to/image.jpg" class="my-img-class" />&nbsp;My text
</a>

I need to vertically align "My text" without affecting the image in the anchor. I can't add any HTML tag, I need to do this only with CSS. Any suggestion?

like image 655
m4t1t0 Avatar asked Feb 01 '13 10:02

m4t1t0


People also ask

Does text-align work on anchor tag?

css cannot be directly applied for the alignment of the anchor tag. The css (text-align:center;) should be applied to the parent div/element for the alignment effect to take place on the anchor tag.

How do you vertically align text in a tag?

Use the CSS vertical-align property The vertical-align property is used to vertically center inline elements. The values of the vertical-align property align the element relative to its parent element: Line-relative values vertically align an element relative to the entire line.

How do you vertically align a text?

Center the text vertically between the top and bottom margins. Select the text that you want to center. in the Page Setup group, and then click the Layout tab. In the Vertical alignment box, click Center.

How do I center text in an anchor?

Simply text-align: center; in your existing class.


1 Answers

You could do this using .my-a-class { line-height: value }. Replace value with the height of your image.

Edit ---

These days it is much better to use flexbox for this:

.my-a-class {
   display: flex;
   align-items: center;
   justify-content: space-between;
}
like image 80
boz Avatar answered Sep 23 '22 01:09

boz