This question already has answers here:
CSS: Element's height based on the image height next to it?
                                
                                    (3个答案)
                                
                        
                                2年前关闭。
            
                    
我正在努力实现以下目标:

--------------------------------------------------
|         | Text: Hello, World! I am a paragraph |
|  Image  | with the same line and height of the |
|         | image                                |
--------------------------------------------------


我试图给代码添加样式“内联块”,但没有做任何事情:

<div>
    <img src="image.png" alt="image" style="inline-block">
    <p>Hello, World! I am a paragraph with the same line and height of the image</p>
</div>


编辑:display:flex;做的工作,但我想补充一点,我也在align-self:center;上使用了<img>,这样图像就不会拉伸

最佳答案

<div style="display: flex">
    <img src="image.png" alt="image">
    <p>Hello, World! I am a paragraph with the same line and height of the image</p>
</div>


尝试这个。

10-07 21:00