Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertical align img and text inside <p>

I can't get the img and text in the span to be vertically aligned:

  <p class="login-button">
                    <input type="submit" id="login-submit" value="Log On" /><img style="padding-left:20px;" id="loadingDiv" src="/Content/ib/images/ajax-loader.gif" />
                    <span id="error" style="color:red; padding-left:13px;">text </span>

                </p>

Any ideas? I tried:

.login-button{ vertical-align:middle; height:30px; line-height:30px;}
.login-button img{ vertical-align:middle;}
like image 727
raklos Avatar asked Mar 07 '11 11:03

raklos


People also ask

How do I align text and image vertically in CSS?

Using flex property in css.To align text vertically center by using in flex using align-items:center; if you want to align text horizontally center by using in flex using justify-content:center; .

How do you align text and images in HTML?

An <img> element is an inline element (display value of inline-block ). It can be easily centered by adding the text-align: center; CSS property to the parent element that contains it. To center an image using text-align: center; you must place the <img> inside of a block-level element such as a div .


1 Answers

I set up a demo for your at: http://jsfiddle.net/audetwebdesign/dCAkx/

I simplified your CSS a tiny bit and added some background color and padding.

You need to apply the vertical-align property to all the inline elements that you want to align.

The vertical-align property is not inherited, so you need to apply it to all the relevant elements.

You can apply padding and margins to control the spacing between the text and the image.

You can experiment a bit by adjusting the line-height of the container p and also, try out other alignment values such as top, bottom, baseline.

This trick is worth mastering because it touches on a key concept of how the CSS box model works, and this pattern is very common, so a good trick to have in your CSS toolbox.

like image 194
Marc Audet Avatar answered Sep 20 '22 15:09

Marc Audet