简体   繁体   English

在图像旁边保留行内块文本

[英]Keep inline-block text next to image

I'm trying to place some text next to an image inside a container div of fixed width.我正在尝试在固定宽度的容器 div 内的图像旁边放置一些文本。 To do this I'm using display: inline-block on the text.为此,我在文本上使用display: inline-block This works when the text is short but when it gets longer than the div it just moves downwards as seen in this snippet:这适用于文本很短但当它比 div 长时它只是向下移动,如以下代码段所示:

 .container { width: 200px; margin-bottom: 10px; border: 1px solid red; } img { height: 50px; width: 50px; border-radius: 50%; border: 1px solid black; vertical-align: middle; } .text-container { margin-left: 10px; display: inline-block; }
 <div class="container"> <img src="http://4.bp.blogspot.com/-e98D9RG3Gzs/U5XAgtaj9tI/AAAAAAAAIIo/hDPgyrWvxy0/s1600/shy-smiley.png"/> <div class="text-container">I AM TEXT</div> </div> <div class="container"> <img src="http://4.bp.blogspot.com/-e98D9RG3Gzs/U5XAgtaj9tI/AAAAAAAAIIo/hDPgyrWvxy0/s1600/shy-smiley.png"/> <div class="text-container">I AM TEXT OH NOOO</div> </div>

What I'd like to achieve is something like this:我想实现的是这样的:

理想的

So the text is still vertically aligned in the middle and next to the image.所以文本仍然在图像的中间和旁边垂直对齐。

Is there any way to achieve this?有没有办法实现这一目标?

The img is an inline element, and the .text-container is an inline block. img是一个内联元素,而.text-container是一个内联块。 When the an inline element can't fit in the current line, the line breaks, and the element is moved to the next line. 当inline元素无法容纳在当前行中时,该行会中断,并且该元素将移至下一行。

To Prevent that set white-space: nowrap on the .container . 要防止设置white-space: nowrap ,请在.container上使用white-space: nowrap To enable the wrap inside the .text-container , define it's width, and return the white space behavior to normal (wrap). 要在.text-container内部启用换行,请定义其宽度,然后将空白行为恢复为正常(换行)。

 .container { width: 200px; margin-bottom: 10px; border: 1px solid red; white-space: nowrap; /** force the elements to stay side by side **/ } img { height: 50px; width: 50px; border-radius: 50%; border: 1px solid black; vertical-align: middle } .text-container { display: inline-block; width: calc(100% - 60px); /** containers width - img width - margin-left **/ margin-left: 10px; white-space: normal; vertical-align: middle } 
 <div class="container"> <img src="http://4.bp.blogspot.com/-e98D9RG3Gzs/U5XAgtaj9tI/AAAAAAAAIIo/hDPgyrWvxy0/s1600/shy-smiley.png" /> <div class="text-container">I AM TEXT</div> </div> <div class="container"> <img src="http://4.bp.blogspot.com/-e98D9RG3Gzs/U5XAgtaj9tI/AAAAAAAAIIo/hDPgyrWvxy0/s1600/shy-smiley.png" /> <div class="text-container">I AM TEXT OH NOOO</div> </div> 

However, you can simplify the structure even further by using a flexbox with align-items: center : 但是,您可以通过使用带有align-items: center的flexbox来进一步简化结构:

 .container { display: flex; /** set the container as flexbox **/ align-items: center; /** align all items vertically **/ width: 200px; margin-bottom: 10px; border: 1px solid red; } img { height: 50px; width: 50px; border-radius: 50%; border: 1px solid black; } .text-container { margin-left: 10px; } 
 <div class="container"> <img src="http://4.bp.blogspot.com/-e98D9RG3Gzs/U5XAgtaj9tI/AAAAAAAAIIo/hDPgyrWvxy0/s1600/shy-smiley.png" /> <div class="text-container">I AM TEXT</div> </div> <div class="container"> <img src="http://4.bp.blogspot.com/-e98D9RG3Gzs/U5XAgtaj9tI/AAAAAAAAIIo/hDPgyrWvxy0/s1600/shy-smiley.png" /> <div class="text-container">I AM TEXT OH NOOO</div> </div> 

And by using flexbox, you can also move the image with it's border to the background of .container to get a single div solution. 而且,通过使用flexbox,您还可以将带有边框的图像移动到.container的背景,以获得单个div解决方案。

 .container { display: flex; /** set the container as flexbox **/ align-items: center; /** align all items vertically **/ box-sizing: border-box; /** the padding and the border are part of the width **/ width: 202px; min-height: 54px; margin-bottom: 10px; padding-left: 60px; /** save space for the image and the margin between the image and the text **/ border: 1px solid red; /** set the image and the image border as backgrounds **/ background: radial-gradient(circle at center, transparent 0, transparent 24px, black 24px, transparent 26px), url(http://4.bp.blogspot.com/-e98D9RG3Gzs/U5XAgtaj9tI/AAAAAAAAIIo/hDPgyrWvxy0/s1600/shy-smiley.png); /** set the backgrounds size **/ background-size: 51px 51px, 50px 50px; background-repeat: no-repeat; /** prevent the backgrounds from repeating themselves to fill the container **/ background-position: left center; /** position them in relation to the container **/ } 
 <div class="container"> I AM TEXT </div> <div class="container"> I AM TEXT OH NOO </div> 

One solution would be to float the image, and use display: block on your text-container . 一种解决方案是浮动图像,然后在您的text-container上使用display: block That would wrap the text down below the image if it got big enough. 如果足够大的话,这会将文字包裹在图片下方。 I also moved the margin style to the image, and you could further adjust the spacing. 我还将margin样式移到了图像上,您可以进一步调整间距。

 .container { width: 200px; margin-bottom: 10px; } img { height: 50px; width: 50px; border-radius: 50%; border: 1px solid black; vertical-align: middle; float: left; margin-right: 10px; } .text-container { display: block; } 
 <div class="container"> <img src="http://4.bp.blogspot.com/-e98D9RG3Gzs/U5XAgtaj9tI/AAAAAAAAIIo/hDPgyrWvxy0/s1600/shy-smiley.png"/> <div class="text-container">I AM TEXT OH NOOO</div> </div> 

Since everything is fixed size, you could also keep things as inline-block , but use a fixed width on your text container. 由于所有内容都是固定大小,因此您也可以将其保留为inline-block ,但在文本容器上使用固定width

CSS shapes shapeOutside:”ellipse()” & float:”left” on the image seems the way to go, lo, the css calc() in the container misses floated elements图像上的 CSS 形状shapeOutside:”ellipse()” & float:”left”似乎是要走的路,瞧,容器中的 css calc()错过了浮动元素

saverparty.xyz 段显示不受 container-css-calc 影响的 img 元素

You are getting this because divs automatically put themselves on a newline. 之所以会这样,是因为div自动将自己置于换行符上。 If you use the <span> tag around the text, it should show up on the same line as the image. 如果在文本周围使用<span>标记,则该标记应显示在与图像相同的行上。

Try replacing: 尝试更换:

<div class="text-container">I AM TEXT OH NOOO</div>

with: 与:

<span class="text-container">I AM TEXT OH NOOO</span>

Hope that helps! 希望有帮助!

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

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