简体   繁体   中英

Need to Align Image and Text Horizontally and Vertically Centered

I can align them vertically and horizontally centered individually, but I need them both as a single unit to be centered horizontally, and the text to be centered vertically next to the image.

Here is what I have:

<div style="text-align: center; height: 50px; position: relative; border: 1px solid black;">
    <img src="img/image.jpg"><span style="margin-left: 150px; position: absolute; top: 50%; left: 50%; -ms-transform: translate(-50%, -50%); transform: translate(-50%, -50%);">SOMETEXT<span>
</div>

If I don't have the "margin-left" on the text then they overlap each other, but with the margin they are off-center.

I've looked through a couple dozen Stackoverflow answers to similar questions, but many are old and no longer work/supported, or it just isn't what I need.

TIA!

You can do also do it by simple css trick

div{
  display:flex;
  flex-wrap:wrap;
  justify-content:center;
  align-items:center;
}

Like this?

 <div style="text-align: center; height: 50px; line-height: 50px; position: relative; border: 1px solid black;"> <img src="img/image.jpg" style="vertical-align: middle;"> <span>SOMETEXT<span> </div> 

.element{ 
  justify-content:center;
  align-items:center; 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM