简体   繁体   English

在图像旁边的 div 中对齐文本,但垂直居中

[英]Align Text in a div next to an Image but vertically centered

 <div> <div style='display:inline'> <img src='https://m.media-amazon.com/images/M/MV5BMjA5MTkzNTY5Ml5BMl5BanBnXkFtZTgwNjU4MzY1MTI@._V1_QL50_SY1000_CR0,0,734,1000_AL_.jpg' height=300px> </div> <div style='display:inline'> Twin Peaks </div> </div>

So I have the above image with a text next to it.所以我有上面的图片,旁边有一个文字。 The thing is that I want the text next to it to appear not on the bottom right but at the middle right/ top right of the image with some space between the image and text.问题是我希望它旁边的文本不是出现在右下角,而是出现在图像的右中/右上角,图像和文本之间有一些空间。 How can I achieve it?我怎样才能实现它?

 <div> <div style='display:inline'> <img src='https://m.media-amazon.com/images/M/MV5BMjA5MTkzNTY5Ml5BMl5BanBnXkFtZTgwNjU4MzY1MTI@._V1_QL50_SY1000_CR0,0,734,1000_AL_.jpg' style='vertical-align:middle' height=300px> </div> <div style='display:inline'> Twin Peaks </div> </div>

Solved it by adding vertial-align:middle !通过添加vertial-align:middle解决了它!

You can use position absolute on the text and then align the text anywhere you want using the top property.您可以在文本上使用 position absolute,然后使用 top 属性将文本对齐到您想要的任何位置。

Example:例子:

 <div> <div style='display:inline'> <img src='https://m.media-amazon.com/images/M/MV5BMjA5MTkzNTY5Ml5BMl5BanBnXkFtZTgwNjU4MzY1MTI@._V1_QL50_SY1000_CR0,0,734,1000_AL_.jpg' height=300px> </div> <div style="display:inline; position:absolute;"> Twin Peaks </div> </div>

You can achieve it by using flex in the parent.您可以通过在父级中使用 flex 来实现它。 Just set align-items: center只需设置align-items: center

 <div style="display: flex; align-items: center;"> <div> <img src='https://m.media-amazon.com/images/M/MV5BMjA5MTkzNTY5Ml5BMl5BanBnXkFtZTgwNjU4MzY1MTI@._V1_QL50_SY1000_CR0,0,734,1000_AL_.jpg' height=300px> </div> <div style="margin-left: 10px;"> Twin Peaks </div> </div>

 <div> <div style="float: left;"> <img src="https://m.media-amazon.com/images/M/MV5BMjA5MTkzNTY5Ml5BMl5BanBnXkFtZTgwNjU4MzY1MTI@._V1_QL50_SY1000_CR0,0,734,1000_AL_.jpg" height="300px" /> </div> <div style="float: left; margin-left: 10px;"> Twin Peaks </div> </div>

It'll set your Text top right side of your image.它会将您的文本设置在图像的右上角。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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