0

.parent{
height:50px;
background:gold;
}

.checka, .radioa, .selecta{
display:inline-block;
vertical-align:middle;
}
<div class='parent'>
<input type='checkbox' class='checka'>
<input type='radio' class='radioa'>

<select class='selecta'>
<option>lorem</option>
<option>lorem</option>
</select>
</div>

What is the problem with the vertical align:middle here? Why child element are not on middle of parent?

  • Because you didn't give them a height? – j08691 Aug 26 '18 at 19:20
  • 2
    @j08691 we don't need height, vertical-align work with the line box (that no one can see) and not the containing block (that everyone can see) ... this will always confuse everyone – Temani Afif Aug 26 '18 at 19:56

2 Answers2

1

You could also add line-height to center them. Just make sure the height and line-height are equal. See CSS vertical-align, how can I remove the little spacing below the text? for another dynamic example of the effect.

.parent{
height:50px;
line-height: 50px;
background:gold;
}

.checka, .radioa, .selecta{
  display:inline-block;
}
<div class='parent'>
  <input type='checkbox' class='checka'>
  <input type='radio' class='radioa'>

  <select class='selecta'>
    <option>lorem</option>
    <option>lorem</option>
  </select>
</div>
SuperDJ
  • 7,488
  • 11
  • 40
  • 74
  • 1
    and we still need `vertical-align:middle` ...increasing the line-height will increase the line box and keep the alignment in the middle as expected ... actually they are not exacly in the middle – Temani Afif Aug 26 '18 at 20:03
0

Use flex for that is better Add the following to your parent:

.parent{
height:50px;
background:gold;
display: flex;
align-items: center;
}

and you can remove styles for : .checka, .radioa, .selecta

IsraGab
  • 4,819
  • 3
  • 27
  • 46