0

I am a beginner in CSS so kindly ignore if this question is too silly. I need your suggestions for fixing an alignment problem. I have a set of buttons along with a glyph icon image. The buttons are equally spaced and aligned (height: 18px) while the image doesn't fit (height: 18px). I tried with padding-top and that doesn't help.

<html>
<head>
  <style type="text/css">
    #glyphImage {
width:16px;
height:18px;
}
    </style>
  </head>
<body>
<table>
<tr>
    <td></td>
    <td>
     <input type="button" value="p">
 <input type="button" value="c"><input type="button" value="/c">
 <input type="button" value="s"><input type="button" value="/s">
 <input type="button" value="l"> <input type="button" value="u">
 <input type="button" value="t"><input type="button" value="tr">
   <img id="glyphImage" src="si-glyph-image.svg">
    </td></tr>
    </table>
</body>
</html>

Alignment issue:

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
javapedia.net
  • 2,531
  • 4
  • 25
  • 50

3 Answers3

1

Well, there are several ways you can go about this.

Option #1

Since you're using a table and those are pretty good at vertical alignment, you can make use of the defaults.

In HTML <td valign="middle"></td>

OR in CSS

td {
  vertical-align: middle;
}

Option #2

Use CSS Flexbox (this can be done just inside a div you wouldn't need the table) Either use your td or a container element of some kind (div, span etc)

td {
   display:flex;
   flex-direction: column;
   justify-content: center;
}

In either case, I would give EACH element its own td if you're going to stick with the table concept. That will give you a lot more control.

<tr>
  <td><input type="button" value="p"></td>
  <td><input type="button" value="c"></td>
  <td><input type="button" value="/c"></td>
  <td><input type="button" value="s"></td>
  <td><input type="button" value="/s"></td>
  <td><input type="button" value="l"></td>
  <td><input type="button" value="u"></td>
  <td><input type="button" value="t"></td>
  <td><input type="button" value="tr"></td>
  <td><img id="glyphImage" src="si-glyph-image.svg"></td>
</tr>
Bryce Howitson
  • 7,339
  • 18
  • 40
1

if you dont want to use flex target the image and set vertical-align:middle or bottom as you want to align it

<html>
<head>
  <style type="text/css">
    #glyphImage {
width:16px;
height:18px;
vertical-align:middle;
}
    </style>
  </head>
<body>
<table>
<tr>
    <td></td>
    <td>
     <input type="button" value="p">
 <input type="button" value="c"><input type="button" value="/c">
 <input type="button" value="s"><input type="button" value="/s">
 <input type="button" value="l"> <input type="button" value="u">
 <input type="button" value="t"><input type="button" value="tr">
   <img id="glyphImage" src="https://images.unsplash.com/photo-1514214246283-d427a95c5d2f?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=673&q=80">
    </td></tr>
    </table>
</body>
</html>
godfather
  • 1,518
  • 2
  • 10
  • 17
0

May not be the most elegant solution but you could use negative margin to align the items. So if the img tag had a margin-bottom: -4px for example the image would line up with the buttons.

Seemingly negative margins get a bad rep but there are some valid points in this post about why it's okay to use them.

David Mc
  • 109
  • 1
  • 6