I'm trying to get the text to be on the right side and vertically center aligned with the image. How can I do that?
My current code:
<div style="display: table;">
<div style="display: table-cell;"><img src="//dummyimage.com/100"></div>
<div style="display: table-cell;">text</div>
</div>
Answer: Use the CSS vertical-align Property You can align an image vertically center inside a <div> by using the CSS vertical-align property in combination with the display: table-cell; on the containing div element.
To place an item at the top or bottom of its cell, insert the "VALIGN=" attribute within the code for that cell. To vertically align an entire row (e.g., placing all data in that row at the tops of the cells), insert the "VALIGN=" attribute within the code for that row.
1 Select the text you want to center between the top and bottom margins. 2 On the Page Layout tab, click the Page Setup Dialog Box Launcher. 3 Select the Layout tab. 4 In the Vertical alignment box, click Center 5 In the Apply to box, click Selected text, and then click OK.
use CSS3 Flexible Box Layout:
from the documentation:
providing for the arrangement of elements on a page such that the elements behave predictably when the page layout must accommodate different screen sizes and different display devices. For many applications, the flexible box model provides an improvement over the block model in that it does not use floats, nor do the flex container's margins collapse with the margins of its contents.
your example with centered text to the right would look like this
#pageElement{display:flex; flex-wrap: nowrap; align-items: center}
<div id="pageElement">
<div> <img src="//dummyimage.com/100"> </div>
<div> text </div>
</div>
I found this cheat-sheet very helpful and browser compatibility you find here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With