简体   繁体   English

如何在换行文字的中间对齐图像

[英]How to align an image at middle of wrapped text

I am trying to wrap an image with some adjacent text, and i can align it to top-left and top-right using align="" attribute or float . 我正在尝试用一些相邻的文本包装图像,并且可以使用align="" attribute或float将其对齐到左上和右上 But how can i align image to the vertical and horizontal center of whole text after wrapping. 但是换行后如何将图像与整个文本的垂直和水平中心对齐。

Like this: 像这样:

这是它的外观

I have tried below code, 我试过下面的代码,

<p>
<img align="middle" src="http://placehold.it/140x100" /> Some More text Here....
</p>

Solution 1 解决方案1

Fiddle 1 for the image shown in the question: 小提琴1为问题中显示的图像:

CSS: CSS:

html, body
{
    height: 100%;
    width:100%;
    margin:0;
    padding:0;
}
#main{
    width:100%;
    text-align:justify;
    margin:0;
    padding:0;
}
.class1{
    position:absolute;
}
#child1{
    position:absolute;
    top:0px;
    left:0px;
    width:100%;
    height:100%;
    text-align:justify;
    margin:0;
    padding:0;
    background:url(http://upload.wikimedia.org/wikipedia/commons/1/1b/Square_200x200.png);
    background-position:50% 50%;
    background-repeat:no-repeat;    
}

Solution 2 解决方案2

If you want to wrap the text around the only way to do it is using float.See this Fiddle 2 如果要环绕文本的唯一方法是使用float,请参阅此提琴2

CSS : CSS

.class1 img{
    float:left;
}

Solution 3 解决方案3

But the above solution will not allow you to center the image.For centering image you will have to do some trick like this Fiddle 3 . 但是上述解决方案不允许您将图像居中。要使图像居中,您将需要像Fiddle 3这样的技巧。 But this will require large amount of effort to adjust writing between the two columns/divs. 但这将需要大量的精力来调整两个列/ div之间的写入。

You won't be able to do it with a single block of text (I can't think of a single instance where it's aesthetically pleasing and functionally desirable, but I'd love to be proved wrong), but you can with two columns. 您将无法使用单个文本块来完成此操作(我无法想到一个在美学上令人愉悦且功能上令人满意的实例,但我想证明事实是错误的),但是您可以使用两列。 There's a great article on it here: 这里有一篇很棒的文章:

http://alistapart.com/article/crosscolumn http://alistapart.com/article/crosscolumn

Essentially, you use pseudo-elements to create an empty space that is the same size as the image, then position the image onto that space. 本质上,您使用伪元素创建与图像大小相同的空白空间,然后将图像放置在该空间上。

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

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