Link to home
Start Free TrialLog in
Avatar of mikha
mikhaFlag for United States of America

asked on

how to align html elements vertically centered

say, when there is a label/span/anchor next to an image/icon , what is the best way them to align them vertically, so that both are centered vertically.
for example , I have anchor tag and a input type of button next to each other in a div, like below, how can i align them vertically centered?

<div>
    <a href="" ...>
     <input type =button....>
</div>
Avatar of Pavel Gushchin
Pavel Gushchin

I always use this construction, because it works in all browsers:
<table cellpadding=0 cellspacing=0 border=0 style="width:100%; padding:0; margin:0;border-collapse:collapse">
<tr valign="middle">
    <td><a href=""><img src="img.png" width="200px" height="200px"></a></td>
    <td><input type="text" name="aaa" value="bbb"></td>
</tr>
</table>

Open in new window

Hi,

I don't recommend to use Table for a form...

I recommend to use Bootstrap it will help you to align things and also this will add responsiveness.

Here is an example of form with Bootstrap
https://getbootstrap.com/docs/4.1/components/forms/

Also they is some helper class to align
https://getbootstrap.com/docs/4.1/utilities/vertical-align/
Bootstrap is more complicated solution, because it requires to load more libraries and makes the web-page loading time longer!
Of course, if author of this question doesn't use bootstrap on his page yet...
Here is the Bootstrap usage statistics
https://trends.builtwith.com/docinfo/Twitter-Bootstrap
Personally I'm not a fan of Bootstrap, though I realize in in the far minority there.  If you're not using it already, then not worth the time and rewrite to implement it.
But please don't use table for layout, very strongly agree with lenamtl on that.

For a simple vertical alignment, you can use this:

.aligned { display:flex; align-items:center; }

Open in new window


<div class="aligned">
    <a href="" ...> 
     <input type =button....> 
</div>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of David S.
David S.
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I agree, that using different CSS tricks you can achieve this result... But, as you said, for old browsers you need extra code, etc...
I don't see that my code on tables has a bad and old style. Why? Thare is no explanation.
"Table" is standard tag, which supported by ALL browsers of ALL versions. And it works anywhere perfectly!
Somebody tells you, that table displays only when the whole content of inner cells will be loaded... Ok, I agree! But, in our 21 century and fast speeds internet, we will not even notice that!
Pavel, in the case of the code I suggested, the unsupported browsers are exceedingly old. Does anyone still support them on public websites?

There definitely is an explanation why it's bad to use a <table> like that. It's called "semantics," and part of that is only using a <table> when you want multiple rows and columns to display tabular data, like in a spreadsheet.