I am trying to vertically center some text inside a div. It sounds easy, but I can't get it. Here is the code, sorry for the poor formatting and actions, it whips something quickly and dirty:
<div id="container" style="text-align:center ;border: 5px solid blue; display:flex; flex-direction:row ; justify-content:center; height:100px">
<div id="first" style=" min-height:100px; min-width:200px; background-color:green">
<div style="vertical-align:middle">
first box
</div>
</div>
<div id="second" style=" min-height:100px; min-width:200px; background-color:yellow">
<div style="vertical-align:middle">
second box
</div>
</div>
<svg version="1.1" id="SVG" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 300 300" preserveAspectRatio="xMidYMid meet" height="100%" width: "auto">
</svg>
<div id="third" style="min-height:100px; min-width:200px; background-color:red">
<div style="">
third box
</div>
</div>
<div id="fourth" style="min-height:100px; min-width:200px; background-color:cyan; vertical-align:middle ">
<p>
fourth box
</p>
</div>
</div>
Run codeHide resultAs you can see, there are four rectangles around the central svg image. The horizontal centering of the elements inside the “container” div was simple; the vertical centering of the text inside each of the elements was not. Not at all.
I tried various solutions, none of which worked as intended (the text just goes up at the top of the window). Am I missing something obvious or trying to do something impossible?
I am looking for a flexible solution that can work without knowing the exact height in pixels of the boxes or container.