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" /> 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?
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.
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.
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.
Simply text-align: center; in your existing class.
You could do this using .my-a-class { line-height: value }
. Replace value
with the height of your image.
These days it is much better to use flexbox for this:
.my-a-class {
display: flex;
align-items: center;
justify-content: space-between;
}
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